1
1
package irismiddleware
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
_ "embed"
7
+ "encoding/json"
6
8
"errors"
7
9
"fmt"
8
10
"io"
@@ -11,7 +13,6 @@ import (
11
13
"net/url"
12
14
"testing"
13
15
14
- "github.com/deepmap/oapi-codegen/pkg/testutil"
15
16
"github.com/getkin/kin-openapi/openapi3"
16
17
"github.com/getkin/kin-openapi/openapi3filter"
17
18
"github.com/kataras/iris/v12"
@@ -28,8 +29,18 @@ func doGet(t *testing.T, i *iris.Application, rawURL string) *httptest.ResponseR
28
29
t .Fatalf ("Invalid url: %s" , rawURL )
29
30
}
30
31
31
- response := testutil .NewRequest ().Get (u .RequestURI ()).WithHost (u .Host ).WithAcceptJson ().GoWithHTTPHandler (t , i )
32
- return response .Recorder
32
+ r , err := http .NewRequest (http .MethodGet , u .String (), nil )
33
+ if err != nil {
34
+ t .Fatalf ("Could not construct a request: %s" , rawURL )
35
+ }
36
+ r .Header .Set ("accept" , "application/json" )
37
+ r .Header .Set ("host" , u .Host )
38
+
39
+ tt := httptest .NewRecorder ()
40
+
41
+ i .ServeHTTP (tt , r )
42
+
43
+ return tt
33
44
}
34
45
35
46
func doPost (t * testing.T , i * iris.Application , rawURL string , jsonBody interface {}) * httptest.ResponseRecorder {
@@ -38,8 +49,24 @@ func doPost(t *testing.T, i *iris.Application, rawURL string, jsonBody interface
38
49
t .Fatalf ("Invalid url: %s" , rawURL )
39
50
}
40
51
41
- response := testutil .NewRequest ().Post (u .RequestURI ()).WithHost (u .Host ).WithJsonBody (jsonBody ).GoWithHTTPHandler (t , i )
42
- return response .Recorder
52
+ body , err := json .Marshal (jsonBody )
53
+ if err != nil {
54
+ t .Fatalf ("Could not marshal request body: %v" , err )
55
+ }
56
+
57
+ r , err := http .NewRequest (http .MethodPost , u .String (), bytes .NewReader (body ))
58
+ if err != nil {
59
+ t .Fatalf ("Could not construct a request for URL %s: %v" , rawURL , err )
60
+ }
61
+ r .Header .Set ("accept" , "application/json" )
62
+ r .Header .Set ("content-type" , "application/json" )
63
+ r .Header .Set ("host" , u .Host )
64
+
65
+ tt := httptest .NewRecorder ()
66
+
67
+ i .ServeHTTP (tt , r )
68
+
69
+ return tt
43
70
}
44
71
45
72
func TestOapiRequestValidator (t * testing.T ) {
@@ -115,30 +142,30 @@ func TestOapiRequestValidator(t *testing.T) {
115
142
116
143
// Let's send the request to the wrong server, this should fail validation
117
144
{
118
- res := doGet (t , i , "https ://not.deepmap.ai/resource" )
145
+ res := doGet (t , i , "http ://not.deepmap.ai/resource" )
119
146
assert .Equal (t , http .StatusBadRequest , res .Code )
120
147
assert .False (t , called , "Handler should not have been called" )
121
148
}
122
149
123
150
// Let's send a good request, it should pass
124
151
{
125
- res := doGet (t , i , "https ://deepmap.ai/resource" )
152
+ res := doGet (t , i , "http ://deepmap.ai/resource" )
126
153
assert .Equal (t , http .StatusOK , res .Code )
127
154
assert .True (t , called , "Handler should have been called" )
128
155
called = false
129
156
}
130
157
131
158
// Send an out-of-spec parameter
132
159
{
133
- res := doGet (t , i , "https ://deepmap.ai/resource?id=500" )
160
+ res := doGet (t , i , "http ://deepmap.ai/resource?id=500" )
134
161
assert .Equal (t , http .StatusBadRequest , res .Code )
135
162
assert .False (t , called , "Handler should not have been called" )
136
163
called = false
137
164
}
138
165
139
166
// Send a bad parameter type
140
167
{
141
- res := doGet (t , i , "https ://deepmap.ai/resource?id=foo" )
168
+ res := doGet (t , i , "http ://deepmap.ai/resource?id=foo" )
142
169
assert .Equal (t , http .StatusBadRequest , res .Code )
143
170
assert .False (t , called , "Handler should not have been called" )
144
171
called = false
@@ -152,7 +179,7 @@ func TestOapiRequestValidator(t *testing.T) {
152
179
}{
153
180
Name : "Marcin" ,
154
181
}
155
- res := doPost (t , i , "https ://deepmap.ai/resource" , body )
182
+ res := doPost (t , i , "http ://deepmap.ai/resource" , body )
156
183
assert .Equal (t , http .StatusNoContent , res .Code )
157
184
assert .True (t , called , "Handler should have been called" )
158
185
called = false
@@ -165,31 +192,31 @@ func TestOapiRequestValidator(t *testing.T) {
165
192
}{
166
193
Name : 7 ,
167
194
}
168
- res := doPost (t , i , "https ://deepmap.ai/resource" , body )
195
+ res := doPost (t , i , "http ://deepmap.ai/resource" , body )
169
196
assert .Equal (t , http .StatusBadRequest , res .Code )
170
197
assert .False (t , called , "Handler should not have been called" )
171
198
called = false
172
199
}
173
200
174
201
// Call a protected function to which we have access
175
202
{
176
- res := doGet (t , i , "https ://deepmap.ai/protected_resource" )
203
+ res := doGet (t , i , "http ://deepmap.ai/protected_resource" )
177
204
assert .Equal (t , http .StatusNoContent , res .Code )
178
205
assert .True (t , called , "Handler should have been called" )
179
206
called = false
180
207
}
181
208
182
209
// Call a protected function to which we don't have access
183
210
{
184
- res := doGet (t , i , "https ://deepmap.ai/protected_resource2" )
211
+ res := doGet (t , i , "http ://deepmap.ai/protected_resource2" )
185
212
assert .Equal (t , http .StatusBadRequest , res .Code )
186
213
assert .False (t , called , "Handler should not have been called" )
187
214
called = false
188
215
}
189
216
190
217
// Call a protected function without credentials
191
218
{
192
- res := doGet (t , i , "https ://deepmap.ai/protected_resource_401" )
219
+ res := doGet (t , i , "http ://deepmap.ai/protected_resource_401" )
193
220
assert .Equal (t , http .StatusBadRequest , res .Code )
194
221
body , err := io .ReadAll (res .Body )
195
222
if assert .NoError (t , err ) {
@@ -234,7 +261,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
234
261
235
262
// Let's send a good request, it should pass
236
263
{
237
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=50&id2=50" )
264
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=50&id2=50" )
238
265
assert .Equal (t , http .StatusOK , res .Code )
239
266
assert .True (t , called , "Handler should have been called" )
240
267
called = false
@@ -243,7 +270,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
243
270
// Let's send a request with a missing parameter, it should return
244
271
// a bad status
245
272
{
246
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=50" )
273
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=50" )
247
274
assert .Equal (t , http .StatusBadRequest , res .Code )
248
275
body , err := io .ReadAll (res .Body )
249
276
if assert .NoError (t , err ) {
@@ -258,7 +285,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
258
285
// Let's send a request with a 2 missing parameters, it should return
259
286
// a bad status
260
287
{
261
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource" )
288
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource" )
262
289
assert .Equal (t , http .StatusBadRequest , res .Code )
263
290
body , err := io .ReadAll (res .Body )
264
291
if assert .NoError (t , err ) {
@@ -275,7 +302,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
275
302
// Let's send a request with a 1 missing parameter, and another outside
276
303
// or the parameters. It should return a bad status
277
304
{
278
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=500" )
305
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=500" )
279
306
assert .Equal (t , http .StatusBadRequest , res .Code )
280
307
body , err := io .ReadAll (res .Body )
281
308
if assert .NoError (t , err ) {
@@ -292,7 +319,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
292
319
// Let's send a request with a parameters that do not meet spec. It should
293
320
// return a bad status
294
321
{
295
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=abc&id2=1" )
322
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=abc&id2=1" )
296
323
assert .Equal (t , http .StatusBadRequest , res .Code )
297
324
body , err := io .ReadAll (res .Body )
298
325
if assert .NoError (t , err ) {
@@ -344,7 +371,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
344
371
345
372
// Let's send a good request, it should pass
346
373
{
347
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=50&id2=50" )
374
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=50&id2=50" )
348
375
assert .Equal (t , http .StatusOK , res .Code )
349
376
assert .True (t , called , "Handler should have been called" )
350
377
called = false
@@ -353,7 +380,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
353
380
// Let's send a request with a missing parameter, it should return
354
381
// a bad status
355
382
{
356
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=50" )
383
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=50" )
357
384
assert .Equal (t , http .StatusBadRequest , res .Code )
358
385
body , err := io .ReadAll (res .Body )
359
386
if assert .NoError (t , err ) {
@@ -368,7 +395,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
368
395
// Let's send a request with a 2 missing parameters, it should return
369
396
// a bad status
370
397
{
371
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource" )
398
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource" )
372
399
assert .Equal (t , http .StatusBadRequest , res .Code )
373
400
body , err := io .ReadAll (res .Body )
374
401
if assert .NoError (t , err ) {
@@ -385,7 +412,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
385
412
// Let's send a request with a 1 missing parameter, and another outside
386
413
// or the parameters. It should return a bad status
387
414
{
388
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=500" )
415
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=500" )
389
416
assert .Equal (t , http .StatusBadRequest , res .Code )
390
417
body , err := io .ReadAll (res .Body )
391
418
if assert .NoError (t , err ) {
@@ -402,7 +429,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
402
429
// Let's send a request with a parameters that do not meet spec. It should
403
430
// return a bad status
404
431
{
405
- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=abc&id2=1" )
432
+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=abc&id2=1" )
406
433
assert .Equal (t , http .StatusBadRequest , res .Code )
407
434
body , err := io .ReadAll (res .Body )
408
435
if assert .NoError (t , err ) {
0 commit comments