feat(apisix): add Cloudron package
- Implements Apache APISIX packaging for Cloudron platform. - Includes Dockerfile, CloudronManifest.json, and start.sh. - Configured to use Cloudron's etcd addon. 🤖 Generated with Gemini CLI Co-Authored-By: Gemini <noreply@google.com>
This commit is contained in:
374
CloudronPackages/APISIX/apisix-source/t/router/graphql.t
Normal file
374
CloudronPackages/APISIX/apisix-source/t/router/graphql.t
Normal file
@@ -0,0 +1,374 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
|
||||
if (!$block->error_log && !$block->no_error_log) {
|
||||
$block->set_value("no_error_log", "[error]\n[alert]");
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route by name
|
||||
--- 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": ["POST"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [["graphql_name", "==", "repo"]]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: route by name
|
||||
--- request
|
||||
POST /hello
|
||||
query repo {
|
||||
owner {
|
||||
name
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 3: set route by operation+name
|
||||
--- 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": ["POST"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [
|
||||
["graphql_operation", "==", "mutation"],
|
||||
["graphql_name", "==", "repo"]
|
||||
]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: route by operation+name
|
||||
--- request
|
||||
POST /hello
|
||||
mutation repo($ep: Episode!, $review: ReviewInput!) {
|
||||
createReview(episode: $ep, review: $review) {
|
||||
stars
|
||||
commentary
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 5: route by operation+name, miss
|
||||
--- request
|
||||
POST /hello
|
||||
query repo {
|
||||
owner {
|
||||
name
|
||||
}
|
||||
}
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 6: multiple operations
|
||||
--- request
|
||||
POST /hello
|
||||
mutation repo($ep: Episode!, $review: ReviewInput!) {
|
||||
createReview(episode: $ep, review: $review) {
|
||||
stars
|
||||
commentary
|
||||
}
|
||||
}
|
||||
query repo {
|
||||
owner {
|
||||
name
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
--- error_log
|
||||
Multiple operations are not supported
|
||||
|
||||
|
||||
|
||||
=== TEST 7: bad graphql
|
||||
--- request
|
||||
POST /hello
|
||||
AA
|
||||
--- error_code: 404
|
||||
--- error_log
|
||||
failed to parse graphql: Syntax error near line 1 body: AA
|
||||
|
||||
|
||||
|
||||
=== TEST 8: set anonymous operation name
|
||||
--- 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": ["POST"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [
|
||||
["graphql_operation", "==", "query"],
|
||||
["graphql_name", "==", ""]
|
||||
]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 9: route by anonymous name
|
||||
--- request
|
||||
POST /hello
|
||||
query {
|
||||
owner {
|
||||
name
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 10: limit the max size
|
||||
--- yaml_config
|
||||
graphql:
|
||||
max_size: 5
|
||||
--- request
|
||||
POST /hello
|
||||
query {
|
||||
owner {
|
||||
name
|
||||
}
|
||||
}
|
||||
--- error_code: 404
|
||||
--- error_log
|
||||
failed to read graphql data
|
||||
|
||||
|
||||
|
||||
=== TEST 11: set graphql_root_fields
|
||||
--- 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": ["POST", "GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [
|
||||
["graphql_operation", "==", "query"],
|
||||
["graphql_root_fields", "has", "owner"]
|
||||
]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 12: single root field
|
||||
--- request
|
||||
POST /hello
|
||||
query {
|
||||
owner {
|
||||
name
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 13: test send http post json data
|
||||
--- request
|
||||
POST /hello
|
||||
{"query":"query{owner{name}}"}
|
||||
--- more_headers
|
||||
Content-Type: application/json
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 14: test send http get query data
|
||||
--- request
|
||||
GET /hello?query=query{owner{name}}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 15: test send http get multiple query data success
|
||||
--- request
|
||||
GET /hello?query=query{owner{name}}&query=query{repo{name}}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 16: test send http get multiple query data failure
|
||||
--- request
|
||||
GET /hello?query=query{repo{name}}&query=query{owner{name}}
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 17: no body (HTTP GET)
|
||||
--- request
|
||||
GET /hello
|
||||
--- error_code: 404
|
||||
--- error_log
|
||||
failed to read graphql data, args[query] is nil
|
||||
|
||||
|
||||
|
||||
=== TEST 18: no body (HTTP POST JSON)
|
||||
--- request
|
||||
POST /hello
|
||||
{}
|
||||
--- more_headers
|
||||
Content-Type: application/json
|
||||
--- error_code: 404
|
||||
--- error_log
|
||||
failed to read graphql data, json body[query] is nil
|
||||
|
||||
|
||||
|
||||
=== TEST 19: multiple root fields
|
||||
--- request
|
||||
POST /hello
|
||||
query {
|
||||
repo {
|
||||
stars
|
||||
}
|
||||
owner {
|
||||
name
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 20: root fields mismatch
|
||||
--- request
|
||||
POST /hello
|
||||
query {
|
||||
repo {
|
||||
name
|
||||
}
|
||||
}
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 21: no body
|
||||
--- request
|
||||
POST /hello
|
||||
--- error_code: 404
|
||||
--- error_log
|
||||
failed to read graphql data, request body has zero size
|
342
CloudronPackages/APISIX/apisix-source/t/router/multi-ssl-certs.t
Normal file
342
CloudronPackages/APISIX/apisix-source/t/router/multi-ssl-certs.t
Normal file
@@ -0,0 +1,342 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
log_level('debug');
|
||||
no_root_location();
|
||||
|
||||
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set ssl(sni: www.test.com)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/apisix.crt")
|
||||
local ssl_key = t.read_file("t/certs/apisix.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "www.test.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "www.test.com"
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: set route(id: 1)
|
||||
--- 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,
|
||||
[[{
|
||||
"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: client request
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "www.test.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
|
||||
local req = "GET /hello HTTP/1.0\r\nHost: www.test.com\r\nConnection: close\r\n\r\n"
|
||||
local bytes, err = sock:send(req)
|
||||
if not bytes then
|
||||
ngx.say("failed to send http request: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("sent http request: ", bytes, " bytes.")
|
||||
|
||||
while true do
|
||||
local line, err = sock:receive()
|
||||
if not line then
|
||||
-- ngx.say("failed to receive response status line: ", err)
|
||||
break
|
||||
end
|
||||
|
||||
ngx.say("received: ", line)
|
||||
end
|
||||
|
||||
local ok, err = sock:close()
|
||||
ngx.say("close: ", ok, " ", err)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body eval
|
||||
qr{connected: 1
|
||||
ssl handshake: true
|
||||
sent http request: 62 bytes.
|
||||
received: HTTP/1.1 200 OK
|
||||
received: Content-Type: text/plain
|
||||
received: Content-Length: 12
|
||||
received: Connection: close
|
||||
received: Server: APISIX/\d\.\d+(\.\d+)?
|
||||
received: \nreceived: hello world
|
||||
close: 1 nil}
|
||||
--- error_log
|
||||
server name: "www.test.com"
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 4: set second ssl(sni: *.test2.com)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "*.test2.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/2',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "*.test2.com"
|
||||
},
|
||||
"key": "/apisix/ssls/2"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 5: client request: www.test2.com
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "www.test2.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body_like
|
||||
connected: 1
|
||||
failed to do SSL handshake: 18: self[- ]signed certificate
|
||||
--- error_log
|
||||
server name: "www.test2.com"
|
||||
we have more than 1 ssl certs now
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 6: set third ssl(sni: apisix.dev)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/apisix_admin_ssl.crt")
|
||||
local ssl_key = t.read_file("t/certs/apisix_admin_ssl.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "apisix.dev"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/3',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "apisix.dev"
|
||||
},
|
||||
"key": "/apisix/ssls/3"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 7: client request: apisix.dev
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "apisix.dev", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body_like
|
||||
connected: 1
|
||||
failed to do SSL handshake: 18: self[- ]signed certificate
|
||||
--- error_log
|
||||
server name: "apisix.dev"
|
||||
we have more than 1 ssl certs now
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 8: remove test ssl certs
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
t.test('/apisix/admin/ssls/1', ngx.HTTP_DELETE)
|
||||
t.test('/apisix/admin/ssls/2', ngx.HTTP_DELETE)
|
||||
t.test('/apisix/admin/ssls/3', ngx.HTTP_DELETE)
|
||||
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
@@ -0,0 +1,171 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
admin_key: null
|
||||
deployment:
|
||||
role: data_plane
|
||||
role_data_plane:
|
||||
config_provider: yaml
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: hit routes(priority: 1 + priority: 2)
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
priority: 1
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
priority: 2
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: test.com
|
||||
--- response_body eval
|
||||
qr/1980/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
||||
|
||||
|
||||
|
||||
=== TEST 2: hit routes(priority: 2 + priority: 1)
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
priority: 2
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
priority: 1
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: test.com
|
||||
--- response_body eval
|
||||
qr/1981/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
||||
|
||||
|
||||
|
||||
=== TEST 3: hit routes(priority: default_value + priority: 1)
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
priority: 1
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: test.com
|
||||
--- response_body eval
|
||||
qr/1980/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
||||
|
||||
|
||||
|
||||
=== TEST 4: hit routes(priority: 1 + priority: default_value)
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
priority: 1
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: test.com
|
||||
--- response_body eval
|
||||
qr/1981/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
@@ -0,0 +1,285 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route(host + 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,
|
||||
[[{
|
||||
"uri": "/hello",
|
||||
"host": "foo.com",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: /not_found
|
||||
--- request
|
||||
GET /not_found
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 3: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 4: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: not_found.com
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 5: hit routes
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: foo.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 6: hit routes
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: foo.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 7: set route(only 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,
|
||||
[[{
|
||||
"uri": "/server_port",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1981": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 8: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 9: hit routes
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: anydomain.com
|
||||
--- response_body_like eval
|
||||
qr/1981/
|
||||
|
||||
|
||||
|
||||
=== TEST 10: set route(only uri + id: 2)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"uri": "/hello",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1981": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 11: /not_found
|
||||
--- request
|
||||
GET /hello2
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 12: hit routes
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: anydomain.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 13: delete route(id: 2)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_DELETE
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 14: set route(wildcard host + 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,
|
||||
[[{
|
||||
"uri": "/hello",
|
||||
"host": "*.foo.com",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 15: hit routes
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: www.foo.com
|
||||
--- response_body
|
||||
hello world
|
@@ -0,0 +1,388 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
admin_key: null
|
||||
deployment:
|
||||
role: data_plane
|
||||
role_data_plane:
|
||||
config_provider: yaml
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: test.com
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
-
|
||||
uri: /server_port
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: test.com
|
||||
--- response_body eval
|
||||
qr/1981/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
||||
|
||||
|
||||
|
||||
=== TEST 2: *.test.com + uri
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /server_port
|
||||
host: "*.test.com"
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
-
|
||||
uri: /server_port
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: www.test.com
|
||||
--- response_body eval
|
||||
qr/1981/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
||||
|
||||
|
||||
|
||||
=== TEST 3: *.test.com + /*
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /*
|
||||
host: "*.test.com"
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
-
|
||||
uri: /server_port
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: www.test.com
|
||||
--- response_body eval
|
||||
qr/1981/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
||||
|
||||
|
||||
|
||||
=== TEST 4: filter_func(not match)
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /*
|
||||
host: "*.test.com"
|
||||
filter_func: "function(vars) return vars.arg_name == 'json' end"
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
-
|
||||
uri: /server_port
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port?name=unknown
|
||||
--- more_headers
|
||||
Host: www.test.com
|
||||
--- response_body eval
|
||||
qr/1980/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
||||
|
||||
|
||||
|
||||
=== TEST 5: filter_func(match)
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /*
|
||||
host: "*.test.com"
|
||||
filter_func: "function(vars) return vars.arg_name == 'json' end"
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
-
|
||||
uri: /server_port
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
#END
|
||||
|
||||
--- request
|
||||
GET /server_port?name=json
|
||||
--- more_headers
|
||||
Host: www.test.com
|
||||
--- response_body eval
|
||||
qr/1981/
|
||||
--- error_log
|
||||
use config_provider: yaml
|
||||
|
||||
|
||||
|
||||
=== TEST 6: set route with ':'
|
||||
--- yaml_config
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
admin_key: null
|
||||
--- 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,
|
||||
[[{
|
||||
"uri": "/file:listReputationHistories",
|
||||
"plugins":{"proxy-rewrite":{"uri":"/hello"}},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 7: hit routes
|
||||
--- yaml_config
|
||||
apisix:
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
--- request
|
||||
GET /file:listReputationHistories
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 8: not hit
|
||||
--- yaml_config
|
||||
apisix:
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
--- request
|
||||
GET /file:xx
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 9: set route with ':' & host
|
||||
--- yaml_config
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
admin_key: null
|
||||
--- 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,
|
||||
[[{
|
||||
"uri": "/do:listReputationHistories",
|
||||
"hosts": ["t.com"],
|
||||
"plugins":{"proxy-rewrite":{"uri":"/hello"}},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 10: hit routes
|
||||
--- yaml_config
|
||||
apisix:
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
--- request
|
||||
GET /do:listReputationHistories
|
||||
--- more_headers
|
||||
Host: t.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 11: not hit
|
||||
--- yaml_config
|
||||
apisix:
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
--- request
|
||||
GET /do:xx
|
||||
--- more_headers
|
||||
Host: t.com
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 12: request host with uppercase
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.com
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
#END
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: tEst.com
|
||||
|
||||
|
||||
|
||||
=== TEST 13: configure host with uppercase
|
||||
--- apisix_yaml
|
||||
routes:
|
||||
-
|
||||
uri: /server_port
|
||||
host: test.coM
|
||||
upstream:
|
||||
nodes:
|
||||
"127.0.0.1:1981": 1
|
||||
type: roundrobin
|
||||
#END
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: test.com
|
||||
|
||||
|
||||
|
||||
=== TEST 14: inherit hosts from services
|
||||
--- apisix_yaml
|
||||
services:
|
||||
- id: 1
|
||||
hosts:
|
||||
- bar.com
|
||||
upstreams:
|
||||
- id: 1
|
||||
nodes:
|
||||
"127.0.0.1:1980": 1
|
||||
type: roundrobin
|
||||
routes:
|
||||
-
|
||||
service_id: 1
|
||||
upstream_id: 1
|
||||
uri: /hello
|
||||
plugins:
|
||||
proxy-rewrite:
|
||||
uri: /hello1
|
||||
-
|
||||
upstream_id: 1
|
||||
uri: /hello
|
||||
priority: -1
|
||||
#END
|
||||
--- more_headers
|
||||
Host: www.foo.com
|
||||
--- request
|
||||
GET /hello
|
||||
--- response_body
|
||||
hello world
|
@@ -0,0 +1,355 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_host_uri'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
|
||||
if (!$block->error_log && !$block->no_error_log &&
|
||||
(defined $block->error_code && $block->error_code != 502))
|
||||
{
|
||||
$block->set_value("no_error_log", "[error]");
|
||||
}
|
||||
|
||||
$block;
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: change hosts in services
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/hello"
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/services/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["foo.com"]
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.sleep(0.1)
|
||||
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"service_id": "1",
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
for _, h in ipairs({"foo.com", "bar.com"}) do
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri, {headers = {Host = h}})
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
if res.status == 404 then
|
||||
ngx.say(res.status)
|
||||
else
|
||||
ngx.print(res.body)
|
||||
end
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/services/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["bar.com"]
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.sleep(0.1)
|
||||
|
||||
for _, h in ipairs({"foo.com", "bar.com"}) do
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri, {headers = {Host = h}})
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
if res.status == 404 then
|
||||
ngx.say(res.status)
|
||||
else
|
||||
ngx.print(res.body)
|
||||
end
|
||||
end
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
404
|
||||
404
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 2: check matched._path
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/hello"
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["foo.com"],
|
||||
"plugins": {
|
||||
"serverless-post-function": {
|
||||
"functions" : ["return function(conf, ctx)
|
||||
ngx.log(ngx.WARN, 'matched uri: ', ctx.curr_req_matched._path);
|
||||
end"]
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 3: hit, plain path
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: foo.com
|
||||
--- grep_error_log eval
|
||||
qr/matched uri: \/\w+/
|
||||
--- grep_error_log_out
|
||||
matched uri: /hello
|
||||
|
||||
|
||||
|
||||
=== TEST 4: check matched._path, wildcard
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/hello"
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["foo.com"],
|
||||
"plugins": {
|
||||
"serverless-post-function": {
|
||||
"functions" : ["return function(conf, ctx)
|
||||
ngx.log(ngx.WARN, 'matched uri: ', ctx.curr_req_matched._path);
|
||||
end"]
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/*"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 5: hit
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: foo.com
|
||||
--- grep_error_log eval
|
||||
qr/matched uri: \/\S+,/
|
||||
--- grep_error_log_out
|
||||
matched uri: /*,
|
||||
|
||||
|
||||
|
||||
=== TEST 6: check matched._host
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/hello"
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["foo.com"],
|
||||
"plugins": {
|
||||
"serverless-post-function": {
|
||||
"functions" : ["return function(conf, ctx)
|
||||
ngx.log(ngx.WARN, 'matched host: ', ctx.curr_req_matched._host);
|
||||
end"]
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 7: hit
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: foo.com
|
||||
--- grep_error_log eval
|
||||
qr/func\(\): matched host: [^,]+/
|
||||
--- grep_error_log_out
|
||||
func(): matched host: foo.com
|
||||
|
||||
|
||||
|
||||
=== TEST 8: check matched._host, wildcard
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/hello"
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["*.com"],
|
||||
"plugins": {
|
||||
"serverless-post-function": {
|
||||
"functions" : ["return function(conf, ctx)
|
||||
ngx.log(ngx.WARN, 'matched host: ', ctx.curr_req_matched._host);
|
||||
end"]
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 9: hit
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: foo.com
|
||||
--- grep_error_log eval
|
||||
qr/func\(\): matched host: [^,]+/
|
||||
--- grep_error_log_out
|
||||
func(): matched host: *.com
|
@@ -0,0 +1,113 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
|
||||
if (!defined $block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route without PURGE method
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: route mismatch
|
||||
--- request
|
||||
PURGE /hello
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 3: set route with PURGE method
|
||||
--- 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", "PURGE"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: route match PURGE method
|
||||
--- request
|
||||
PURGE /hello
|
||||
--- error_code: 200
|
826
CloudronPackages/APISIX/apisix-source/t/router/radixtree-sni.t
Normal file
826
CloudronPackages/APISIX/apisix-source/t/router/radixtree-sni.t
Normal file
@@ -0,0 +1,826 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
log_level('debug');
|
||||
no_root_location();
|
||||
|
||||
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set ssl(sni: www.test.com)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/apisix.crt")
|
||||
local ssl_key = t.read_file("t/certs/apisix.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "www.test.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "www.test.com"
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: set route(id: 1)
|
||||
--- 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,
|
||||
[[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 3: client request
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "www.test.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
|
||||
local req = "GET /hello HTTP/1.0\r\nHost: www.test.com\r\nConnection: close\r\n\r\n"
|
||||
local bytes, err = sock:send(req)
|
||||
if not bytes then
|
||||
ngx.say("failed to send http request: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("sent http request: ", bytes, " bytes.")
|
||||
|
||||
while true do
|
||||
local line, err = sock:receive()
|
||||
if not line then
|
||||
-- ngx.say("failed to receive response status line: ", err)
|
||||
break
|
||||
end
|
||||
|
||||
ngx.say("received: ", line)
|
||||
end
|
||||
|
||||
local ok, err = sock:close()
|
||||
ngx.say("close: ", ok, " ", err)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body eval
|
||||
qr{connected: 1
|
||||
ssl handshake: true
|
||||
sent http request: 62 bytes.
|
||||
received: HTTP/1.1 200 OK
|
||||
received: Content-Type: text/plain
|
||||
received: Content-Length: 12
|
||||
received: Connection: close
|
||||
received: Server: APISIX/\d\.\d+(\.\d+)?
|
||||
received: \nreceived: hello world
|
||||
close: 1 nil}
|
||||
--- error_log
|
||||
server name: "www.test.com"
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 4: client request(no cert domain)
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "no-cert.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
end
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
connected: 1
|
||||
failed to do SSL handshake: handshake failed
|
||||
--- error_log
|
||||
failed to match any SSL certificate by SNI
|
||||
|
||||
|
||||
|
||||
=== TEST 5: set ssl(sni: wildcard)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/apisix.crt")
|
||||
local ssl_key = t.read_file("t/certs/apisix.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "*.test.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "*.test.com"
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 6: client request
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "www.test.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
|
||||
local req = "GET /hello HTTP/1.0\r\nHost: www.test.com\r\nConnection: close\r\n\r\n"
|
||||
local bytes, err = sock:send(req)
|
||||
if not bytes then
|
||||
ngx.say("failed to send http request: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("sent http request: ", bytes, " bytes.")
|
||||
|
||||
while true do
|
||||
local line, err = sock:receive()
|
||||
if not line then
|
||||
-- ngx.say("failed to receive response status line: ", err)
|
||||
break
|
||||
end
|
||||
|
||||
ngx.say("received: ", line)
|
||||
end
|
||||
|
||||
local ok, err = sock:close()
|
||||
ngx.say("close: ", ok, " ", err)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body eval
|
||||
qr{connected: 1
|
||||
ssl handshake: true
|
||||
sent http request: 62 bytes.
|
||||
received: HTTP/1.1 200 OK
|
||||
received: Content-Type: text/plain
|
||||
received: Content-Length: 12
|
||||
received: Connection: close
|
||||
received: Server: APISIX/\d\.\d+(\.\d+)?
|
||||
received: \nreceived: hello world
|
||||
close: 1 nil}
|
||||
--- error_log
|
||||
server name: "www.test.com"
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 7: set ssl(sni: test.com)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/apisix.crt")
|
||||
local ssl_key = t.read_file("t/certs/apisix.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "test.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "test.com"
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 8: client request: test.com
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "test.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
|
||||
local req = "GET /hello HTTP/1.0\r\nHost: test.com\r\nConnection: close\r\n\r\n"
|
||||
local bytes, err = sock:send(req)
|
||||
if not bytes then
|
||||
ngx.say("failed to send http request: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("sent http request: ", bytes, " bytes.")
|
||||
|
||||
while true do
|
||||
local line, err = sock:receive()
|
||||
if not line then
|
||||
-- ngx.say("failed to receive response status line: ", err)
|
||||
break
|
||||
end
|
||||
|
||||
ngx.say("received: ", line)
|
||||
end
|
||||
|
||||
local ok, err = sock:close()
|
||||
ngx.say("close: ", ok, " ", err)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body eval
|
||||
qr{connected: 1
|
||||
ssl handshake: true
|
||||
sent http request: 58 bytes.
|
||||
received: HTTP/1.1 200 OK
|
||||
received: Content-Type: text/plain
|
||||
received: Content-Length: 12
|
||||
received: Connection: close
|
||||
received: Server: APISIX/\d\.\d+(\.\d+)?
|
||||
received: \nreceived: hello world
|
||||
close: 1 nil}
|
||||
--- error_log
|
||||
server name: "test.com"
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 9: set ssl(sni: *.test2.com)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "*.test2.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "*.test2.com"
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 10: client request: www.test2.com
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "www.test2.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body_like
|
||||
connected: 1
|
||||
failed to do SSL handshake: 18: self[- ]signed certificate
|
||||
--- error_log
|
||||
server name: "www.test2.com"
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 11: client request: aa.bb.test2.com
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "aa.bb.test2.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
connected: 1
|
||||
failed to do SSL handshake: handshake failed
|
||||
--- error_log
|
||||
server name: "aa.bb.test2.com"
|
||||
failed to find any SSL certificate by SNI: aa.bb.test2.com matched SNI: *.test2.com
|
||||
--- no_error_log
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 12: disable ssl(sni: *.test2.com)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local data = {status = 0}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PATCH,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"status": 0
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 13: client request: www.test2.com -- failed by disable
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "www.test2.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
connected: 1
|
||||
failed to do SSL handshake: handshake failed
|
||||
--- error_log
|
||||
server name: "www.test2.com"
|
||||
--- no_error_log
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 14: enable ssl(sni: *.test2.com)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local data = {status = 1}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PATCH,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"status": 1
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 15: client request: www.test2.com again
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "www.test2.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body_like
|
||||
connected: 1
|
||||
failed to do SSL handshake: 18: self[- ]signed certificate
|
||||
--- error_log
|
||||
server name: "www.test2.com"
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 16: set ssl(snis: {test2.com, *.test2.com})
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, snis = {"test2.com", "*.test2.com"}}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"snis": ["test2.com", "*.test2.com"]
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 17: client request: test2.com
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "test2.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body_like
|
||||
connected: 1
|
||||
failed to do SSL handshake: 18: self[- ]signed certificate
|
||||
--- error_log
|
||||
server name: "test2.com"
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 18: client request: aa.bb.test2.com -- snis un-include
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "aa.bb.test2.com", true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
connected: 1
|
||||
failed to do SSL handshake: handshake failed
|
||||
--- error_log
|
||||
server name: "aa.bb.test2.com"
|
||||
failed to find any SSL certificate by SNI: aa.bb.test2.com matched SNIs: ["*.test2.com","test2.com"]
|
||||
--- no_error_log
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 19: set ssl(encrypt ssl key with another iv)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.aes_encrypt(t.read_file("t/certs/test2.key"))
|
||||
local data = {cert = ssl_cert, key = ssl_key, snis = {"test2.com", "*.test2.com"}}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data)
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.print(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
{"error_msg":"failed to decrypt previous encrypted key"}
|
||||
--- error_code: 400
|
||||
--- error_log
|
||||
decrypt ssl key failed
|
822
CloudronPackages/APISIX/apisix-source/t/router/radixtree-sni2.t
Normal file
822
CloudronPackages/APISIX/apisix-source/t/router/radixtree-sni2.t
Normal file
@@ -0,0 +1,822 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
log_level('debug');
|
||||
no_root_location();
|
||||
|
||||
BEGIN {
|
||||
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
|
||||
$ENV{TEST_ENV_SSL_CRT} = "-----BEGIN CERTIFICATE-----
|
||||
MIIEsTCCAxmgAwIBAgIUMbgUUCYHkuKDaPy0bzZowlK0JG4wDQYJKoZIhvcNAQEL
|
||||
BQAwVzELMAkGA1UEBhMCQ04xEjAQBgNVBAgMCUd1YW5nRG9uZzEPMA0GA1UEBwwG
|
||||
Wmh1SGFpMQ8wDQYDVQQKDAZpcmVzdHkxEjAQBgNVBAMMCXRlc3QyLmNvbTAgFw0y
|
||||
MDA0MDQyMjE3NTJaGA8yMTIwMDMxMTIyMTc1MlowVzELMAkGA1UEBhMCQ04xEjAQ
|
||||
BgNVBAgMCUd1YW5nRG9uZzEPMA0GA1UEBwwGWmh1SGFpMQ8wDQYDVQQKDAZpcmVz
|
||||
dHkxEjAQBgNVBAMMCXRlc3QyLmNvbTCCAaIwDQYJKoZIhvcNAQEBBQADggGPADCC
|
||||
AYoCggGBAMQGBk35V3zaNVDWzEzVGd+EkZnUOrRpXQg5mmcnoKnrQ5rQQMsQCbMO
|
||||
gFvLt/9OEZQmbE2HuEKsPzL79Yjdu8rGjSoQdbJZ9ccO32uvln1gn68iK79o7Tvm
|
||||
TCi+BayyNA+lo9IxrBm1wGBkOU1ZPasGYzgBAbMLTSDps1EYxNR8t4l9PrTTRsh6
|
||||
NZyTYoDeVIsKZ9SckpjWVnxHOkF+AzZzIJJSe2pj572TDLYA/Xw9I4X3L+SHzwTl
|
||||
iGWNXb2tU367LHERHvensQzdle7mQN2kE5GpB7QPWB+t9V4mn30jc/LyDvOaei6L
|
||||
+pbl5CriGBTjaR80oXhK765K720BQeKUezri15bQlMaUGQRnzr53ZsqA4PEh6WCX
|
||||
hUT2ibO32+uZFXzVQw8y/JUkPf76pZagi8DoLV+sfSbUtnpbQ8wyV2qqTM2eCuPi
|
||||
RgUwXQi2WssKKzrqcgKil3vksHZozLtOmyZiNE4qfNxv+UGoIybJtZmB+9spY0Rw
|
||||
5zBRuULycQIDAQABo3MwcTAdBgNVHQ4EFgQUCmZefzpizPrb3VbiIDhrA48ypB8w
|
||||
HwYDVR0jBBgwFoAUCmZefzpizPrb3VbiIDhrA48ypB8wDAYDVR0TBAUwAwEB/zAh
|
||||
BgNVHREEGjAYggl0ZXN0Mi5jb22CCyoudGVzdDIuY29tMA0GCSqGSIb3DQEBCwUA
|
||||
A4IBgQA0nRTv1zm1ACugJFfYZfxZ0mLJfRUCFMmFfhy+vGiIu6QtnOFVw/tEOyMa
|
||||
m78lBiqac15n3YWYiHiC5NFffTZ7XVlOjN2i4x2z2IJsHNa8tU80AX0Q/pizGK/d
|
||||
+dzlcsGBb9MGT18h/B3/EYQFKLjUsr0zvDb1T0YDlRUsN3Bq6CvZmvfe9F7Yh4Z/
|
||||
XO5R+rX8w9c9A2jzM5isBw2qp/Ggn5RQodMwApEYkJdu80MuxaY6s3dssS4Ay8wP
|
||||
VNFEeLcdauJ00ES1OnbnuNiYSiSMOgWBsnR+c8AaSRB/OZLYQQKGGYbq0tspwRjM
|
||||
MGJRrI/jdKnvJQ8p02abdvA9ZuFChoD3Wg03qQ6bna68ZKPd9peBPpMrDDGDLkGI
|
||||
NzZ6bLJKILnQkV6b1OHVnPDsKXfXjUTTNK/QLJejTXu9RpMBakYZMzs/SOSDtFlS
|
||||
A+q25t6+46nvA8msUSBKyOGBX42mJcKvR4OgG44PfDjYfmjn2l+Dz/jNXDclpb+Q
|
||||
XAzBnfM=
|
||||
-----END CERTIFICATE-----";
|
||||
$ENV{TEST_ENV_SSL_KEY} = "-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIG5QIBAAKCAYEAxAYGTflXfNo1UNbMTNUZ34SRmdQ6tGldCDmaZyegqetDmtBA
|
||||
yxAJsw6AW8u3/04RlCZsTYe4Qqw/Mvv1iN27ysaNKhB1sln1xw7fa6+WfWCfryIr
|
||||
v2jtO+ZMKL4FrLI0D6Wj0jGsGbXAYGQ5TVk9qwZjOAEBswtNIOmzURjE1Hy3iX0+
|
||||
tNNGyHo1nJNigN5Uiwpn1JySmNZWfEc6QX4DNnMgklJ7amPnvZMMtgD9fD0jhfcv
|
||||
5IfPBOWIZY1dva1TfrsscREe96exDN2V7uZA3aQTkakHtA9YH631XiaffSNz8vIO
|
||||
85p6Lov6luXkKuIYFONpHzSheErvrkrvbQFB4pR7OuLXltCUxpQZBGfOvndmyoDg
|
||||
8SHpYJeFRPaJs7fb65kVfNVDDzL8lSQ9/vqllqCLwOgtX6x9JtS2eltDzDJXaqpM
|
||||
zZ4K4+JGBTBdCLZayworOupyAqKXe+SwdmjMu06bJmI0Tip83G/5QagjJsm1mYH7
|
||||
2yljRHDnMFG5QvJxAgMBAAECggGBAIELlkruwvGmlULKpWRPReEn3NJwLNVoJ56q
|
||||
jUMri1FRWAgq4PzNahU+jrHfwxmHw3rMcK/5kQwTaOefh1y63E35uCThARqQroSE
|
||||
/gBeb6vKWFVrIXG5GbQ9QBXyQroV9r/2Q4q0uJ+UTzklwbNx9G8KnXbY8s1zuyrX
|
||||
rvzMWYepMwqIMSfJjuebzH9vZ4F+3BlMmF4XVUrYj8bw/SDwXB0UXXT2Z9j6PC1J
|
||||
CS0oKbgIZ8JhoF3KKjcHBGwWTIf5+byRxeG+z99PBEBafm1Puw1vLfOjD3DN/fso
|
||||
8xCEtD9pBPBJ+W97x/U+10oKetmP1VVEr2Ph8+s2VH1zsRF5jo5d0GtvJqOwIQJ7
|
||||
z3OHJ7lLODw0KAjB1NRXW4dTTUDm6EUuUMWFkGAV6YTyhNLAT0DyrUFJck9RiY48
|
||||
3QN8vSf3n/+3wwg1gzcJ9w3W4DUbvGqu86CaUQ4UegfYJlusY/3YGp5bGNQdxmws
|
||||
lgIoSRrHp6UJKsP8Yl08MIvT/oNLgQKBwQD75SuDeyE0ukhEp0t6v+22d18hfSef
|
||||
q3lLWMI1SQR9Kiem9Z1KdRkIVY8ZAHANm6D8wgjOODT4QZtiqJd2BJn3Xf+aLfCd
|
||||
CW0hPvmGTcp/E4sDZ2u0HbIrUStz7ZcgXpjD2JJAJGEKY2Z7J65gnTqbqoBDrw1q
|
||||
1+FqtikkHRte1UqxjwnWBpSdoRQFgNPHxPWffhML1xsD9Pk1B1b7JoakYcKsNoQM
|
||||
oXUKPLxSZEtd0hIydqmhGYTa9QWBPNDlA5UCgcEAxzfGbOrPBAOOYZd3jORXQI6p
|
||||
H7SddTHMQyG04i+OWUd0HZFkK7/k6r26GFmImNIsQMB26H+5XoKRFKn+sUl14xHY
|
||||
FwB140j0XSav2XzT38UpJ9CptbgK1eKGQVp41xwRYjHVScE5hJuA3a1TKM0l26rp
|
||||
hny/KaP+tXuqt9QbxcUN6efubNYyFP+m6nq2/XdX74bJuGpXLq8W0oFdiocO6tmF
|
||||
4/Hsc4dCVrcwULqXQa0lJ57zZpfIPARqWM2847xtAoHBANVUNbDpg6rTJMc34722
|
||||
dAy3NhL3mqooH9aG+hsEls+l9uT4WFipqSScyU8ERuHPbt0BO1Hi2kFx1rYMUBG8
|
||||
PeT4b7NUutVUGV8xpUNv+FH87Bta6CUnjTAQUzuf+QCJ/NjIPrwh0yloG2+roIvk
|
||||
PLF/CZfI1hUpdZfZZChYmkiLXPHZURw4gH6q33j1rOYf0WFc9aZua0vDmZame6zB
|
||||
6P+oZ6VPmi/UQXoFC/y/QfDYK18fjfOI2DJTlnDoX4XErQKBwGc3M5xMz/MRcJyJ
|
||||
oIwj5jzxbRibOJV2tpD1jsU9xG/nQHbtVEwCgTVKFXf2M3qSMhFeZn0xZ7ZayZY+
|
||||
OVJbcDO0lBPezjVzIAB/Qc7aCOBAQ4F4b+VRtHN6iPqlSESTK0KH9Szgas+UzeCM
|
||||
o7BZEctNMu7WBSkq6ZXXu+zAfZ8q6HmPDA3hsFMG3dFQwSxzv+C/IhZlKkRqvNVV
|
||||
50QVk5oEF4WxW0PECY/qG6NH+YQylDSB+zPlYf4Of5cBCWOoxQKBwQCeo37JpEAR
|
||||
kYtqSjXkC5GpPTz8KR9lCY4SDuC1XoSVCP0Tk23GX6GGyEf4JWE+fb/gPEFx4Riu
|
||||
7pvxRwq+F3LaAa/FFTNUpY1+8UuiMO7J0B1RkVXkyJjFUF/aQxAnOoZPmzrdZhWy
|
||||
bpe2Ka+JS/aXSd1WRN1nmo/DarpWFvdLWZFwUt6zMziH40o1gyPHEuXOqVtf2QCe
|
||||
Q6WC9xnEz4lbb/fR2TF9QRA4FtoRpDe/f3ZGIpWE0RdwyZZ6uA7T1+Q=
|
||||
-----END RSA PRIVATE KEY-----";
|
||||
}
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1 set ssl with multiple certificates.
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/apisix.crt")
|
||||
local ssl_key = t.read_file("t/certs/apisix.key")
|
||||
local ssl_ecc_cert = t.read_file("t/certs/apisix_ecc.crt")
|
||||
local ssl_ecc_key = t.read_file("t/certs/apisix_ecc.key")
|
||||
|
||||
local data = {
|
||||
cert = ssl_cert,
|
||||
key = ssl_key,
|
||||
certs = { ssl_ecc_cert },
|
||||
keys = { ssl_ecc_key },
|
||||
sni = "test.com",
|
||||
}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "test.com"
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: client request using ECC certificate
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
location /t {
|
||||
lua_ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384;
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "test.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
connected: 1
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 3: client request using RSA certificate
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
lua_ssl_ciphers ECDHE-RSA-AES256-SHA384;
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "test.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
connected: 1
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 4: set ssl(sni: *.test2.com) once again
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "*.test2.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "*.test2.com"
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 5: caching of parsed certs and pkeys
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
local work = function()
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say("connected: ", ok)
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "www.test2.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
local ok, err = sock:close()
|
||||
ngx.say("close: ", ok, " ", err)
|
||||
end -- do
|
||||
|
||||
work()
|
||||
work()
|
||||
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body eval
|
||||
qr{connected: 1
|
||||
ssl handshake: true
|
||||
close: 1 nil
|
||||
connected: 1
|
||||
ssl handshake: true
|
||||
close: 1 nil}
|
||||
--- grep_error_log eval
|
||||
qr/parsing (cert|(priv key)) for sni: www.test2.com/
|
||||
--- grep_error_log_out
|
||||
parsing cert for sni: www.test2.com
|
||||
parsing priv key for sni: www.test2.com
|
||||
|
||||
|
||||
|
||||
=== TEST 6: set ssl(encrypt ssl keys with another iv)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local raw_ssl_key = t.read_file("t/certs/test2.key")
|
||||
local ssl_key = t.aes_encrypt(raw_ssl_key)
|
||||
local data = {
|
||||
certs = { ssl_cert },
|
||||
keys = { ssl_key },
|
||||
snis = {"test2.com", "*.test2.com"},
|
||||
cert = ssl_cert,
|
||||
key = raw_ssl_key,
|
||||
}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data)
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.print(body)
|
||||
}
|
||||
}
|
||||
--- error_code: 400
|
||||
--- response_body
|
||||
{"error_msg":"failed to handle cert-key pair[1]: failed to decrypt previous encrypted key"}
|
||||
--- error_log
|
||||
decrypt ssl key failed
|
||||
|
||||
|
||||
|
||||
=== TEST 7: set miss_head ssl certificate
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/incorrect.crt")
|
||||
local ssl_key = t.read_file("t/certs/incorrect.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "www.test.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data)
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.print(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
{"error_msg":"failed to parse cert: PEM_read_bio_X509_AUX() failed"}
|
||||
--- error_code: 400
|
||||
--- no_error_log
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 8: client request without sni
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, nil, true)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
failed to do SSL handshake: handshake failed
|
||||
--- error_log
|
||||
failed to find SNI: please check if the client requests via IP or uses an outdated protocol
|
||||
--- no_error_log
|
||||
[alert]
|
||||
|
||||
|
||||
|
||||
=== TEST 9: client request without sni, but fallback_sni is set
|
||||
--- yaml_config
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
ssl:
|
||||
fallback_sni: "a.test2.com"
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, nil, false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 10: set sni with uppercase
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "*.TesT2.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data)
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 11: match case insensitive
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "a.test2.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 12: set snis with uppercase
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, snis = {"TesT2.com", "a.com"}}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data)
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 13: match case insensitive
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "TEST2.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 14: ensure table is reused in TLS handshake
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "TEST2.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- extra_init_by_lua
|
||||
local tablepool = require("apisix.core").tablepool
|
||||
local old_fetch = tablepool.fetch
|
||||
tablepool.fetch = function(name, ...)
|
||||
ngx.log(ngx.WARN, "fetch table ", name)
|
||||
return old_fetch(name, ...)
|
||||
end
|
||||
|
||||
local old_release = tablepool.release
|
||||
tablepool.release = function(name, ...)
|
||||
ngx.log(ngx.WARN, "release table ", name)
|
||||
return old_release(name, ...)
|
||||
end
|
||||
--- response_body
|
||||
ssl handshake: true
|
||||
--- grep_error_log eval
|
||||
qr/(fetch|release) table \w+/
|
||||
--- grep_error_log_out
|
||||
fetch table api_ctx
|
||||
release table api_ctx
|
||||
|
||||
|
||||
|
||||
=== TEST 15: store secret into vault
|
||||
--- exec
|
||||
VAULT_TOKEN='root' VAULT_ADDR='http://0.0.0.0:8200' vault kv put kv/apisix/ssl test2.com.crt=@t/certs/test2.crt test2.com.key=@t/certs/test2.key
|
||||
--- response_body
|
||||
Success! Data written to: kv/apisix/ssl
|
||||
|
||||
|
||||
|
||||
=== TEST 16: set ssl conf with secret ref: vault
|
||||
--- request
|
||||
GET /t
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
-- put secret vault config
|
||||
local code, body = t('/apisix/admin/secrets/vault/test1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"uri": "http://127.0.0.1:8200",
|
||||
"prefix": "kv/apisix",
|
||||
"token" : "root"
|
||||
}]]
|
||||
)
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
return ngx.say(body)
|
||||
end
|
||||
-- set ssl
|
||||
local code, body = t('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"cert": "$secret://vault/test1/ssl/test2.com.crt",
|
||||
"key": "$secret://vault/test1/ssl/test2.com.key",
|
||||
"sni": "test2.com"
|
||||
}]]
|
||||
)
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
return ngx.say(body)
|
||||
end
|
||||
|
||||
ngx.say("passed")
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 17: get cert and key from vault
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "test2.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 18: set ssl conf with secret ref: env
|
||||
--- request
|
||||
GET /t
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
-- set ssl
|
||||
local code, body = t('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"cert": "$env://TEST_ENV_SSL_CRT",
|
||||
"key": "$env://TEST_ENV_SSL_KEY",
|
||||
"sni": "test2.com"
|
||||
}]]
|
||||
)
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
return ngx.say(body)
|
||||
end
|
||||
|
||||
ngx.say("passed")
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 19: get cert and key from env
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "test2.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 20: set ssl conf with secret ref: only cert use env
|
||||
--- request
|
||||
GET /t
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
-- set ssl
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {
|
||||
cert = "$env://TEST_ENV_SSL_CRT",
|
||||
key = ssl_key,
|
||||
sni = "TesT2.com"
|
||||
}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data)
|
||||
)
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
return ngx.say(body)
|
||||
end
|
||||
|
||||
ngx.say("passed")
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 21: get cert from env
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "test2.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
ssl handshake: true
|
283
CloudronPackages/APISIX/apisix-source/t/router/radixtree-sni3.t
Normal file
283
CloudronPackages/APISIX/apisix-source/t/router/radixtree-sni3.t
Normal file
@@ -0,0 +1,283 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
log_level('debug');
|
||||
no_root_location();
|
||||
|
||||
BEGIN {
|
||||
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
|
||||
}
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set sni with trailing period
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "*.test.com"}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data)
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
--- error_code: 201
|
||||
|
||||
|
||||
|
||||
=== TEST 2: match against sni with no trailing period
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "a.test.com.", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 3: set snis with trailing period
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
|
||||
local ssl_cert = t.read_file("t/certs/test2.crt")
|
||||
local ssl_key = t.read_file("t/certs/test2.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, snis = {"test2.com", "a.com"}}
|
||||
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data)
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: match against sni with no trailing period
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
|
||||
sock:settimeout(2000)
|
||||
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local sess, err = sock:sslhandshake(nil, "test2.com.", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
ssl handshake: true
|
||||
|
||||
|
||||
|
||||
=== TEST 5: set ssl(sni: www.test.com.)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local t = require("lib.test_admin")
|
||||
local ssl_cert = t.read_file("t/certs/test-dot.crt")
|
||||
local ssl_key = t.read_file("t/certs/test-dot.key")
|
||||
local data = {cert = ssl_cert, key = ssl_key, sni = "www.test.com."}
|
||||
local code, body = t.test('/apisix/admin/ssls/1',
|
||||
ngx.HTTP_PUT,
|
||||
core.json.encode(data),
|
||||
[[{
|
||||
"value": {
|
||||
"sni": "www.test.com."
|
||||
},
|
||||
"key": "/apisix/ssls/1"
|
||||
}]]
|
||||
)
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 6: set route(id: 1)
|
||||
--- 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,
|
||||
[[{
|
||||
"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 7: client request
|
||||
--- config
|
||||
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- etcd sync
|
||||
ngx.sleep(0.2)
|
||||
do
|
||||
local sock = ngx.socket.tcp()
|
||||
sock:settimeout(2000)
|
||||
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("connected: ", ok)
|
||||
local sess, err = sock:sslhandshake(nil, "www.test.com", false)
|
||||
if not sess then
|
||||
ngx.say("failed to do SSL handshake: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("ssl handshake: ", sess ~= nil)
|
||||
local req = "GET /hello HTTP/1.0\r\nHost: www.test.com\r\nConnection: close\r\n\r\n"
|
||||
local bytes, err = sock:send(req)
|
||||
if not bytes then
|
||||
ngx.say("failed to send http request: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("sent http request: ", bytes, " bytes.")
|
||||
while true do
|
||||
local line, err = sock:receive()
|
||||
if not line then
|
||||
-- ngx.say("failed to receive response status line: ", err)
|
||||
break
|
||||
end
|
||||
ngx.say("received: ", line)
|
||||
end
|
||||
local ok, err = sock:close()
|
||||
ngx.say("close: ", ok, " ", err)
|
||||
end -- do
|
||||
-- collectgarbage()
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body eval
|
||||
qr{connected: 1
|
||||
ssl handshake: true
|
||||
sent http request: 62 bytes.
|
||||
received: HTTP/1.1 200 OK
|
||||
received: Content-Type: text/plain
|
||||
received: Content-Length: 12
|
||||
received: Connection: close
|
||||
received: Server: APISIX/\d\.\d+(\.\d+)?
|
||||
received: \nreceived: hello world
|
||||
close: 1 nil}
|
||||
--- error_log
|
||||
server name: "www.test.com"
|
||||
--- no_error_log
|
||||
[error]
|
||||
[alert]
|
@@ -0,0 +1,561 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route(id: 1)
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"host": "*.foo.com",
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: /not_found
|
||||
--- request
|
||||
GET /not_found
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 3: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 4: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: not_found.com
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 5: hit routes (www.foo.com)
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: www.foo.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 6: hit routes (user.foo.com)
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: user.foo.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 7: set route(id: 1)
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"host": "foo.com",
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 8: /not_found
|
||||
--- request
|
||||
GET /not_found
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 9: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 10: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: www.foo.com
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 11: hit routes (foo.com)
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: foo.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 12: set route(id: 1)
|
||||
--- 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,
|
||||
[[{
|
||||
"uri": "/hello",
|
||||
"filter_func": "function(vars) return vars.arg_name == 'json' end",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 13: not hit: name=unknown
|
||||
--- request
|
||||
GET /hello?name=unknown
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 14: hit routes
|
||||
--- request
|
||||
GET /hello?name=json
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 15: set route with ':'
|
||||
--- 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,
|
||||
[[{
|
||||
"uri": "/file:listReputationHistories",
|
||||
"plugins":{"proxy-rewrite":{"uri":"/hello"}},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 16: hit routes
|
||||
--- request
|
||||
GET /file:listReputationHistories
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 17: not hit
|
||||
--- request
|
||||
GET /file:xx
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 18: inherit hosts from services
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/services/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["bar.com"]
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"plugins": {
|
||||
"proxy-rewrite":{"uri":"/hello1"}
|
||||
},
|
||||
"service_id": "1",
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"priority": -1
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 19: hit
|
||||
--- more_headers
|
||||
Host: www.foo.com
|
||||
--- request
|
||||
GET /hello
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 20: change hosts in services
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/hello"
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/services/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["foo.com"]
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.sleep(0.1)
|
||||
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri, {headers = {Host = "foo.com"}})
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.print(res.body)
|
||||
|
||||
local code, body = t('/apisix/admin/services/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["bar.com"]
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.sleep(0.1)
|
||||
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri, {headers = {Host = "foo.com"}})
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.print(res.body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
hello1 world
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 21: unbind services
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/hello"
|
||||
local t = require("lib.test_admin").test
|
||||
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"plugins": {
|
||||
"proxy-rewrite":{"uri":"/hello1"}
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.sleep(0.1)
|
||||
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri, {headers = {Host = "foo.com"}})
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.print(res.body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
hello1 world
|
||||
|
||||
|
||||
|
||||
=== TEST 22: host from route is preferred
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/hello"
|
||||
local t = require("lib.test_admin").test
|
||||
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"hosts": ["foo.com"],
|
||||
"plugins": {
|
||||
"proxy-rewrite":{"uri":"/hello1"}
|
||||
},
|
||||
"service_id": "1",
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.sleep(0.1)
|
||||
|
||||
for _, h in ipairs({"foo.com", "bar.com"}) do
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri, {headers = {Host = h}})
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.print(res.body)
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"host": "foo.com",
|
||||
"plugins": {
|
||||
"proxy-rewrite":{"uri":"/hello1"}
|
||||
},
|
||||
"service_id": "1",
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
ngx.sleep(0.1)
|
||||
|
||||
for _, h in ipairs({"foo.com", "bar.com"}) do
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri, {headers = {Host = h}})
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.print(res.body)
|
||||
end
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
hello1 world
|
||||
hello world
|
||||
hello1 world
|
||||
hello world
|
@@ -0,0 +1,87 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri'
|
||||
delete_uri_tail_slash: true
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route
|
||||
--- 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,
|
||||
[[{
|
||||
"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 2: hit route
|
||||
--- request
|
||||
GET /hello
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 3: hit route
|
||||
--- request
|
||||
GET /hello/
|
||||
--- error_code: 404
|
@@ -0,0 +1,210 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route(id: 1)
|
||||
--- 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,
|
||||
[[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/server_port"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: set route(id: 2)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1981": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/server_port/*"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 3: set route(id: 3)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/3',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1982": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/server_port/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: /not_found
|
||||
--- request
|
||||
GET /not_found
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 5: hit route 1
|
||||
--- request
|
||||
GET /server_port
|
||||
--- response_body eval
|
||||
qr/1980/
|
||||
|
||||
|
||||
|
||||
=== TEST 6: hit route 2
|
||||
--- request
|
||||
GET /server_port/route2
|
||||
--- response_body eval
|
||||
qr/1981/
|
||||
|
||||
|
||||
|
||||
=== TEST 7: hit route 3
|
||||
--- request
|
||||
GET /server_port/hello
|
||||
--- response_body eval
|
||||
qr/1982/
|
||||
|
||||
|
||||
|
||||
=== TEST 8: delete route(id: 2)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_DELETE
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 9: delete route(id: 3)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/3',
|
||||
ngx.HTTP_DELETE
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
@@ -0,0 +1,178 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route(id: 1 + priority: 2)
|
||||
--- 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,
|
||||
[[{
|
||||
"uri": "/server_port*",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"priority": 2
|
||||
}]])
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: hit routes
|
||||
--- request
|
||||
GET /server_port/aa
|
||||
--- response_body eval
|
||||
1980
|
||||
|
||||
|
||||
|
||||
=== TEST 3: set route(id: 2 + priority: 1)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"uri": "/server_port*",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1981": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"priority": 1
|
||||
}]])
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: hit routes
|
||||
--- request
|
||||
GET /server_port/aa
|
||||
--- response_body eval
|
||||
1980
|
||||
|
||||
|
||||
|
||||
=== TEST 5: set route(id: 2 + priority: 3)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"uri": "/server_port*",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1981": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"priority": 3
|
||||
}]])
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 6: hit routes
|
||||
--- request
|
||||
GET /server_port/aa
|
||||
--- response_body eval
|
||||
1981
|
||||
|
||||
|
||||
|
||||
=== TEST 7: set route(id: 2 + priority: 3)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_DELETE)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
@@ -0,0 +1,381 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $servlet_yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri'
|
||||
normalize_uri_like_servlet: true
|
||||
_EOC_
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route(id: 1)
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"host": "foo.com",
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: /not_found
|
||||
--- request
|
||||
GET /not_found
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 3: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 4: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: not_found.com
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 5: hit routes
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: foo.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 6: set route(id: 1)
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1981": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/server_port"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 7: /not_found
|
||||
--- request
|
||||
GET /hello
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 8: hit routes
|
||||
--- request
|
||||
GET /server_port
|
||||
--- more_headers
|
||||
Host: anydomain.com
|
||||
--- response_body_like eval
|
||||
qr/1981/
|
||||
|
||||
|
||||
|
||||
=== TEST 9: set route(id: 2)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1981": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 10: /not_found
|
||||
--- request
|
||||
GET /hello2
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 11: hit routes
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
Host: anydomain.com
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 12: delete route(id: 2)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_DELETE
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 13: set route(id: 1)
|
||||
--- 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,
|
||||
[[{
|
||||
"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: hit route with /hello
|
||||
--- request
|
||||
GET /hello
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 15: miss route
|
||||
--- request
|
||||
GET /hello/
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 16: match route like servlet
|
||||
--- yaml_config eval: $::servlet_yaml_config
|
||||
--- request
|
||||
GET /hello;world
|
||||
--- response_body eval
|
||||
qr/404 Not Found/
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 17: plugin should work on the normalized url
|
||||
--- 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,
|
||||
[[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/*",
|
||||
"plugins": {
|
||||
"uri-blocker": {
|
||||
"block_rules": ["/hello/world"]
|
||||
}
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 18: hit
|
||||
--- yaml_config eval: $::servlet_yaml_config
|
||||
--- request
|
||||
GET /hello;a=b/world;a/;
|
||||
--- error_code: 403
|
||||
|
||||
|
||||
|
||||
=== TEST 19: reject bad uri
|
||||
--- yaml_config eval: $::servlet_yaml_config
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
for _, path in ipairs({
|
||||
"/;/a", "/%2e;", "/%2E%2E;", "/.;", "/..;",
|
||||
"/%2E%2e;", "/b/;/c"
|
||||
}) do
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri .. path)
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
|
||||
if res.status ~= 400 then
|
||||
ngx.say(path, " ", res.status)
|
||||
end
|
||||
end
|
||||
|
||||
ngx.say("ok")
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
ok
|
||||
--- error_log
|
||||
failed to normalize
|
@@ -0,0 +1,439 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route(id: 1) with vars(user_agent ~* android)
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [["http_user_agent", "~*", "android"]]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: not found because user_agent=ios
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: ios
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 3: hit routes with user_agent=android
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: android
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 4: hit routes with user_agent=Android
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: Android
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 5: set route(id: 1) with vars(user_agent ! ~* android)
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [["http_user_agent", "!", "~*", "android"]]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 6: not found because user_agent=android
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: android
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 7: hit routes with user_agent=ios
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: ios
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 8: set route(id: 1) with vars(in table)
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [["http_user_agent", "IN", ["android", "ios"]]]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 9: hit routes with user_agent=ios
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: ios
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 10: hit routes with user_agent=android
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: android
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 11: set route(id: 1) with vars(null)
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [["http_user_agent", "==", null]]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 12: not found because user_agent=android
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: android
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 13: hit route
|
||||
--- request
|
||||
GET /hello
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 14: set route(id: 1) with vars(items are two)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
-- deprecated, will be removed soon
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[=[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [["http_user_agent", "ios"]]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 15: hit routes with user_agent=ios
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: ios
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 16: vars rule with logical operator (set)
|
||||
--- 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,
|
||||
[=[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": [
|
||||
"!OR",
|
||||
["http_user_agent", "==", "ios"],
|
||||
["http_demo", "==", "test"]
|
||||
]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 17: vars rule with logical operator (hit)
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: android
|
||||
demo: prod
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 18: vars rule with logical operator (miss)
|
||||
--- request
|
||||
GET /hello
|
||||
--- more_headers
|
||||
User-Agent: ios
|
||||
demo: prod
|
||||
--- error_code: 404
|
||||
|
||||
|
||||
|
||||
=== TEST 19: be compatible with empty vars
|
||||
--- 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"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": []
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 20: hit
|
||||
--- request
|
||||
GET /hello
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 21: bad vars rule
|
||||
--- 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,
|
||||
[=[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello",
|
||||
"vars": ["http_user_agent", "~*", "android"]
|
||||
}]=]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.print(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- error_code: 400
|
||||
--- response_body
|
||||
{"error_msg":"failed to validate the 'vars' expression: rule should be wrapped inside brackets"}
|
@@ -0,0 +1,272 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri_with_parameter'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route
|
||||
--- 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,
|
||||
[[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/name/:name/bar"
|
||||
}]],
|
||||
[[{
|
||||
"value": {
|
||||
"uri": "/name/:name/bar",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
},
|
||||
"key": "/apisix/routes/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: /not_found
|
||||
--- request
|
||||
GET /not_found
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 3: /name/json/foo
|
||||
--- request
|
||||
GET /name/json2/foo
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 4: /name/json/
|
||||
--- request
|
||||
GET /name/json/
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 5: /name//bar
|
||||
--- request
|
||||
GET /name//bar
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 6: hit route: /name/json/bar
|
||||
--- request
|
||||
GET /name/json/bar
|
||||
--- error_code: 404
|
||||
--- response_body eval
|
||||
qr/404 Not Found/
|
||||
|
||||
|
||||
|
||||
=== TEST 7: set route,uri=/:name/foo
|
||||
--- 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,
|
||||
[[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/:name/foo"
|
||||
}]],
|
||||
[[{
|
||||
"value": {
|
||||
"uri": "/:name/foo",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
},
|
||||
"key": "/apisix/routes/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 8: /json/foo
|
||||
--- request
|
||||
GET /json/foo
|
||||
--- error_code: 404
|
||||
--- response_body eval
|
||||
qr/404 Not Found/
|
||||
|
||||
|
||||
|
||||
=== TEST 9: /json/bbb/foo
|
||||
--- request
|
||||
GET /json/bbb/foo
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 10: inherit hosts from services
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/services/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"hosts": ["bar.com"]
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"plugins": {
|
||||
"proxy-rewrite":{"uri":"/hello1"}
|
||||
},
|
||||
"service_id": "1",
|
||||
"uri": "/:name/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/routes/2',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"methods": ["GET"],
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"plugins": {
|
||||
"proxy-rewrite":{"uri":"/hello"}
|
||||
},
|
||||
"uri": "/:name/hello",
|
||||
"priority": -1
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 11: hit
|
||||
--- more_headers
|
||||
Host: www.foo.com
|
||||
--- request
|
||||
GET /john/hello
|
||||
--- response_body
|
||||
hello world
|
@@ -0,0 +1,108 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
log_level('info');
|
||||
worker_connections(256);
|
||||
no_root_location();
|
||||
no_shuffle();
|
||||
|
||||
our $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
router:
|
||||
http: 'radixtree_uri_with_parameter'
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->yaml_config) {
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route with :name as a uri parameter
|
||||
--- 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,
|
||||
[[{
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"plugins": {
|
||||
"proxy-rewrite":{"uri":"/hello"}
|
||||
},
|
||||
"uri": "/name/:name/bar"
|
||||
}]],
|
||||
[[{
|
||||
"value": {
|
||||
"uri": "/name/:name/bar",
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"plugins": {
|
||||
"proxy-rewrite":{"uri":"/hello"}
|
||||
},
|
||||
},
|
||||
"key": "/apisix/routes/1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: parameters with special characters should pass
|
||||
--- request
|
||||
GET /name/with%20space/bar
|
||||
--- error_code: 200
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 3: failing case for the above test
|
||||
--- request
|
||||
GET /name/with%20space/foo
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
Reference in New Issue
Block a user