|
| 1 | +# vim:set ft= ts=4 sw=4 et fdm=marker: |
| 2 | + |
| 3 | +use t::RX 'no_plan'; |
| 4 | + |
| 5 | +repeat_each(1); |
| 6 | +run_tests(); |
| 7 | + |
| 8 | +__DATA__ |
| 9 | + |
| 10 | +=== TEST 1: prefix matching with method, and matched is not nil. |
| 11 | +--- config |
| 12 | + location /t { |
| 13 | + content_by_lua_block { |
| 14 | + local radix = require("resty.radixtree") |
| 15 | + local rx = radix.new({ |
| 16 | + { |
| 17 | + paths = "/hello*", |
| 18 | + methods = {"GET"}, |
| 19 | + metadata = "metadata prefix matching with GET, matched = {}", |
| 20 | + }, |
| 21 | + }) |
| 22 | + local opts = {method = "GET", matched = {}} |
| 23 | + ngx.say(rx:match("/hello1", opts)) |
| 24 | + } |
| 25 | + } |
| 26 | +--- request |
| 27 | +GET /t |
| 28 | +--- no_error_log |
| 29 | +[error] |
| 30 | +--- response_body |
| 31 | +metadata prefix matching with GET, matched = {} |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +=== TEST 2: prefix matching with host, and matched is not nil. |
| 36 | +--- config |
| 37 | + location /t { |
| 38 | + content_by_lua_block { |
| 39 | + local json = require("cjson.safe") |
| 40 | + local radix = require("resty.radixtree") |
| 41 | + local rx = radix.new({ |
| 42 | + { |
| 43 | + paths = "/hello*", |
| 44 | + hosts = {"foo.com"}, |
| 45 | + metadata = "metadata prefix matching with host, matched = {}", |
| 46 | + }, |
| 47 | + }) |
| 48 | + local opts = {host = "foo.com", matched = {}} |
| 49 | + ngx.say(rx:match("/hello1", opts)) |
| 50 | + } |
| 51 | + } |
| 52 | +--- request |
| 53 | +GET /t |
| 54 | +--- no_error_log |
| 55 | +[error] |
| 56 | +--- response_body |
| 57 | +metadata prefix matching with host, matched = {} |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +=== TEST 3: prefix matching with vars, and matched is not nil. |
| 62 | +--- config |
| 63 | + location /t { |
| 64 | + content_by_lua_block { |
| 65 | + local json = require("cjson.safe") |
| 66 | + local radix = require("resty.radixtree") |
| 67 | + local rx = radix.new({ |
| 68 | + { |
| 69 | + paths = "/hello*", |
| 70 | + vars = { |
| 71 | + {"arg_k", "==", "v"}, |
| 72 | + }, |
| 73 | + metadata = "metadata prefix matching with host, matched = {}", |
| 74 | + }, |
| 75 | + }) |
| 76 | + local opts = {vars = ngx.var, matched = {}} |
| 77 | + ngx.say(rx:match("/hello1", opts)) |
| 78 | + } |
| 79 | + } |
| 80 | +--- request |
| 81 | +GET /t?k=v |
| 82 | +--- no_error_log |
| 83 | +[error] |
| 84 | +--- response_body |
| 85 | +metadata prefix matching with host, matched = {} |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +=== TEST 4: prefix matching with method, host, vars and matched is not nil. |
| 90 | +--- config |
| 91 | + location /t { |
| 92 | + content_by_lua_block { |
| 93 | + local json = require("cjson.safe") |
| 94 | + local radix = require("resty.radixtree") |
| 95 | + local rx = radix.new({ |
| 96 | + { |
| 97 | + paths = "/hello*", |
| 98 | + hosts = {"foo.com"}, |
| 99 | + methods = {"GET"}, |
| 100 | + vars = { |
| 101 | + {"arg_k", "==", "v"}, |
| 102 | + }, |
| 103 | + metadata = "metadata prefix matching with method, host, vars, matched = {}", |
| 104 | + }, |
| 105 | + }) |
| 106 | + local opts = { |
| 107 | + method = "GET", |
| 108 | + host = "foo.com", |
| 109 | + vars = ngx.var, |
| 110 | + matched = {} |
| 111 | + } |
| 112 | + ngx.say(rx:match("/hello1", opts)) |
| 113 | + } |
| 114 | + } |
| 115 | +--- request |
| 116 | +GET /t?k=v |
| 117 | +--- no_error_log |
| 118 | +[error] |
| 119 | +--- response_body |
| 120 | +metadata prefix matching with method, host, vars, matched = {} |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +=== TEST 5: get matched when callback route.handler after dispatch success |
| 125 | +--- config |
| 126 | + location /t { |
| 127 | + content_by_lua_block { |
| 128 | + local json = require("cjson.safe") |
| 129 | + local radix = require("resty.radixtree") |
| 130 | + local rx = radix.new({ |
| 131 | + { |
| 132 | + paths = "/hello*", |
| 133 | + hosts = {"foo.com"}, |
| 134 | + methods = {"GET"}, |
| 135 | + vars = { |
| 136 | + {"arg_k", "==", "v"}, |
| 137 | + }, |
| 138 | + handler = function (opts) |
| 139 | + ngx.say("after dispatch success get matched: ", json.encode(opts.matched)) |
| 140 | + end, |
| 141 | + }, |
| 142 | + }) |
| 143 | + local opts = { |
| 144 | + method = "GET", |
| 145 | + host = "foo.com", |
| 146 | + vars = ngx.var, |
| 147 | + matched = {} |
| 148 | + } |
| 149 | + rx:dispatch("/hello1", opts, opts) |
| 150 | + } |
| 151 | + } |
| 152 | +--- request |
| 153 | +GET /t?k=v |
| 154 | +--- no_error_log |
| 155 | +[error] |
| 156 | +--- response_body |
| 157 | +after dispatch success get matched: {"_path":"\/hello*","_method":"GET","_host":"foo.com"} |
0 commit comments