Skip to content

Commit

Permalink
tests(azure-functions): remove usage of mockbin in azure-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
windmgc authored and chobits committed Nov 29, 2023
1 parent ea226be commit c1bd797
Showing 1 changed file with 80 additions and 12 deletions.
92 changes: 80 additions & 12 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("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,28 +58,58 @@ for _, strategy in helpers.each_strategy() do
protocols = { "http", "https" },
}

-- this plugin definition results in an upstream url to
-- http://mockbin.org/request
-- which will echo the request for inspection
-- 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)
return _request_uri(self, new_uri, params)
end
]]
}
}
}

db.plugins:insert {
name = "azure-functions",
route = { id = route2.id },
config = {
https = true,
appname = "mockbin",
hostdomain = "org",
https = false,
appname = "azure",
hostdomain = "example.com",
routeprefix = "request",
functionname = "test-func-name",
apikey = "anything_but_an_API_key",
clientid = "and_no_clientid",
},
}

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

fixtures.dns_mock:A({
name = "azure.example.com",
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 +122,7 @@ for _, strategy in helpers.each_strategy() do

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


Expand All @@ -70,7 +138,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 +155,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 +169,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

0 comments on commit c1bd797

Please sign in to comment.