-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlpath_test.go
More file actions
204 lines (153 loc) · 3.38 KB
/
Copy pathurlpath_test.go
File metadata and controls
204 lines (153 loc) · 3.38 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) CattleCloud LLC
// SPDX-License-Identifier: BSD-3-Clause
package urlpath
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/shoenig/test/must"
)
func Test_MustParse(t *testing.T) {
t.Parallel()
router := mux.NewRouter()
executed := false
router.HandleFunc("/v1/{foo}/{bar}", func(_ http.ResponseWriter, r *http.Request) {
var foo string
var bar int
MustParse(r, Schema{
"foo": String(&foo),
"bar": Int(&bar),
})
must.Eq(t, "blah", foo)
must.Eq(t, 31, bar)
executed = true
})
w := httptest.NewRecorder()
ctx := context.Background()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "/v1/blah/31", nil)
must.NoError(t, err)
router.ServeHTTP(w, request)
must.True(t, executed)
}
func Test_MustParse_panic(t *testing.T) {
t.Parallel()
router := mux.NewRouter()
executed := false
router.HandleFunc("/v1/{foo}/{bar}", func(_ http.ResponseWriter, r *http.Request) {
var foo string
var bar int
executed = true
must.Panic(t, func() {
MustParse(r, Schema{
"foo": String(&foo),
"bar": Int(&bar),
})
})
})
w := httptest.NewRecorder()
ctx := context.Background()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "/v1/blah/bad", nil)
must.NoError(t, err)
router.ServeHTTP(w, request)
must.True(t, executed)
}
func Test_Parse(t *testing.T) {
t.Parallel()
router := mux.NewRouter()
executed := false
router.HandleFunc("/v1/{foo}/{bar}", func(_ http.ResponseWriter, r *http.Request) {
var foo string
var bar int
err := Parse(r, Schema{
"foo": String(&foo),
"bar": Int(&bar),
})
must.NoError(t, err)
must.Eq(t, "blah", foo)
must.Eq(t, 31, bar)
executed = true
})
w := httptest.NewRecorder()
ctx := context.Background()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "/v1/blah/31", nil)
must.NoError(t, err)
router.ServeHTTP(w, request)
must.True(t, executed)
}
func Test_ParseValues(t *testing.T) {
t.Parallel()
var foo string
var bar int
values := map[string]string{
"foo": "blah",
"bar": "21",
}
err := ParseValues(values, Schema{
"foo": String(&foo),
"bar": Int(&bar),
})
must.NoError(t, err)
must.Eq(t, "blah", foo)
must.Eq(t, 21, bar)
}
func Test_ParseValues_incompatible(t *testing.T) {
t.Parallel()
var foo string
var bar int
values := map[string]string{
"foo": "blah",
"bar": "not an int",
}
err := ParseValues(values, Schema{
"foo": String(&foo),
"bar": Int(&bar),
})
must.Error(t, err)
}
func Test_ParseValues_missing(t *testing.T) {
t.Parallel()
var foo string
var bar int
values := map[string]string{
"foo": "blah",
}
err := ParseValues(values, Schema{
"foo": String(&foo),
"bar": Int(&bar),
})
must.Error(t, err)
}
func Test_Parameter_String(t *testing.T) {
t.Parallel()
p := Parameter("foo")
s := p.String()
must.EqOp(t, "{foo}", s)
}
func Test_ParseValues_StringType_Parse(t *testing.T) {
t.Parallel()
values := map[string]string{
"KEY": "value",
}
type ident string
var id ident
err := ParseValues(values, Schema{
"KEY": String(&id),
})
must.NoError(t, err)
must.Eq(t, "value", id)
}
func Test_ParseValues_IntType_Parse(t *testing.T) {
t.Parallel()
type years int
var age years
values := map[string]string{
"age": "51",
}
err := ParseValues(values, Schema{
"age": Int(&age),
})
must.NoError(t, err)
must.Eq(t, 51, age)
}