From 45058b120df30431e764f418df7be92877ecd9df Mon Sep 17 00:00:00 2001 From: Rob LaRubbio Date: Wed, 15 Feb 2012 09:39:38 -0800 Subject: [PATCH 1/5] Allow overiding of pub_addr and sub_addr. Ignore config_db if not set in config --- tir/engine.lua | 6 +++++- tir/m2.lua | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) 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 From 2fc71b2597af474d14276beff420dc3779a4154a Mon Sep 17 00:00:00 2001 From: Rob LaRubbio Date: Wed, 15 Feb 2012 09:41:11 -0800 Subject: [PATCH 2/5] Add tnetstrings as dependency --- rockspec/tir-scm-0.rockspec | 1 + 1 file changed, 1 insertion(+) 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", From 5fafe6d285d525dc88db075d062bd94c14fc6393 Mon Sep 17 00:00:00 2001 From: Rob LaRubbio Date: Wed, 15 Feb 2012 09:41:46 -0800 Subject: [PATCH 3/5] Add space so cookie matches test --- tir/util.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From c2a98e952ebb8b8e39caa83cf8c2da2b2231045b Mon Sep 17 00:00:00 2001 From: Rob LaRubbio Date: Wed, 15 Feb 2012 12:01:54 -0800 Subject: [PATCH 4/5] Add tests for the pub and sub addr overide --- tests/base/engine_tests.lua | 33 +++++++++++++++++++++++++++++++++ tests/data/config.lua | 1 + 2 files changed, 34 insertions(+) create mode 100644 tests/base/engine_tests.lua create mode 100644 tests/data/config.lua 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/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' From abbb2600092bb7256cd52c7a21eddc16c653979e Mon Sep 17 00:00:00 2001 From: Rob LaRubbio Date: Wed, 15 Feb 2012 12:02:26 -0800 Subject: [PATCH 5/5] Align broken tests with code. These could use some refactoring since they are now coded to depend on some implementation details --- tests/base/session_tests.lua | 20 +++++++++++++++----- tests/base/web_tests.lua | 9 +++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) 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)