Skip to content

Commit

Permalink
hotfix(db-miss) porting #1914 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Jan 12, 2017
1 parent 45f6b01 commit 96f987a
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion spec/02-integration/04-core/02-hooks_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ local pl_path = require "pl.path"
local pl_file = require "pl.file"
local pl_stringx = require "pl.stringx"

-- cache entry inserted as a sentinel whenever a db lookup returns nothing
local db_miss_sentinel = { null = true }

describe("Core Hooks", function()
describe("Global", function()
describe("Global Plugin entity invalidation on API", function()
describe("Plugin entity invalidation on API", function()
local client, api_client
local plugin

Expand All @@ -22,6 +25,10 @@ describe("Core Hooks", function()
request_host = "hooks1.com",
upstream_url = "http://mockbin.com"
})
assert(helpers.dao.apis:insert {
request_host = "hooks2.com",
upstream_url = "http://mockbin.com"
})

plugin = assert(helpers.dao.plugins:insert {
name = "rate-limiting",
Expand All @@ -36,6 +43,72 @@ describe("Core Hooks", function()
helpers.stop_kong()
end)

it("should invalidate a global plugin when adding", function()
-- on a db-miss a sentinel value is inserted in the cache to prevent
-- too many db lookups. This sentinel value should be invalidated when
-- adding a plugin.

-- Making a request to populate the cache
local res = assert(client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "hooks2.com"
}
})
assert.response(res).has.status(200)

-- Make sure the cache is not populated
local res = assert(api_client:send {
method = "GET",
path = "/cache/"..cache.plugin_key("basic-auth", nil, nil)
})
local entry = cjson.decode(assert.res_status(200, res))
assert.same(db_miss_sentinel, entry) -- db-miss sentinel value

-- Add plugin
local res = assert(api_client:send {
method = "POST",
path = "/plugins/",
headers = {
["Content-Type"] = "application/json"
},
body = cjson.encode({
name = "basic-auth"
})
})
assert.response(res).has.status(201)

-- Wait for cache to be invalidated
helpers.wait_until(function()
local res = assert(api_client:send {
method = "GET",
path = "/cache/"..cache.plugin_key("basic-auth", nil, nil)
})
res:read_body()
return res.status == 404
end, 3)

-- Making a request: replacing the db-miss sentinel value in cache
local res = assert(client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "hooks2.com"
}
})
assert.response(res).has.status(401) -- in effect plugin, so failure

-- Make sure the cache is populated
local res = assert(api_client:send {
method = "GET",
path = "/cache/"..cache.plugin_key("basic-auth", nil, nil)
})
local entry = cjson.decode(assert.res_status(200, res))
assert.is_true(entry.enabled)
assert.is.same("basic-auth", entry.name)
end)

it("should invalidate a global plugin when deleting", function()
-- Making a request to populate the cache
local res = assert(client:send {
Expand Down

0 comments on commit 96f987a

Please sign in to comment.