forked from 3scale/APIcast
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapicast-upstream-balancer.t
151 lines (120 loc) · 2.96 KB
/
apicast-upstream-balancer.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
use lib 't';
use Test::APIcast 'no_plan';
require("policies.pl");
run_tests();
__DATA__
=== TEST 1: resolver
--- http_config
lua_package_path "$TEST_NGINX_LUA_PATH";
--- config
location /t {
rewrite_by_lua_block {
local resty_resolver = require 'resty.resolver'
local dns_client = require 'resty.dns.resolver'
local dns = dns_client:new{ nameservers = { { "127.0.0.1", $TEST_NGINX_RANDOM_PORT } } }
local resolver = resty_resolver.new(dns)
local servers = resolver:get_servers('3scale.net')
servers.answers = nil
ngx.ctx.upstream = servers
}
content_by_lua_block {
local upstream = {}
for _,server in ipairs(ngx.ctx.upstream) do
table.insert(upstream, server)
end
ngx.say(require('cjson').encode(upstream))
}
}
--- udp_listen random_port env chomp
$TEST_NGINX_RANDOM_PORT
--- udp_reply dns
[ "localhost", "127.0.0.1" ]
--- request
GET /t
--- expected_json
[{"ttl":0,"address":"127.0.0.1"}]
--- error_code: 200
=== TEST 2: balancer
--- http_config
lua_package_path "$TEST_NGINX_LUA_PATH";
upstream upstream {
server 0.0.0.1:1;
balancer_by_lua_block {
local balancer = require 'apicast.balancer'
local Upstream = require 'apicast.upstream'
local upstream = Upstream.new('http://127.0.0.1:$TEST_NGINX_SERVER_PORT')
upstream.servers = { { address = '127.0.0.1', port = $TEST_NGINX_SERVER_PORT } }
assert(balancer:call({ upstream = upstream }))
}
keepalive 32;
}
--- config
location /api {
echo 'yay';
}
location /t {
proxy_pass http://upstream/api;
}
--- request
GET /t
--- response_body
yay
--- error_code: 200
=== TEST 3: upstream + balancer
--- http_config
lua_package_path "$TEST_NGINX_LUA_PATH";
upstream upstream {
server 0.0.0.1:1;
balancer_by_lua_block {
local balancer = require 'apicast.balancer'
assert(balancer:call(ngx.ctx))
}
keepalive 32;
}
--- config
location /api {
echo 'yay';
}
location /t {
rewrite_by_lua_block {
local Upstream = require 'apicast.upstream'
local upstream = Upstream.new('http://127.0.0.1:$TEST_NGINX_SERVER_PORT')
upstream:resolve()
ngx.ctx.upstream = upstream
}
proxy_pass http://upstream/api;
}
--- udp_listen random_port env chomp
$TEST_NGINX_RANDOM_PORT
--- udp_reply dns
[ "localhost", "127.0.0.1" ]
--- request
GET /t
--- response_body
yay
--- error_code: 200
=== TEST 4: unsupported scheme
Using an unsopported URI scheme causes an exception
--- http_config
lua_package_path "$TEST_NGINX_LUA_PATH";
upstream upstream {
server 0.0.0.1:1;
balancer_by_lua_block {
local balancer = require 'apicast.balancer'
ngx.ctx.upstream = { { address = '127.0.0.1', port = $TEST_NGINX_SERVER_PORT } }
local peers = balancer:call()
}
keepalive 32;
}
--- config
location /api {
echo 'yay';
}
location /t {
set $proxy_pass "unsupported://upstream/api";
proxy_pass $proxy_pass;
}
--- request
GET /t
--- error_code: 500
--- error_log: invalid URL prefix in