Skip to content

Commit

Permalink
bugfix: cached hit "path" when enabled prefix matching and matched
Browse files Browse the repository at this point in the history
…option. (#70)

fix #67 .
  • Loading branch information
tzssangglass authored Oct 22, 2020
1 parent 6b5d65e commit b725eeb
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ local function compare_param(req_path, route, opts)

local pat, names = fetch_pat(route.path_org)
log_info("pcre pat: ", pat)
if #names == 0 then
return true
end

local m = re_match(req_path, pat, "jo")
if not m then
return false
Expand Down
157 changes: 157 additions & 0 deletions t/matched.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:

use t::RX 'no_plan';

repeat_each(1);
run_tests();

__DATA__

=== TEST 1: prefix matching with method, and matched is not nil.
--- config
location /t {
content_by_lua_block {
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = "/hello*",
methods = {"GET"},
metadata = "metadata prefix matching with GET, matched = {}",
},
})
local opts = {method = "GET", matched = {}}
ngx.say(rx:match("/hello1", opts))
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
metadata prefix matching with GET, matched = {}



=== TEST 2: prefix matching with host, and matched is not nil.
--- config
location /t {
content_by_lua_block {
local json = require("cjson.safe")
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = "/hello*",
hosts = {"foo.com"},
metadata = "metadata prefix matching with host, matched = {}",
},
})
local opts = {host = "foo.com", matched = {}}
ngx.say(rx:match("/hello1", opts))
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
metadata prefix matching with host, matched = {}



=== TEST 3: prefix matching with vars, and matched is not nil.
--- config
location /t {
content_by_lua_block {
local json = require("cjson.safe")
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = "/hello*",
vars = {
{"arg_k", "==", "v"},
},
metadata = "metadata prefix matching with host, matched = {}",
},
})
local opts = {vars = ngx.var, matched = {}}
ngx.say(rx:match("/hello1", opts))
}
}
--- request
GET /t?k=v
--- no_error_log
[error]
--- response_body
metadata prefix matching with host, matched = {}



=== TEST 4: prefix matching with method, host, vars and matched is not nil.
--- config
location /t {
content_by_lua_block {
local json = require("cjson.safe")
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = "/hello*",
hosts = {"foo.com"},
methods = {"GET"},
vars = {
{"arg_k", "==", "v"},
},
metadata = "metadata prefix matching with method, host, vars, matched = {}",
},
})
local opts = {
method = "GET",
host = "foo.com",
vars = ngx.var,
matched = {}
}
ngx.say(rx:match("/hello1", opts))
}
}
--- request
GET /t?k=v
--- no_error_log
[error]
--- response_body
metadata prefix matching with method, host, vars, matched = {}



=== TEST 5: get matched when callback route.handler after dispatch success
--- config
location /t {
content_by_lua_block {
local json = require("cjson.safe")
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = "/hello*",
hosts = {"foo.com"},
methods = {"GET"},
vars = {
{"arg_k", "==", "v"},
},
handler = function (opts)
ngx.say("after dispatch success get matched: ", json.encode(opts.matched))
end,
},
})
local opts = {
method = "GET",
host = "foo.com",
vars = ngx.var,
matched = {}
}
rx:dispatch("/hello1", opts, opts)
}
}
--- request
GET /t?k=v
--- no_error_log
[error]
--- response_body
after dispatch success get matched: {"_path":"\/hello*","_method":"GET","_host":"foo.com"}

0 comments on commit b725eeb

Please sign in to comment.