@marcel91200 / caddy Mirror · read-only / commits / ad90b273
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

core: Add tests to Replacer; fix panic (fixes #2852)

Matthew Holt committed Nov 11, 2019 at 19:29 UTC ad90b273dbb13bf45644f39f6c0be1248c943d9a
2 files changed +67 -10
replacer.go
+6 -3
@@ -116,11 +116,14 @@ func (r *replacer) replace(input, empty string,
116 continue
117 }
118
119 - // write the substring from the last cursor to this point
120 - sb.WriteString(input[lastWriteCursor:i])
121 -
119 // find the end of the placeholder
120 end := strings.Index(input[i:], string(phClose)) + i
121 + if end < i {
122 + continue
123 + }
124 +
125 + // write the substring from the last cursor to this point
126 + sb.WriteString(input[lastWriteCursor:i])
127
128 // trim opening bracket
129 key := input[i+1 : end]
replacer_test.go
+61 -7
@@ -22,16 +22,62 @@ import (
22 "testing"
23 )
24
25 -func fakeReplacer() replacer {
26 - return replacer{
27 - providers: make([]ReplacementFunc, 0),
28 - static: make(map[string]string),
25 +func TestReplacer(t *testing.T) {
26 + type testCase struct {
27 + input, expect, empty string
28 + }
29 +
30 + rep := testReplacer()
31 +
32 + // ReplaceAll
33 + for i, tc := range []testCase{
34 + {
35 + input: "{",
36 + expect: "{",
37 + },
38 + {
39 + input: "foo{",
40 + expect: "foo{",
41 + },
42 + {
43 + input: "foo{bar",
44 + expect: "foo{bar",
45 + },
46 + {
47 + input: "foo{bar}",
48 + expect: "foo",
49 + },
50 + {
51 + input: "}",
52 + expect: "}",
53 + },
54 + {
55 + input: "{}",
56 + expect: "",
57 + },
58 + {
59 + input: `{"json": "object"}`,
60 + expect: "",
61 + },
62 + {
63 + input: `{{`,
64 + expect: "{{",
65 + },
66 + {
67 + input: `{{}`,
68 + expect: "",
69 + },
70 + } {
71 + actual := rep.ReplaceAll(tc.input, tc.empty)
72 + if actual != tc.expect {
73 + t.Errorf("Test %d: '%s': expected '%s' but got '%s'",
74 + i, tc.input, tc.expect, actual)
75 + }
76 }
77 }
78
32 -// Tests the Set method by setting some variables and check if they are added to static
79 func TestReplacerSet(t *testing.T) {
34 - rep := fakeReplacer()
80 + rep := testReplacer()
81
82 for _, tc := range []struct {
83 variable string
@@ -191,7 +237,7 @@ func TestReplacerDelete(t *testing.T) {
237 }
238
239 func TestReplacerMap(t *testing.T) {
194 - rep := fakeReplacer()
240 + rep := testReplacer()
241
242 for i, tc := range []ReplacementFunc{
243 func(key string) (val string, ok bool) {
@@ -228,6 +274,7 @@ func TestReplacerNew(t *testing.T) {
274 // test if default global replacements are added as the first provider
275 hostname, _ := os.Hostname()
276 os.Setenv("CADDY_REPLACER_TEST", "envtest")
277 + defer os.Setenv("CADDY_REPLACER_TEST", "")
278
279 for _, tc := range []struct {
280 variable string
@@ -267,3 +314,10 @@ func TestReplacerNew(t *testing.T) {
314 t.Errorf("Expected type of replacer %T got %T ", &replacer{}, tc)
315 }
316 }
317 +
318 +func testReplacer() replacer {
319 + return replacer{
320 + providers: make([]ReplacementFunc, 0),
321 + static: make(map[string]string),
322 + }
323 +}