1
1
package parameters
2
2
3
3
import (
4
+ "github.com/pb33f/libopenapi"
5
+ "github.com/stretchr/testify/assert"
6
+ "net/http"
4
7
"testing"
5
8
6
9
"github.com/stretchr/testify/require"
@@ -13,3 +16,181 @@ func Test_ForceCompilerError(t *testing.T) {
13
16
// Ideally this would result in an error response, current behavior swallows the error
14
17
require .Empty (t , result )
15
18
}
19
+
20
+ func TestHeaderSchemaNoType (t * testing.T ) {
21
+
22
+ bytes := []byte (`{
23
+ "openapi": "3.0.0",
24
+ "info": {
25
+ "title": "API Spec With Mandatory Header",
26
+ "version": "1.0.0"
27
+ },
28
+ "paths": {
29
+ "/api-endpoint": {
30
+ "get": {
31
+ "summary": "Restricted API Endpoint",
32
+ "parameters": [
33
+ {
34
+ "name": "apiKey",
35
+ "in": "header",
36
+ "required": true,
37
+ "schema": {
38
+ "oneOf": [
39
+ {
40
+ "type": "boolean"
41
+ },
42
+ {
43
+ "type": "integer"
44
+ }
45
+ ]
46
+ }
47
+ }
48
+ ],
49
+ "responses": {
50
+ "200": {
51
+ "description": "Successful response"
52
+ }
53
+ }
54
+ }
55
+ }
56
+ },
57
+ "components": {
58
+ "securitySchemes": {
59
+ "ApiKeyHeader": {
60
+ "type": "apiKey",
61
+ "name": "apiKey",
62
+ "in": "header"
63
+ }
64
+ }
65
+ },
66
+ "security": [
67
+ {
68
+ "ApiKeyHeader": []
69
+ }
70
+ ]
71
+ }` )
72
+
73
+ doc , err := libopenapi .NewDocument (bytes )
74
+ if err != nil {
75
+ t .Fatalf ("error while creating open api spec document: %v" , err )
76
+ }
77
+
78
+ req , err := http .NewRequest ("GET" , "/api-endpoint" , nil )
79
+ if err != nil {
80
+ t .Fatalf ("error while creating request: %v" , err )
81
+ }
82
+
83
+ req .Header .Set ("Content-Type" , "application/json" )
84
+ req .Header .Set ("apiKey" , "headerValue" )
85
+
86
+ v3Model , errs := doc .BuildV3Model ()
87
+ if len (errs ) > 0 {
88
+ t .Fatalf ("error while building v3 model: %v" , errs )
89
+ }
90
+
91
+ v3Model .Model .Servers = nil
92
+ // render the document back to bytes and reload the model.
93
+ _ , doc , v3Model , errs = doc .RenderAndReload ()
94
+
95
+ validator := NewParameterValidator (& v3Model .Model )
96
+
97
+ isSuccess , valErrs := validator .ValidateHeaderParams (req )
98
+
99
+ assert .False (t , isSuccess )
100
+ assert .Len (t , valErrs , 1 )
101
+ assert .Equal (t , "schema 'apiKey' is defined as an boolean or integer, however it failed to pass a schema validation" , valErrs [0 ].Reason )
102
+ assert .Len (t , valErrs [0 ].SchemaValidationErrors , 2 )
103
+ assert .Equal (t , "got string, want boolean" , valErrs [0 ].SchemaValidationErrors [0 ].Reason )
104
+ assert .Equal (t , "got string, want integer" , valErrs [0 ].SchemaValidationErrors [1 ].Reason )
105
+
106
+ }
107
+
108
+ func TestHeaderSchemaNoType_AllPoly (t * testing.T ) {
109
+
110
+ bytes := []byte (`{
111
+ "openapi": "3.0.0",
112
+ "info": {
113
+ "title": "API Spec With Mandatory Header",
114
+ "version": "1.0.0"
115
+ },
116
+ "paths": {
117
+ "/api-endpoint": {
118
+ "get": {
119
+ "summary": "Restricted API Endpoint",
120
+ "parameters": [
121
+ {
122
+ "name": "apiKey",
123
+ "in": "header",
124
+ "required": true,
125
+ "schema": {
126
+ "oneOf": [
127
+ {
128
+ "type": "boolean"
129
+ },
130
+ {
131
+ "type": "integer"
132
+ }
133
+ ],
134
+ "allOf": [
135
+ {
136
+ "type": "boolean"
137
+ },
138
+ ]
139
+ }
140
+ }
141
+ ],
142
+ "responses": {
143
+ "200": {
144
+ "description": "Successful response"
145
+ }
146
+ }
147
+ }
148
+ }
149
+ },
150
+ "components": {
151
+ "securitySchemes": {
152
+ "ApiKeyHeader": {
153
+ "type": "apiKey",
154
+ "name": "apiKey",
155
+ "in": "header"
156
+ }
157
+ }
158
+ },
159
+ "security": [
160
+ {
161
+ "ApiKeyHeader": []
162
+ }
163
+ ]
164
+ }` )
165
+
166
+ doc , err := libopenapi .NewDocument (bytes )
167
+ if err != nil {
168
+ t .Fatalf ("error while creating open api spec document: %v" , err )
169
+ }
170
+
171
+ req , err := http .NewRequest ("GET" , "/api-endpoint" , nil )
172
+ if err != nil {
173
+ t .Fatalf ("error while creating request: %v" , err )
174
+ }
175
+
176
+ req .Header .Set ("Content-Type" , "application/json" )
177
+ req .Header .Set ("apiKey" , "headerValue" )
178
+
179
+ v3Model , errs := doc .BuildV3Model ()
180
+ if len (errs ) > 0 {
181
+ t .Fatalf ("error while building v3 model: %v" , errs )
182
+ }
183
+
184
+ v3Model .Model .Servers = nil
185
+ // render the document back to bytes and reload the model.
186
+ _ , doc , v3Model , errs = doc .RenderAndReload ()
187
+
188
+ validator := NewParameterValidator (& v3Model .Model )
189
+
190
+ isSuccess , valErrs := validator .ValidateHeaderParams (req )
191
+
192
+ assert .False (t , isSuccess )
193
+ assert .Len (t , valErrs , 1 )
194
+ assert .Equal (t , "schema 'apiKey' is defined as an boolean and a integer, however it failed to pass a schema validation" , valErrs [0 ].Reason )
195
+ assert .Len (t , valErrs [0 ].SchemaValidationErrors , 3 )
196
+ }
0 commit comments