15
15
package echomiddleware
16
16
17
17
import (
18
+ "bytes"
18
19
"context"
19
20
_ "embed"
21
+ "encoding/json"
20
22
"errors"
21
23
"io"
22
24
"net/http"
23
25
"net/http/httptest"
24
26
"net/url"
25
27
"testing"
26
28
27
- "github.com/deepmap/oapi-codegen/pkg/testutil"
28
29
"github.com/getkin/kin-openapi/openapi3"
29
30
"github.com/getkin/kin-openapi/openapi3filter"
30
31
"github.com/labstack/echo/v4"
@@ -42,8 +43,18 @@ func doGet(t *testing.T, e *echo.Echo, rawURL string) *httptest.ResponseRecorder
42
43
t .Fatalf ("Invalid url: %s" , rawURL )
43
44
}
44
45
45
- response := testutil .NewRequest ().Get (u .RequestURI ()).WithHost (u .Host ).WithAcceptJson ().GoWithHTTPHandler (t , e )
46
- return response .Recorder
46
+ r , err := http .NewRequest (http .MethodGet , u .String (), nil )
47
+ if err != nil {
48
+ t .Fatalf ("Could not construct a request: %s" , rawURL )
49
+ }
50
+ r .Header .Set ("accept" , "application/json" )
51
+ r .Header .Set ("host" , u .Host )
52
+
53
+ tt := httptest .NewRecorder ()
54
+
55
+ e .ServeHTTP (tt , r )
56
+
57
+ return tt
47
58
}
48
59
49
60
func doPost (t * testing.T , e * echo.Echo , rawURL string , jsonBody interface {}) * httptest.ResponseRecorder {
@@ -52,8 +63,24 @@ func doPost(t *testing.T, e *echo.Echo, rawURL string, jsonBody interface{}) *ht
52
63
t .Fatalf ("Invalid url: %s" , rawURL )
53
64
}
54
65
55
- response := testutil .NewRequest ().Post (u .RequestURI ()).WithHost (u .Host ).WithJsonBody (jsonBody ).GoWithHTTPHandler (t , e )
56
- return response .Recorder
66
+ body , err := json .Marshal (jsonBody )
67
+ if err != nil {
68
+ t .Fatalf ("Could not marshal request body: %v" , err )
69
+ }
70
+
71
+ r , err := http .NewRequest (http .MethodPost , u .String (), bytes .NewReader (body ))
72
+ if err != nil {
73
+ t .Fatalf ("Could not construct a request for URL %s: %v" , rawURL , err )
74
+ }
75
+ r .Header .Set ("accept" , "application/json" )
76
+ r .Header .Set ("content-type" , "application/json" )
77
+ r .Header .Set ("host" , u .Host )
78
+
79
+ tt := httptest .NewRecorder ()
80
+
81
+ e .ServeHTTP (tt , r )
82
+
83
+ return tt
57
84
}
58
85
59
86
func TestOapiRequestValidator (t * testing.T ) {
0 commit comments