@@ -81,6 +81,65 @@ paths:
8181 assert .Equal (t , "/burgers/createBurger" , errors [0 ].SpecPath )
8282}
8383
84+ func TestValidateBody_MissingContentType4XX (t * testing.T ) {
85+ spec := `openapi: 3.1.0
86+ paths:
87+ /burgers/createBurger:
88+ post:
89+ responses:
90+ 4XX:
91+ content:
92+ application/json:
93+ schema:
94+ type: object
95+ properties:
96+ error:
97+ type: string`
98+
99+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
100+
101+ m , _ := doc .BuildV3Model ()
102+ v := NewResponseBodyValidator (& m .Model )
103+
104+ body := map [string ]interface {}{
105+ "name" : "Big Mac" ,
106+ "patties" : false ,
107+ "vegetarian" : 2 ,
108+ }
109+
110+ bodyBytes , _ := json .Marshal (body )
111+
112+ // build a request
113+ request , _ := http .NewRequest (http .MethodPost , "https://things.com/burgers/createBurger" , bytes .NewReader (bodyBytes ))
114+ request .Header .Set (helpers .ContentTypeHeader , helpers .JSONContentType )
115+
116+ // simulate a request/response
117+ res := httptest .NewRecorder ()
118+ handler := func (w http.ResponseWriter , r * http.Request ) {
119+ w .Header ().Set (helpers .ContentTypeHeader , "cheeky/monkey" )
120+ w .WriteHeader (http .StatusBadRequest )
121+ _ , _ = w .Write (bodyBytes )
122+ }
123+
124+ // fire the request
125+ handler (res , request )
126+
127+ // record response
128+ response := res .Result ()
129+
130+ // validate!
131+ valid , errors := v .ValidateResponseBody (request , response )
132+
133+ assert .False (t , valid )
134+ assert .Len (t , errors , 1 )
135+ assert .Equal (t , "POST / 4XX operation response content type 'cheeky/monkey' does not exist" , errors [0 ].Message )
136+ assert .Equal (t , "The content type is invalid, Use one of the 1 " +
137+ "supported types for this operation: application/json" , errors [0 ].HowToFix )
138+ assert .Equal (t , request .Method , errors [0 ].RequestMethod )
139+ assert .Equal (t , request .URL .Path , errors [0 ].RequestPath )
140+ assert .Equal (t , "/burgers/createBurger" , errors [0 ].SpecPath )
141+ }
142+
84143func TestValidateBody_MissingPath (t * testing.T ) {
85144 spec := `openapi: 3.1.0
86145paths:
0 commit comments