MCPcopy
hub / github.com/caddyserver/caddy / TestStaticResponseHandler

Function TestStaticResponseHandler

modules/caddyhttp/staticresp_test.go:28–58  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestStaticResponseHandler(t *testing.T) {
29 r := fakeRequest()
30 w := httptest.NewRecorder()
31
32 s := StaticResponse{
33 StatusCode: WeakString(strconv.Itoa(http.StatusNotFound)),
34 Headers: http.Header{
35 "X-Test": []string{"Testing"},
36 },
37 Body: "Text",
38 Close: true,
39 }
40
41 err := s.ServeHTTP(w, r, nil)
42 if err != nil {
43 t.Errorf("did not expect an error, but got: %v", err)
44 }
45
46 resp := w.Result()
47 respBody, _ := io.ReadAll(resp.Body)
48
49 if resp.StatusCode != http.StatusNotFound {
50 t.Errorf("expected status %d but got %d", http.StatusNotFound, resp.StatusCode)
51 }
52 if resp.Header.Get("X-Test") != "Testing" {
53 t.Errorf("expected x-test header to be 'testing' but was '%s'", resp.Header.Get("X-Test"))
54 }
55 if string(respBody) != "Text" {
56 t.Errorf("expected body to be 'test' but was '%s'", respBody)
57 }
58}
59
60func fakeRequest() *http.Request {
61 r, _ := http.NewRequest("GET", "/", nil)

Callers

nothing calls this directly

Calls 4

ServeHTTPMethod · 0.95
fakeRequestFunction · 0.85
WeakStringTypeAlias · 0.85
GetMethod · 0.65

Tested by

no test coverage detected