Skip to content

Commit

Permalink
fix: add TestNewValidator_ValidateHttpRequestSync_ValidPostSimpleSche…
Browse files Browse the repository at this point in the history
…ma_FoundPath to cover missing lines
  • Loading branch information
commoddity authored and daveshanley committed May 4, 2024
1 parent e73b5cb commit ab13cf1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions requests/validate_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package requests

import (
"fmt"
"net/http"
"strings"

Expand Down Expand Up @@ -31,6 +32,7 @@ func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool,

operation := helpers.ExtractOperation(request, pathItem)
if operation == nil {
fmt.Println("HERE!!!!")
return false, []*errors.ValidationError{errors.OperationNotFound(pathItem, request, request.Method, foundPath)}
}
if operation.RequestBody == nil {
Expand Down
47 changes: 47 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/helpers"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -235,6 +236,52 @@ paths:

}

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

v, _ := NewValidator(doc)
v.(*validator).foundPath = &v3.PathItem{
Post: &v3.Operation{},
}
v.(*validator).foundPathValue = "/burgers/createBurger"

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))
request.Header.Set("Content-Type", "application/json")

valid, errors := v.ValidateHttpRequestSync(request)

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

}

func TestNewValidator_slash_server_url(t *testing.T) {

spec := `openapi: 3.1.0
Expand Down

0 comments on commit ab13cf1

Please sign in to comment.