-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstaticresponse_test.go
More file actions
92 lines (82 loc) · 2.28 KB
/
staticresponse_test.go
File metadata and controls
92 lines (82 loc) · 2.28 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package staticresponse_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/jdel/staticresponse"
)
type TestCase struct {
desc string
cfg *staticresponse.Config
expectedError bool
}
func TestStaticResponse(t *testing.T) {
testCases := []TestCase{
{
desc: "default config",
cfg: staticresponse.CreateConfig(),
},
{
desc: "custom config teapot multi-value header",
cfg: &staticresponse.Config{
StatusCode: 418,
Body: "I'm a teapot",
Headers: http.Header{"Cake": {"One", "Two"}},
},
},
{
desc: "custom config json multiple headers",
cfg: &staticresponse.Config{
StatusCode: 200,
Body: "{\"statusCode\": \"OK\"}",
Headers: http.Header{"One": {"1"}, "Two": {"2"}},
},
},
{
desc: "custom config 200 no body",
cfg: &staticresponse.Config{StatusCode: 200},
},
{
desc: "custom config 1000 error",
cfg: &staticresponse.Config{StatusCode: 1000, Body: "NOK"},
expectedError: true,
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
handler, err := staticresponse.New(ctx, next, test.cfg, "staticresponse")
if test.expectedError {
if err == nil {
t.Fatal("error expected")
}
} else {
recorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
assertResult(t, *recorder, test)
}
})
}
}
func assertResult(t *testing.T, recorder httptest.ResponseRecorder, test TestCase) {
t.Helper()
if recorder.Result().StatusCode != test.cfg.StatusCode {
t.Errorf("invalid response code: %d (expected %d)", recorder.Result().StatusCode, test.cfg.StatusCode)
}
if b, err := io.ReadAll(recorder.Result().Body); err == nil && string(b) != test.cfg.Body {
t.Errorf("invalid response body: %s (expected %s)", string(b), test.cfg.Body)
}
if !reflect.DeepEqual(recorder.Result().Header, test.cfg.Headers) {
t.Errorf("headers mismatch: %v (expected %v)", test.cfg.Headers, recorder.Result().Header)
}
}