@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 11, 01:00 UTC

master
go 172 lines 4.03 KB
Raw
1 package integration
2
3 import (
4 "bytes"
5 "net/http"
6 "testing"
7
8 "github.com/caddyserver/caddy/v2/caddytest"
9 )
10
11 func TestBrowse(t *testing.T) {
12 tester := caddytest.NewTester(t)
13 tester.InitServer(`
14 {
15 skip_install_trust
16 admin localhost:2999
17 http_port 9080
18 https_port 9443
19 grace_period 1ns
20 }
21 http://localhost:9080 {
22 file_server browse
23 }
24 `, "caddyfile")
25
26 req, err := http.NewRequest(http.MethodGet, "http://localhost:9080/", nil)
27 if err != nil {
28 t.Fail()
29 return
30 }
31 tester.AssertResponseCode(req, 200)
32 }
33
34 func TestRespondWithJSON(t *testing.T) {
35 tester := caddytest.NewTester(t)
36 tester.InitServer(`
37 {
38 skip_install_trust
39 admin localhost:2999
40 http_port 9080
41 https_port 9443
42 grace_period 1ns
43 }
44 localhost {
45 respond {http.request.body}
46 }
47 `, "caddyfile")
48
49 res, _ := tester.AssertPostResponseBody("https://localhost:9443/",
50 nil,
51 bytes.NewBufferString(`{
52 "greeting": "Hello, world!"
53 }`), 200, `{
54 "greeting": "Hello, world!"
55 }`)
56 if res.Header.Get("Content-Type") != "application/json" {
57 t.Errorf("expected Content-Type to be application/json, but was %s", res.Header.Get("Content-Type"))
58 }
59 }
60
61 func TestRequestBodyPlaceholderRespectsMaxSizeInTemplate(t *testing.T) {
62 tester := caddytest.NewTester(t)
63 tester.InitServer(`
64 {
65 skip_install_trust
66 admin localhost:2999
67 http_port 9080
68 https_port 9443
69 grace_period 1ns
70 }
71 http://localhost:9080 {
72 request_body {
73 max_size 10
74 }
75 respond "{{placeholder \"http.request.body\"}}"
76 templates
77 }
78 `, "caddyfile")
79
80 res := tester.AssertResponseCode(newUnknownLengthPost(t, "abcdefghijklm"), http.StatusRequestEntityTooLarge)
81 res.Body.Close()
82 }
83
84 func TestRequestBodyPlaceholderRespectsMaxSizeInVarsRegexp(t *testing.T) {
85 tester := caddytest.NewTester(t)
86 tester.InitServer(`
87 {
88 skip_install_trust
89 admin localhost:2999
90 http_port 9080
91 https_port 9443
92 grace_period 1ns
93 }
94 http://localhost:9080 {
95 request_body {
96 max_size 10
97 }
98 @bigbody {
99 vars_regexp {http.request.body} "^.{20}"
100 }
101 handle @bigbody {
102 respond "the body was too long matched"
103 }
104 respond "no match"
105 }
106 `, "caddyfile")
107
108 res := tester.AssertResponseCode(newUnknownLengthPost(t, "abcdefghijklmnopqrstuvwxyz"), http.StatusRequestEntityTooLarge)
109 res.Body.Close()
110 }
111
112 func TestRequestBodyPlaceholderDirectRespondExpansion(t *testing.T) {
113 tester := caddytest.NewTester(t)
114 tester.InitServer(`
115 {
116 skip_install_trust
117 admin localhost:2999
118 http_port 9080
119 https_port 9443
120 grace_period 1ns
121 }
122 http://localhost:9080 {
123 request_body {
124 max_size 10
125 }
126 respond "{http.request.body}"
127 }
128 `, "caddyfile")
129
130 // direct placeholder consumers expand the sanitized request-body limit
131 // marker to its text instead of failing the request with 413; only the
132 // templates and vars matchers propagate the 413 status
133 tester.AssertResponse(newUnknownLengthPost(t, "abcdefghijklm"), http.StatusOK, "request body: http: request body too large")
134 }
135
136 // newUnknownLengthPost sends no Content-Length, so the body gets past
137 // request_body's early check and max_size trips while handlers read it.
138 func newUnknownLengthPost(t *testing.T, body string) *http.Request {
139 t.Helper()
140 req, err := http.NewRequest(http.MethodPost, "http://localhost:9080/", bytes.NewBufferString(body))
141 if err != nil {
142 t.Fatalf("creating request: %v", err)
143 }
144 req.ContentLength = -1
145 return req
146 }
147
148 func TestRequestBodyMaxSizeLimitsErrorRoute(t *testing.T) {
149 tester := caddytest.NewTester(t)
150 tester.InitServer(`
151 {
152 skip_install_trust
153 admin localhost:2999
154 http_port 9080
155 https_port 9443
156 grace_period 1ns
157 }
158 http://localhost:9080 {
159 request_body {
160 max_size 10
161 }
162 respond "next handler reached"
163 handle_errors {
164 respond "{http.request.body}" {err.status_code}
165 }
166 }
167 `, "caddyfile")
168
169 // the declared length is rejected before respond runs, and the error
170 // route reading the body must still be held to max_size
171 tester.AssertPostResponseBody("http://localhost:9080/", nil, bytes.NewBufferString("abcdefghijklm"), http.StatusRequestEntityTooLarge, "request body: http: request body too large")
172 }