| 1 |
// Copyright 2018 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package livereloadinject |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"io" |
| 19 |
"net/url" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
qt "github.com/frankban/quicktest" |
| 24 |
"github.com/gohugoio/hugo/transform" |
| 25 |
) |
| 26 |
|
| 27 |
func TestLiveReloadInject(t *testing.T) { |
| 28 |
c := qt.New(t) |
| 29 |
|
| 30 |
lrurl, err := url.Parse("http://localhost:1234/subpath") |
| 31 |
if err != nil { |
| 32 |
t.Errorf("Parsing test URL failed") |
| 33 |
return |
| 34 |
} |
| 35 |
expectBase := `<script src="/subpath/livereload.js?mindelay=10&v=2&port=1234&path=subpath/livereload" data-no-instant defer></script>` |
| 36 |
apply := func(s string) string { |
| 37 |
out := new(bytes.Buffer) |
| 38 |
in := strings.NewReader(s) |
| 39 |
|
| 40 |
tr := transform.New(New(lrurl)) |
| 41 |
tr.Apply(out, in) |
| 42 |
|
| 43 |
return out.String() |
| 44 |
} |
| 45 |
|
| 46 |
c.Run("Inject after head tag", func(c *qt.C) { |
| 47 |
c.Assert(apply("<!doctype html><html><head>after"), qt.Equals, "<!doctype html><html><head>"+expectBase+"after") |
| 48 |
}) |
| 49 |
|
| 50 |
c.Run("Inject after head tag when doctype and html omitted", func(c *qt.C) { |
| 51 |
c.Assert(apply("<head>after"), qt.Equals, "<head>"+expectBase+"after") |
| 52 |
}) |
| 53 |
|
| 54 |
c.Run("Inject after html when head omitted", func(c *qt.C) { |
| 55 |
c.Assert(apply("<html>after"), qt.Equals, "<html>"+expectBase+"after") |
| 56 |
}) |
| 57 |
|
| 58 |
c.Run("Inject after doctype when head and html omitted", func(c *qt.C) { |
| 59 |
c.Assert(apply("<!doctype html>after"), qt.Equals, "<!doctype html>"+expectBase+"after") |
| 60 |
}) |
| 61 |
|
| 62 |
c.Run("Inject nothing if no doctype, html or head found", func(c *qt.C) { |
| 63 |
c.Assert(apply("<title>after</title>"), qt.Equals, "<title>after</title>") |
| 64 |
}) |
| 65 |
|
| 66 |
c.Run("Inject nothing if no tag found", func(c *qt.C) { |
| 67 |
c.Assert(apply("after"), qt.Equals, "after") |
| 68 |
}) |
| 69 |
|
| 70 |
c.Run("Inject after HeAd tag MiXed CaSe", func(c *qt.C) { |
| 71 |
c.Assert(apply("<HeAd>AfTer"), qt.Equals, "<HeAd>"+expectBase+"AfTer") |
| 72 |
}) |
| 73 |
|
| 74 |
c.Run("Inject after HtMl tag MiXed CaSe", func(c *qt.C) { |
| 75 |
c.Assert(apply("<HtMl>AfTer"), qt.Equals, "<HtMl>"+expectBase+"AfTer") |
| 76 |
}) |
| 77 |
|
| 78 |
c.Run("Inject after doctype mixed case", func(c *qt.C) { |
| 79 |
c.Assert(apply("<!DocType HtMl>AfTer"), qt.Equals, "<!DocType HtMl>"+expectBase+"AfTer") |
| 80 |
}) |
| 81 |
|
| 82 |
c.Run("Inject after html tag with attributes", func(c *qt.C) { |
| 83 |
c.Assert(apply(`<html lang="en">after`), qt.Equals, `<html lang="en">`+expectBase+"after") |
| 84 |
}) |
| 85 |
|
| 86 |
c.Run("Inject after html tag with newline", func(c *qt.C) { |
| 87 |
c.Assert(apply("<html\n>after"), qt.Equals, "<html\n>"+expectBase+"after") |
| 88 |
}) |
| 89 |
|
| 90 |
c.Run("Skip comments and whitespace", func(c *qt.C) { |
| 91 |
c.Assert( |
| 92 |
apply(" <!--x--> <!doctype html>\n<?xml instruction ?> <head>after"), |
| 93 |
qt.Equals, |
| 94 |
" <!--x--> <!doctype html>\n<?xml instruction ?> <head>"+expectBase+"after", |
| 95 |
) |
| 96 |
}) |
| 97 |
|
| 98 |
c.Run("Do not search inside comment", func(c *qt.C) { |
| 99 |
c.Assert(apply("<html><!--<head>-->"), qt.Equals, "<html><!--<head>-->"+expectBase) |
| 100 |
}) |
| 101 |
|
| 102 |
c.Run("Do not search inside scripts", func(c *qt.C) { |
| 103 |
c.Assert(apply("<html><script>`<head>`</script>"), qt.Equals, "<html>"+expectBase+"<script>`<head>`</script>") |
| 104 |
}) |
| 105 |
|
| 106 |
c.Run("Do not search inside templates", func(c *qt.C) { |
| 107 |
c.Assert(apply("<html><template><head></template>"), qt.Not(qt.Equals), "<html><template><head>"+expectBase+"</template>") |
| 108 |
}) |
| 109 |
|
| 110 |
c.Run("Search from the start of the input", func(c *qt.C) { |
| 111 |
c.Assert(apply("<head>after<head>"), qt.Equals, "<head>"+expectBase+"after<head>") |
| 112 |
}) |
| 113 |
|
| 114 |
c.Run("Do not mistake header for head", func(c *qt.C) { |
| 115 |
c.Assert(apply("<html><header>"), qt.Equals, "<html>"+expectBase+"<header>") |
| 116 |
}) |
| 117 |
|
| 118 |
c.Run("Do not mistake custom elements for head", func(c *qt.C) { |
| 119 |
c.Assert(apply("<html><head-custom>"), qt.Equals, "<html>"+expectBase+"<head-custom>") |
| 120 |
}) |
| 121 |
} |
| 122 |
|
| 123 |
func BenchmarkLiveReloadInject(b *testing.B) { |
| 124 |
s := ` |
| 125 |
<html> |
| 126 |
<head> |
| 127 |
</head> |
| 128 |
<body> |
| 129 |
</body> |
| 130 |
</html> |
| 131 |
` |
| 132 |
in := strings.NewReader(s) |
| 133 |
lrurl, err := url.Parse("http://localhost:1234/subpath") |
| 134 |
if err != nil { |
| 135 |
b.Fatalf("Parsing test URL failed") |
| 136 |
} |
| 137 |
tr := transform.New(New(lrurl)) |
| 138 |
|
| 139 |
for b.Loop() { |
| 140 |
in.Seek(0, 0) |
| 141 |
tr.Apply(io.Discard, in) |
| 142 |
} |
| 143 |
} |