Skip to content

Commit

Permalink
t: test headers policy
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Nov 22, 2017
1 parent 45b9d9d commit 5b5b137
Showing 1 changed file with 277 additions and 0 deletions.
277 changes: 277 additions & 0 deletions t/apicast-policy-headers.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
use lib 't';
use TestAPIcastBlackbox 'no_plan';

repeat_each(1);
run_tests();

__DATA__

=== TEST 1: add and delete response headers
We configure the headers policy so it adds a custom header in the response
('Custom-Response-Header'). We also add a header in the upstream
('Added-By-Upstream') and configure the policy so that header does not appear
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.apicast" },
{
"name": "apicast.policy.headers",
"configuration":
{
"response":
{
"headers_to_add": { "Custom-Response-Header": "abc" },
"headers_to_delete": ["Added-By-Upstream"]
}
}
}
],
"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"
local args = ngx.var.args
if args == expected then
ngx.exit(200)
else
ngx.log(ngx.ERR, expected, ' did not match: ', args)
ngx.exit(403)
end
}
}
--- upstream
location / {
content_by_lua_block {
ngx.header['Added-By-Upstream'] = '123'
ngx.say('yay, api backend')
}
}
--- request
GET /?user_key=value
--- response_body
yay, api backend
--- response_headers
Custom-Response-Header: abc
Added-By-Upstream:
--- error_code: 200
--- no_error_log
[error]

=== TEST 2: add and delete request headers
We configure the headers policy so it adds a custom header in the request
('Custom-Request-Header') and verify that it's received in the upstream.
We also add a header in the request ('Included-In-Request'), and configure the
policy so it deletes that header.
Finally, we also verify that none of those headers appears 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.apicast" },
{
"name": "apicast.policy.headers",
"configuration":
{
"request":
{
"headers_to_add": { "Custom-Request-Header": "abc" },
"headers_to_delete": ["Included-In-Request"]
}
}
}
],
"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"
local args = ngx.var.args
if args == expected then
ngx.exit(200)
else
ngx.log(ngx.ERR, expected, ' did not match: ', args)
ngx.exit(403)
end
}
}
--- upstream
location / {
content_by_lua_block {
if ngx.req.get_headers()['Included-In-Request'] then
ngx.log(ngx.ERR, 'Unexpected header received in upstream: Included-In-Request');
elseif ngx.req.get_headers()['Custom-Request-Header'] ~= 'abc' then
ngx.log(ngx.ERR, 'Custom-Request-Header not set or set incorrectly');
else
ngx.say('yay, api backend');
end
}
}
--- request
GET /?user_key=value
Included-In-Request: 123123
--- response_body
yay, api backend
--- response_headers
Included-In-Request:
Custom-Request-Header:
--- error_code: 200
--- no_error_log
[error]

=== TEST 3: overwrite existing response header
When a response header is set, and the headers policy is configured to add it,
the value of the header is overwritten.
--- configuration
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"policy_chain": [
{ "name": "apicast.policy.apicast" },
{
"name": "apicast.policy.headers",
"configuration":
{
"response":
{
"headers_to_add": { "Custom-Response-Header": "config_value" }
}
}
}
],
"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"
local args = ngx.var.args
if args == expected then
ngx.exit(200)
else
ngx.log(ngx.ERR, expected, ' did not match: ', args)
ngx.exit(403)
end
}
}
--- upstream
location / {
content_by_lua_block {
ngx.header['Custom-Response-Header'] = "upstream_value"
ngx.say('yay, api backend')
}
}
--- request
GET /?user_key=value
--- response_body
yay, api backend
--- response_headers
Custom-Response-Header: config_value
--- error_code: 200
--- no_error_log
[error]


=== TEST 4: overwrite existing request header
When a request header is set, and the headers policy is configured to add it,
the value of the header is overwritten.
--- configuration
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"policy_chain": [
{ "name": "apicast.policy.apicast" },
{
"name": "apicast.policy.headers",
"configuration":
{
"request":
{
"headers_to_add": { "Custom-Request-Header": "config_value" }
}
}
}
],
"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"
local args = ngx.var.args
if args == expected then
ngx.exit(200)
else
ngx.log(ngx.ERR, expected, ' did not match: ', args)
ngx.exit(403)
end
}
}
--- upstream
location / {
content_by_lua_block {
if ngx.req.get_headers()['Custom-Request-Header'] ~= 'config_value' then
ngx.log(ngx.ERR, 'Custom-Request-Header as incorrect value');
else
ngx.say('yay, api backend');
end
}
}
--- request
GET /?user_key=value
Custom-Request-Header: request_value
--- response_body
yay, api backend
--- error_code: 200
--- no_error_log
[error]

0 comments on commit 5b5b137

Please sign in to comment.