-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathearlygrave_test.go
259 lines (238 loc) · 7.31 KB
/
earlygrave_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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package earlygrave
import (
"errors"
"fmt"
"net/http"
"testing"
)
func decorator1(f Filter) Filter {
return Filter(func(r *http.Request) (*http.Request, error) {
rr, err := http.NewRequest(http.MethodHead, r.URL.String(), r.Body)
if err != nil {
return rr, err
}
return f(rr)
})
}
func decorator2(f Filter) Filter {
return Filter(func(r *http.Request) (*http.Request, error) {
rr, err := http.NewRequest(r.Method, fmt.Sprintf("%s/world", r.URL.String()), r.Body)
if err != nil {
return rr, err
}
return f(rr)
})
}
func TestNew(t *testing.T) {
funct := New(decorator2, decorator1)
rr, err := http.NewRequest(http.MethodGet, "/hello", nil)
if err != nil {
t.Errorf("creating a new request failed: %s", err)
}
req, err2 := funct(rr)
if err2 != nil {
t.Errorf("funct returned error: %s", err)
}
expectedURL := "/hello/world"
expectedMethod := http.MethodHead
if req.URL.String() != expectedURL {
t.Errorf("Expected request URL to be %s but got %s", expectedURL, req.URL.String())
}
if req.Method != expectedMethod {
t.Errorf("Expected request method to be %s but got %s", expectedMethod, req.Method)
}
}
func TestSortValidator(t *testing.T) {
tt := []struct {
Url string
ExpectedErr error
}{
{Url: "/", ExpectedErr: nil},
{Url: "/?sort=name", ExpectedErr: nil},
{Url: "/?sort=-name", ExpectedErr: nil},
{Url: "/?sort=rank", ExpectedErr: fmt.Errorf("rank is not sortable")},
{Url: "/?sort=-rank", ExpectedErr: fmt.Errorf("rank is not sortable")},
}
for _, te := range tt {
funct := New(SortValidator([]string{"name", "role"}))
rr, err := http.NewRequest(http.MethodGet, te.Url, nil)
if err != nil {
t.Errorf("Failed creating request: %q", err)
}
_, pErr := funct(rr)
if te.ExpectedErr == nil && pErr != nil {
t.Errorf("Expected no error but got %q", pErr)
}
if te.ExpectedErr != nil && pErr == nil {
t.Errorf("Expected error %q but got nothing", te.ExpectedErr)
}
if te.ExpectedErr != nil && pErr != nil && te.ExpectedErr.Error() != pErr.Error() {
t.Errorf("Expected error %q but got %q", te.ExpectedErr, pErr)
}
}
}
func TestPaginationValidator(t *testing.T) {
tt := []struct {
Url string
ExpectedErr error
}{
{Url: "/", ExpectedErr: nil},
{Url: "/?limit=100", ExpectedErr: nil},
{Url: "/?offset=3", ExpectedErr: nil},
{Url: "/?offset=3&limit=34", ExpectedErr: nil},
{Url: "/?limit=s200", ExpectedErr: fmt.Errorf("strconv.Atoi: parsing \"s200\": invalid syntax")},
{Url: "/?offset=blabla", ExpectedErr: fmt.Errorf("strconv.Atoi: parsing \"blabla\": invalid syntax")},
}
for _, te := range tt {
funct := New(PaginationValidator())
rr, err := http.NewRequest(http.MethodGet, te.Url, nil)
if err != nil {
t.Errorf("Failed creating request: %q", err)
}
_, pErr := funct(rr)
if te.ExpectedErr == nil && pErr != nil {
t.Errorf("Expected no error but got %q", pErr)
}
if te.ExpectedErr != nil && pErr == nil {
t.Errorf("Expected error %q but got nothing", te.ExpectedErr)
}
if te.ExpectedErr != nil && pErr != nil && te.ExpectedErr.Error() != pErr.Error() {
t.Errorf("Expected error %q but got %q", te.ExpectedErr, pErr)
}
}
}
func TestPaginationExtractor(t *testing.T) {
tt := []struct {
Url string
Result Pagination
}{
{Url: "/", Result: Pagination{Offset: "0", Limit: "30"}},
{Url: "/?offset=3&limit=34", Result: Pagination{Offset: "3", Limit: "34"}},
{Url: "/?offset=34", Result: Pagination{Offset: "34", Limit: "30"}},
{Url: "/?limit=20", Result: Pagination{Offset: "0", Limit: "20"}},
}
for _, te := range tt {
funct := New(PaginationExtractor(Pagination{Limit: "30", Offset: "0"}))
rr, err := http.NewRequest(http.MethodGet, te.Url, nil)
if err != nil {
t.Errorf("Failed creating request: %q", err)
}
res, pErr := funct(rr)
if pErr != nil {
t.Errorf("Expected no error but got %q", pErr)
}
v, eErr := GetPaginationContext(res)
if eErr != nil {
t.Errorf("Expected to have pagination data in context but found none")
}
if v.Limit != te.Result.Limit {
t.Errorf("Expected limit to be %q but got %q", te.Result.Limit, v.Limit)
}
if v.Offset != te.Result.Offset {
t.Errorf("Expected offset to be %q but got %q", te.Result.Offset, v.Offset)
}
}
}
func TestSortExtractor(t *testing.T) {
tt := []struct {
Url string
Result Sort
}{
{Url: "/", Result: Sort{Column: "name", Direction: "DESC"}},
{Url: "/?sort=user", Result: Sort{Column: "user", Direction: "ASC"}},
{Url: "/?sort=-user", Result: Sort{Column: "user", Direction: "DESC"}},
}
for _, te := range tt {
funct := New(SortExtractor(Sort{Column: "name", Direction: "DESC"}))
rr, err := http.NewRequest(http.MethodGet, te.Url, nil)
if err != nil {
t.Errorf("Failed creating request: %q", err)
}
res, pErr := funct(rr)
if pErr != nil {
t.Errorf("Expected no error but got %q", pErr)
}
v, eErr := GetSortContext(res)
if eErr != nil {
t.Errorf("Expected to have pagination data in context but found none")
}
if v.Direction != te.Result.Direction {
t.Errorf("Expected direction to be %q but got %q", te.Result.Direction, v.Direction)
}
if v.Column != te.Result.Column {
t.Errorf("Expected column to be %q but got %q", te.Result.Column, v.Column)
}
}
}
func TestGetPaginationContextFailure(t *testing.T) {
rr, err := http.NewRequest(http.MethodGet, "/?sort=-name", nil)
if err != nil {
t.Errorf("Failed creating request: %q", err)
}
_, err = GetPaginationContext(rr)
if err == nil {
t.Errorf("Expected to have error but got none")
}
if _, ok := err.(ECtxNotExist); !ok {
t.Errorf("Expected error to be %q but got %q", "No pagination was found", err)
}
}
func TestGetSortContextFailure(t *testing.T) {
rr, err := http.NewRequest(http.MethodGet, "/?limit=10", nil)
if err != nil {
t.Errorf("Failed creating request: %q", err)
}
_, err = GetSortContext(rr)
if err == nil {
t.Errorf("Expected to have error but got none")
}
if _, ok := err.(ECtxNotExist); !ok {
t.Errorf("Expected error to be %q but got %q", "No sort was found", err)
}
}
func extractor1(r *http.Request) (*http.Request, error) {
return nil, fmt.Errorf("Oops!")
}
func TestExtractParamFailure(t *testing.T) {
funct := New(ExtractParam(extractor1))
rr, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Errorf("creating a new request failed: %s", err)
}
_, err = funct(rr)
if err == nil {
t.Errorf("Expected to have error but got none")
}
}
func TestChoiceValidator(t *testing.T) {
funct := New(ChoiceValidator("currency", map[string]bool{"USD": true, "NIS": true}))
tcs := []struct {
name string
expectedErr error
url string
}{
{
name: "invalid choice",
expectedErr: errors.New("BLA is an invalid option for currency"),
url: "/?currency=BLA",
},
}
for _, tc := range tcs {
rr, err := http.NewRequest(http.MethodGet, tc.url, nil)
if err != nil {
t.Errorf("%s: creating a new request failed: %s", tc.name, err)
}
_, err = funct(rr)
if tc.expectedErr != nil && err != nil {
if err.Error() != tc.expectedErr.Error() {
t.Errorf("%s: expected error to be %s but got %s", tc.name, tc.expectedErr, err)
}
}
if err == nil && tc.expectedErr != nil {
t.Errorf("%s: expected to have error but got none", tc.name)
}
if err != nil && tc.expectedErr == nil {
t.Errorf("%s: expected to have no errors but got: %s", tc.name, err)
}
}
}