Skip to content

Commit 5cc2283

Browse files
ySnoopyDogydaveshanley
authored andcommitted
bump coverage
1 parent bdc8f0a commit 5cc2283

3 files changed

Lines changed: 138 additions & 1 deletion

File tree

requests/validate_body_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,45 @@ paths:
15781578
assert.Len(t, errors, 0)
15791579
}
15801580

1581+
func TestValidateRequestBody_XMLMarshalError(t *testing.T) {
1582+
spec := []byte(`
1583+
openapi: 3.1.0
1584+
info:
1585+
title: Test Spec
1586+
version: 1.0.0
1587+
paths:
1588+
/test:
1589+
post:
1590+
requestBody:
1591+
required: true
1592+
content:
1593+
application/xml:
1594+
schema:
1595+
type: object
1596+
properties:
1597+
bad_number:
1598+
type: number
1599+
responses:
1600+
'200':
1601+
description: Success
1602+
`)
1603+
1604+
doc, _ := libopenapi.NewDocument([]byte(spec))
1605+
1606+
m, _ := doc.BuildV3Model()
1607+
v := NewRequestBodyValidator(&m.Model, config.WithXmlBodyValidation())
1608+
1609+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/test",
1610+
bytes.NewBuffer([]byte("<bad_number>NaN</bad_number>")))
1611+
request.Header.Set("Content-Type", "application/xml")
1612+
1613+
valid, errors := v.ValidateRequestBody(request)
1614+
1615+
assert.False(t, valid)
1616+
assert.Len(t, errors, 1)
1617+
assert.Equal(t, errors[0].Message, "xml example is malformed")
1618+
}
1619+
15811620
func TestValidateBody_XmlRequest(t *testing.T) {
15821621
spec := `openapi: 3.1.0
15831622
paths:

responses/validate_body_test.go

Lines changed: 80 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)}
47+
tb := validateResponseTestBed{responseBodyValidator: NewResponseBodyValidator(&m.Model, config.WithXmlBodyValidation())}
4848
tb.httpTestServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4949
if tb.responseHandlerFunc != nil {
5050
tb.responseHandlerFunc(w, r)
@@ -1281,6 +1281,85 @@ components:
12811281
strings.Contains(errors[0].Reason, "not found"),
12821282
"Expected error about circular reference or JSON pointer not found, got: %s", errors[0].Reason)
12831283
}
1284+
func TestValidateResponseBody_XMLMarshalError(t *testing.T) {
1285+
tb := newvalidateResponseTestBed(
1286+
t,
1287+
[]byte(`
1288+
openapi: 3.1.0
1289+
info:
1290+
title: Test Spec
1291+
version: 1.0.0
1292+
paths:
1293+
/test:
1294+
get:
1295+
responses:
1296+
'200':
1297+
description: Success
1298+
content:
1299+
application/xml:
1300+
schema:
1301+
type: object
1302+
properties:
1303+
bad_number:
1304+
type: number
1305+
`,
1306+
),
1307+
)
1308+
1309+
req, res := tb.makeRequestWithReponse(
1310+
t,
1311+
http.MethodGet,
1312+
"/test",
1313+
func(w http.ResponseWriter, r *http.Request) {
1314+
w.Header().Set(helpers.ContentTypeHeader, "application/xml")
1315+
w.WriteHeader(http.StatusOK)
1316+
_, _ = w.Write([]byte("<bad_number>NaN</bad_number>"))
1317+
},
1318+
)
1319+
1320+
valid, errors := tb.responseBodyValidator.ValidateResponseBody(req, res)
1321+
1322+
assert.False(t, valid)
1323+
assert.Len(t, errors, 1)
1324+
assert.Equal(t, errors[0].Message, "xml example is malformed")
1325+
}
1326+
1327+
func TestValidateResponseBody_NilSchema(t *testing.T) {
1328+
tb := newvalidateResponseTestBed(
1329+
t,
1330+
[]byte(`
1331+
openapi: 3.1.0
1332+
info:
1333+
title: Test Spec
1334+
version: 1.0.0
1335+
paths:
1336+
/test:
1337+
get:
1338+
responses:
1339+
'200':
1340+
description: Success
1341+
content:
1342+
application/json: {}
1343+
`,
1344+
),
1345+
)
1346+
1347+
req, res := tb.makeRequestWithReponse(
1348+
t,
1349+
http.MethodGet,
1350+
"/test",
1351+
func(w http.ResponseWriter, r *http.Request) {
1352+
w.Header().Set(helpers.ContentTypeHeader, helpers.JSONContentType)
1353+
w.WriteHeader(http.StatusOK)
1354+
_, _ = w.Write(nil)
1355+
},
1356+
)
1357+
1358+
valid, errors := tb.responseBodyValidator.ValidateResponseBody(req, res)
1359+
1360+
assert.True(t, valid)
1361+
assert.Len(t, errors, 0)
1362+
}
12841363

12851364
func TestValidateBody_CheckHeader(t *testing.T) {
12861365
spec := `openapi: "3.0.0"

schema_validation/validate_xml_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/pb33f/libopenapi"
99
"github.com/pb33f/libopenapi-validator/helpers"
1010
"github.com/pb33f/libopenapi/datamodel/high/base"
11+
"github.com/pb33f/libopenapi/orderedmap"
1112
"github.com/stretchr/testify/assert"
1213
)
1314

@@ -1232,3 +1233,21 @@ func TestApplyXMLTransformations_IncorrectSchema(t *testing.T) {
12321233
assert.Equal(t, "got string, want boolean", err[0].SchemaValidationErrors[0].Reason)
12331234
assert.Equal(t, "schema does not pass validation", err[0].Message)
12341235
}
1236+
func TestApplyXMLTransformations_NilPropSchema(t *testing.T) {
1237+
schema := &base.Schema{
1238+
Properties: orderedmap.New[string, *base.SchemaProxy](),
1239+
}
1240+
1241+
emptyProxy := &base.SchemaProxy{}
1242+
schema.Properties.Set("broken_ref_prop", emptyProxy)
1243+
1244+
data := map[string]any{
1245+
"broken_ref_prop": "some_value",
1246+
}
1247+
xmlNsMap := make(map[string]string)
1248+
1249+
result, errs := applyXMLTransformations(data, schema, &xmlNsMap)
1250+
1251+
assert.Len(t, errs, 0)
1252+
assert.NotNil(t, result)
1253+
}

0 commit comments

Comments
 (0)