Skip to content

Commit

Permalink
spec/proxy: bring rejection reason extraction test from cache_handler
Browse files Browse the repository at this point in the history
Also stop checking whether the cache_handler methods return true or
false, they no longer return that.
  • Loading branch information
davidor committed Jan 5, 2018
1 parent 8c2104f commit 19403f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
44 changes: 8 additions & 36 deletions spec/backend/cache_handler_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@

local _M = require('apicast.backend.cache_handler')
local lrucache = require('resty.lrucache')

local response = require('resty.http_ng.response')


describe('Cache Handler', function()

describe('.new', function()
it('has a default handler', function()
local handler = _M.new()
Expand Down Expand Up @@ -37,7 +32,7 @@ describe('Cache Handler', function()
local cache = lrucache.new(1)
ngx.var = { cached_key = nil }

assert.truthy(handler(cache, 'foobar', { status = 200 }))
handler(cache, 'foobar', { status = 200 })

assert.equal(200, cache:get('foobar'))
end)
Expand All @@ -46,7 +41,7 @@ describe('Cache Handler', function()
local cache = lrucache.new(1)
ngx.var = { cached_key = 'foobar' } -- means it is performed in post_action

assert.truthy(handler(cache, 'foobar', { status = 200 }))
handler(cache, 'foobar', { status = 200 })

assert.falsy(cache:get('foobar'))
end)
Expand All @@ -55,7 +50,7 @@ describe('Cache Handler', function()
local cache = lrucache.new(1)
cache:set('foobar', 200)

assert.falsy(handler(cache, 'foobar', { status = 403 }))
handler(cache, 'foobar', { status = 403 })

assert.falsy(cache:get('foobar'))
end)
Expand All @@ -64,47 +59,35 @@ describe('Cache Handler', function()
local cache = lrucache.new(1)
cache:set('foobar', 200)

assert.falsy(handler(cache, 'foobar', { status = 503 }))
handler(cache, 'foobar', { status = 503 })

assert.falsy(cache:get('foobar'))
end)

it('returns a rejection reason when given', function()
local cache = lrucache.new(1)

local authorized, rejection_reason = handler(
cache, 'foobar', response.new(nil, 403, { ['3scale-rejection-reason'] = 'some_reason' }, ''))

assert.falsy(authorized)
assert.equal('some_reason', rejection_reason)
assert.falsy(cache:get('foobar'))
end)
end)


describe('resilient', function()
local handler = _M.handlers.resilient

it('caches successful response', function()
local cache = lrucache.new(1)

assert.truthy(handler(cache, 'foobar', { status = 200 }))
handler(cache, 'foobar', { status = 200 })

assert.equal(200, cache:get('foobar'))
end)

it('caches forbidden response', function()
local cache = lrucache.new(1)

assert.falsy(handler(cache, 'foobar', { status = 403 }))
handler(cache, 'foobar', { status = 403 })

assert.equal(403, cache:get('foobar'))
end)

it('not caches server errors', function()
local cache = lrucache.new(1)

assert.falsy(handler(cache, 'foobar', { status = 503 }))
handler(cache, 'foobar', { status = 503 })

assert.falsy(cache:get('foobar'))
end)
Expand All @@ -114,21 +97,10 @@ describe('Cache Handler', function()

cache:set('foobar', 200)

assert.falsy(handler(cache, 'foobar', { status = 503 }))
handler(cache, 'foobar', { status = 503 })

assert.equal(200, cache:get('foobar'))
end)

it('returns a rejection reason when given', function()
local cache = lrucache.new(1)

local authorized, rejection_reason = handler(
cache, 'foobar', response.new(nil, 403, { ['3scale-rejection-reason'] = 'some_reason' }, ''))

assert.falsy(authorized)
assert.equal('some_reason', rejection_reason)
assert.equal(403, cache:get('foobar'))
end)
end)

end)
15 changes: 15 additions & 0 deletions spec/proxy_spec.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local response = require('resty.http_ng.response')
local lrucache = require('resty.lrucache')

local configuration_store = require 'apicast.configuration_store'
local Service = require 'apicast.configuration.service'

Expand Down Expand Up @@ -109,4 +112,16 @@ describe('Proxy', function()
assert.spy(proxy.cache_handler).was.called_with(proxy.cache, 'client_id=blah:foo=0', response, nil)
end)
end)

describe('.handle_backend_response', function()
it('returns a rejection reason when given', function()
local authorized, rejection_reason = proxy:handle_backend_response(
lrucache.new(1),
response.new(nil, 403, { ['3scale-rejection-reason'] = 'some_reason' }, ''),
nil)

assert.falsy(authorized)
assert.equal('some_reason', rejection_reason)
end)
end)
end)

0 comments on commit 19403f4

Please sign in to comment.