| 1 |
// Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// https://pb33f.io |
| 3 |
|
| 4 |
package errors |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/pb33f/libopenapi" |
| 10 |
"github.com/pb33f/libopenapi-validator/helpers" |
| 11 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
) |
| 14 |
|
| 15 |
func getTestSchema() *base.Schema { |
| 16 |
spec := `openapi: 3.0.0 |
| 17 |
paths: |
| 18 |
/pet: |
| 19 |
get: |
| 20 |
responses: |
| 21 |
'200': |
| 22 |
content: |
| 23 |
application/xml: |
| 24 |
schema: |
| 25 |
type: object |
| 26 |
properties: |
| 27 |
age: |
| 28 |
type: integer |
| 29 |
xml: |
| 30 |
name: Cat` |
| 31 |
|
| 32 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 33 |
v3Doc, _ := doc.BuildV3Model() |
| 34 |
|
| 35 |
return v3Doc.Model.Paths.PathItems.GetOrZero("/pet").Get.Responses.Codes.GetOrZero("200"). |
| 36 |
Content.GetOrZero("application/xml").Schema.Schema() |
| 37 |
} |
| 38 |
|
| 39 |
func TestMissingPrefixError(t *testing.T) { |
| 40 |
schema := getTestSchema() |
| 41 |
err := MissingPrefix(schema, "prx") |
| 42 |
|
| 43 |
assert.NotNil(t, *err) |
| 44 |
assert.Equal(t, helpers.XmlValidationPrefix, (*err).ValidationSubType) |
| 45 |
} |
| 46 |
|
| 47 |
func TestMissingNamespaceError(t *testing.T) { |
| 48 |
schema := getTestSchema() |
| 49 |
err := MissingNamespace(schema, "http://ex.c") |
| 50 |
|
| 51 |
assert.NotNil(t, *err) |
| 52 |
assert.Equal(t, helpers.XmlValidationNamespace, (*err).ValidationSubType) |
| 53 |
} |
| 54 |
|
| 55 |
func TestInvalidPrefixError(t *testing.T) { |
| 56 |
schema := getTestSchema() |
| 57 |
err := InvalidPrefix(schema, "prx") |
| 58 |
|
| 59 |
assert.NotNil(t, *err) |
| 60 |
assert.Equal(t, helpers.XmlValidationPrefix, (*err).ValidationSubType) |
| 61 |
} |
| 62 |
|
| 63 |
func TestInvalidNamespaceError(t *testing.T) { |
| 64 |
schema := getTestSchema() |
| 65 |
err := InvalidNamespace(schema, "other", "http://ex.c", "prx") |
| 66 |
|
| 67 |
assert.NotNil(t, *err) |
| 68 |
assert.Equal(t, helpers.XmlValidationNamespace, (*err).ValidationSubType) |
| 69 |
} |
| 70 |
|
| 71 |
func TestInvalidParsing(t *testing.T) { |
| 72 |
err := InvalidXMLParsing("no data sent", "invalid-xml") |
| 73 |
|
| 74 |
assert.NotNil(t, (*err)) |
| 75 |
assert.Equal(t, (*err).SchemaValidationErrors[0].Reason, "no data sent") |
| 76 |
assert.Equal(t, (*err).SchemaValidationErrors[0].ReferenceObject, "invalid-xml") |
| 77 |
assert.Equal(t, helpers.Schema, (*err).ValidationSubType) |
| 78 |
} |