@marcel91200 / caddy Mirror · read-only / commits / 0c8763a7
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 14, 05:15 UTC

Add simple tests for static responder

Matthew Holt committed Jun 11, 2019 at 17:46 UTC 0c8763a7283daaf955cfdb3b0e88dc24fd6d3b2b
1 file changed +51
modules/caddyhttp/staticresp_test.go new
+51
@@ -0,0 +1,51 @@
1 +package caddyhttp
2 +
3 +import (
4 + "context"
5 + "io/ioutil"
6 + "net/http"
7 + "net/http/httptest"
8 + "testing"
9 +
10 + "github.com/caddyserver/caddy2"
11 +)
12 +
13 +func TestStaticResponseHandler(t *testing.T) {
14 + r := fakeRequest()
15 + w := httptest.NewRecorder()
16 +
17 + s := Static{
18 + StatusCode: http.StatusNotFound,
19 + Headers: http.Header{
20 + "X-Test": []string{"Testing"},
21 + },
22 + Body: "Text",
23 + Close: true,
24 + }
25 +
26 + err := s.ServeHTTP(w, r)
27 + if err != nil {
28 + t.Errorf("did not expect an error, but got: %v", err)
29 + }
30 +
31 + resp := w.Result()
32 + respBody, _ := ioutil.ReadAll(resp.Body)
33 +
34 + if resp.StatusCode != http.StatusNotFound {
35 + t.Errorf("expected status %d but got %d", http.StatusNotFound, resp.StatusCode)
36 + }
37 + if resp.Header.Get("X-Test") != "Testing" {
38 + t.Errorf("expected x-test header to be 'testing' but was '%s'", resp.Header.Get("X-Test"))
39 + }
40 + if string(respBody) != "Text" {
41 + t.Errorf("expected body to be 'test' but was '%s'", respBody)
42 + }
43 +}
44 +
45 +func fakeRequest() *http.Request {
46 + r, _ := http.NewRequest("GET", "/", nil)
47 + repl := caddy2.NewReplacer()
48 + ctx := context.WithValue(r.Context(), caddy2.ReplacerCtxKey, repl)
49 + r = r.WithContext(ctx)
50 + return r
51 +}