diff --git a/requests/validate_body_test.go b/requests/validate_body_test.go index c2d3e1f..c0b9849 100644 --- a/requests/validate_body_test.go +++ b/requests/validate_body_test.go @@ -149,6 +149,56 @@ paths: assert.Equal(t, "", errors[0].SpecPath) } +func TestValidateBody_OperationNotFound(t *testing.T) { + spec := `openapi: 3.1.0 +paths: + /burgers/createBurger: + post: + requestBody: + content: + application/json: + schema: + type: object + properties: + name: + type: string + patties: + type: integer + vegetarian: + type: boolean` + + doc, _ := libopenapi.NewDocument([]byte(spec)) + + m, _ := doc.BuildV3Model() + v := NewRequestBodyValidator(&m.Model) + + // mix up the primitives to fire two schema violations. + body := map[string]interface{}{ + "name": "Big Mac", + "patties": false, + "vegetarian": 2, + } + + bodyBytes, _ := json.Marshal(body) + + request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/createBurger", + bytes.NewBuffer(bodyBytes)) + request.Header.Set("Content-Type", "application/json") + + pathItem := m.Model.Paths.PathItems.First().Value() + pathValue := m.Model.Paths.PathItems.First().Key() + v.SetPathItem(pathItem, pathValue) + + valid, errors := v.ValidateRequestBody(request) + + assert.False(t, valid) + assert.Len(t, errors, 1) + assert.Equal(t, "GET operation request content type 'GET' does not exist", errors[0].Message) + assert.Equal(t, request.Method, errors[0].RequestMethod) + assert.Equal(t, request.URL.Path, errors[0].RequestPath) + assert.Equal(t, "/burgers/createBurger", errors[0].SpecPath) +} + func TestValidateBody_SetPath(t *testing.T) { spec := `openapi: 3.1.0 paths: