2018-08-05 09:13:49 +00:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
2015-12-10 22:19:38 +00: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 09:13:49 +00:00
|
|
|
package livereloadinject
|
2015-10-14 07:41:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-12-02 11:52:26 +00:00
|
|
|
"net/url"
|
2016-05-07 21:34:53 +00:00
|
|
|
"strings"
|
2015-10-14 07:41:54 +00:00
|
|
|
"testing"
|
2018-08-05 09:13:49 +00:00
|
|
|
|
2020-01-29 11:46:18 +00:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-08-05 09:13:49 +00:00
|
|
|
"github.com/gohugoio/hugo/transform"
|
2015-10-14 07:41:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLiveReloadInject(t *testing.T) {
|
2020-01-29 11:46:18 +00:00
|
|
|
c := qt.New(t)
|
2016-02-06 17:28:26 +00:00
|
|
|
|
2020-12-02 11:52:26 +00: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 11:46:18 +00:00
|
|
|
apply := func(s string) string {
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
in := strings.NewReader(s)
|
2015-10-14 07:41:54 +00:00
|
|
|
|
2020-12-02 11:52:26 +00:00
|
|
|
tr := transform.New(New(*lrurl))
|
2020-01-29 11:46:18 +00:00
|
|
|
tr.Apply(out, in)
|
2015-10-14 07:41:54 +00:00
|
|
|
|
2020-01-29 11:46:18 +00:00
|
|
|
return out.String()
|
2015-10-14 07:41:54 +00:00
|
|
|
}
|
2020-01-29 11:46:18 +00: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>")
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("No match", func(c *qt.C) {
|
|
|
|
c.Assert(apply("<h1>No match</h1>"), qt.Equals, "<h1>No match</h1>")
|
|
|
|
})
|
2015-10-14 07:41:54 +00:00
|
|
|
}
|