Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #9 from NZKoz/wildcard-routes
Browse files Browse the repository at this point in the history
Add support for Wildcard Routes
  • Loading branch information
mikz committed Apr 5, 2016
2 parents f64fed2 + c492349 commit 77d70e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions router.lua
Expand Up @@ -30,6 +30,7 @@ local router = {
}

local COLON_BYTE = string.byte(':', 1)
local WILDCARD_BYTE = string.byte('*', 1)

local function match_one_path(node, path, f)
for token in path:gmatch("[^/.]+") do
Expand Down Expand Up @@ -60,6 +61,10 @@ local function resolve(path, node, params)
if f then return f, bindings end

params[param_name] = param_value -- reset the params table.
elseif child_token:byte(1) == WILDCARD_BYTE then -- it's a *
local param_name = child_token:sub(2)
params[param_name] = current_token .. path
return node[child_token]["LEAF"], params
end
end

Expand Down
29 changes: 29 additions & 0 deletions spec/router_spec.lua
Expand Up @@ -234,6 +234,35 @@ describe("Router", function()
end) -- :execute
end) -- default params

describe('Wildcard routes', function()
before_each(function ()
r:match({
GET = {
["/a/b/*args"] = write_dummy,
},
POST = {
}
})
end)

it("match a single segment", function()
local ok, err = r:execute("GET", "/a/b/c")
assert.is_true(ok)
assert.same(dummy.params.args, "c")
end)

it("match multiple segments", function()
local ok, err = r:execute("GET", "/a/b/c/d/e/f")
assert.is_true(ok)
assert.same(dummy.params.args, "c/d/e/f")
end)

it("don't match if the segment is empty", function()
local ok, err = r:execute("GET", "/a/b")
assert.is_nil(ok)
end)
end)

describe("shortcuts", function()
for method in ("get post put patch delete trace connect options head"):gmatch("%S+") do
local verb = method:upper()
Expand Down

0 comments on commit 77d70e4

Please sign in to comment.