-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_test.go
96 lines (89 loc) · 2.55 KB
/
header_test.go
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
package openapi_test
import (
"testing"
"github.com/MarkRosemaker/openapi"
"github.com/go-json-experiment/json/jsontext"
)
func TestHeader_JSON(t *testing.T) {
t.Parallel()
// A simple header of type `integer`:
testJSON(t, []byte(`{
"description": "The number of allowed requests in the current period",
"schema": {
"type": "integer"
}
}`), &openapi.Header{})
}
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{},
},
"schema.type is required",
},
{
openapi.Header{
Content: openapi.Content{},
},
"content is invalid: must contain exactly one entry, got 0",
},
{
openapi.Header{
Content: openapi.Content{
openapi.MediaRangeJSON: {
Schema: &openapi.SchemaRef{
Value: &openapi.Schema{},
},
},
},
},
`content["application/json"].schema.type is required`,
},
{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: true,
}, `explode (true) is invalid: property has no effect when schema is not present`},
{openapi.Header{
Schema: &openapi.Schema{Type: openapi.TypeString},
Explode: true,
}, `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)
}
})
}
}