Repository navigation
Expand file tree
/
Copy pathquery_test.go
More file actions
188 lines (162 loc) · 5.4 KB
/
Copy pathquery_test.go
File metadata and controls
188 lines (162 loc) · 5.4 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
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
package api
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/jgolang/errors"
"github.com/jgolang/errors/codes"
)
func queryRequest(rawQuery string) *http.Request {
r := httptest.NewRequest(http.MethodGet, "/tasks", nil)
r.URL.RawQuery = rawQuery
return r
}
func TestQueryInt(t *testing.T) {
tests := []struct {
name string
query string
wantValue int
wantOK bool
wantCode codes.Coder
}{
{name: "present", query: "limit=20", wantValue: 20, wantOK: true},
{name: "signed", query: "limit=-5", wantValue: -5, wantOK: true},
{name: "absent", query: "", wantOK: false},
{name: "empty", query: "limit=", wantOK: true, wantCode: codes.ValFormat},
{name: "not a number", query: "limit=abc", wantOK: true, wantCode: codes.ValFormat},
{name: "first value wins", query: "limit=abc&limit=5", wantOK: true, wantCode: codes.ValFormat},
{name: "malformed escape", query: "limit=%zz", wantOK: true, wantCode: codes.ValFormat},
{name: "semicolon", query: "a=1;limit=5", wantOK: true, wantCode: codes.ValFormat},
{name: "malformed elsewhere", query: "a=%zz&limit=7", wantValue: 7, wantOK: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, ok, err := QueryInt(queryRequest(tt.query), "limit")
if value != tt.wantValue || ok != tt.wantOK || errors.CodeOf(err) != tt.wantCode {
t.Fatalf("QueryInt = %d, %v, %v; want %d, %v, code %v", value, ok, err, tt.wantValue, tt.wantOK, tt.wantCode)
}
})
}
}
func TestQueryBool(t *testing.T) {
tests := []struct {
name string
query string
wantValue bool
wantOK bool
wantErr bool
}{
{name: "true", query: "in_inbox=true", wantValue: true, wantOK: true},
{name: "absent", query: "", wantOK: false},
{name: "empty", query: "in_inbox=", wantOK: true, wantErr: true},
{name: "invalid", query: "in_inbox=maybe", wantOK: true, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, ok, err := QueryBool(queryRequest(tt.query), "in_inbox")
if value != tt.wantValue || ok != tt.wantOK || (err != nil) != tt.wantErr {
t.Fatalf("QueryBool = %v, %v, %v", value, ok, err)
}
})
}
}
func TestQueryEnum(t *testing.T) {
tests := []struct {
name string
query string
wantValue string
wantOK bool
wantErr bool
}{
{name: "allowed", query: "in_inbox=true", wantValue: "true", wantOK: true},
{name: "outside the enum", query: "in_inbox=false", wantOK: true, wantErr: true},
{name: "absent", query: "", wantOK: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, ok, err := QueryEnum(queryRequest(tt.query), "in_inbox", []string{"true"})
if value != tt.wantValue || ok != tt.wantOK || (err != nil) != tt.wantErr {
t.Fatalf("QueryEnum = %q, %v, %v", value, ok, err)
}
})
}
}
func TestQueryEnumErrorNamesTheAllowedValues(t *testing.T) {
_, _, err := QueryEnum(queryRequest("status=x"), "status", []string{"open", "done"})
if err == nil || err.Error() != `[b002](Invalid format.): query parameter "status" must be one of open, done` {
t.Fatalf("unexpected error %v", err)
}
}
func TestQueryString(t *testing.T) {
tests := []struct {
name string
query string
wantValue string
wantOK bool
}{
{name: "present", query: "q=tasks", wantValue: "tasks", wantOK: true},
{name: "empty", query: "q=", wantValue: "", wantOK: true},
{name: "absent", query: "", wantValue: "", wantOK: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, ok, err := QueryString(queryRequest(tt.query), "q")
if value != tt.wantValue || ok != tt.wantOK || err != nil {
t.Fatalf("QueryString = %q, %v, %v", value, ok, err)
}
})
}
}
func TestQueryErrorAnswers400(t *testing.T) {
_, _, err := QueryInt(queryRequest("limit=abc"), "limit")
if StatusForKind(errors.KindOf(err)) != http.StatusBadRequest {
t.Fatalf("expected a validation kind answering 400, got %v", errors.KindOf(err))
}
}
func TestWithQueryCodeOverridesTheCode(t *testing.T) {
limitInvalid := codes.New("PLN-050", "limit must be a number").WithKind(codes.KindValidation)
tests := []struct {
name string
call func(QueryOption) error
}{
{name: "QueryInt", call: func(o QueryOption) error { _, _, err := QueryInt(queryRequest("v=x"), "v", o); return err }},
{name: "QueryBool", call: func(o QueryOption) error { _, _, err := QueryBool(queryRequest("v=x"), "v", o); return err }},
{name: "QueryEnum", call: func(o QueryOption) error {
_, _, err := QueryEnum(queryRequest("v=x"), "v", []string{"y"}, o)
return err
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := errors.CodeOf(tt.call(WithQueryCode(limitInvalid))); got != limitInvalid {
t.Fatalf("expected the override code, got %v", got)
}
})
}
}
func TestNilQueryOptionsAreIgnored(t *testing.T) {
tests := []struct {
name string
opt QueryOption
}{
{name: "nil option", opt: nil},
{name: "nil code", opt: WithQueryCode(nil)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, _, err := QueryInt(queryRequest("limit=abc"), "limit", tt.opt)
if errors.CodeOf(err) != codes.ValFormat {
t.Fatalf("expected the default code, got %v", err)
}
})
}
}
func BenchmarkQueryInt(b *testing.B) {
r := queryRequest("limit=20&offset=40&in_inbox=true")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, _, err := QueryInt(r, "limit"); err != nil {
b.Fatal(err)
}
}
}