Skip to content

Commit

Permalink
spec/policy/url_rewriting: avoid testing internals of QueryParams
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed May 28, 2018
1 parent 6194bb7 commit 73745e9
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions spec/policy/url_rewriting/url_rewriting_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
local URLRewriting = require('apicast.policy.url_rewriting')

-- Mock QueryParams module. In these tests we are only interested in checking
-- that a QueryParams instance is called with the appropriate params. We do
-- not want to check its internals.
local QueryParams = require('apicast.policy.url_rewriting.query_params')
local noop = function() end
local mocked_query_params = { push = noop, set = noop, add = noop }
QueryParams.new = function() return mocked_query_params end
local spy_mocked_query_params_push
local spy_mocked_query_params_set
local spy_mocked_query_params_add

describe('URL rewriting policy', function()
describe('.rewrite', function()
before_each(function()
Expand All @@ -11,6 +22,10 @@ describe('URL rewriting policy', function()

stub(ngx.req, 'get_uri_args', function() return {} end)
stub(ngx.req, 'set_uri_args')

spy_mocked_query_params_push = spy.on(mocked_query_params, 'push')
spy_mocked_query_params_set = spy.on(mocked_query_params, 'set')
spy_mocked_query_params_add = spy.on(mocked_query_params, 'add')
end)

it('can rewrite URLs using sub', function()
Expand Down Expand Up @@ -83,54 +98,45 @@ describe('URL rewriting policy', function()
end)

it('can apply the "push" operation to args in the query', function()
stub(ngx.req, 'get_uri_args', function()
return { an_arg = '1'}
end)

local config_to_add_args = {
query_args_commands = {
{ op = 'push', arg = 'an_arg', value = '2' }
{ op = 'push', arg = 'an_arg', value = '1' }
}
}
local url_rewriting = URLRewriting.new(config_to_add_args)

url_rewriting:rewrite()

assert.stub(ngx.req.set_uri_args).was_called_with({ an_arg = { '1', '2' } })
assert.spy(spy_mocked_query_params_push).was_called_with(
mocked_query_params, 'an_arg', '1')
end)

it('can apply the "set" operation to args in the query', function()
stub(ngx.req, 'get_uri_args', function()
return { an_arg = 'original_value'}
end)

local config_to_replace_args = {
query_args_commands = {
{ op = 'set', arg = 'an_arg', value = 'new_val' }
{ op = 'set', arg = 'an_arg', value = '1' }
}
}
local url_rewriting = URLRewriting.new(config_to_replace_args)

url_rewriting:rewrite()

assert.stub(ngx.req.set_uri_args).was_called_with({ an_arg = 'new_val' })
assert.spy(spy_mocked_query_params_set).was_called_with(
mocked_query_params, 'an_arg', '1')
end)

it('can apply the "add" operation to args in the query', function()
stub(ngx.req, 'get_uri_args', function()
return { an_arg = '1'}
end)

local config_to_add_args = {
query_args_commands = {
{ op = 'add', arg = 'an_arg', value = '2' }
{ op = 'add', arg = 'an_arg', value = '1' }
}
}
local url_rewriting = URLRewriting.new(config_to_add_args)

url_rewriting:rewrite()

assert.stub(ngx.req.set_uri_args).was_called_with({ an_arg = { '1', '2' } })
assert.spy(spy_mocked_query_params_add).was_called_with(
mocked_query_params, 'an_arg', '1')
end)

it('supports liquid templates when pushing query args', function()
Expand All @@ -151,8 +157,8 @@ describe('URL rewriting policy', function()

url_rewriting:rewrite(context)

assert.stub(ngx.req.set_uri_args).was_called_with(
{ an_arg = context.var_in_context })
assert.spy(spy_mocked_query_params_push).was_called_with(
mocked_query_params, 'an_arg', context.var_in_context)
end)

it('supports liquid templates when setting query args', function()
Expand All @@ -173,11 +179,11 @@ describe('URL rewriting policy', function()

url_rewriting:rewrite(context)

assert.stub(ngx.req.set_uri_args).was_called_with(
{ an_arg = context.var_in_context })
assert.spy(spy_mocked_query_params_set).was_called_with(
mocked_query_params, 'an_arg', context.var_in_context)
end)

it('supports liquid templates when replacing query args', function()
it('supports liquid templates when adding query args', function()
stub(ngx.req, 'get_uri_args', function()
return { an_arg = 'original_value'}
end)
Expand All @@ -199,8 +205,8 @@ describe('URL rewriting policy', function()

url_rewriting:rewrite(context)

assert.stub(ngx.req.set_uri_args).was_called_with(
{ an_arg = { 'original_value', context.var_in_context } })
assert.spy(spy_mocked_query_params_add).was_called_with(
mocked_query_params, 'an_arg', context.var_in_context)
end)
end)
end)

0 comments on commit 73745e9

Please sign in to comment.