mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 06:03:48 +03:00
core: Add tests to Replacer; fix panic (fixes #2852)
This commit is contained in:
parent
93bc1b72e3
commit
ad90b273db
2 changed files with 67 additions and 10 deletions
|
@ -116,11 +116,14 @@ func (r *replacer) replace(input, empty string,
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the substring from the last cursor to this point
|
|
||||||
sb.WriteString(input[lastWriteCursor:i])
|
|
||||||
|
|
||||||
// find the end of the placeholder
|
// find the end of the placeholder
|
||||||
end := strings.Index(input[i:], string(phClose)) + i
|
end := strings.Index(input[i:], string(phClose)) + i
|
||||||
|
if end < i {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the substring from the last cursor to this point
|
||||||
|
sb.WriteString(input[lastWriteCursor:i])
|
||||||
|
|
||||||
// trim opening bracket
|
// trim opening bracket
|
||||||
key := input[i+1 : end]
|
key := input[i+1 : end]
|
||||||
|
|
|
@ -22,16 +22,62 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func fakeReplacer() replacer {
|
func TestReplacer(t *testing.T) {
|
||||||
return replacer{
|
type testCase struct {
|
||||||
providers: make([]ReplacementFunc, 0),
|
input, expect, empty string
|
||||||
static: make(map[string]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) {
|
func TestReplacerSet(t *testing.T) {
|
||||||
rep := fakeReplacer()
|
rep := testReplacer()
|
||||||
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
variable string
|
variable string
|
||||||
|
@ -191,7 +237,7 @@ func TestReplacerDelete(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReplacerMap(t *testing.T) {
|
func TestReplacerMap(t *testing.T) {
|
||||||
rep := fakeReplacer()
|
rep := testReplacer()
|
||||||
|
|
||||||
for i, tc := range []ReplacementFunc{
|
for i, tc := range []ReplacementFunc{
|
||||||
func(key string) (val string, ok bool) {
|
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
|
// test if default global replacements are added as the first provider
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
os.Setenv("CADDY_REPLACER_TEST", "envtest")
|
os.Setenv("CADDY_REPLACER_TEST", "envtest")
|
||||||
|
defer os.Setenv("CADDY_REPLACER_TEST", "")
|
||||||
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
variable string
|
variable string
|
||||||
|
@ -267,3 +314,10 @@ func TestReplacerNew(t *testing.T) {
|
||||||
t.Errorf("Expected type of replacer %T got %T ", &replacer{}, tc)
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue