Skip to content

Commit 91930e8

Browse files
nickajacks1daveshanley
authored andcommitted
test: restore 100% line coverage
1 parent c2981af commit 91930e8

File tree

2 files changed

+137
-5
lines changed

2 files changed

+137
-5
lines changed

requests/validate_body.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool,
3030

3131
operation := helpers.ExtractOperation(request, pathItem)
3232
if operation.RequestBody == nil {
33-
// TODO: check if requestBody is marked as required
3433
return true, nil
3534
}
3635

3736
// extract the content type from the request
3837
contentType := request.Header.Get(helpers.ContentTypeHeader)
3938
if contentType == "" {
40-
//TODO: should this ever return errors?
41-
return true, nil
39+
return false, []*errors.ValidationError{errors.RequestContentTypeNotFound(operation, request)}
4240
}
4341

4442
// extract the media type from the content type header.

requests/validate_body_test.go

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ package requests
66
import (
77
"bytes"
88
"encoding/json"
9+
"net/http"
10+
"testing"
11+
912
"github.com/pb33f/libopenapi"
1013
"github.com/pb33f/libopenapi-validator/paths"
1114
"github.com/stretchr/testify/assert"
12-
"net/http"
13-
"testing"
1415
)
1516

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

62+
func TestValidateBody_SkipValidationForNonJSON(t *testing.T) {
63+
spec := `openapi: 3.1.0
64+
paths:
65+
/burgers/createBurger:
66+
post:
67+
requestBody:
68+
content:
69+
application/yaml:
70+
schema:
71+
type: object
72+
properties:
73+
name:
74+
type: string
75+
patties:
76+
type: integer
77+
vegetarian:
78+
type: boolean`
79+
80+
doc, _ := libopenapi.NewDocument([]byte(spec))
81+
82+
m, _ := doc.BuildV3Model()
83+
v := NewRequestBodyValidator(&m.Model)
84+
85+
body := map[string]interface{}{
86+
"name": "Big Mac",
87+
"patties": false,
88+
"vegetarian": 2,
89+
}
90+
91+
bodyBytes, _ := json.Marshal(body)
92+
93+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
94+
bytes.NewBuffer(bodyBytes))
95+
request.Header.Set("Content-Type", "application/yaml")
96+
97+
valid, errors := v.ValidateRequestBody(request)
98+
99+
assert.True(t, valid)
100+
assert.Len(t, errors, 0)
101+
}
102+
61103
func TestValidateBody_PathNotFound(t *testing.T) {
62104
spec := `openapi: 3.1.0
63105
paths:
@@ -195,6 +237,51 @@ paths:
195237

196238
}
197239

240+
func TestValidateBody_ContentTypeNotSet(t *testing.T) {
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+
m, _ := doc.BuildV3Model()
261+
v := NewRequestBodyValidator(&m.Model)
262+
263+
body := map[string]interface{}{
264+
"name": "Big Mac",
265+
"patties": 2,
266+
"vegetarian": true,
267+
}
268+
269+
bodyBytes, _ := json.Marshal(body)
270+
271+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
272+
bytes.NewBuffer(bodyBytes))
273+
274+
// preset the path
275+
path, _, pv := paths.FindPath(request, &m.Model)
276+
v.SetPathItem(path, pv)
277+
278+
valid, errors := v.ValidateRequestBody(request)
279+
280+
assert.False(t, valid)
281+
assert.Len(t, errors, 1)
282+
283+
}
284+
198285
func TestValidateBody_InvalidBasicSchema(t *testing.T) {
199286
spec := `openapi: 3.1.0
200287
paths:
@@ -750,6 +837,53 @@ components:
750837
assert.Equal(t, 11, errors[0].SchemaValidationErrors[0].Column)
751838
}
752839

840+
func TestValidateBody_SchemaHasNoRequestBody(t *testing.T) {
841+
spec := `openapi: 3.1.0
842+
paths:
843+
/burgers/createBurger:
844+
post:`
845+
846+
doc, _ := libopenapi.NewDocument([]byte(spec))
847+
848+
m, _ := doc.BuildV3Model()
849+
v := NewRequestBodyValidator(&m.Model)
850+
851+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
852+
http.NoBody)
853+
request.Header.Set("Content-Type", "application/json")
854+
855+
valid, errors := v.ValidateRequestBody(request)
856+
857+
assert.True(t, valid)
858+
assert.Len(t, errors, 0)
859+
860+
}
861+
862+
func TestValidateBody_MediaTypeHasNullSchema(t *testing.T) {
863+
spec := `openapi: 3.1.0
864+
paths:
865+
/burgers/createBurger:
866+
post:
867+
requestBody:
868+
content:
869+
application/json:`
870+
871+
doc, _ := libopenapi.NewDocument([]byte(spec))
872+
873+
m, _ := doc.BuildV3Model()
874+
v := NewRequestBodyValidator(&m.Model)
875+
876+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
877+
http.NoBody)
878+
request.Header.Set("Content-Type", "application/json")
879+
880+
valid, errors := v.ValidateRequestBody(request)
881+
882+
assert.True(t, valid)
883+
assert.Len(t, errors, 0)
884+
885+
}
886+
753887
func TestValidateBody_MissingBody(t *testing.T) {
754888
spec := `openapi: 3.1.0
755889
paths:

0 commit comments

Comments
 (0)