Skip to content

Commit

Permalink
Added test for code coverage reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobm-splunk authored and daveshanley committed Apr 12, 2024
1 parent ab93afc commit b3f6e33
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions requests/validate_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b3f6e33

Please sign in to comment.