2018-08-05 05:13:49 -04:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
package livereloadinject
|
2015-10-14 03:41:54 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-12-02 06:52:26 -05:00
|
|
|
"net/url"
|
2016-05-07 17:34:53 -04:00
|
|
|
"strings"
|
2015-10-14 03:41:54 -04:00
|
|
|
"testing"
|
2018-08-05 05:13:49 -04:00
|
|
|
|
2020-01-29 06:46:18 -05:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-08-05 05:13:49 -04:00
|
|
|
"github.com/gohugoio/hugo/transform"
|
2015-10-14 03:41:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLiveReloadInject(t *testing.T) {
|
2020-01-29 06:46:18 -05:00
|
|
|
c := qt.New(t)
|
2016-02-06 12:28:26 -05:00
|
|
|
|
2020-12-02 06:52:26 -05:00
|
|
|
lrurl, err := url.Parse("http://localhost:1234/subpath")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Parsing test URL failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expectBase := `<script src="/subpath/livereload.js?mindelay=10&v=2&port=1234&path=subpath/livereload" data-no-instant defer></script>`
|
2020-01-29 06:46:18 -05:00
|
|
|
apply := func(s string) string {
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
in := strings.NewReader(s)
|
2015-10-14 03:41:54 -04:00
|
|
|
|
2020-12-02 06:52:26 -05:00
|
|
|
tr := transform.New(New(*lrurl))
|
2020-01-29 06:46:18 -05:00
|
|
|
tr.Apply(out, in)
|
2015-10-14 03:41:54 -04:00
|
|
|
|
2020-01-29 06:46:18 -05:00
|
|
|
return out.String()
|
2015-10-14 03:41:54 -04:00
|
|
|
}
|
2020-01-29 06:46:18 -05:00
|
|
|
|
|
|
|
c.Run("Head lower", func(c *qt.C) {
|
|
|
|
c.Assert(apply("<html><head>foo"), qt.Equals, "<html><head>"+expectBase+"foo")
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Head upper", func(c *qt.C) {
|
|
|
|
c.Assert(apply("<html><HEAD>foo"), qt.Equals, "<html><HEAD>"+expectBase+"foo")
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Body lower", func(c *qt.C) {
|
|
|
|
c.Assert(apply("foo</body>"), qt.Equals, "foo"+expectBase+"</body>")
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Body upper", func(c *qt.C) {
|
|
|
|
c.Assert(apply("foo</BODY>"), qt.Equals, "foo"+expectBase+"</BODY>")
|
|
|
|
})
|
|
|
|
|
2022-08-07 10:15:28 -04:00
|
|
|
c.Run("Html upper", func(c *qt.C) {
|
|
|
|
c.Assert(apply("<html>foo"), qt.Equals, "<html>"+expectBase+warnScript+"foo")
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Html upper with attr", func(c *qt.C) {
|
|
|
|
c.Assert(apply(`<html lang="en">foo`), qt.Equals, `<html lang="en">`+expectBase+warnScript+"foo")
|
|
|
|
})
|
|
|
|
|
2020-01-29 06:46:18 -05:00
|
|
|
c.Run("No match", func(c *qt.C) {
|
2022-08-07 10:15:28 -04:00
|
|
|
c.Assert(apply("<h1>No match</h1>"), qt.Equals, "<h1>No match</h1>"+expectBase+warnScript)
|
2020-01-29 06:46:18 -05:00
|
|
|
})
|
2015-10-14 03:41:54 -04:00
|
|
|
}
|