...
1 package cleanhttp
2
3 import (
4 "fmt"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8 )
9
10 func TestPrintablePathCheckHandler(t *testing.T) {
11 getTestHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12 fmt.Fprintln(w, "Hello, client")
13 })
14
15 cases := map[string]struct {
16 path string
17 expectCode int
18 input *HandlerInput
19 }{
20 "valid nil input": {
21 path: "/valid",
22 expectCode: http.StatusOK,
23 input: nil,
24 },
25
26 "valid empty error status": {
27 path: "/valid",
28 expectCode: http.StatusOK,
29 input: &HandlerInput{},
30 },
31
32 "invalid newline": {
33 path: "/invalid%0A",
34 expectCode: http.StatusBadRequest,
35 },
36
37 "invalid carriage return": {
38 path: "/invalid%0D",
39 expectCode: http.StatusBadRequest,
40 },
41
42 "invalid null": {
43 path: "/invalid%00",
44 expectCode: http.StatusBadRequest,
45 },
46
47 "invalid alternate status": {
48 path: "/invalid%0A",
49 expectCode: http.StatusInternalServerError,
50 input: &HandlerInput{
51 ErrStatus: http.StatusInternalServerError,
52 },
53 },
54 }
55
56 for name, tc := range cases {
57 t.Run(name, func(t *testing.T) {
58
59 ts := httptest.NewServer(PrintablePathCheckHandler(getTestHandler, tc.input))
60 defer ts.Close()
61
62 res, err := http.Get(ts.URL + tc.path)
63 if err != nil {
64 t.Fatal(err)
65 }
66
67 if tc.expectCode != res.StatusCode {
68 t.Fatalf("expected %d, got :%d", tc.expectCode, res.StatusCode)
69 }
70 })
71 }
72 }
73
View as plain text