@@ -44,12 +44,28 @@ func TestAPIKey(t *testing.T) {
44
44
r = httptest .NewRequest ("GET" , "/" , nil )
45
45
rw = httptest .NewRecorder ()
46
46
)
47
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
47
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
48
48
res := rw .Result ()
49
49
defer res .Body .Close ()
50
50
require .Equal (t , http .StatusUnauthorized , res .StatusCode )
51
51
})
52
52
53
+ t .Run ("NoCookieRedirects" , func (t * testing.T ) {
54
+ t .Parallel ()
55
+ var (
56
+ db = databasefake .New ()
57
+ r = httptest .NewRequest ("GET" , "/" , nil )
58
+ rw = httptest .NewRecorder ()
59
+ )
60
+ httpmw .ExtractAPIKey (db , nil , true )(successHandler ).ServeHTTP (rw , r )
61
+ res := rw .Result ()
62
+ defer res .Body .Close ()
63
+ location , err := res .Location ()
64
+ require .NoError (t , err )
65
+ require .NotEmpty (t , location .Query ().Get ("message" ))
66
+ require .Equal (t , http .StatusTemporaryRedirect , res .StatusCode )
67
+ })
68
+
53
69
t .Run ("InvalidFormat" , func (t * testing.T ) {
54
70
t .Parallel ()
55
71
var (
@@ -62,7 +78,7 @@ func TestAPIKey(t *testing.T) {
62
78
Value : "test-wow-hello" ,
63
79
})
64
80
65
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
81
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
66
82
res := rw .Result ()
67
83
defer res .Body .Close ()
68
84
require .Equal (t , http .StatusUnauthorized , res .StatusCode )
@@ -80,7 +96,7 @@ func TestAPIKey(t *testing.T) {
80
96
Value : "test-wow" ,
81
97
})
82
98
83
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
99
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
84
100
res := rw .Result ()
85
101
defer res .Body .Close ()
86
102
require .Equal (t , http .StatusUnauthorized , res .StatusCode )
@@ -98,7 +114,7 @@ func TestAPIKey(t *testing.T) {
98
114
Value : "testtestid-wow" ,
99
115
})
100
116
101
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
117
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
102
118
res := rw .Result ()
103
119
defer res .Body .Close ()
104
120
require .Equal (t , http .StatusUnauthorized , res .StatusCode )
@@ -117,7 +133,7 @@ func TestAPIKey(t *testing.T) {
117
133
Value : fmt .Sprintf ("%s-%s" , id , secret ),
118
134
})
119
135
120
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
136
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
121
137
res := rw .Result ()
122
138
defer res .Body .Close ()
123
139
require .Equal (t , http .StatusUnauthorized , res .StatusCode )
@@ -145,7 +161,7 @@ func TestAPIKey(t *testing.T) {
145
161
UserID : user .ID ,
146
162
})
147
163
require .NoError (t , err )
148
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
164
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
149
165
res := rw .Result ()
150
166
defer res .Body .Close ()
151
167
require .Equal (t , http .StatusUnauthorized , res .StatusCode )
@@ -172,7 +188,7 @@ func TestAPIKey(t *testing.T) {
172
188
UserID : user .ID ,
173
189
})
174
190
require .NoError (t , err )
175
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
191
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
176
192
res := rw .Result ()
177
193
defer res .Body .Close ()
178
194
require .Equal (t , http .StatusUnauthorized , res .StatusCode )
@@ -200,7 +216,7 @@ func TestAPIKey(t *testing.T) {
200
216
UserID : user .ID ,
201
217
})
202
218
require .NoError (t , err )
203
- httpmw .ExtractAPIKey (db , nil )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
219
+ httpmw .ExtractAPIKey (db , nil , false )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
204
220
// Checks that it exists on the context!
205
221
_ = httpmw .APIKey (r )
206
222
httpapi .Write (rw , http .StatusOK , httpapi.Response {
@@ -238,7 +254,7 @@ func TestAPIKey(t *testing.T) {
238
254
UserID : user .ID ,
239
255
})
240
256
require .NoError (t , err )
241
- httpmw .ExtractAPIKey (db , nil )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
257
+ httpmw .ExtractAPIKey (db , nil , false )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
242
258
// Checks that it exists on the context!
243
259
_ = httpmw .APIKey (r )
244
260
httpapi .Write (rw , http .StatusOK , httpapi.Response {
@@ -273,7 +289,7 @@ func TestAPIKey(t *testing.T) {
273
289
UserID : user .ID ,
274
290
})
275
291
require .NoError (t , err )
276
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
292
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
277
293
res := rw .Result ()
278
294
defer res .Body .Close ()
279
295
require .Equal (t , http .StatusOK , res .StatusCode )
@@ -308,7 +324,7 @@ func TestAPIKey(t *testing.T) {
308
324
UserID : user .ID ,
309
325
})
310
326
require .NoError (t , err )
311
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
327
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
312
328
res := rw .Result ()
313
329
defer res .Body .Close ()
314
330
require .Equal (t , http .StatusOK , res .StatusCode )
@@ -344,7 +360,7 @@ func TestAPIKey(t *testing.T) {
344
360
UserID : user .ID ,
345
361
})
346
362
require .NoError (t , err )
347
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
363
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
348
364
res := rw .Result ()
349
365
defer res .Body .Close ()
350
366
require .Equal (t , http .StatusOK , res .StatusCode )
@@ -391,7 +407,7 @@ func TestAPIKey(t *testing.T) {
391
407
return token , nil
392
408
}),
393
409
},
394
- })(successHandler ).ServeHTTP (rw , r )
410
+ }, false )(successHandler ).ServeHTTP (rw , r )
395
411
res := rw .Result ()
396
412
defer res .Body .Close ()
397
413
require .Equal (t , http .StatusOK , res .StatusCode )
@@ -428,7 +444,7 @@ func TestAPIKey(t *testing.T) {
428
444
UserID : user .ID ,
429
445
})
430
446
require .NoError (t , err )
431
- httpmw .ExtractAPIKey (db , nil )(successHandler ).ServeHTTP (rw , r )
447
+ httpmw .ExtractAPIKey (db , nil , false )(successHandler ).ServeHTTP (rw , r )
432
448
res := rw .Result ()
433
449
defer res .Body .Close ()
434
450
require .Equal (t , http .StatusOK , res .StatusCode )
0 commit comments