Skip to content

Commit

Permalink
Merge pull request APItools#28 from firoxer/master
Browse files Browse the repository at this point in the history
Support multiple methods in a single match call
  • Loading branch information
mikz authored Sep 12, 2018
2 parents d40c94f + 61abfef commit 4a65dae
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
17 changes: 15 additions & 2 deletions router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,22 @@ end

function Router:match(method, path, fun)
if type(method) == 'string' then -- always make the method to table.
method = {[method] = {[path] = fun}}
method = {method}
end
for m, routes in pairs(method) do

local parsed_methods = {}
for k, v in pairs(method) do
if type(v) == 'string' then
-- convert shorthand methods into longhand
if parsed_methods[v] == nil then parsed_methods[v] = {} end
parsed_methods[v][path] = fun
else
-- pass methods already in longhand format onwards
parsed_methods[k] = v
end
end

for m, routes in pairs(parsed_methods) do
for p, f in pairs(routes) do
if not self._tree[m] then self._tree[m] = {} end
match_one_path(self._tree[m], p, f)
Expand Down
59 changes: 58 additions & 1 deletion spec/router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,64 @@ describe("Router", function()
end)
end)

describe('when first param is a table', function()
describe("when first param is a numeric table", function()
it("understands fixed strings", function()
r:match({"GET", "POST"}, "/foo", write_dummy)

r:execute("GET", "/foo", {status = "ok"})
assert.same(dummy.params, {status = "ok"})

r:execute("POST", "/foo", {status = "ok"})
assert.same(dummy.params, {status = "ok"})
end)

it("understands chained fixed strings", function()
r:match({"GET", "POST"}, "/foo/bar", write_dummy)

r:execute("GET", "/foo/bar", {status = "ok"})
assert.same(dummy.params, {status = "ok"})

r:execute("POST", "/foo/bar", {status = "ok"})
assert.same(dummy.params, {status = "ok"})
end)

it("understands params", function()
r:match({"GET", "POST"}, "/foo/:id", write_dummy)

r:execute("GET", "/foo/bar")
assert.same(dummy.params, {id="bar"})

r:execute("POST", "/foo/bar")
assert.same(dummy.params, {id="bar"})
end)

it("does not duplicate the same node twice for the same param id", function()
r:match({"GET", "POST"}, "/foo/:id/bar", write_dummy)
r:match({"GET", "POST"}, "/foo/:id/baz", write_dummy)

r:execute("GET", "/foo/1/bar")
assert.same(dummy.params, {id="1"})
r:execute("POST", "/foo/1/bar")
assert.same(dummy.params, {id="1"})

r:execute("GET", "/foo/2/baz")
assert.same(dummy.params, {id="2"})
r:execute("POST", "/foo/2/baz")
assert.same(dummy.params, {id="2"})
end)

it("supports an extension on a param", function()
r:match({"GET", "POST"}, "/foo/:id.json", write_dummy)

r:execute("GET", "/foo/1.json")
assert.same(dummy.params, {id="1"})

r:execute("POST", "/foo/1.json")
assert.same(dummy.params, {id="1"})
end)
end)

describe('when first param is a hash table', function()
it("understands fixed strings", function()
r:match({ GET = { ["/foo"] = write_dummy} })
r:execute("GET", "/foo", {status = "ok"})
Expand Down

0 comments on commit 4a65dae

Please sign in to comment.