-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
140 lines (134 loc) · 3.07 KB
/
handler_test.go
File metadata and controls
140 lines (134 loc) · 3.07 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package jsonapi
import (
"errors"
"net/http/httptest"
"testing"
)
func TestHandler(t *testing.T) {
fac := func(data interface{}, err error) Handler {
return func(req Request) (interface{}, error) {
return data, err
}
}
run := func(h Handler) *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
basicCases := []struct {
name string
expect string
h Handler
status int
}{
{
name: "nil-nil",
expect: `{"data":null}`,
h: fac(nil, nil),
status: 200,
},
{
name: "int-nil",
expect: `{"data":1}`,
h: fac(1, nil),
status: 200,
},
{
name: "string-nil",
expect: `{"data":"asd"}`,
h: fac("asd", nil),
status: 200,
},
{
name: "struct-nil",
expect: `{"data":{"A":1}}`,
h: fac(struct{ A int }{A: 1}, nil),
status: 200,
},
{
name: "chan-nil",
expect: `{"errors":[{"detail":"Failed to marshal data"}]}`,
h: fac(make(chan int), nil),
status: 500,
},
{
name: "nil-error",
expect: `{"errors":[{"detail":"my error"}]}`,
h: fac(nil, errors.New("my error")),
status: 500,
},
{
name: "data-error",
expect: `{"errors":[{"detail":"my error"}]}`,
h: fac(1, errors.New("my error")),
status: 500,
},
{
name: "nil-APPERR",
expect: `{"errors":[{"code":"qq","detail":"my error"}]}`,
h: fac(nil, APPERR.SetCode("qq").SetData("my error")),
status: 200,
},
{
name: "data-APPERR",
expect: `{"errors":[{"code":"qq","detail":"my error"}]}`,
h: fac(1, APPERR.SetCode("qq").SetData("my error")),
status: 200,
},
{
name: "nil-E404",
expect: `{"errors":[{"code":"qq","detail":"my error"}]}`,
h: fac(nil, E404.SetCode("qq").SetData("my error")),
status: 404,
},
{
name: "data-E404",
expect: `{"errors":[{"code":"qq","detail":"my error"}]}`,
h: fac(1, E404.SetCode("qq").SetData("my error")),
status: 404,
},
}
for _, c := range basicCases {
c.expect += "\n"
t.Run(c.name, func(t *testing.T) {
w := run(c.h)
if w.Code != c.status {
t.Errorf(
"expected status %d, got %d",
c.status,
w.Code,
)
}
if actual := w.Body.String(); c.expect != actual {
t.Errorf(
"expected %#v, got %#v",
c.expect,
actual,
)
}
})
}
uri := "http://a.b"
t.Run("nil-E301", func(t *testing.T) {
w := run(fac(nil, E301.SetData(uri)))
if w.Code != 301 {
t.Errorf("unexpected status: %d", w.Code)
}
if l := w.HeaderMap.Get("Location"); l != uri {
t.Errorf("unexpected location header: %s", l)
}
})
t.Run("data-E301", func(t *testing.T) {
w := run(fac(1, E301.SetData(uri)))
if w.Code != 301 {
t.Errorf("unexpected status: %d", w.Code)
}
if l := w.HeaderMap.Get("Location"); l != uri {
t.Errorf("unexpected location header: %s", l)
}
})
}