aboutsummaryrefslogtreecommitdiffhomepage
path: root/replacer_test.go
diff options
context:
space:
mode:
authorMatthew Holt <[email protected]>2019-11-11 19:29:31 -0700
committerMatthew Holt <[email protected]>2019-11-11 19:29:31 -0700
commitad90b273dbb13bf45644f39f6c0be1248c943d9a (patch)
tree04dbcbd3a90c3a0d3bb33b21b8e8474d35e59236 /replacer_test.go
parent93bc1b72e3cd566e6447ad7a1f832474aad5dfcc (diff)
downloadcaddy-ad90b273dbb13bf45644f39f6c0be1248c943d9a.tar.gz
caddy-ad90b273dbb13bf45644f39f6c0be1248c943d9a.zip
core: Add tests to Replacer; fix panic (fixes #2852)
Diffstat (limited to 'replacer_test.go')
-rw-r--r--replacer_test.go68
1 files changed, 61 insertions, 7 deletions
diff --git a/replacer_test.go b/replacer_test.go
index 435582bb6..fa9b56930 100644
--- a/replacer_test.go
+++ b/replacer_test.go
@@ -22,16 +22,62 @@ import (
"testing"
)
-func fakeReplacer() replacer {
- return replacer{
- providers: make([]ReplacementFunc, 0),
- static: make(map[string]string),
+func TestReplacer(t *testing.T) {
+ type testCase struct {
+ input, expect, empty string
+ }
+
+ rep := testReplacer()
+
+ // ReplaceAll
+ for i, tc := range []testCase{
+ {
+ input: "{",
+ expect: "{",
+ },
+ {
+ input: "foo{",
+ expect: "foo{",
+ },
+ {
+ input: "foo{bar",
+ expect: "foo{bar",
+ },
+ {
+ input: "foo{bar}",
+ expect: "foo",
+ },
+ {
+ input: "}",
+ expect: "}",
+ },
+ {
+ input: "{}",
+ expect: "",
+ },
+ {
+ input: `{"json": "object"}`,
+ expect: "",
+ },
+ {
+ input: `{{`,
+ expect: "{{",
+ },
+ {
+ input: `{{}`,
+ expect: "",
+ },
+ } {
+ actual := rep.ReplaceAll(tc.input, tc.empty)
+ if actual != tc.expect {
+ t.Errorf("Test %d: '%s': expected '%s' but got '%s'",
+ i, tc.input, tc.expect, actual)
+ }
}
}
-// Tests the Set method by setting some variables and check if they are added to static
func TestReplacerSet(t *testing.T) {
- rep := fakeReplacer()
+ rep := testReplacer()
for _, tc := range []struct {
variable string
@@ -191,7 +237,7 @@ func TestReplacerDelete(t *testing.T) {
}
func TestReplacerMap(t *testing.T) {
- rep := fakeReplacer()
+ rep := testReplacer()
for i, tc := range []ReplacementFunc{
func(key string) (val string, ok bool) {
@@ -228,6 +274,7 @@ func TestReplacerNew(t *testing.T) {
// test if default global replacements are added as the first provider
hostname, _ := os.Hostname()
os.Setenv("CADDY_REPLACER_TEST", "envtest")
+ defer os.Setenv("CADDY_REPLACER_TEST", "")
for _, tc := range []struct {
variable string
@@ -267,3 +314,10 @@ func TestReplacerNew(t *testing.T) {
t.Errorf("Expected type of replacer %T got %T ", &replacer{}, tc)
}
}
+
+func testReplacer() replacer {
+ return replacer{
+ providers: make([]ReplacementFunc, 0),
+ static: make(map[string]string),
+ }
+}