diff --git a/rockspec/tir-scm-0.rockspec b/rockspec/tir-scm-0.rockspec index e9792ca..b6e10b8 100644 --- a/rockspec/tir-scm-0.rockspec +++ b/rockspec/tir-scm-0.rockspec @@ -19,6 +19,7 @@ dependencies = { "luajson", "lsqlite3", "telescope", + "tnetstrings", } build = { type = "none", diff --git a/tests/base/engine_tests.lua b/tests/base/engine_tests.lua new file mode 100644 index 0000000..8cd825a --- /dev/null +++ b/tests/base/engine_tests.lua @@ -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) + diff --git a/tests/base/session_tests.lua b/tests/base/session_tests.lua index 82bb8e7..779960d 100644 --- a/tests/base/session_tests.lua +++ b/tests/base/session_tests.lua @@ -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() @@ -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) @@ -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) diff --git a/tests/base/web_tests.lua b/tests/base/web_tests.lua index 58cc990..3252218 100644 --- a/tests/base/web_tests.lua +++ b/tests/base/web_tests.lua @@ -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) diff --git a/tests/data/config.lua b/tests/data/config.lua new file mode 100644 index 0000000..d109498 --- /dev/null +++ b/tests/data/config.lua @@ -0,0 +1 @@ +config_db = 'tests/data/config.sqlite' diff --git a/tir/engine.lua b/tir/engine.lua index eb4a1c0..55dbae6 100644 --- a/tir/engine.lua +++ b/tir/engine.lua @@ -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 diff --git a/tir/m2.lua b/tir/m2.lua index 99c6fcb..30bc904 100644 --- a/tir/m2.lua +++ b/tir/m2.lua @@ -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 diff --git a/tir/util.lua b/tir/util.lua index 0cf0c55..af6a74d 100644 --- a/tir/util.lua +++ b/tir/util.lua @@ -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