1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package spec
16
17 import (
18 "encoding/json"
19 "testing"
20
21 "github.com/go-openapi/swag"
22 "github.com/stretchr/testify/assert"
23 "github.com/stretchr/testify/require"
24 )
25
26 const epsilon = 1e-9
27
28 func float64Ptr(f float64) *float64 {
29 return &f
30 }
31 func int64Ptr(f int64) *int64 {
32 return &f
33 }
34
35 var header = Header{
36 VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{
37 "x-framework": "swagger-go",
38 }},
39 HeaderProps: HeaderProps{Description: "the description of this header"},
40 SimpleSchema: SimpleSchema{
41 Items: &Items{
42 Refable: Refable{Ref: MustCreateRef("Cat")},
43 },
44 Type: "string",
45 Format: "date",
46 Default: "8",
47 },
48 CommonValidations: CommonValidations{
49 Maximum: float64Ptr(100),
50 ExclusiveMaximum: true,
51 ExclusiveMinimum: true,
52 Minimum: float64Ptr(5),
53 MaxLength: int64Ptr(100),
54 MinLength: int64Ptr(5),
55 Pattern: "\\w{1,5}\\w+",
56 MaxItems: int64Ptr(100),
57 MinItems: int64Ptr(5),
58 UniqueItems: true,
59 MultipleOf: float64Ptr(5),
60 Enum: []interface{}{"hello", "world"},
61 },
62 }
63
64 const headerJSON = `{
65 "items": {
66 "$ref": "Cat"
67 },
68 "x-framework": "swagger-go",
69 "description": "the description of this header",
70 "maximum": 100,
71 "minimum": 5,
72 "exclusiveMaximum": true,
73 "exclusiveMinimum": true,
74 "maxLength": 100,
75 "minLength": 5,
76 "pattern": "\\w{1,5}\\w+",
77 "maxItems": 100,
78 "minItems": 5,
79 "uniqueItems": true,
80 "multipleOf": 5,
81 "enum": ["hello", "world"],
82 "type": "string",
83 "format": "date",
84 "default": "8"
85 }`
86
87 func TestIntegrationHeader(t *testing.T) {
88 var actual Header
89 require.NoError(t, json.Unmarshal([]byte(headerJSON), &actual))
90 assert.EqualValues(t, actual, header)
91
92 assertParsesJSON(t, headerJSON, header)
93 }
94
95 func TestJSONLookupHeader(t *testing.T) {
96 var def string
97 res, err := header.JSONLookup("default")
98 require.NoError(t, err)
99 require.NotNil(t, res)
100 require.IsType(t, def, res)
101
102 var ok bool
103 def, ok = res.(string)
104 require.True(t, ok)
105 assert.Equal(t, "8", def)
106
107 var x *interface{}
108 res, err = header.JSONLookup("x-framework")
109 require.NoError(t, err)
110 require.NotNil(t, res)
111 require.IsType(t, x, res)
112
113 x, ok = res.(*interface{})
114 require.True(t, ok)
115 assert.EqualValues(t, "swagger-go", *x)
116
117 res, err = header.JSONLookup("unknown")
118 require.Error(t, err)
119 require.Nil(t, res)
120
121 var max *float64
122 res, err = header.JSONLookup("maximum")
123 require.NoError(t, err)
124 require.NotNil(t, res)
125 require.IsType(t, max, res)
126
127 max, ok = res.(*float64)
128 require.True(t, ok)
129 assert.InDelta(t, float64(100), *max, epsilon)
130 }
131
132 func TestResponseHeaueder(t *testing.T) {
133 var expectedHeader *Header
134 h := ResponseHeader()
135 assert.IsType(t, expectedHeader, h)
136 }
137
138 func TestWithHeader(t *testing.T) {
139 h := new(Header).WithDescription("header description").Typed("integer", "int32")
140 assert.Equal(t, "header description", h.Description)
141 assert.Equal(t, "integer", h.Type)
142 assert.Equal(t, "int32", h.Format)
143
144 i := new(Items).Typed("string", "date")
145 h = new(Header).CollectionOf(i, "pipe")
146
147 assert.EqualValues(t, *i, *h.Items)
148 assert.Equal(t, "pipe", h.CollectionFormat)
149
150 h = new(Header).WithDefault([]string{"a", "b", "c"}).WithMaxLength(10).WithMinLength(3)
151
152 assert.Equal(t, int64(10), *h.MaxLength)
153 assert.Equal(t, int64(3), *h.MinLength)
154 assert.EqualValues(t, []string{"a", "b", "c"}, h.Default)
155
156 h = new(Header).WithPattern("^abc$")
157 assert.Equal(t, Header{
158 CommonValidations: CommonValidations{
159 Pattern: "^abc$",
160 },
161 }, *h)
162 h = new(Header).WithEnum("a", "b", "c")
163 assert.Equal(t, Header{
164 CommonValidations: CommonValidations{
165 Enum: []interface{}{
166 "a",
167 "b",
168 "c",
169 },
170 },
171 }, *h)
172 }
173
174 func TestHeaderWithValidation(t *testing.T) {
175 h := new(Header).WithValidations(CommonValidations{MaxLength: swag.Int64(15)})
176 assert.EqualValues(t, swag.Int64(15), h.MaxLength)
177 }
178
View as plain text