Skip to content

Commit

Permalink
test: restore 100% line coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nickajacks1 authored and daveshanley committed Nov 4, 2023
1 parent c2981af commit 91930e8
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 5 deletions.
4 changes: 1 addition & 3 deletions requests/validate_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool,

operation := helpers.ExtractOperation(request, pathItem)
if operation.RequestBody == nil {
// TODO: check if requestBody is marked as required
return true, nil
}

// extract the content type from the request
contentType := request.Header.Get(helpers.ContentTypeHeader)
if contentType == "" {
//TODO: should this ever return errors?
return true, nil
return false, []*errors.ValidationError{errors.RequestContentTypeNotFound(operation, request)}
}

// extract the media type from the content type header.
Expand Down
138 changes: 136 additions & 2 deletions requests/validate_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package requests
import (
"bytes"
"encoding/json"
"net/http"
"testing"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestValidateBody_MissingContentType(t *testing.T) {
Expand Down Expand Up @@ -58,6 +59,47 @@ paths:
"supported types for this operation: application/json", errors[0].HowToFix)
}

func TestValidateBody_SkipValidationForNonJSON(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
requestBody:
content:
application/yaml:
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)

body := map[string]interface{}{
"name": "Big Mac",
"patties": false,
"vegetarian": 2,
}

bodyBytes, _ := json.Marshal(body)

request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
bytes.NewBuffer(bodyBytes))
request.Header.Set("Content-Type", "application/yaml")

valid, errors := v.ValidateRequestBody(request)

assert.True(t, valid)
assert.Len(t, errors, 0)
}

func TestValidateBody_PathNotFound(t *testing.T) {
spec := `openapi: 3.1.0
paths:
Expand Down Expand Up @@ -195,6 +237,51 @@ paths:

}

func TestValidateBody_ContentTypeNotSet(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)

body := map[string]interface{}{
"name": "Big Mac",
"patties": 2,
"vegetarian": true,
}

bodyBytes, _ := json.Marshal(body)

request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
bytes.NewBuffer(bodyBytes))

// preset the path
path, _, pv := paths.FindPath(request, &m.Model)
v.SetPathItem(path, pv)

valid, errors := v.ValidateRequestBody(request)

assert.False(t, valid)
assert.Len(t, errors, 1)

}

func TestValidateBody_InvalidBasicSchema(t *testing.T) {
spec := `openapi: 3.1.0
paths:
Expand Down Expand Up @@ -750,6 +837,53 @@ components:
assert.Equal(t, 11, errors[0].SchemaValidationErrors[0].Column)
}

func TestValidateBody_SchemaHasNoRequestBody(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:`

doc, _ := libopenapi.NewDocument([]byte(spec))

m, _ := doc.BuildV3Model()
v := NewRequestBodyValidator(&m.Model)

request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
http.NoBody)
request.Header.Set("Content-Type", "application/json")

valid, errors := v.ValidateRequestBody(request)

assert.True(t, valid)
assert.Len(t, errors, 0)

}

func TestValidateBody_MediaTypeHasNullSchema(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
requestBody:
content:
application/json:`

doc, _ := libopenapi.NewDocument([]byte(spec))

m, _ := doc.BuildV3Model()
v := NewRequestBodyValidator(&m.Model)

request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
http.NoBody)
request.Header.Set("Content-Type", "application/json")

valid, errors := v.ValidateRequestBody(request)

assert.True(t, valid)
assert.Len(t, errors, 0)

}

func TestValidateBody_MissingBody(t *testing.T) {
spec := `openapi: 3.1.0
paths:
Expand Down

0 comments on commit 91930e8

Please sign in to comment.