Skip to content

Commit 5b5b137

Browse files
committed
t: test headers policy
1 parent 45b9d9d commit 5b5b137

File tree

1 file changed

+277
-0
lines changed

1 file changed

+277
-0
lines changed

t/apicast-policy-headers.t

+277
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
use lib 't';
2+
use TestAPIcastBlackbox 'no_plan';
3+
4+
repeat_each(1);
5+
run_tests();
6+
7+
__DATA__
8+
9+
=== TEST 1: add and delete response headers
10+
We configure the headers policy so it adds a custom header in the response
11+
('Custom-Response-Header'). We also add a header in the upstream
12+
('Added-By-Upstream') and configure the policy so that header does not appear
13+
in the response.
14+
--- configuration
15+
{
16+
"services": [
17+
{
18+
"id": 42,
19+
"backend_version": 1,
20+
"backend_authentication_type": "service_token",
21+
"backend_authentication_value": "token-value",
22+
"proxy": {
23+
"policy_chain": [
24+
{ "name": "apicast.policy.apicast" },
25+
{
26+
"name": "apicast.policy.headers",
27+
"configuration":
28+
{
29+
"response":
30+
{
31+
"headers_to_add": { "Custom-Response-Header": "abc" },
32+
"headers_to_delete": ["Added-By-Upstream"]
33+
}
34+
}
35+
}
36+
],
37+
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
38+
"proxy_rules": [
39+
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
40+
]
41+
}
42+
}
43+
]
44+
}
45+
--- backend
46+
location /transactions/authrep.xml {
47+
content_by_lua_block {
48+
local expected = "service_token=token-value&service_id=42&usage%5Bhits%5D=2&user_key=value"
49+
local args = ngx.var.args
50+
if args == expected then
51+
ngx.exit(200)
52+
else
53+
ngx.log(ngx.ERR, expected, ' did not match: ', args)
54+
ngx.exit(403)
55+
end
56+
}
57+
}
58+
--- upstream
59+
location / {
60+
content_by_lua_block {
61+
ngx.header['Added-By-Upstream'] = '123'
62+
ngx.say('yay, api backend')
63+
}
64+
}
65+
--- request
66+
GET /?user_key=value
67+
--- response_body
68+
yay, api backend
69+
--- response_headers
70+
Custom-Response-Header: abc
71+
Added-By-Upstream:
72+
--- error_code: 200
73+
--- no_error_log
74+
[error]
75+
76+
=== TEST 2: add and delete request headers
77+
We configure the headers policy so it adds a custom header in the request
78+
('Custom-Request-Header') and verify that it's received in the upstream.
79+
We also add a header in the request ('Included-In-Request'), and configure the
80+
policy so it deletes that header.
81+
Finally, we also verify that none of those headers appears in the response.
82+
--- configuration
83+
{
84+
"services": [
85+
{
86+
"id": 42,
87+
"backend_version": 1,
88+
"backend_authentication_type": "service_token",
89+
"backend_authentication_value": "token-value",
90+
"proxy": {
91+
"policy_chain": [
92+
{ "name": "apicast.policy.apicast" },
93+
{
94+
"name": "apicast.policy.headers",
95+
"configuration":
96+
{
97+
"request":
98+
{
99+
"headers_to_add": { "Custom-Request-Header": "abc" },
100+
"headers_to_delete": ["Included-In-Request"]
101+
}
102+
}
103+
}
104+
],
105+
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
106+
"proxy_rules": [
107+
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
108+
]
109+
}
110+
}
111+
]
112+
}
113+
--- backend
114+
location /transactions/authrep.xml {
115+
content_by_lua_block {
116+
local expected = "service_token=token-value&service_id=42&usage%5Bhits%5D=2&user_key=value"
117+
local args = ngx.var.args
118+
if args == expected then
119+
ngx.exit(200)
120+
else
121+
ngx.log(ngx.ERR, expected, ' did not match: ', args)
122+
ngx.exit(403)
123+
end
124+
}
125+
}
126+
--- upstream
127+
location / {
128+
content_by_lua_block {
129+
if ngx.req.get_headers()['Included-In-Request'] then
130+
ngx.log(ngx.ERR, 'Unexpected header received in upstream: Included-In-Request');
131+
elseif ngx.req.get_headers()['Custom-Request-Header'] ~= 'abc' then
132+
ngx.log(ngx.ERR, 'Custom-Request-Header not set or set incorrectly');
133+
else
134+
ngx.say('yay, api backend');
135+
end
136+
}
137+
}
138+
--- request
139+
GET /?user_key=value
140+
Included-In-Request: 123123
141+
--- response_body
142+
yay, api backend
143+
--- response_headers
144+
Included-In-Request:
145+
Custom-Request-Header:
146+
--- error_code: 200
147+
--- no_error_log
148+
[error]
149+
150+
=== TEST 3: overwrite existing response header
151+
When a response header is set, and the headers policy is configured to add it,
152+
the value of the header is overwritten.
153+
--- configuration
154+
{
155+
"services": [
156+
{
157+
"id": 42,
158+
"backend_version": 1,
159+
"backend_authentication_type": "service_token",
160+
"backend_authentication_value": "token-value",
161+
"proxy": {
162+
"policy_chain": [
163+
{ "name": "apicast.policy.apicast" },
164+
{
165+
"name": "apicast.policy.headers",
166+
"configuration":
167+
{
168+
"response":
169+
{
170+
"headers_to_add": { "Custom-Response-Header": "config_value" }
171+
}
172+
}
173+
}
174+
],
175+
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
176+
"proxy_rules": [
177+
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
178+
]
179+
}
180+
}
181+
]
182+
}
183+
--- backend
184+
location /transactions/authrep.xml {
185+
content_by_lua_block {
186+
local expected = "service_token=token-value&service_id=42&usage%5Bhits%5D=2&user_key=value"
187+
local args = ngx.var.args
188+
if args == expected then
189+
ngx.exit(200)
190+
else
191+
ngx.log(ngx.ERR, expected, ' did not match: ', args)
192+
ngx.exit(403)
193+
end
194+
}
195+
}
196+
--- upstream
197+
location / {
198+
content_by_lua_block {
199+
ngx.header['Custom-Response-Header'] = "upstream_value"
200+
ngx.say('yay, api backend')
201+
}
202+
}
203+
--- request
204+
GET /?user_key=value
205+
--- response_body
206+
yay, api backend
207+
--- response_headers
208+
Custom-Response-Header: config_value
209+
--- error_code: 200
210+
--- no_error_log
211+
[error]
212+
213+
214+
=== TEST 4: overwrite existing request header
215+
When a request header is set, and the headers policy is configured to add it,
216+
the value of the header is overwritten.
217+
--- configuration
218+
{
219+
"services": [
220+
{
221+
"id": 42,
222+
"backend_version": 1,
223+
"backend_authentication_type": "service_token",
224+
"backend_authentication_value": "token-value",
225+
"proxy": {
226+
"policy_chain": [
227+
{ "name": "apicast.policy.apicast" },
228+
{
229+
"name": "apicast.policy.headers",
230+
"configuration":
231+
{
232+
"request":
233+
{
234+
"headers_to_add": { "Custom-Request-Header": "config_value" }
235+
}
236+
}
237+
}
238+
],
239+
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
240+
"proxy_rules": [
241+
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
242+
]
243+
}
244+
}
245+
]
246+
}
247+
--- backend
248+
location /transactions/authrep.xml {
249+
content_by_lua_block {
250+
local expected = "service_token=token-value&service_id=42&usage%5Bhits%5D=2&user_key=value"
251+
local args = ngx.var.args
252+
if args == expected then
253+
ngx.exit(200)
254+
else
255+
ngx.log(ngx.ERR, expected, ' did not match: ', args)
256+
ngx.exit(403)
257+
end
258+
}
259+
}
260+
--- upstream
261+
location / {
262+
content_by_lua_block {
263+
if ngx.req.get_headers()['Custom-Request-Header'] ~= 'config_value' then
264+
ngx.log(ngx.ERR, 'Custom-Request-Header as incorrect value');
265+
else
266+
ngx.say('yay, api backend');
267+
end
268+
}
269+
}
270+
--- request
271+
GET /?user_key=value
272+
Custom-Request-Header: request_value
273+
--- response_body
274+
yay, api backend
275+
--- error_code: 200
276+
--- no_error_log
277+
[error]

0 commit comments

Comments
 (0)