@@ -49,18 +49,18 @@ type user struct {
49
49
50
50
func TestWithValidation (t * testing.T ) {
51
51
testCases := []struct {
52
- name string
53
52
handler http.Handler
54
53
request func (origin string ) * http.Request
55
54
routeErrReporter func (w http.ResponseWriter , r * http.Request , err error )
56
55
reqErrReporter func (w http.ResponseWriter , r * http.Request , err error )
57
56
resErrReporter func (w http.ResponseWriter , r * http.Request , err error )
57
+ name string
58
58
}{
59
59
{
60
60
name : "GET /users/{id}: ok" ,
61
61
handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
62
62
w .Header ().Set ("content-type" , "application/json" )
63
- _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" })
63
+ _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" }) //nolint:errcheck,errchkjson
64
64
}),
65
65
request : func (origin string ) * http.Request {
66
66
return mustRequest (newRequest (http .MethodGet , origin + "/users/123" , map [string ]string {}, "" ))
@@ -70,7 +70,7 @@ func TestWithValidation(t *testing.T) {
70
70
name : "GET /users/{id}: response error" ,
71
71
handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
72
72
w .Header ().Set ("content-type" , "application/json" )
73
- _ = json .NewEncoder (w ).Encode (map [string ]interface {}{"name" : "aereal" , "age" : 17 })
73
+ _ = json .NewEncoder (w ).Encode (map [string ]interface {}{"name" : "aereal" , "age" : 17 }) //nolint:errcheck,errchkjson
74
74
}),
75
75
request : func (origin string ) * http.Request {
76
76
return mustRequest (newRequest (http .MethodGet , origin + "/users/123" , map [string ]string {}, "" ))
@@ -80,14 +80,15 @@ func TestWithValidation(t *testing.T) {
80
80
name : "GET /users/{id}: response error with custom error handler" ,
81
81
handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
82
82
w .Header ().Set ("content-type" , "application/json" )
83
- _ = json .NewEncoder (w ).Encode (map [string ]interface {}{"name" : "aereal" , "age" : 17 })
83
+ _ = json .NewEncoder (w ).Encode (map [string ]interface {}{"name" : "aereal" , "age" : 17 }) //nolint:errcheck,errchkjson
84
84
}),
85
85
request : func (origin string ) * http.Request {
86
86
return mustRequest (newRequest (http .MethodGet , origin + "/users/123" , map [string ]string {}, "" ))
87
87
},
88
88
resErrReporter : func (w http.ResponseWriter , r * http.Request , err error ) {
89
89
requestNonNil := r != nil
90
- _ , errTypeOK := err .(* openapi3filter.ResponseError )
90
+ respErr := new (openapi3filter.ResponseError )
91
+ errTypeOK := errors .As (err , & respErr )
91
92
w .Header ().Set ("content-type" , "text/plain" )
92
93
w .WriteHeader (http .StatusInternalServerError )
93
94
_ , _ = fmt .Fprintf (w , "the custom response validation error handler is called: errTypeOK=%t, request=%t" , errTypeOK , requestNonNil )
@@ -97,7 +98,7 @@ func TestWithValidation(t *testing.T) {
97
98
name : "GET /unknown: find route error (not found)" ,
98
99
handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
99
100
w .Header ().Set ("content-type" , "application/json" )
100
- _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" })
101
+ _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" }) //nolint:errcheck,errchkjson
101
102
}),
102
103
request : func (origin string ) * http.Request {
103
104
return mustRequest (newRequest (http .MethodGet , origin + "/unknown" , map [string ]string {}, "" ))
@@ -114,7 +115,7 @@ func TestWithValidation(t *testing.T) {
114
115
name : "GET /users: find route error (method not allowed)" ,
115
116
handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
116
117
w .Header ().Set ("content-type" , "application/json" )
117
- _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" })
118
+ _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" }) //nolint:errcheck,errchkjson
118
119
}),
119
120
request : func (origin string ) * http.Request {
120
121
return mustRequest (newRequest (http .MethodGet , origin + "/users" , map [string ]string {}, "" ))
@@ -131,7 +132,7 @@ func TestWithValidation(t *testing.T) {
131
132
name : "POST /users: ok" ,
132
133
handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
133
134
w .Header ().Set ("content-type" , "application/json" )
134
- _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" })
135
+ _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" }) //nolint:errcheck,errchkjson
135
136
}),
136
137
request : func (origin string ) * http.Request {
137
138
return mustRequest (newRequest (http .MethodPost , origin + "/users" , map [string ]string {"content-type" : "application/json" }, `{"name":"aereal","age":17}` ))
@@ -156,7 +157,8 @@ func TestWithValidation(t *testing.T) {
156
157
},
157
158
reqErrReporter : func (w http.ResponseWriter , r * http.Request , err error ) {
158
159
requestNonNil := r != nil
159
- _ , errTypeOK := err .(* openapi3filter.RequestError )
160
+ reqErr := new (openapi3filter.RequestError )
161
+ errTypeOK := errors .As (err , & reqErr )
160
162
w .Header ().Set ("content-type" , "text/plain" )
161
163
w .WriteHeader (http .StatusBadRequest )
162
164
_ , _ = fmt .Fprintf (w , "the custom response validation error handler is called: errTypeOK=%t, request=%t" , errTypeOK , requestNonNil )
@@ -177,10 +179,12 @@ func TestWithValidation(t *testing.T) {
177
179
if err != nil {
178
180
t .Fatal (err )
179
181
}
182
+ t .Cleanup (func () { gotResp .Body .Close () })
180
183
expectedResp , err := resumeResponse (t .Name (), gotResp )
181
184
if err != nil {
182
185
t .Fatal (err )
183
186
}
187
+ t .Cleanup (func () { expectedResp .Body .Close () })
184
188
if err := testResponse (expectedResp , gotResp ); err != nil {
185
189
t .Error (err )
186
190
}
@@ -190,8 +194,8 @@ func TestWithValidation(t *testing.T) {
190
194
191
195
func TestWithValidation_otel (t * testing.T ) {
192
196
testCases := []struct {
193
- name string
194
197
buildOptions func (tp trace.TracerProvider ) MiddlewareOptions
198
+ name string
195
199
wantSpans int
196
200
}{
197
201
{
@@ -226,15 +230,15 @@ func TestWithValidation_otel(t *testing.T) {
226
230
227
231
withOtel := func (next http.Handler ) http.Handler {
228
232
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
229
- ctx := otel .GetTextMapPropagator ().Extract (r .Context (), propagation .HeaderCarrier (r .Header ))
230
- ctx , span := tp .Tracer ("test" ).Start (ctx , fmt .Sprintf ("%s %s" , r .Method , r .URL .Path ))
233
+ reqCtx := otel .GetTextMapPropagator ().Extract (r .Context (), propagation .HeaderCarrier (r .Header ))
234
+ reqCtx , span := tp .Tracer ("test" ).Start (reqCtx , fmt .Sprintf ("%s %s" , r .Method , r .URL .Path ))
231
235
defer span .End ()
232
- next .ServeHTTP (w , r .WithContext (ctx ))
236
+ next .ServeHTTP (w , r .WithContext (reqCtx ))
233
237
})
234
238
}
235
239
handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
236
240
w .Header ().Set ("content-type" , "application/json" )
237
- _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" })
241
+ _ = json .NewEncoder (w ).Encode (user {Name : "aereal" , Age : 17 , ID : "123" }) //nolint:errcheck,errchkjson
238
242
})
239
243
srv := httptest .NewServer (withOtel (WithValidation (tc .buildOptions (tp ))(handler )))
240
244
defer srv .Close ()
@@ -328,8 +332,14 @@ func testResponse(expected, got *http.Response) error {
328
332
if got .StatusCode != expected .StatusCode {
329
333
return fmt .Errorf ("StatusCode: got=%d expected=%d" , got .StatusCode , expected .StatusCode )
330
334
}
331
- expectedBody , _ := io .ReadAll (expected .Body )
332
- gotBody , _ := io .ReadAll (got .Body )
335
+ expectedBody , err := io .ReadAll (expected .Body )
336
+ if err != nil {
337
+ return err
338
+ }
339
+ gotBody , err := io .ReadAll (got .Body )
340
+ if err != nil {
341
+ return err
342
+ }
333
343
defer func () {
334
344
// rewind body
335
345
expected .Body = io .NopCloser (bytes .NewReader (expectedBody ))
0 commit comments