-
Notifications
You must be signed in to change notification settings - Fork 0
/
caddyfile_test.go
88 lines (79 loc) · 1.71 KB
/
caddyfile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package caddyfile_ext
import (
"encoding/json"
"testing"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
)
func testParser(t *testing.T, input string, expected string) {
d := caddyfile.NewTestDispenser(input)
d.Next()
out, err := parseArgs(d, "", nil)
if err != nil {
t.Error(err)
} else {
out, _ := json.Marshal(out)
if string(out) != expected {
t.Errorf("assertion failed:\nexpected: %s\n got: %s\n", expected, out)
}
}
}
func TestScalarVal(t *testing.T) {
testParser(t, `true`, `true`)
testParser(t, `123`, `123`)
testParser(t, `-4.5`, `-4.5`)
testParser(t, `"false"`, `"false"`)
}
func TestNestedObject(t *testing.T) {
testParser(
t,
`key1 key2 {
key3 12.3
key4 4.56
}`,
`{"key1":{"key2":{"key3":12.3,"key4":4.56}}}`,
)
}
func TestUpdateObject(t *testing.T) {
testParser(
t,
`key1 {
key2 key3 12.3
key2 key4 4.56
}`,
`{"key1":{"key2":{"key3":12.3,"key4":4.56}}}`,
)
}
func TestUpdateArray(t *testing.T) {
testParser(
t,
`key1 {
+key2 key3 12.3
+key2 key3 4.56
}`,
`{"key1":{"key2":[{"key3":12.3},{"key3":4.56}]}}`,
)
}
func testCaddyfile(t *testing.T, input string, expected string) {
out, _, err := caddyfile.Adapter{
ServerType: httpcaddyfile.ServerType{},
}.Adapt([]byte(input), nil)
if err != nil {
t.Error(err)
} else {
if string(out) != expected {
t.Errorf("assertion failed:\nexpected: %s\n got: %s\n", expected, out)
}
}
}
func TestAppExt(t *testing.T) {
testCaddyfile(
t,
`{
app1 app1 listen :1081
app2 app2 listen :1082
app3 app3 listen :1083
}`,
`{"apps":{"app1":{"listen":":1081"},"app2":{"listen":":1082"},"app3":{"listen":":1083"}}}`,
)
}