forked from 3scale/APIcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend-cache-handler.t
106 lines (93 loc) · 2.87 KB
/
backend-cache-handler.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
use lib 't';
use Test::APIcast 'no_plan';
repeat_each(1); # Can't be two as the second call would hit the cache
run_tests();
__DATA__
=== TEST 1: resilient backend will keep calls through without backend connection
When backend returns server error the call will be let through.
--- main_config
env APICAST_BACKEND_CACHE_HANDLER=resilient;
--- http_config
include $TEST_NGINX_UPSTREAM_CONFIG;
lua_package_path "$TEST_NGINX_LUA_PATH";
init_by_lua_block {
require('apicast.configuration_loader').mock({
services = {
{
id = 42,
backend_version = 1,
proxy = {
api_backend = "http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api-backend/",
proxy_rules = {
{ pattern = '/', http_method = 'GET', metric_system_name = 'hits', delta = 2 }
}
}
}
}
})
require('apicast.proxy').shared_cache():set('42:foo:usage%5Bhits%5D=2', 200)
}
lua_shared_dict api_keys 10m;
--- config
include $TEST_NGINX_APICAST_CONFIG;
location /transactions/authrep.xml {
content_by_lua_block { ngx.exit(502) }
}
location /api-backend/ {
echo 'yay, api backend';
}
location = /t {
echo_subrequest GET /test/one -q user_key=value;
echo_subrequest GET /test/two -q user_key=value;
}
--- request eval
["GET /test?user_key=foo", "GET /foo?user_key=foo"]
--- response_body eval
["yay, api backend\x{0a}", "yay, api backend\x{0a}" ]
--- error_code eval
[ 200, 200 ]
=== TEST 2: strict backend will remove cache after not successful status
When backend returns server error the next call will be reauthorized.
--- main_config
env APICAST_BACKEND_CACHE_HANDLER=strict;
--- http_config
include $TEST_NGINX_UPSTREAM_CONFIG;
lua_package_path "$TEST_NGINX_LUA_PATH";
init_by_lua_block {
require('apicast.configuration_loader').mock({
services = {
{
id = 42,
backend_version = 1,
proxy = {
error_status_auth_failed = 402,
error_auth_failed = 'credentials invalid!',
api_backend = "http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api-backend/",
proxy_rules = {
{ pattern = '/', http_method = 'GET', metric_system_name = 'hits', delta = 2 }
}
}
}
}
})
require('apicast.proxy').shared_cache():set('42:foo:usage%5Bhits%5D=2', 200)
}
lua_shared_dict api_keys 10m;
--- config
include $TEST_NGINX_APICAST_CONFIG;
location /transactions/authrep.xml {
content_by_lua_block { ngx.exit(502) }
}
location /api-backend/ {
echo 'yay, api backend';
}
location = /t {
echo_subrequest GET /test/one -q user_key=value;
echo_subrequest GET /test/two -q user_key=value;
}
--- request eval
["GET /test?user_key=foo", "GET /foo?user_key=foo"]
--- response_body eval
["yay, api backend\x{0a}", "credentials invalid!" ]
--- error_code eval
[ 200, 402 ]