Skip to content

Commit

Permalink
spec: add specs for SOAP policy
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Jan 30, 2018
1 parent 15d8b5c commit 4f32753
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions spec/policy/soap/policy_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe('policy', function()
describe('.rewrite', function()
local test_service
local context
local soap_policy

before_each(function()
-- Called in the code, but in busted there is no request. Mocking.
ngx.req.get_method = function() return 'GET' end

-- Initialize a service that mocks get_usage() so we do not need to
-- configure all the things we need to apply the real matching of
-- mapping rules.
-- We mock this get_usage() so it returns a different result depending
-- on the SOAP action we pass so we can distinguish whether it comes
-- from the SOAPAction header or the Content-Type one.
test_service = {
get_usage = function(_, method, soap_action_uri)
if method == 'GET' and soap_action_uri == '/soap_action_ctype' then
return { hits = 2 }
elseif method == 'GET' and soap_action_uri == '/soap_action_header' then
return { hits = 3 }
else
return {}
end
end
}

context = { service = test_service }

soap_policy = require('apicast.policy.soap').new()
end)

describe('when the SOAP action is in the SOAPAction header', function()
it('saves the metrics to increase in the context', function()
ngx.req.get_headers = function() return { SOAPAction = '/soap_action_header' } end

soap_policy:rewrite(context)

assert.same({ hits = 3 }, context.add_to_usage)
end)
end)

describe('when the SOAP action is in the Content-Type header', function()
it('saves the metrics to increase in the context', function()
ngx.req.get_headers = function()
return { ["Content-Type"] = "application/soap+xml?action=/soap_action_ctype" }
end

soap_policy:rewrite(context)

assert.same({ hits = 2 }, context.add_to_usage)
end)
end)

describe('when the SOAP action is in the SOAPAction and the Content-Type headers', function()
it('saves the metrics to increase in the context, ctype takes precedence', function()
ngx.req.get_headers = function()
return {
SOAPAction = '/soap_action_header',
["Content-Type"] = "application/soap+xml?action=/soap_action_ctype"
}
end

soap_policy:rewrite(context)

assert.same({ hits = 2 }, context.add_to_usage)
end)
end)

describe('when the SOAP action is not specified', function()
it('does not save anything in the context', function()
ngx.req.get_headers = function() return {} end

soap_policy:rewrite(context)

assert.is_nil(context.add_to_usage)
end)
end)
end)
end)

0 comments on commit 4f32753

Please sign in to comment.