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