@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 10, 23:45 UTC

master
go 244 lines 6.04 KB
Raw
1 package caddytest
2
3 import (
4 "bytes"
5 "net/http"
6 "strings"
7 "testing"
8 )
9
10 func TestReplaceCertificatePaths(t *testing.T) {
11 rawConfig := `a.caddy.localhost:9443 {
12 tls /caddy.localhost.crt /caddy.localhost.key {
13 }
14
15 redir / https://b.caddy.localhost:9443/version 301
16
17 respond /version 200 {
18 body "hello from a.caddy.localhost"
19 }
20 }`
21
22 r := prependCaddyFilePath(rawConfig)
23
24 if !strings.Contains(r, getIntegrationDir()+"/caddy.localhost.crt") {
25 t.Error("expected the /caddy.localhost.crt to be expanded to include the full path")
26 }
27
28 if !strings.Contains(r, getIntegrationDir()+"/caddy.localhost.key") {
29 t.Error("expected the /caddy.localhost.crt to be expanded to include the full path")
30 }
31
32 if !strings.Contains(r, "https://b.caddy.localhost:9443/version") {
33 t.Error("expected redirect uri to be unchanged")
34 }
35 }
36
37 func TestLoadUnorderedJSON(t *testing.T) {
38 tester := NewTester(t)
39 tester.InitServer(`
40 {
41 "logging": {
42 "logs": {
43 "default": {
44 "level": "DEBUG",
45 "writer": {
46 "output": "stdout"
47 }
48 },
49 "sStdOutLogs": {
50 "level": "DEBUG",
51 "writer": {
52 "output": "stdout"
53 },
54 "include": [
55 "http.*",
56 "admin.*"
57 ]
58 },
59 "sFileLogs": {
60 "level": "DEBUG",
61 "writer": {
62 "output": "stdout"
63 },
64 "include": [
65 "http.*",
66 "admin.*"
67 ]
68 }
69 }
70 },
71 "admin": {
72 "listen": "localhost:2999"
73 },
74 "apps": {
75 "pki": {
76 "certificate_authorities" : {
77 "local" : {
78 "install_trust": false
79 }
80 }
81 },
82 "http": {
83 "http_port": 9080,
84 "https_port": 9443,
85 "servers": {
86 "s_server": {
87 "listen": [
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 }
130
131 func TestCheckID(t *testing.T) {
132 tester := NewTester(t)
133 tester.InitServer(`{
134 "admin": {
135 "listen": "localhost:2999"
136 },
137 "apps": {
138 "http": {
139 "http_port": 9080,
140 "servers": {
141 "s_server": {
142 "@id": "s_server",
143 "listen": [
144 ":9080"
145 ],
146 "routes": [
147 {
148 "handle": [
149 {
150 "handler": "static_response",
151 "body": "Hello"
152 }
153 ]
154 }
155 ]
156 }
157 }
158 }
159 }
160 }
161 `, "json")
162 headers := []string{"Content-Type:application/json"}
163 sServer1 := []byte(`{"@id":"s_server","listen":[":9080"],"routes":[{"@id":"route1","handle":[{"handler":"static_response","body":"Hello 2"}]}]}`)
164
165 // PUT to an existing ID should fail with a 409 conflict
166 tester.AssertPutResponseBody(
167 "http://localhost:2999/id/s_server",
168 headers,
169 bytes.NewBuffer(sServer1),
170 409,
171 `{"error":"[/config/apps/http/servers/s_server] key already exists: s_server"}`+"\n")
172
173 // POST replaces the object fully
174 tester.AssertPostResponseBody(
175 "http://localhost:2999/id/s_server",
176 headers,
177 bytes.NewBuffer(sServer1),
178 200,
179 "")
180
181 // Verify the server is running the new route
182 tester.AssertGetResponse(
183 "http://localhost:9080/",
184 200,
185 "Hello 2")
186
187 // Update the existing route to ensure IDs are handled correctly when replaced
188 tester.AssertPostResponseBody(
189 "http://localhost:2999/id/s_server",
190 headers,
191 bytes.NewBuffer([]byte(`{"@id":"s_server","listen":[":9080"],"routes":[{"@id":"route1","handle":[{"handler":"static_response","body":"Hello2"}],"match":[{"path":["/route_1/*"]}]}]}`)),
192 200,
193 "")
194
195 sServer2 := []byte(`{"@id":"s_server","listen":[":9080"],"routes":[{"@id":"route1","handle":[{"handler":"static_response","body":"Hello2"}],"match":[{"path":["/route_1/*"]}]}]}`)
196
197 // Identical patch should succeed and return 200 (config is unchanged branch)
198 tester.AssertPatchResponseBody(
199 "http://localhost:2999/id/s_server",
200 headers,
201 bytes.NewBuffer(sServer2),
202 200,
203 "")
204
205 route2 := []byte(`{"@id":"route2","handle": [{"handler": "static_response","body": "route2"}],"match":[{"path":["/route_2/*"]}]}`)
206
207 // Put a new route2 object before the route1 object due to the path of /id/route1
208 // Being translated to: /config/apps/http/servers/s_server/routes/0
209 tester.AssertPutResponseBody(
210 "http://localhost:2999/id/route1",
211 headers,
212 bytes.NewBuffer(route2),
213 200,
214 "")
215
216 // Verify that the whole config looks correct, now containing both route1 and route2
217 tester.AssertGetResponse(
218 "http://localhost:2999/config/",
219 200,
220 `{"admin":{"listen":"localhost:2999"},"apps":{"http":{"http_port":9080,"servers":{"s_server":{"@id":"s_server","listen":[":9080"],"routes":[{"@id":"route2","handle":[{"body":"route2","handler":"static_response"}],"match":[{"path":["/route_2/*"]}]},{"@id":"route1","handle":[{"body":"Hello2","handler":"static_response"}],"match":[{"path":["/route_1/*"]}]}]}}}}}`+"\n")
221
222 // Try to add another copy of route2 using POST to test duplicate ID handling
223 // Since the first route2 ended up at array index 0, and we are appending to the array, the index for the new element would be 2
224 tester.AssertPostResponseBody(
225 "http://localhost:2999/id/route2",
226 headers,
227 bytes.NewBuffer(route2),
228 400,
229 `{"error":"indexing config: duplicate ID 'route2' found at /config/apps/http/servers/s_server/routes/0 and /config/apps/http/servers/s_server/routes/2"}`+"\n")
230
231 // Use PATCH to modify an existing object successfully
232 tester.AssertPatchResponseBody(
233 "http://localhost:2999/id/route1",
234 headers,
235 bytes.NewBuffer([]byte(`{"@id":"route1","handle":[{"handler":"static_response","body":"route1"}],"match":[{"path":["/route_1/*"]}]}`)),
236 200,
237 "")
238
239 // Verify the PATCH updated the server state
240 tester.AssertGetResponse(
241 "http://localhost:9080/route_1/",
242 200,
243 "route1")
244 }