blob: 4d7465862d69aafd192aaac601b94aa28602c338 [file] [log] [blame]
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
BEGIN {
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
$SkipReason = "unavailable for the hup tests";
} else {
$ENV{TEST_NGINX_USE_HUP} = 1;
undef $ENV{TEST_NGINX_USE_STAP};
}
}
use t::APISIX 'no_plan';
repeat_each(1);
no_long_string();
no_shuffle();
no_root_location();
run_tests;
__DATA__
=== TEST 1: sanity
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.proxy-rewrite")
local ok, err = plugin.check_schema({
uri = '/apisix/home',
host = 'apisix.iresty.com'
})
if not ok then
ngx.say(err)
end
ngx.say("done")
}
}
--- request
GET /t
--- response_body
done
=== TEST 2: add plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/test/add",
"host": "apisix.iresty.com"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 3: update plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/test/update",
"host": "apisix.iresty.com"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 4: disable plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 5: set route(rewrite host)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"uri": "/plugin_proxy_rewrite",
"host": "apisix.iresty.com"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 6: rewrite host
--- request
GET /hello HTTP/1.1
--- response_body
uri: /plugin_proxy_rewrite
host: apisix.iresty.com
scheme: http
=== TEST 7: set route(rewrite host + upstream scheme is https)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"uri": "/plugin_proxy_rewrite",
"host": "test.com"
}
},
"upstream": {
"scheme": "https",
"nodes": {
"127.0.0.1:1983": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 8: rewrite host + upstream scheme is https
--- request
GET /hello HTTP/1.1
--- response_body
uri: /plugin_proxy_rewrite
host: test.com
scheme: https
=== TEST 9: set route(rewrite headers)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri/plugin_proxy_rewrite",
"headers": {
"X-Api-Version": "v2"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 10: rewrite headers
--- request
GET /hello HTTP/1.1
--- more_headers
X-Api-Version:v1
--- response_body
uri: /uri/plugin_proxy_rewrite
host: localhost
x-api-version: v2
x-real-ip: 127.0.0.1
=== TEST 11: set route(add headers)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri/plugin_proxy_rewrite",
"headers": {
"X-Api-Engine": "apisix"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 12: add headers
--- request
GET /hello HTTP/1.1
--- response_body
uri: /uri/plugin_proxy_rewrite
host: localhost
x-api-engine: apisix
x-real-ip: 127.0.0.1
=== TEST 13: set route(rewrite empty headers)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri/plugin_proxy_rewrite",
"headers": {
"X-Api-Test": "hello"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 14: rewrite empty headers
--- request
GET /hello HTTP/1.1
--- more_headers
X-Api-Test:
--- response_body
uri: /uri/plugin_proxy_rewrite
host: localhost
x-api-test: hello
x-real-ip: 127.0.0.1
=== TEST 15: set route(rewrite uri args)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/plugin_proxy_rewrite_args"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 16: rewrite uri args
--- request
GET /hello?q=apisix&a=iresty HTTP/1.1
--- response_body
uri: /plugin_proxy_rewrite_args
a: iresty
q: apisix
=== TEST 17: set route(rewrite uri empty args)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/plugin_proxy_rewrite_args"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 18: rewrite uri empty args
--- request
GET /hello HTTP/1.1
--- response_body
uri: /plugin_proxy_rewrite_args
=== TEST 19: remove header
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri/plugin_proxy_rewrite",
"headers": {
"X-Api-Engine": "APISIX",
"X-Api-Test": ""
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 20: remove header
--- request
GET /hello HTTP/1.1
--- more_headers
X-Api-Test: foo
X-Api-Engine: bar
--- response_body
uri: /uri/plugin_proxy_rewrite
host: localhost
x-api-engine: APISIX
x-real-ip: 127.0.0.1
=== TEST 21: set route(only using regex_uri)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/test/(.*)/(.*)/(.*)", "/$1_$2_$3"]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/test/*"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 22: hit route(rewrite uri using regex_uri)
--- request
GET /test/plugin/proxy/rewrite HTTP/1.1
--- response_body
uri: /plugin_proxy_rewrite
host: localhost
scheme: http
=== TEST 23: hit route(404 not found)
--- request
GET /test/not/found HTTP/1.1
--- error_code: 404
=== TEST 24: set route(Using both uri and regex_uri)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/hello",
"regex_uri": ["^/test/(.*)", "/${1}1"]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/test/*"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 25: hit route(rewrite uri using uri & regex_uri property)
--- request
GET /test/hello HTTP/1.1
--- response_body
hello world
=== TEST 26: set route(invalid regex_uri)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/test/(.*)"]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/test/*"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
=== TEST 27: set route(invalid regex syntax for the first element)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"regex_uri": ["[^/test/(.*)", "/$1"]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/test/*"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/invalid regex_uri/
=== TEST 28: set route(invalid regex syntax for the second element)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/test/(.*)", "/$`1"]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/test/*"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- error_log
invalid capturing variable name found
=== TEST 29: set route(invalid uri)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "hello"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/failed to match pattern/
=== TEST 30: wrong value of uri
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.proxy-rewrite")
local ok, err = plugin.check_schema({
uri = 'home'
})
if not ok then
ngx.say(err)
return
end
ngx.say("done")
}
}
--- request
GET /t
--- response_body
property "uri" validation failed: failed to match pattern "^\\/.*" with "home"
=== TEST 31: set route(invalid header field)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri/plugin_proxy_rewrite",
"headers": {
"X-Api:Version": "v2"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/invalid field character/
--- error_log
header field: X-Api:Version
=== TEST 32: set route(invalid header value)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri/plugin_proxy_rewrite",
"headers": {
"X-Api-Version": "v2\r\n"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/invalid value character/
=== TEST 33: set route(rewrite uri with args)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/plugin_proxy_rewrite_args?q=apisix"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 34: rewrite uri with args
--- request
GET /hello?a=iresty
--- response_body_like eval
qr/uri: \/plugin_proxy_rewrite_args(
q: apisix
a: iresty|
a: iresty
q: apisix)
/
=== TEST 35: print the plugin `conf` in etcd, no dirty data
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local t = require("lib.test_admin").test
local encode_with_keys_sorted = require("toolkit.json").encode
local code, _, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri/plugin_proxy_rewrite",
"headers": {
"X-Api": "v2"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
local resp_data = core.json.decode(body)
ngx.say(encode_with_keys_sorted(resp_data.value.plugins))
}
}
--- request
GET /t
--- response_body
{"proxy-rewrite":{"headers":{"X-Api":"v2"},"uri":"/uri/plugin_proxy_rewrite","use_real_request_uri_unsafe":false}}
=== TEST 36: set route(header contains nginx variables)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri",
"headers": {
"x-api": "$remote_addr",
"name": "$arg_name",
"x-key": "$http_key"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 37: hit route(header supports nginx variables)
--- request
GET /hello?name=Bill HTTP/1.1
--- more_headers
key: X-APISIX
--- response_body
uri: /uri
host: localhost
key: X-APISIX
name: Bill
x-api: 127.0.0.1
x-key: X-APISIX
x-real-ip: 127.0.0.1
=== TEST 38: set route(nginx variable does not exist)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri",
"headers": {
"x-api": "$helle",
"name": "$arg_world",
"x-key": "$http_key",
"Version": "nginx_var_does_not_exist"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 39: hit route(get nginx variable is nil)
--- request
GET /hello HTTP/1.1
--- response_body
uri: /uri
host: localhost
version: nginx_var_does_not_exist
x-real-ip: 127.0.0.1
=== TEST 40: set route(rewrite uri based on ctx.var)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/$arg_new_uri"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/test"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 41: hit route(upstream uri: should be /hello)
--- request
GET /test?new_uri=hello
--- response_body
hello world
=== TEST 42: host with port
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.proxy-rewrite")
local ok, err = plugin.check_schema({
host = 'apisix.iresty.com:6443',
})
if not ok then
ngx.say(err)
end
ngx.say("done")
}
}
--- request
GET /t
--- response_body
done
=== TEST 43: set route(rewrite host with port), ensure ngx.var.uri matched the rewritten version
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"uri": "/uri",
"host": "test.com:6443"
},
"serverless-post-function": {
"phase": "access",
"functions" : ["return function(conf, ctx)
assert(ngx.var.uri == \"/uri\", \"proxy-rewrite do not call ngx.req.set_uri\")
end"]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 44: rewrite host with port
--- request
GET /hello
--- response_body
uri: /uri
host: test.com:6443
x-real-ip: 127.0.0.1