forked from 3scale/APIcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapicast-policy-cors.t
239 lines (228 loc) · 7.32 KB
/
apicast-policy-cors.t
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use lib 't';
use Test::APIcast::Blackbox 'no_plan';
run_tests();
__DATA__
=== TEST 1: CORS preflight request
Returns 204 and sets the appropriate headers. This test does not configure the
CORS policy with custom headers, so the response headers will be set to accept
the request received.
--- configuration
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"policy_chain": [
{ "name": "apicast.policy.cors" },
{ "name": "apicast.policy.apicast" }
],
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
]
}
}
]
}
--- request
OPTIONS /
--- more_headers
Origin: localhost
Access-Control-Request-Method: GET
--- error_code: 204
--- response_headers
Access-Control-Allow-Methods: GET
Access-Control-Allow-Origin: localhost
Access-Control-Max-Age: 600
--- no_error_log
[error]
=== TEST 2: CORS actual request with default config
This tests a CORS actual (not preflight) request. The only difference with a
non-CORS request is that the CORS headers will be included in the response.
In this test, we are not using a custom config for the CORS policy. This means
that the response will contain the default CORS headers. By default, all of
them (allow-headers, allow-methods, etc.) simply match the headers received in
the request. So for example, if the request sets the 'Origin' header to
'example.com' the response will set 'Access-Control-Allow-Origin' to
'example.com'.
--- configuration
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"policy_chain": [
{ "name": "apicast.policy.cors" },
{ "name": "apicast.policy.apicast" }
],
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
]
}
}
]
}
--- backend
location /transactions/authrep.xml {
content_by_lua_block {
local expected = "service_token=token-value&service_id=42&usage%5Bhits%5D=2&user_key=value"
require('luassert').same(ngx.decode_args(expected), ngx.req.get_uri_args(0))
}
}
--- upstream
location / {
echo 'yay, api backend: $http_host';
}
--- request
GET /?user_key=value
--- more_headers
Origin: http://example.com
Access-Control-Request-Method: GET
Access-Control-Request-Headers: Content-Type
--- response_body env
yay, api backend: test:$TEST_NGINX_SERVER_PORT
--- response_headers
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET
Access-Control-Allow-Origin: http://example.com
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 600
--- error_code: 200
--- no_error_log
[error]
=== TEST 3: CORS actual request with custom config
This tests a CORS actual (not preflight) request. We use a custom config to set
the CORS headers in the response.
--- configuration
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"policy_chain": [
{ "name": "apicast.policy.cors",
"configuration": { "allow_headers": [ "X-Custom-Header-1", "X-Custom-Header-2" ],
"allow_methods": [ "POST", "GET", "OPTIONS" ],
"allow_origin" : "*",
"max_age" : 200,
"allow_credentials": false } },
{ "name": "apicast.policy.apicast" }
],
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
]
}
}
]
}
--- backend
location /transactions/authrep.xml {
content_by_lua_block {
local expected = "service_token=token-value&service_id=42&usage%5Bhits%5D=2&user_key=value"
require('luassert').same(ngx.decode_args(expected), ngx.req.get_uri_args(0))
}
}
--- upstream
location / {
echo 'yay, api backend: $http_host';
}
--- request
GET /?user_key=value
--- more_headers
Origin: http://example.com
Access-Control-Request-Method: GET
Access-Control-Request-Headers: Content-Type
--- response_body env
yay, api backend: test:$TEST_NGINX_SERVER_PORT
--- response_headers
Access-Control-Allow-Headers: X-Custom-Header-1, X-Custom-Header-2
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: false
Access-Control-Max-Age: 200
--- error_code: 200
--- no_error_log
[error]
=== TEST 4: CORS allowing multiple origins
This tests a CORS actual (not preflight) request with multiple Origins.
--- configuration
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"policy_chain": [
{ "name": "apicast.policy.cors",
"configuration": { "allow_headers": [ ],
"allow_methods": [ "POST", "GET", "OPTIONS" ],
"allow_origin" : "(api|web).test.com",
"max_age" : 200,
"allow_credentials": false } },
{ "name": "apicast.policy.apicast" }
],
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
]
}
}
]
}
--- backend
location /transactions/authrep.xml {
content_by_lua_block {
local expected = "service_token=token-value&service_id=42&usage%5Bhits%5D=2&user_key=value"
require('luassert').same(ngx.decode_args(expected), ngx.req.get_uri_args(0))
}
}
--- upstream
location / {
echo 'yay, api backend';
}
--- request eval
[ "GET /?user_key=value", "GET /?user_key=value", "GET /?user_key=value" ]
--- more_headers eval
[
"Origin: http://api.test.com\n".
"Access-Control-Request-Method: GET\n".
"Access-Control-Request-Headers: Content-Type\n",
"Origin: http://web.test.com\n".
"Access-Control-Request-Method: GET\n".
"Access-Control-Request-Headers: Content-Type\n",
"Origin: http://staging.test.com\n".
"Access-Control-Request-Method: GET\n".
"Access-Control-Request-Headers: Content-Type\n",
]
--- response_headers eval
[
"Access-Control-Allow-Methods: POST, GET, OPTIONS\n".
"Access-Control-Allow-Origin: http://api.test.com\n".
"Access-Control-Allow-Credentials: false\n".
"Access-Control-Max-Age: 200\n",
"Access-Control-Allow-Methods: POST, GET, OPTIONS\n".
"Access-Control-Allow-Origin: http://web.test.com\n".
"Access-Control-Allow-Credentials: false\n".
"Access-Control-Max-Age: 200\n",
"Access-Control-Allow-Methods: POST, GET, OPTIONS\n".
"Access-Control-Allow-Credentials: false\n".
"Access-Control-Max-Age: 200\n",
]
--- response_body eval
["yay, api backend\n", "yay, api backend\n", "yay, api backend\n"]
--- error_code eval
[ 200, 200, 200]
--- no_error_log