Skip to content

Commit

Permalink
Json encode all controllers responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ostinelli committed Sep 21, 2013
1 parent ea001d9 commit a4f0b7c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ $ brew install lua luarocks
```
luarocks install busted
```

##### The Json converter
```
luarocks install lua-cjson
```
4 changes: 2 additions & 2 deletions app/controllers/messages_controller.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local MessagesController = {}

function MessagesController:index()
return 'Index of messages for user: ' .. self.params.user_id
return { recipient = self.params.user_id, messages = { 'abc' } }
end

function MessagesController:show()
return 'Messages with id: ' .. self.params.id .. ' of user: ' .. self.params.user_id
return { message = { id = self.params.id, recipient = self.params.user_id, body = "Ralis is awesome." } }
end

return MessagesController
4 changes: 2 additions & 2 deletions app/controllers/users_controller.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local UsersController = {}

function UsersController:index()
return 'Index of Users!'
return { users = { 'roberto' } }
end

function UsersController:show()
return 'Page of user with id: ' .. self.params.id
return { name = self.params.id }
end

return UsersController
3 changes: 2 additions & 1 deletion core/router.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package.path = './app/controllers/?.lua;' .. package.path

-- init routes and load modules
local cjson = require 'cjson'
local routes = require 'config/routes'
local Controller = require 'core/controller'

Expand All @@ -23,7 +24,7 @@ function Router.handler(ngx)
setmetatable(controller_instance, {__index = matched_controller})
-- call action
local result = controller_instance[action](controller_instance)
ngx.say(result)
ngx.print(cjson.encode(result))
else
-- 404
ngx.exit(ngx.HTTP_NOT_FOUND)
Expand Down
75 changes: 50 additions & 25 deletions spec/core/router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("Router", function()
ngx = {
HTTP_NOT_FOUND = 404,
exit = function(code) return end,
say = function(say) return end,
print = function(print) return end,
header = { content_type = '' }
}
end)
Expand All @@ -25,40 +25,65 @@ describe("Router", function()
end)

describe(".handler", function()
it("raises a 404 error if no match is found", function()
-- redefine the matching function
router.match = function(ngx) return end
stub(ngx, 'exit')
describe("when no match is found", function()
before_each(function()
router.match = function(ngx) return end
end)

router.handler(ngx)
it("raises a 404 error if no match is found", function()
stub(ngx, 'exit')

assert.stub(ngx.exit).was.called_with(ngx.HTTP_NOT_FOUND)
router.handler(ngx)

ngx.exit:revert()
assert.stub(ngx.exit).was.called_with(ngx.HTTP_NOT_FOUND)

ngx.exit:revert()
end)
end)

it("calls the action of an instance of the matched controller name", function()
-- redefine the matching function
router.match = function(ngx) return "controller_name", "action", "params" end
describe("when a match is found", function()
before_each(function()
router.match = function(ngx) return "controller_name", "action", "params" end

instance = {} -- we're going to set self to instance so we can assert on it
TestController = {}
function TestController:action()
instance = self
return { name = 'ralis' }
end
-- dinamically load package controller_name (hack to stub a 'require' statement)
package.loaded['controller_name'] = TestController
end)

after_each(function()
instance = nil
TestController = nil
package.loaded['controller_name'] = nil
end)

it("calls the action of an instance of the matched controller name", function()
spy.on(TestController, 'action')

router.handler(ngx)

assert.spy(TestController.action).was.called()

-- assert the instance was initialized with the correct arguments
assert.are.same(ngx, instance.ngx)
assert.are.same("params", instance.params)

local instance = {} -- we're going to set self to instance so we can assert on it
local TestController = {}
function TestController:action()
instance = self
return
end
-- dinamically load package controller_name (hack to stub a 'require' statement)
package.loaded['controller_name'] = TestController
TestController.action:revert()
end)

spy.on(TestController, 'action')
it("calls nginx with the serialized json of the controller response", function()
spy.on(ngx, 'print')

router.handler(ngx)
router.handler(ngx)

assert.spy(TestController.action).was.called()
assert.are.same(ngx, instance.ngx)
assert.are.same("params", instance.params)
assert.spy(ngx.print).was_called_with('{"name":"ralis"}')

TestController.action:revert()
ngx.print:revert()
end)
end)
end)

Expand Down

0 comments on commit a4f0b7c

Please sign in to comment.