Skip to content

Commit ab13cf1

Browse files
commodditydaveshanley
authored andcommitted
fix: add TestNewValidator_ValidateHttpRequestSync_ValidPostSimpleSchema_FoundPath to cover missing lines
1 parent e73b5cb commit ab13cf1

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

requests/validate_body.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package requests
55

66
import (
7+
"fmt"
78
"net/http"
89
"strings"
910

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

3233
operation := helpers.ExtractOperation(request, pathItem)
3334
if operation == nil {
35+
fmt.Println("HERE!!!!")
3436
return false, []*errors.ValidationError{errors.OperationNotFound(pathItem, request, request.Method, foundPath)}
3537
}
3638
if operation.RequestBody == nil {

validator_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/pb33f/libopenapi"
1515
"github.com/pb33f/libopenapi-validator/helpers"
16+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
1617
"github.com/stretchr/testify/assert"
1718
"github.com/stretchr/testify/require"
1819
)
@@ -235,6 +236,52 @@ paths:
235236

236237
}
237238

239+
func TestNewValidator_ValidateHttpRequestSync_ValidPostSimpleSchema_FoundPath(t *testing.T) {
240+
241+
spec := `openapi: 3.1.0
242+
paths:
243+
/burgers/createBurger:
244+
post:
245+
requestBody:
246+
content:
247+
application/json:
248+
schema:
249+
type: object
250+
properties:
251+
name:
252+
type: string
253+
patties:
254+
type: integer
255+
vegetarian:
256+
type: boolean`
257+
258+
doc, _ := libopenapi.NewDocument([]byte(spec))
259+
260+
v, _ := NewValidator(doc)
261+
v.(*validator).foundPath = &v3.PathItem{
262+
Post: &v3.Operation{},
263+
}
264+
v.(*validator).foundPathValue = "/burgers/createBurger"
265+
266+
body := map[string]interface{}{
267+
"name": "Big Mac",
268+
"patties": 2,
269+
"vegetarian": true,
270+
}
271+
272+
bodyBytes, _ := json.Marshal(body)
273+
274+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
275+
bytes.NewBuffer(bodyBytes))
276+
request.Header.Set("Content-Type", "application/json")
277+
278+
valid, errors := v.ValidateHttpRequestSync(request)
279+
280+
assert.True(t, valid)
281+
assert.Len(t, errors, 0)
282+
283+
}
284+
238285
func TestNewValidator_slash_server_url(t *testing.T) {
239286

240287
spec := `openapi: 3.1.0

0 commit comments

Comments
 (0)