Skip to content

Commit 4c90f14

Browse files
authored
caddytest: normalize the JSON config (#6316)
* caddytest: normalize the JSON config
1 parent fb63e2e commit 4c90f14

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

caddytest/caddytest.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
136136
})
137137

138138
rawConfig = prependCaddyFilePath(rawConfig)
139+
// normalize JSON config
140+
if configType == "json" {
141+
tc.t.Logf("Before: %s", rawConfig)
142+
var conf any
143+
if err := json.Unmarshal([]byte(rawConfig), &conf); err != nil {
144+
return err
145+
}
146+
c, err := json.Marshal(conf)
147+
if err != nil {
148+
return err
149+
}
150+
rawConfig = string(c)
151+
tc.t.Logf("After: %s", rawConfig)
152+
}
139153
client := &http.Client{
140154
Timeout: Default.LoadRequestTimeout,
141155
}

caddytest/caddytest_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package caddytest
22

33
import (
4+
"net/http"
45
"strings"
56
"testing"
67
)
@@ -31,3 +32,98 @@ func TestReplaceCertificatePaths(t *testing.T) {
3132
t.Error("expected redirect uri to be unchanged")
3233
}
3334
}
35+
36+
func TestLoadUnorderedJSON(t *testing.T) {
37+
tester := NewTester(t)
38+
tester.InitServer(`
39+
{
40+
"logging": {
41+
"logs": {
42+
"default": {
43+
"level": "DEBUG",
44+
"writer": {
45+
"output": "stdout"
46+
}
47+
},
48+
"sStdOutLogs": {
49+
"level": "DEBUG",
50+
"writer": {
51+
"output": "stdout"
52+
},
53+
"include": [
54+
"http.*",
55+
"admin.*"
56+
]
57+
},
58+
"sFileLogs": {
59+
"level": "DEBUG",
60+
"writer": {
61+
"output": "stdout"
62+
},
63+
"include": [
64+
"http.*",
65+
"admin.*"
66+
]
67+
}
68+
}
69+
},
70+
"admin": {
71+
"listen": "localhost:2999"
72+
},
73+
"apps": {
74+
"pki": {
75+
"certificate_authorities" : {
76+
"local" : {
77+
"install_trust": false
78+
}
79+
}
80+
},
81+
"http": {
82+
"http_port": 9080,
83+
"https_port": 9443,
84+
"servers": {
85+
"s_server": {
86+
"listen": [
87+
":9443",
88+
":9080"
89+
],
90+
"routes": [
91+
{
92+
"handle": [
93+
{
94+
"handler": "static_response",
95+
"body": "Hello"
96+
}
97+
]
98+
},
99+
{
100+
"match": [
101+
{
102+
"host": [
103+
"localhost",
104+
"127.0.0.1"
105+
]
106+
}
107+
]
108+
}
109+
],
110+
"logs": {
111+
"default_logger_name": "sStdOutLogs",
112+
"logger_names": {
113+
"localhost": "sStdOutLogs",
114+
"127.0.0.1": "sFileLogs"
115+
}
116+
}
117+
}
118+
}
119+
}
120+
}
121+
}
122+
`, "json")
123+
req, err := http.NewRequest(http.MethodGet, "http://localhost:9080/", nil)
124+
if err != nil {
125+
t.Fail()
126+
return
127+
}
128+
tester.AssertResponseCode(req, 200)
129+
}

0 commit comments

Comments
 (0)