Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(*): fix azure plugin test due to mockbin sunset #11879

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 80 additions & 8 deletions spec/03-plugins/35-azure-functions/01-access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
local helpers = require "spec.helpers"
local meta = require "kong.meta"

local http_mock = require "spec.helpers.http_mock"

local server_tokens = meta._SERVER_TOKENS


for _, strategy in helpers.each_strategy() do
describe("#flaky Plugin: Azure Functions (access) [#" .. strategy .. "]", function()
describe("Plugin: Azure Functions (access) [#" .. strategy .. "]", function()
local mock
local proxy_client
local mock_http_server_port = helpers.get_available_port()

mock = http_mock.new("127.0.0.1:" .. mock_http_server_port, {
["/"] = {
access = [[
local json = require "cjson"
local method = ngx.req.get_method()
local uri = ngx.var.request_uri
local headers = ngx.req.get_headers(nil, true)
local query_args = ngx.req.get_uri_args()
ngx.req.read_body()
local body
-- collect body
body = ngx.req.get_body_data()
if not body then
local file = ngx.req.get_body_file()
if file then
local f = io.open(file, "r")
if f then
body = f:read("*a")
f:close()
end
end
end
ngx.say(json.encode({
query_args = query_args,
uri = uri,
method = method,
headers = headers,
body = body,
status = 200,
}))
]]
},
})

setup(function()
local _, db = helpers.get_db_utils(strategy, {
Expand All @@ -21,14 +58,37 @@ for _, strategy in helpers.each_strategy() do
protocols = { "http", "https" },
}

-- Mocking lua-resty-http's request_uri function
db.plugins:insert {
name = "pre-function",
route = { id = route2.id },
config = {
access = {
[[
local http = require "resty.http"
local json = require "cjson"
local _request_uri = http.request_uri
http.request_uri = function (self, uri, params)
local scheme, host, port, _, _ = unpack(http:parse_uri(uri))
local mock_server_port = ]] .. mock_http_server_port .. [[
-- Replace the port with the mock server port
local new_uri = string.format("%s://%s:%d", scheme, host, mock_server_port)
ngx.log(ngx.ERR, "REPLACE URI WITH:" .. new_uri)
windmgc marked this conversation as resolved.
Show resolved Hide resolved
return _request_uri(self, new_uri, params)
end
]]
}
}
}

-- this plugin definition results in an upstream url to
-- http://mockbin.org/request
-- which will echo the request for inspection
db.plugins:insert {
name = "azure-functions",
route = { id = route2.id },
config = {
https = true,
https = false,
appname = "mockbin",
hostdomain = "org",
routeprefix = "request",
Expand All @@ -38,11 +98,22 @@ for _, strategy in helpers.each_strategy() do
},
}

assert(helpers.start_kong{
database = strategy,
plugins = "azure-functions",
local fixtures = {
dns_mock = helpers.dns_mock.new()
}

fixtures.dns_mock:A({
name = "mockbin.org",
windmgc marked this conversation as resolved.
Show resolved Hide resolved
address = "127.0.0.1",
})

assert(helpers.start_kong({
database = strategy,
untrusted_lua = "on",
plugins = "azure-functions,pre-function",
}, nil, nil, fixtures))

assert(mock:start())
end) -- setup

before_each(function()
Expand All @@ -55,6 +126,7 @@ for _, strategy in helpers.each_strategy() do

teardown(function()
helpers.stop_kong()
assert(mock:stop())
end)


Expand All @@ -70,7 +142,7 @@ for _, strategy in helpers.each_strategy() do

assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
assert.same({ hello ="world" }, json.queryString)
assert.same({ hello ="world" }, json.query_args)
end)

it("passes request body", function()
Expand All @@ -87,7 +159,7 @@ for _, strategy in helpers.each_strategy() do

assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
assert.same(body, json.postData.text)
assert.same(body, json.body)
end)

it("passes the path parameters", function()
Expand All @@ -101,7 +173,7 @@ for _, strategy in helpers.each_strategy() do

assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
assert.matches("mockbin.org/request/test%-func%-name/and/then/some", json.url)
assert.matches("/request/test%-func%-name/and/then/some", json.uri)
end)

it("passes the method", function()
Expand Down