diff --git a/tests/busted_test_spec.lua b/tests/busted_test_spec.lua index 4ab61fe..16c75eb 100644 --- a/tests/busted_test_spec.lua +++ b/tests/busted_test_spec.lua @@ -13,7 +13,8 @@ describe("unit test -", function() request_uri = "/abs/2018EPJWC.18608001A/abstract", QUERY_STRING = "?whoknows=True", scheme = "http", - host = "ui.adsabs.harvard.edu" + host = "ui.adsabs.harvard.edu", + ERR = "4" }, header = { content_type = "html" @@ -23,7 +24,8 @@ describe("unit test -", function() capture = function(s) return { header = {}, status = 200, body = ""} end }, exit = function(s) end, - unescape_uri = function(s) return s end + unescape_uri = function(s) return s end, + log = function(type, s) print(s) end } -- mock ngx object using busted function @@ -135,14 +137,38 @@ describe("unit test -", function() -- run main function abs.run() - -- check that connect was called, correct error message displayed and 503 returned + -- check that connect was called, correct error message displayed assert.spy(_G.pg.connect).was.called() - assert.spy(_G.ngx.say).was.called_with("Could not connect to the database.") - assert.spy(_G.ngx.exit).was.called_with(503) + assert.spy(_G.ngx.log).was.called(_G.ngx.ERR, err) + + -- checks that location.capture was called with correct parameters + assert.spy(_G.ngx.location.capture).was.called() + assert.spy(_G.ngx.location.capture).was.called_with("/proxy_abs/" .. ngx.var.request_uri:sub(6) .. "?" .. ngx.var.QUERY_STRING) -- clear ngx function call history _G.ngx.say:clear() _G.ngx.exit:clear() + + -- redefine function to test for nil return value from proxy + ngx.location.capture = function() return nil end + spy.on(_G.ngx.location, 'capture') + + -- run main function + abs.run() + + assert.spy(_G.ngx.location.capture).was.called() + assert.spy(_G.ngx.location.capture).was.called_with("/proxy_abs/" .. ngx.var.request_uri:sub(6) .. "?" .. ngx.var.QUERY_STRING) + + -- check that connect was called, correct error message displayed and 503 returned + assert.spy(_G.ngx.say).was.called_with("Could not proxy to the service.") + -- + -- check that it exits + assert.spy(_G.ngx.exit).was.called() + + -- check exit code + assert.spy(_G.ngx.exit).was.called_with(503) + + end) it('checks the return value when target is not found in db', function() diff --git a/turbobee/abs.lua b/turbobee/abs.lua index a823019..7d4d699 100644 --- a/turbobee/abs.lua +++ b/turbobee/abs.lua @@ -16,22 +16,49 @@ local function split (input, s) return i, t end +local function proxy_abs (destination, parameters) + local url = "" + success = false + err = "" + + if parameters then + url = "/proxy_abs/" .. destination .. "?" .. parameters + else + url = "/proxy_abs/" .. destination + end + + local res = ngx.location.capture(url) + + if res then + ngx.header = res.header + ngx.status = res.status + ngx.print(res.body) + success = true + else + err = "Could not proxy to the service." + ngx.log(ngx.ERR, err) + end + + return success, err +end + function M.run() - success, err = pg:connect() + local destination = ngx.var.request_uri:sub(6) -- Ignore '/abs/' + local parameters = ngx.var.QUERY_STRING - if success then - local destination = ngx.var.request_uri:sub(6) -- Ignore '/abs/' + _, success, err = pcall(pg['connect'], pg) + + if success == true then local i, parts = split(destination, '/') local bibcode = ngx.unescape_uri(parts[1]) - if bibcode == nil or i < 1 then ngx.status=404 ngx.say("Invalid URI.") ngx.exit(404) - else + else local target = "//" .. ngx.var.host .. "/abs/" -- //dev.adsabs.harvard.edu/abs/ local result = nil @@ -53,30 +80,26 @@ function M.run() pg:query("INSERT into pages (qid, target) values (md5(random()::text || clock_timestamp()::text)::cstring, " .. pg:escape_literal(target .. bibcode) .. ")") end end - - local parameters = ngx.var.QUERY_STRING - local url = "" - if parameters then - url = "/proxy_abs/" .. destination .. "?" .. parameters - else - url = "/proxy_abs/" .. destination - end - local res = ngx.location.capture(url) - if res then - ngx.header = res.header - ngx.status = res.status - ngx.print(res.body) - else + + success, err = proxy_abs(destination, parameters) + if success ~= true then ngx.status = 503 - ngx.say("Could not proxy to the service.") + ngx.say(err) return ngx.exit(503) end end end else - ngx.status = 503 - ngx.say("Could not connect to the database.") - return ngx.exit(503) + -- logging for db connection failure and errors + err = err or success + ngx.log(ngx.ERR, err) + + success, err = proxy_abs(destination, parameters) + if success ~= true then + ngx.status = 503 + ngx.say(err) + return ngx.exit(503) + end end end