Skip to content

Commit b3f6e33

Browse files
jacobm-splunkdaveshanley
authored andcommitted
Added test for code coverage reasons
1 parent ab93afc commit b3f6e33

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

requests/validate_body_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,56 @@ paths:
149149
assert.Equal(t, "", errors[0].SpecPath)
150150
}
151151

152+
func TestValidateBody_OperationNotFound(t *testing.T) {
153+
spec := `openapi: 3.1.0
154+
paths:
155+
/burgers/createBurger:
156+
post:
157+
requestBody:
158+
content:
159+
application/json:
160+
schema:
161+
type: object
162+
properties:
163+
name:
164+
type: string
165+
patties:
166+
type: integer
167+
vegetarian:
168+
type: boolean`
169+
170+
doc, _ := libopenapi.NewDocument([]byte(spec))
171+
172+
m, _ := doc.BuildV3Model()
173+
v := NewRequestBodyValidator(&m.Model)
174+
175+
// mix up the primitives to fire two schema violations.
176+
body := map[string]interface{}{
177+
"name": "Big Mac",
178+
"patties": false,
179+
"vegetarian": 2,
180+
}
181+
182+
bodyBytes, _ := json.Marshal(body)
183+
184+
request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/createBurger",
185+
bytes.NewBuffer(bodyBytes))
186+
request.Header.Set("Content-Type", "application/json")
187+
188+
pathItem := m.Model.Paths.PathItems.First().Value()
189+
pathValue := m.Model.Paths.PathItems.First().Key()
190+
v.SetPathItem(pathItem, pathValue)
191+
192+
valid, errors := v.ValidateRequestBody(request)
193+
194+
assert.False(t, valid)
195+
assert.Len(t, errors, 1)
196+
assert.Equal(t, "GET operation request content type 'GET' does not exist", errors[0].Message)
197+
assert.Equal(t, request.Method, errors[0].RequestMethod)
198+
assert.Equal(t, request.URL.Path, errors[0].RequestPath)
199+
assert.Equal(t, "/burgers/createBurger", errors[0].SpecPath)
200+
}
201+
152202
func TestValidateBody_SetPath(t *testing.T) {
153203
spec := `openapi: 3.1.0
154204
paths:

0 commit comments

Comments
 (0)