Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 12:00 UTC
caddytest: normalize the JSON config (#6316)
* caddytest: normalize the JSON config
Mohammed Al Sahaf committed
May 14, 2024 at 10:50 UTC
4c90f1427f22a85f767dbeabb3bedcc8f9355c84
2 files changed
+110
caddytest/caddytest.go
+14
@@ -136,6 +136,20 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
136
})
137
138
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
+ }
153
client := &http.Client{
154
Timeout: Default.LoadRequestTimeout,
155
}
caddytest/caddytest_test.go
+96
@@ -1,6 +1,7 @@
1
package caddytest
2
3
import (
4
+ "net/http"
5
"strings"
6
"testing"
7
)
@@ -31,3 +32,98 @@ func TestReplaceCertificatePaths(t *testing.T) {
32
t.Error("expected redirect uri to be unchanged")
33
}
34
}
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
+}