From ab13cf178e6d066453e8fe5bcd9eca96344eb0a1 Mon Sep 17 00:00:00 2001 From: commoddity Date: Fri, 3 May 2024 16:33:43 +0100 Subject: [PATCH] fix: add TestNewValidator_ValidateHttpRequestSync_ValidPostSimpleSchema_FoundPath to cover missing lines --- requests/validate_body.go | 2 ++ validator_test.go | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/requests/validate_body.go b/requests/validate_body.go index c13294d..dea071e 100644 --- a/requests/validate_body.go +++ b/requests/validate_body.go @@ -4,6 +4,7 @@ package requests import ( + "fmt" "net/http" "strings" @@ -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 { diff --git a/validator_test.go b/validator_test.go index 3c1d503..d543131 100644 --- a/validator_test.go +++ b/validator_test.go @@ -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" ) @@ -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