@@ -49,18 +49,18 @@ type user struct {
4949
5050func TestWithValidation (t * testing.T ) {
5151 testCases := []struct {
52- name string
5352 handler http.Handler
5453 request func (origin string ) * http.Request
5554 routeErrReporter func (w http.ResponseWriter , r * http.Request , err error )
5655 reqErrReporter func (w http.ResponseWriter , r * http.Request , err error )
5756 resErrReporter func (w http.ResponseWriter , r * http.Request , err error )
57+ name string
5858 }{
5959 {
6060 name : "GET /users/{id}: ok" ,
6161 handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
6262 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
6464 }),
6565 request : func (origin string ) * http.Request {
6666 return mustRequest (newRequest (http .MethodGet , origin + "/users/123" , map [string ]string {}, "" ))
@@ -70,7 +70,7 @@ func TestWithValidation(t *testing.T) {
7070 name : "GET /users/{id}: response error" ,
7171 handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
7272 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
7474 }),
7575 request : func (origin string ) * http.Request {
7676 return mustRequest (newRequest (http .MethodGet , origin + "/users/123" , map [string ]string {}, "" ))
@@ -80,14 +80,15 @@ func TestWithValidation(t *testing.T) {
8080 name : "GET /users/{id}: response error with custom error handler" ,
8181 handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
8282 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
8484 }),
8585 request : func (origin string ) * http.Request {
8686 return mustRequest (newRequest (http .MethodGet , origin + "/users/123" , map [string ]string {}, "" ))
8787 },
8888 resErrReporter : func (w http.ResponseWriter , r * http.Request , err error ) {
8989 requestNonNil := r != nil
90- _ , errTypeOK := err .(* openapi3filter.ResponseError )
90+ respErr := new (openapi3filter.ResponseError )
91+ errTypeOK := errors .As (err , & respErr )
9192 w .Header ().Set ("content-type" , "text/plain" )
9293 w .WriteHeader (http .StatusInternalServerError )
9394 _ , _ = 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) {
9798 name : "GET /unknown: find route error (not found)" ,
9899 handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
99100 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
101102 }),
102103 request : func (origin string ) * http.Request {
103104 return mustRequest (newRequest (http .MethodGet , origin + "/unknown" , map [string ]string {}, "" ))
@@ -114,7 +115,7 @@ func TestWithValidation(t *testing.T) {
114115 name : "GET /users: find route error (method not allowed)" ,
115116 handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
116117 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
118119 }),
119120 request : func (origin string ) * http.Request {
120121 return mustRequest (newRequest (http .MethodGet , origin + "/users" , map [string ]string {}, "" ))
@@ -131,7 +132,7 @@ func TestWithValidation(t *testing.T) {
131132 name : "POST /users: ok" ,
132133 handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
133134 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
135136 }),
136137 request : func (origin string ) * http.Request {
137138 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) {
156157 },
157158 reqErrReporter : func (w http.ResponseWriter , r * http.Request , err error ) {
158159 requestNonNil := r != nil
159- _ , errTypeOK := err .(* openapi3filter.RequestError )
160+ reqErr := new (openapi3filter.RequestError )
161+ errTypeOK := errors .As (err , & reqErr )
160162 w .Header ().Set ("content-type" , "text/plain" )
161163 w .WriteHeader (http .StatusBadRequest )
162164 _ , _ = 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) {
177179 if err != nil {
178180 t .Fatal (err )
179181 }
182+ t .Cleanup (func () { gotResp .Body .Close () })
180183 expectedResp , err := resumeResponse (t .Name (), gotResp )
181184 if err != nil {
182185 t .Fatal (err )
183186 }
187+ t .Cleanup (func () { expectedResp .Body .Close () })
184188 if err := testResponse (expectedResp , gotResp ); err != nil {
185189 t .Error (err )
186190 }
@@ -190,8 +194,8 @@ func TestWithValidation(t *testing.T) {
190194
191195func TestWithValidation_otel (t * testing.T ) {
192196 testCases := []struct {
193- name string
194197 buildOptions func (tp trace.TracerProvider ) MiddlewareOptions
198+ name string
195199 wantSpans int
196200 }{
197201 {
@@ -226,15 +230,15 @@ func TestWithValidation_otel(t *testing.T) {
226230
227231 withOtel := func (next http.Handler ) http.Handler {
228232 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 ))
231235 defer span .End ()
232- next .ServeHTTP (w , r .WithContext (ctx ))
236+ next .ServeHTTP (w , r .WithContext (reqCtx ))
233237 })
234238 }
235239 handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
236240 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
238242 })
239243 srv := httptest .NewServer (withOtel (WithValidation (tc .buildOptions (tp ))(handler )))
240244 defer srv .Close ()
@@ -328,8 +332,14 @@ func testResponse(expected, got *http.Response) error {
328332 if got .StatusCode != expected .StatusCode {
329333 return fmt .Errorf ("StatusCode: got=%d expected=%d" , got .StatusCode , expected .StatusCode )
330334 }
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+ }
333343 defer func () {
334344 // rewind body
335345 expected .Body = io .NopCloser (bytes .NewReader (expectedBody ))
0 commit comments