aboutsummaryrefslogtreecommitdiffhomepage
path: root/replacer_test.go
diff options
context:
space:
mode:
authorBill Glover <[email protected]>2020-03-08 21:36:59 +0000
committerGitHub <[email protected]>2020-03-08 15:36:59 -0600
commit36a6c7daf0f45353efe860e254aa148b7574b04e (patch)
tree4f1aefd250dcbef27a060047663da20a48eb8e91 /replacer_test.go
parentca6e54bbb8ab2bad3cc9192bd4857be7e9501c6c (diff)
downloadcaddy-36a6c7daf0f45353efe860e254aa148b7574b04e.tar.gz
caddy-36a6c7daf0f45353efe860e254aa148b7574b04e.zip
Rework Replacer loop to handle escaped braces (#3121)
Fixes #3116 * Rework Replacer loop to ignore escaped braces * Add benchmark tests for replacer * Optimise handling of escaped braces * Handle escaped closing braces * Remove additional check for closing brace This commit removes the additional check for input in which the closing brace appears before the opening brace. This check has been removed for performance reasons as it is deemed an unlikely edge case. * Check for escaped closing braces in placeholder name
Diffstat (limited to 'replacer_test.go')
-rw-r--r--replacer_test.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/replacer_test.go b/replacer_test.go
index 4b5619451..66bb53790 100644
--- a/replacer_test.go
+++ b/replacer_test.go
@@ -36,30 +36,86 @@ func TestReplacer(t *testing.T) {
expect: "{",
},
{
+ input: `\{`,
+ expect: `{`,
+ },
+ {
input: "foo{",
expect: "foo{",
},
{
+ input: `foo\{`,
+ expect: `foo{`,
+ },
+ {
input: "foo{bar",
expect: "foo{bar",
},
{
+ input: `foo\{bar`,
+ expect: `foo{bar`,
+ },
+ {
input: "foo{bar}",
expect: "foo",
},
{
+ input: `foo\{bar\}`,
+ expect: `foo{bar}`,
+ },
+ {
input: "}",
expect: "}",
},
{
+ input: `\}`,
+ expect: `\}`,
+ },
+ {
input: "{}",
expect: "",
},
{
+ input: `\{\}`,
+ expect: `{}`,
+ },
+ {
input: `{"json": "object"}`,
expect: "",
},
{
+ input: `\{"json": "object"}`,
+ expect: `{"json": "object"}`,
+ },
+ {
+ input: `\{"json": "object"\}`,
+ expect: `{"json": "object"}`,
+ },
+ {
+ input: `\{"json": "object{bar}"\}`,
+ expect: `{"json": "object"}`,
+ },
+ {
+ input: `\{"json": \{"nested": "object"\}\}`,
+ expect: `{"json": {"nested": "object"}}`,
+ },
+ {
+ input: `\{"json": \{"nested": "{bar}"\}\}`,
+ expect: `{"json": {"nested": ""}}`,
+ },
+ {
+ input: `pre \{"json": \{"nested": "{bar}"\}\}`,
+ expect: `pre {"json": {"nested": ""}}`,
+ },
+ {
+ input: `\{"json": \{"nested": "{bar}"\}\} post`,
+ expect: `{"json": {"nested": ""}} post`,
+ },
+ {
+ input: `pre \{"json": \{"nested": "{bar}"\}\} post`,
+ expect: `pre {"json": {"nested": ""}} post`,
+ },
+ {
input: `{{`,
expect: "{{",
},
@@ -68,10 +124,38 @@ func TestReplacer(t *testing.T) {
expect: "",
},
{
+ input: `{"json": "object"\}`,
+ expect: "",
+ },
+ {
input: `{unknown}`,
empty: "-",
expect: "-",
},
+ {
+ input: `back\slashes`,
+ expect: `back\slashes`,
+ },
+ {
+ input: `double back\\slashes`,
+ expect: `double back\\slashes`,
+ },
+ {
+ input: `placeholder {with \{ brace} in name`,
+ expect: `placeholder in name`,
+ },
+ {
+ input: `placeholder {with \} brace} in name`,
+ expect: `placeholder in name`,
+ },
+ {
+ input: `placeholder {with \} \} braces} in name`,
+ expect: `placeholder in name`,
+ },
+ {
+ input: `\{'group':'default','max_age':3600,'endpoints':[\{'url':'https://some.domain.local/a/d/g'\}],'include_subdomains':true\}`,
+ expect: `{'group':'default','max_age':3600,'endpoints':[{'url':'https://some.domain.local/a/d/g'}],'include_subdomains':true}`,
+ },
} {
actual := rep.ReplaceAll(tc.input, tc.empty)
if actual != tc.expect {
@@ -81,6 +165,35 @@ func TestReplacer(t *testing.T) {
}
}
+func BenchmarkReplacer(b *testing.B) {
+ type testCase struct {
+ name, input, empty string
+ }
+
+ rep := testReplacer()
+
+ for _, bm := range []testCase{
+ {
+ name: "no placeholder",
+ input: `simple string`,
+ },
+ {
+ name: "placeholder",
+ input: `{"json": "object"}`,
+ },
+ {
+ name: "escaped placeholder",
+ input: `\{"json": \{"nested": "{bar}"\}\}`,
+ },
+ } {
+ b.Run(bm.name, func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ rep.ReplaceAll(bm.input, bm.empty)
+ }
+ })
+ }
+}
+
func TestReplacerSet(t *testing.T) {
rep := testReplacer()