Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rockspec/tir-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = {
"luajson",
"lsqlite3",
"telescope",
"tnetstrings",
}
build = {
type = "none",
Expand Down
33 changes: 33 additions & 0 deletions tests/base/engine_tests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'tir.engine'


function fake_main()
print("TEST")
end

context("Tir", function()
context("engine", function()
test("start", function()
Tir.run = function() return true end
Tir.M2.connect = function() return true end

local config = {config_file = 'tests/data/config.lua', route = '/arc'}
Tir.start(config)

assert_equal('tcp://127.0.0.1:9990', config.sub_addr)
assert_equal('tcp://127.0.0.1:9989', config.pub_addr)
end)

test("start-overide", function()
Tir.run = function() return true end
Tir.M2.connect = function() return true end

local config = {config_file = 'tests/data/config.lua', route = '/arc', pub_addr = 'tcp://10.234.56.71:9990', sub_addr = 'tcp://10.234.56.71:9989'}
Tir.start(config)

assert_equal('tcp://10.234.56.71:9990', config.pub_addr)
assert_equal('tcp://10.234.56.71:9989', config.sub_addr)
end)
end)
end)

20 changes: 15 additions & 5 deletions tests/base/session_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ context("Tir", function()
test("make_expires", function()
local expire = Tir.make_expires()
assert_not_nil(expire)
assert_match('^[A-Z][%w]-, %d%d%-[A-Z][%w]-%-%d%d%d%d %d%d:%d%d:%d%d GMT$', expire)
assert_equal("number", type(expire))
assert_match('^[A-Z][%w]-, %d%d%-[A-Z][%w]-%-%d%d%d%d %d%d:%d%d:%d%d GMT$', os.date("%a, %d-%b-%Y %X GMT", expire))
end)

test("make_session_cookie", function()
local cookie = Tir.make_session_cookie(Tir.make_session_id())
assert_not_nil(cookie)

assert_match("^session=.-; version=1; path=/; expires=.+$", cookie)
local req = {}
Tir.set_http_cookie(req, cookie)
assert_match("^session=.-; path=/; expires=.+$", req.headers['set-cookie'][1])
end)

test("json_ident", function()
Expand All @@ -38,8 +41,12 @@ context("Tir", function()
end)

test("http_cookie_ident", function()
local req = { headers = {
cookie = Tir.make_session_cookie(Tir.make_session_id())
local req = {}
Tir.set_http_cookie(req, Tir.make_session_cookie(Tir.make_session_id()))
local cookie_str = req.headers['set-cookie'][1]

req = { headers = {
cookie = cookie_str
}}

local ident = Tir.http_cookie_ident(req)
Expand All @@ -59,12 +66,15 @@ context("Tir", function()
assert_equal(req.data.session_id, ident)
end


do
local req = { headers = {
cookie = Tir.make_session_cookie(Tir.make_session_id())
}}

Tir.set_http_cookie(req, req.headers.cookie)
req.headers['cookie'] = req.headers['set-cookie'][1]
req.headers['set-cookie'] = nil

local ident = Tir.default_ident(req)

assert_not_nil(ident)
Expand Down
9 changes: 7 additions & 2 deletions tests/base/web_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ context("Tir", function()

test("get_cookie", function()
local web = Tir.web(fake_conn, fake_main, fake_req, false)
local cookie = web:get_cookie()

Tir.set_http_cookie(web.req, web.req.headers.cookie)
web.req.headers['cookie'] = web.req.headers['set-cookie'][1]
web.req.headers['set-cookie'] = nil

local cookie = web:get_cookies()
assert_not_nil(cookie)
end)

test("set_cookie", function()
local web = Tir.web(fake_conn, fake_main, fake_req, false)
web:set_cookie("testing")
web:set_cookie({key = "testing", value = ""})
assert_not_nil(fake_req.headers['set-cookie'])
fake_req.headers['set-cookie'] = nil
end)
Expand Down
1 change: 1 addition & 0 deletions tests/data/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config_db = 'tests/data/config.sqlite'
6 changes: 5 additions & 1 deletion tir/engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ function start(config)

config.allowed_methods_header = table.concat(allowed, ' ')

Tir.M2.load_config(config)
if config.config_db then Tir.M2.load_config(config) end

assert(config.sub_addr, "Failed to find sub_addr.")
assert(config.pub_addr, "Failed to find pub_addr.")

local conn = assert(Tir.M2.connect(config), "Failed to connect to Mongrel2.")

-- Run the engine
Expand Down
4 changes: 2 additions & 2 deletions tir/m2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ function load_config(config)
assert(handler, "Failed to find route: " .. config.route ..
". Make sure you set config.host to a host in your mongrel2.conf.")

config.sub_addr = handler.send_spec
config.pub_addr = handler.recv_spec
config.sub_addr = config.sub_addr or handler.send_spec
config.pub_addr = config.pub_addr or handler.recv_spec
end


Expand Down
4 changes: 2 additions & 2 deletions tir/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ function set_http_cookie(req, cookie)
cookie_str = cookie_str .. '; ' .. 'path=' .. (cookie.path or '/')

if cookie.domain then
cookie_str = cookie_str .. ';' .. 'domain=' .. cookie.domain
cookie_str = cookie_str .. '; ' .. 'domain=' .. cookie.domain
end

if cookie.expires then
assert("number" == type(cookie.expires), "expires value must be a number - UNIX epoch seconds")
cookie_str = cookie_str .. ';' .. 'expires=' .. os.date("%a, %d-%b-%Y %X GMT", cookie.expires)
cookie_str = cookie_str .. '; ' .. 'expires=' .. os.date("%a, %d-%b-%Y %X GMT", cookie.expires)
end

if cookie.http_only then cookie_str = cookie_str .. '; httponly' end
Expand Down