Skip to content

Commit c7e173d

Browse files
ySnoopyDogydaveshanley
authored andcommitted
bump error coverage
1 parent 4f0ba57 commit c7e173d

4 files changed

Lines changed: 101 additions & 5 deletions

File tree

requests/validate_body.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,15 @@ func (v *requestBodyValidator) ValidateRequestBodyWithPathItem(request *http.Req
111111
return false, prevalidationErrors
112112
}
113113

114-
// If prevalidationErrors has no items, jsonBody is a valid JSON structure
115-
transformedBytes, _ := json.Marshal(jsonBody)
114+
transformedBytes, err := json.Marshal(jsonBody)
115+
if err != nil {
116+
switch {
117+
case isXml:
118+
return false, []*errors.ValidationError{errors.InvalidXMLParsing(err.Error(), stringedBody)}
119+
case isUrlEncoded:
120+
return false, []*errors.ValidationError{errors.InvalidURLEncodedParsing(err.Error(), stringedBody)}
121+
}
122+
}
116123

117124
request.Body = io.NopCloser(bytes.NewBuffer(transformedBytes))
118125
}

requests/validate_body_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,45 @@ paths:
16171617
assert.Equal(t, errors[0].Message, "xml example is malformed")
16181618
}
16191619

1620+
func TestValidateRequestBody_URLEncodedMarshalError(t *testing.T) {
1621+
spec := []byte(`
1622+
openapi: 3.1.0
1623+
info:
1624+
title: Test Spec
1625+
version: 1.0.0
1626+
paths:
1627+
/test:
1628+
post:
1629+
requestBody:
1630+
required: true
1631+
content:
1632+
application/x-www-form-urlencoded:
1633+
schema:
1634+
type: object
1635+
properties:
1636+
bad_number:
1637+
type: number
1638+
responses:
1639+
'200':
1640+
description: Success
1641+
`)
1642+
1643+
doc, _ := libopenapi.NewDocument([]byte(spec))
1644+
1645+
m, _ := doc.BuildV3Model()
1646+
v := NewRequestBodyValidator(&m.Model, config.WithURLEncodedBodyValidation())
1647+
1648+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/test",
1649+
bytes.NewBuffer([]byte("bad_number=NaN")))
1650+
request.Header.Set("Content-Type", helpers.URLEncodedContentType)
1651+
1652+
valid, errors := v.ValidateRequestBody(request)
1653+
1654+
assert.False(t, valid)
1655+
assert.Len(t, errors, 1)
1656+
assert.Equal(t, errors[0].Message, "Unable to parse form-urlencoded body")
1657+
}
1658+
16201659
func TestValidateBody_URLEncodedRequest(t *testing.T) {
16211660
spec := `openapi: 3.1.0
16221661
paths:

responses/validate_body.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,15 @@ func (v *responseBodyValidator) checkResponseSchema(
176176
return prevalidationErrors
177177
}
178178

179-
// If prevalidationErrors has no items, jsonBody is a valid JSON structure
180-
transformedBytes, _ := json.Marshal(jsonBody)
179+
transformedBytes, err := json.Marshal(jsonBody)
180+
if err != nil {
181+
switch {
182+
case isXml:
183+
return []*errors.ValidationError{errors.InvalidXMLParsing(err.Error(), stringedBody)}
184+
case isUrlEncoded:
185+
return []*errors.ValidationError{errors.InvalidURLEncodedParsing(err.Error(), stringedBody)}
186+
}
187+
}
181188

182189
response.Body = io.NopCloser(bytes.NewBuffer(transformedBytes))
183190
}

responses/validate_body_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newvalidateResponseTestBed(
4444
t.Fatalf("failed to build v3 model: %v", err)
4545
}
4646

47-
tb := validateResponseTestBed{responseBodyValidator: NewResponseBodyValidator(&m.Model, config.WithXmlBodyValidation())}
47+
tb := validateResponseTestBed{responseBodyValidator: NewResponseBodyValidator(&m.Model, config.WithXmlBodyValidation(), config.WithURLEncodedBodyValidation())}
4848
tb.httpTestServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4949
if tb.responseHandlerFunc != nil {
5050
tb.responseHandlerFunc(w, r)
@@ -1325,6 +1325,49 @@ paths:
13251325
assert.Equal(t, errors[0].Message, "xml example is malformed")
13261326
}
13271327

1328+
func TestValidateResponseBody_URLEncodedMarshalError(t *testing.T) {
1329+
tb := newvalidateResponseTestBed(
1330+
t,
1331+
[]byte(`
1332+
openapi: 3.1.0
1333+
info:
1334+
title: Test Spec
1335+
version: 1.0.0
1336+
paths:
1337+
/test:
1338+
get:
1339+
responses:
1340+
'200':
1341+
description: Success
1342+
content:
1343+
application/x-www-form-urlencoded:
1344+
schema:
1345+
type: object
1346+
properties:
1347+
bad_number:
1348+
type: number
1349+
`,
1350+
),
1351+
)
1352+
1353+
req, res := tb.makeRequestWithReponse(
1354+
t,
1355+
http.MethodGet,
1356+
"/test",
1357+
func(w http.ResponseWriter, r *http.Request) {
1358+
w.Header().Set(helpers.ContentTypeHeader, helpers.URLEncodedContentType)
1359+
w.WriteHeader(http.StatusOK)
1360+
_, _ = w.Write([]byte("bad_number=NaN"))
1361+
},
1362+
)
1363+
1364+
valid, errors := tb.responseBodyValidator.ValidateResponseBody(req, res)
1365+
1366+
assert.False(t, valid)
1367+
assert.Len(t, errors, 1)
1368+
assert.Equal(t, errors[0].Message, "Unable to parse form-urlencoded body")
1369+
}
1370+
13281371
func TestValidateResponseBody_NilSchema(t *testing.T) {
13291372
tb := newvalidateResponseTestBed(
13301373
t,

0 commit comments

Comments
 (0)