Skip to content

Commit

Permalink
[policy] .request_phases
Browse files Browse the repository at this point in the history
Offer an interator for just request phases skipping the init and
init_worker.
  • Loading branch information
mikz committed Feb 12, 2019
1 parent 04bf4bf commit 271b3f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 14 additions & 2 deletions gateway/src/apicast/policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

local _M = { }

local PHASES = {
'init', 'init_worker',
local REQUEST_PHASES = {
'rewrite', 'access',
'content', 'balancer',
'header_filter', 'body_filter',
Expand All @@ -18,6 +17,15 @@ local PHASES = {
'ssl_certificate',
}

local INIT_PHASES = {
'init', 'init_worker',
}

local PHASES = { }

table.move(INIT_PHASES, 1, #INIT_PHASES, #PHASES + 1, PHASES)
table.move(REQUEST_PHASES, 1, #REQUEST_PHASES, #PHASES + 1, PHASES)

local GC = require('apicast.gc')
local setmetatable_gc_clone = GC.setmetatable_gc_clone
local ipairs = ipairs
Expand Down Expand Up @@ -64,4 +72,8 @@ function _M.phases()
return ipairs(PHASES)
end

function _M.request_phases()
return ipairs(REQUEST_PHASES)
end

return _M
16 changes: 16 additions & 0 deletions spec/policy_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local policy = require 'apicast.policy'
local tab_clone = require('table.clone')

describe('policy', function()
local phases = {
Expand Down Expand Up @@ -117,6 +118,21 @@ describe('policy', function()
end)
end)

describe('.request_phases', function()
it('returns the request nginx phases where policies can run, sorted by order of execution', function()
local request_phases = tab_clone(phases)
table.remove(request_phases, 1) -- init
table.remove(request_phases, 1) -- init_worker

local res = {}
for _, phase in policy.request_phases() do
table.insert(res, phase)
end

assert.same(request_phases, res)
end)
end)

describe('garbage collection', function()
it('runs __gc metamethod when a policy instance is garbage-collected', function()
local MyPolicy, mt = policy.new('my_policy', '1.0')
Expand Down

0 comments on commit 271b3f1

Please sign in to comment.