-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
279 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lua_modules | ||
t/servroot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
*.swp | ||
lua_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
luarocks { | ||
group 'production' { | ||
module { 'lua-resty-iputils' }, | ||
}, | ||
|
||
group { 'development', 'test' } { | ||
module { 'apicast' }, | ||
module { 'lua-resty-repl' }, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
apicast scm-1|b28109bd80571cb04c4364b99281db8a27ac5374 | ||
argparse 0.5.0-1| | ||
inspect 3.1.1-0| | ||
liquid scm-1|811a73e38fdd9fdea116be4baf310ca326b96c77 | ||
lua-resty-env 0.4.0-1| | ||
lua-resty-execvp 0.1.0-1| | ||
lua-resty-http 0.12-0| | ||
lua-resty-iputils 0.3.0-1| | ||
lua-resty-jwt 0.1.11-0| | ||
lua-resty-repl 0.0.6-0|3878f41b7e8f97b1c96919db19dbee9496569dda | ||
lua-resty-url 0.2.0-1| | ||
luafilesystem 1.7.0-2| | ||
penlight 1.5.4-1| | ||
router 2.1-0| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requires 'Test::APIcast', '0.03'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
local resty_resolver = require('resty.resolver') | ||
local resty_url = require('resty.url') | ||
local format = string.format | ||
|
||
local _M = require('apicast.policy').new('Upstream', '0.1') | ||
|
||
local new = _M.new | ||
|
||
local empty = {} | ||
function _M.new(configuration) | ||
local policy = new(configuration) | ||
local config = configuration or empty | ||
|
||
local url = resty_url.parse(config.url) or empty | ||
local host = config.host or url.host | ||
|
||
policy.host = host | ||
policy.url = url | ||
|
||
return policy | ||
end | ||
|
||
function _M:content() | ||
local url = self.url | ||
local host = self.host | ||
|
||
ngx.ctx.upstream = resty_resolver:instance():get_servers(url.host, { port = url.port }) | ||
ngx.var.proxy_pass = format('%s://upstream%s', url.scheme, url.path or '') | ||
ngx.req.set_header('Host', host or ngx.var.host) | ||
|
||
if not ngx.headers_sent then | ||
ngx.exec("@upstream") | ||
end | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
BEGIN { | ||
$ENV{TEST_NGINX_APICAST_BINARY} ||= 'rover exec apicast'; | ||
} | ||
|
||
use strict; | ||
use warnings FATAL => 'all'; | ||
use Test::APIcast::Blackbox 'no_plan'; | ||
|
||
repeat_each(1); | ||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: balancer blacklist | ||
The module does not crash without configuration. | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "cloud_hosted.balancer_blacklist" }, | ||
{ "name": "apicast.policy.echo", "configuration": { } } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
GET /t HTTP/1.1 | ||
--- error_code: 200 | ||
--- no_error_log | ||
[error] | ||
=== TEST 2: balancer upstream blacklist | ||
Going to prevent connecting to local upstream. | ||
--- ONLY | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "cloud_hosted.balancer_blacklist" }, | ||
{ "name": "cloud_hosted.upstream", | ||
"configuration": { | ||
"url": "http://test:$TEST_NGINX_SERVER_PORT", "host": "test" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- upstream | ||
location /t { | ||
content_by_lua_block { ngx.say('ok') } | ||
} | ||
--- request | ||
GET /t | ||
--- error_code: 503 | ||
--- error_log | ||
failed to set current backend peer: blacklisted while connecting to upstream | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
BEGIN { | ||
$ENV{TEST_NGINX_APICAST_BINARY} ||= 'rover exec apicast'; | ||
} | ||
|
||
use strict; | ||
use warnings FATAL => 'all'; | ||
use Test::APIcast::Blackbox 'no_plan'; | ||
|
||
repeat_each(1); | ||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: rate limit without limit | ||
The module does not crash without configuration. | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "cloud_hosted.rate_limit" }, | ||
{ "name": "apicast.policy.echo", "configuration": { } } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
GET /t HTTP/1.1 | ||
--- error_code: 200 | ||
--- no_error_log | ||
[error] | ||
=== TEST 2: rate limit with limit | ||
The module does rate limiting. | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "cloud_hosted.rate_limit", "configuration": { "limit": 1 } }, | ||
{ "name": "apicast.policy.echo", "configuration": { } } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request eval | ||
["GET /t", "GET /t"] | ||
--- error_code eval | ||
[200, 503] | ||
--- grep_error_log | ||
rejected request over limit, key: localhost | ||
--- grep_error_log_out eval | ||
["", "rejected request over limit, key: localhost"] | ||
=== TEST 3: rate limit with limit and burst | ||
Delays the request. | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "cloud_hosted.rate_limit", "configuration": { "limit": 1, "burst": 1 } }, | ||
{ "name": "apicast.policy.echo", "configuration": { } } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request eval | ||
["GET /t", "GET /t"] | ||
--- response_body eval | ||
["GET /t HTTP/1.1\n", "GET /t HTTP/1.1\n" ] | ||
--- error_code eval | ||
[200, 200] | ||
--- grep_error_log | ||
delaying request: localhost for 1s, excess: 1 | ||
--- grep_error_log_out eval | ||
["", "delaying request: localhost for 1s, excess: 1"] | ||
=== TEST 4: rate limit with custom status | ||
Sends the custom status code. | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "cloud_hosted.rate_limit", "configuration": { "limit": 1, "status": 429 } }, | ||
{ "name": "apicast.policy.echo", "configuration": { } } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request eval | ||
["GET /t", "GET /t"] | ||
--- error_code eval | ||
[200, 429] |