Repository navigation
Expand file tree
/
Copy pathheader_test.go
More file actions
123 lines (114 loc) · 3.48 KB
/
Copy pathheader_test.go
File metadata and controls
123 lines (114 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package openapi_test
import (
"encoding/json/jsontext"
"testing"
"github.com/MarkRosemaker/openapi"
)
func TestHeader_Resolve_ContentError(t *testing.T) {
t.Parallel()
// A header that uses `content` with a schema $ref that cannot be resolved.
// The error path must name the field "content", not "examples" (copy-paste bug).
_, err := openapi.LoadFromDataJSON([]byte(`{
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"paths": {
"/test": {
"get": {
"responses": {
"200": {
"description": "OK",
"headers": {
"X-Test": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/NonExistent"}
}
}
}
}
}
}
}
}
}
}`))
if err == nil {
t.Fatal("expected error")
}
const want = `paths["/test"].GET.responses["200"].headers["X-Test"].content["application/json"].schema: couldn't resolve "#/components/schemas/NonExistent"`
if got := err.Error(); got != want {
t.Fatalf("want: %s\n got: %s", want, got)
}
}
var y, yes = true, &y
func TestHeader_Validate_Error(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
header openapi.Header
err string
}{
{openapi.Header{}, "schema or content is required"},
{
openapi.Header{
Schema: &openapi.Schema{},
Content: openapi.Content{},
},
"schema and content are mutually exclusive",
},
{
openapi.Header{
Schema: &openapi.Schema{Type: openapi.TypeString, Required: []string{"id"}},
},
"schema.required is invalid: only valid for object type, got string",
},
{
openapi.Header{
Content: openapi.Content{},
},
"content is invalid: must contain exactly one entry, got 0",
},
{
openapi.Header{
Content: openapi.Content{
openapi.MediaRangeJSON: {
Schema: &openapi.Schema{Type: openapi.TypeString, Required: []string{"id"}},
},
},
},
`content["application/json"].schema.required is invalid: only valid for object type, got string`,
},
{openapi.Header{
Content: openapi.Content{openapi.MediaRangeJSON: {}},
Style: "foo",
}, `style ("foo") is invalid, must be one of: "matrix", "label", "form", "simple", "spaceDelimited", "pipeDelimited", "deepObject"`},
{openapi.Header{
Content: openapi.Content{openapi.MediaRangeJSON: {}},
Explode: yes,
}, `explode (true) is invalid: property has no effect when schema is not present`},
{openapi.Header{
Schema: &openapi.Schema{Type: openapi.TypeString},
Explode: yes,
}, `explode (true) is invalid: property has no effect when schema type is not array or object, got "string"`},
{openapi.Header{
Schema: &openapi.Schema{Type: openapi.TypeString},
Example: jsontext.Value("foo"),
Examples: openapi.Examples{},
}, `example and examples are mutually exclusive`},
{openapi.Header{
Schema: &openapi.Schema{Type: openapi.TypeString},
Examples: openapi.Examples{"foo": invalidExample},
}, `examples["foo"]: value and externalValue are mutually exclusive`},
{openapi.Header{
Schema: &openapi.Schema{Type: openapi.TypeString},
Extensions: jsontext.Value(`{"foo": "bar"}`),
}, `foo: ` + openapi.ErrUnknownField.Error()},
} {
t.Run(tc.err, func(t *testing.T) {
if err := tc.header.Validate(); err == nil {
t.Fatal("expected error")
} else if want := tc.err; want != err.Error() {
t.Fatalf("want: %s, got: %s", want, err)
}
})
}
}