11package irismiddleware
22
33import (
4+ "bytes"
45 "context"
56 _ "embed"
7+ "encoding/json"
68 "errors"
79 "fmt"
810 "io"
@@ -11,7 +13,6 @@ import (
1113 "net/url"
1214 "testing"
1315
14- "github.com/deepmap/oapi-codegen/pkg/testutil"
1516 "github.com/getkin/kin-openapi/openapi3"
1617 "github.com/getkin/kin-openapi/openapi3filter"
1718 "github.com/kataras/iris/v12"
@@ -28,8 +29,18 @@ func doGet(t *testing.T, i *iris.Application, rawURL string) *httptest.ResponseR
2829 t .Fatalf ("Invalid url: %s" , rawURL )
2930 }
3031
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
3344}
3445
3546func 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
3849 t .Fatalf ("Invalid url: %s" , rawURL )
3950 }
4051
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
4370}
4471
4572func TestOapiRequestValidator (t * testing.T ) {
@@ -115,30 +142,30 @@ func TestOapiRequestValidator(t *testing.T) {
115142
116143 // Let's send the request to the wrong server, this should fail validation
117144 {
118- res := doGet (t , i , "https ://not.deepmap.ai/resource" )
145+ res := doGet (t , i , "http ://not.deepmap.ai/resource" )
119146 assert .Equal (t , http .StatusBadRequest , res .Code )
120147 assert .False (t , called , "Handler should not have been called" )
121148 }
122149
123150 // Let's send a good request, it should pass
124151 {
125- res := doGet (t , i , "https ://deepmap.ai/resource" )
152+ res := doGet (t , i , "http ://deepmap.ai/resource" )
126153 assert .Equal (t , http .StatusOK , res .Code )
127154 assert .True (t , called , "Handler should have been called" )
128155 called = false
129156 }
130157
131158 // Send an out-of-spec parameter
132159 {
133- res := doGet (t , i , "https ://deepmap.ai/resource?id=500" )
160+ res := doGet (t , i , "http ://deepmap.ai/resource?id=500" )
134161 assert .Equal (t , http .StatusBadRequest , res .Code )
135162 assert .False (t , called , "Handler should not have been called" )
136163 called = false
137164 }
138165
139166 // Send a bad parameter type
140167 {
141- res := doGet (t , i , "https ://deepmap.ai/resource?id=foo" )
168+ res := doGet (t , i , "http ://deepmap.ai/resource?id=foo" )
142169 assert .Equal (t , http .StatusBadRequest , res .Code )
143170 assert .False (t , called , "Handler should not have been called" )
144171 called = false
@@ -152,7 +179,7 @@ func TestOapiRequestValidator(t *testing.T) {
152179 }{
153180 Name : "Marcin" ,
154181 }
155- res := doPost (t , i , "https ://deepmap.ai/resource" , body )
182+ res := doPost (t , i , "http ://deepmap.ai/resource" , body )
156183 assert .Equal (t , http .StatusNoContent , res .Code )
157184 assert .True (t , called , "Handler should have been called" )
158185 called = false
@@ -165,31 +192,31 @@ func TestOapiRequestValidator(t *testing.T) {
165192 }{
166193 Name : 7 ,
167194 }
168- res := doPost (t , i , "https ://deepmap.ai/resource" , body )
195+ res := doPost (t , i , "http ://deepmap.ai/resource" , body )
169196 assert .Equal (t , http .StatusBadRequest , res .Code )
170197 assert .False (t , called , "Handler should not have been called" )
171198 called = false
172199 }
173200
174201 // Call a protected function to which we have access
175202 {
176- res := doGet (t , i , "https ://deepmap.ai/protected_resource" )
203+ res := doGet (t , i , "http ://deepmap.ai/protected_resource" )
177204 assert .Equal (t , http .StatusNoContent , res .Code )
178205 assert .True (t , called , "Handler should have been called" )
179206 called = false
180207 }
181208
182209 // Call a protected function to which we don't have access
183210 {
184- res := doGet (t , i , "https ://deepmap.ai/protected_resource2" )
211+ res := doGet (t , i , "http ://deepmap.ai/protected_resource2" )
185212 assert .Equal (t , http .StatusBadRequest , res .Code )
186213 assert .False (t , called , "Handler should not have been called" )
187214 called = false
188215 }
189216
190217 // Call a protected function without credentials
191218 {
192- res := doGet (t , i , "https ://deepmap.ai/protected_resource_401" )
219+ res := doGet (t , i , "http ://deepmap.ai/protected_resource_401" )
193220 assert .Equal (t , http .StatusBadRequest , res .Code )
194221 body , err := io .ReadAll (res .Body )
195222 if assert .NoError (t , err ) {
@@ -234,7 +261,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
234261
235262 // Let's send a good request, it should pass
236263 {
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" )
238265 assert .Equal (t , http .StatusOK , res .Code )
239266 assert .True (t , called , "Handler should have been called" )
240267 called = false
@@ -243,7 +270,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
243270 // Let's send a request with a missing parameter, it should return
244271 // a bad status
245272 {
246- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=50" )
273+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=50" )
247274 assert .Equal (t , http .StatusBadRequest , res .Code )
248275 body , err := io .ReadAll (res .Body )
249276 if assert .NoError (t , err ) {
@@ -258,7 +285,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
258285 // Let's send a request with a 2 missing parameters, it should return
259286 // a bad status
260287 {
261- res := doGet (t , i , "https ://deepmap.ai/multiparamresource" )
288+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource" )
262289 assert .Equal (t , http .StatusBadRequest , res .Code )
263290 body , err := io .ReadAll (res .Body )
264291 if assert .NoError (t , err ) {
@@ -275,7 +302,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
275302 // Let's send a request with a 1 missing parameter, and another outside
276303 // or the parameters. It should return a bad status
277304 {
278- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=500" )
305+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=500" )
279306 assert .Equal (t , http .StatusBadRequest , res .Code )
280307 body , err := io .ReadAll (res .Body )
281308 if assert .NoError (t , err ) {
@@ -292,7 +319,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
292319 // Let's send a request with a parameters that do not meet spec. It should
293320 // return a bad status
294321 {
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" )
296323 assert .Equal (t , http .StatusBadRequest , res .Code )
297324 body , err := io .ReadAll (res .Body )
298325 if assert .NoError (t , err ) {
@@ -344,7 +371,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
344371
345372 // Let's send a good request, it should pass
346373 {
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" )
348375 assert .Equal (t , http .StatusOK , res .Code )
349376 assert .True (t , called , "Handler should have been called" )
350377 called = false
@@ -353,7 +380,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
353380 // Let's send a request with a missing parameter, it should return
354381 // a bad status
355382 {
356- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=50" )
383+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=50" )
357384 assert .Equal (t , http .StatusBadRequest , res .Code )
358385 body , err := io .ReadAll (res .Body )
359386 if assert .NoError (t , err ) {
@@ -368,7 +395,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
368395 // Let's send a request with a 2 missing parameters, it should return
369396 // a bad status
370397 {
371- res := doGet (t , i , "https ://deepmap.ai/multiparamresource" )
398+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource" )
372399 assert .Equal (t , http .StatusBadRequest , res .Code )
373400 body , err := io .ReadAll (res .Body )
374401 if assert .NoError (t , err ) {
@@ -385,7 +412,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
385412 // Let's send a request with a 1 missing parameter, and another outside
386413 // or the parameters. It should return a bad status
387414 {
388- res := doGet (t , i , "https ://deepmap.ai/multiparamresource?id=500" )
415+ res := doGet (t , i , "http ://deepmap.ai/multiparamresource?id=500" )
389416 assert .Equal (t , http .StatusBadRequest , res .Code )
390417 body , err := io .ReadAll (res .Body )
391418 if assert .NoError (t , err ) {
@@ -402,7 +429,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
402429 // Let's send a request with a parameters that do not meet spec. It should
403430 // return a bad status
404431 {
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" )
406433 assert .Equal (t , http .StatusBadRequest , res .Code )
407434 body , err := io .ReadAll (res .Body )
408435 if assert .NoError (t , err ) {
0 commit comments