Skip to content

Commit

Permalink
Merge pull request #625 from 3scale/reset-ngx-state-in-busted-helper
Browse files Browse the repository at this point in the history
Reset ngx state on every test
  • Loading branch information
davidor authored Feb 26, 2018
2 parents 5168008 + cd26d24 commit 985a24a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
25 changes: 13 additions & 12 deletions spec/ngx_helper.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
local busted = require('busted')
local misc = require('resty.core.misc')
local deepcopy = require('pl.tablex').deepcopy

local ngx_var = ngx.var
local ngx_ctx = ngx.ctx
local ngx_shared = ngx.shared
local ngx_header = ngx.header
local ngx_var_original = deepcopy(ngx.var)
local ngx_ctx_original = deepcopy(ngx.ctx)
local ngx_shared_original = deepcopy(ngx.shared)
local ngx_header_original = deepcopy(ngx.header)

local register_getter = misc.register_ngx_magic_key_getter
local register_setter = misc.register_ngx_magic_key_setter
Expand All @@ -25,14 +26,17 @@ end
local get_status = getlocal(register_getter, 'ngx_magic_key_getters').status
local set_status = getlocal(register_setter, 'ngx_magic_key_setters').status

local function cleanup()
ngx.var = ngx_var
ngx.ctx = ngx_ctx
ngx.shared = ngx_shared
ngx.header = ngx_header
local function reset_ngx_state()
ngx.var = deepcopy(ngx_var_original)
ngx.ctx = deepcopy(ngx_ctx_original)
ngx.shared = deepcopy(ngx_shared_original)
ngx.header = deepcopy(ngx_header_original)
end

local function cleanup()
register_getter('status', get_status)
register_setter('status', set_status)
reset_ngx_state()
end

local function setup()
Expand All @@ -43,9 +47,6 @@ local function setup()
end)

register_getter('status', function() return status end)

ngx.ctx = { }
ngx.header = { }
end

busted.after_each(cleanup)
Expand Down
5 changes: 3 additions & 2 deletions spec/policy/cors/cors_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ describe('CORS policy', function()

describe('.header_filter', function()
before_each(function()
local headers = {}
stub(ngx.header, function() return headers end)
-- Replace original ngx.header. Openresty does not allow to modify it when
-- running busted tests.
ngx.header = {}
end)

describe('when the policy configuration defines CORS headers', function()
Expand Down
5 changes: 3 additions & 2 deletions spec/policy/headers/headers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ describe('Headers policy', function()
local response_headers = 'response' -- header_filter() only modifies resp headers

before_each(function()
local headers = {}
stub(ngx.header, function() return headers end)
-- Replace original ngx.header. Openresty does not allow to modify it when
-- running busted tests.
ngx.header = {}
end)

describe('the push operation', function()
Expand Down
4 changes: 4 additions & 0 deletions spec/proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe('Proxy', function()
describe(':rewrite', function()
local service
before_each(function()
-- Replace original ngx.header. Openresty does not allow to modify it when
-- running busted tests.
ngx.header = {}

ngx.var = { backend_endpoint = 'http://localhost:1853', uri = '/a/uri' }
ngx.req = { get_method = function () return 'GET' end}
service = Service.new({ extract_usage = function() end })
Expand Down

0 comments on commit 985a24a

Please sign in to comment.