From 692e7de0386fee5e3966b362abb5b294d1428d09 Mon Sep 17 00:00:00 2001 From: windmgc Date: Mon, 30 Oct 2023 12:19:38 +0800 Subject: [PATCH 1/3] tests(*): fix azure plugin test due to mockbin sunset --- .../35-azure-functions/01-access_spec.lua | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/spec/03-plugins/35-azure-functions/01-access_spec.lua b/spec/03-plugins/35-azure-functions/01-access_spec.lua index dfcc0ffc787..f93abd11236 100644 --- a/spec/03-plugins/35-azure-functions/01-access_spec.lua +++ b/spec/03-plugins/35-azure-functions/01-access_spec.lua @@ -21,6 +21,33 @@ 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" + http.request_uri = function (self, uri, params) + return { + status = 200, + body = json.encode({ + status = 200, + uri = uri, + params = params, + }), + headers = { + ["Content-Type"] = "application/json", + } + } + end + ]] + } + } + } + -- this plugin definition results in an upstream url to -- http://mockbin.org/request -- which will echo the request for inspection @@ -40,7 +67,8 @@ for _, strategy in helpers.each_strategy() do assert(helpers.start_kong{ database = strategy, - plugins = "azure-functions", + untrusted_lua = "on", + plugins = "azure-functions,pre-function", }) end) -- setup @@ -70,7 +98,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.params.query) end) it("passes request body", function() @@ -87,7 +115,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.params.body) end) it("passes the path parameters", function() @@ -101,7 +129,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.params.path) end) it("passes the method", function() @@ -115,7 +143,7 @@ for _, strategy in helpers.each_strategy() do assert.response(res).has.status(200) local json = assert.response(res).has.jsonbody() - assert.same("POST", json.method) + assert.same("POST", json.params.method) end) it("passes the headers", function() @@ -130,7 +158,7 @@ for _, strategy in helpers.each_strategy() do assert.response(res).has.status(200) local json = assert.response(res).has.jsonbody() - assert.same("just a value", json.headers["just-a-header"]) + assert.same("just a value", json.params.headers["just-a-header"]) end) it("injects the apikey and clientid", function() @@ -145,8 +173,8 @@ for _, strategy in helpers.each_strategy() do assert.response(res).has.status(200) local json = assert.response(res).has.jsonbody() --assert.same({}, json.headers) - assert.same("anything_but_an_API_key", json.headers["x-functions-key"]) - assert.same("and_no_clientid", json.headers["x-functions-clientid"]) + assert.same("anything_but_an_API_key", json.params.headers["x-functions-key"]) + assert.same("and_no_clientid", json.params.headers["x-functions-clientid"]) end) it("returns server tokens with Via header", function() From 3d4deab3d6ffc2a420d4c8eed31c6f57a710181b Mon Sep 17 00:00:00 2001 From: windmgc Date: Mon, 30 Oct 2023 16:48:03 +0800 Subject: [PATCH 2/3] use http mock and pack resty-http request_uri function --- .../35-azure-functions/01-access_spec.lua | 88 ++++++++++++++----- 1 file changed, 66 insertions(+), 22 deletions(-) diff --git a/spec/03-plugins/35-azure-functions/01-access_spec.lua b/spec/03-plugins/35-azure-functions/01-access_spec.lua index f93abd11236..ef2b16ca3da 100644 --- a/spec/03-plugins/35-azure-functions/01-access_spec.lua +++ b/spec/03-plugins/35-azure-functions/01-access_spec.lua @@ -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, { @@ -30,18 +67,14 @@ for _, strategy in helpers.each_strategy() do [[ local http = require "resty.http" local json = require "cjson" + local _request_uri = http.request_uri http.request_uri = function (self, uri, params) - return { - status = 200, - body = json.encode({ - status = 200, - uri = uri, - params = params, - }), - headers = { - ["Content-Type"] = "application/json", - } - } + 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) + return _request_uri(self, new_uri, params) end ]] } @@ -55,7 +88,7 @@ for _, strategy in helpers.each_strategy() do name = "azure-functions", route = { id = route2.id }, config = { - https = true, + https = false, appname = "mockbin", hostdomain = "org", routeprefix = "request", @@ -65,12 +98,22 @@ for _, strategy in helpers.each_strategy() do }, } - assert(helpers.start_kong{ + local fixtures = { + dns_mock = helpers.dns_mock.new() + } + + fixtures.dns_mock:A({ + name = "mockbin.org", + 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() @@ -83,6 +126,7 @@ for _, strategy in helpers.each_strategy() do teardown(function() helpers.stop_kong() + assert(mock:stop()) end) @@ -98,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.params.query) + assert.same({ hello ="world" }, json.query_args) end) it("passes request body", function() @@ -115,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.params.body) + assert.same(body, json.body) end) it("passes the path parameters", function() @@ -129,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("/request/test%-func%-name/and/then/some", json.params.path) + assert.matches("/request/test%-func%-name/and/then/some", json.uri) end) it("passes the method", function() @@ -143,7 +187,7 @@ for _, strategy in helpers.each_strategy() do assert.response(res).has.status(200) local json = assert.response(res).has.jsonbody() - assert.same("POST", json.params.method) + assert.same("POST", json.method) end) it("passes the headers", function() @@ -158,7 +202,7 @@ for _, strategy in helpers.each_strategy() do assert.response(res).has.status(200) local json = assert.response(res).has.jsonbody() - assert.same("just a value", json.params.headers["just-a-header"]) + assert.same("just a value", json.headers["just-a-header"]) end) it("injects the apikey and clientid", function() @@ -173,8 +217,8 @@ for _, strategy in helpers.each_strategy() do assert.response(res).has.status(200) local json = assert.response(res).has.jsonbody() --assert.same({}, json.headers) - assert.same("anything_but_an_API_key", json.params.headers["x-functions-key"]) - assert.same("and_no_clientid", json.params.headers["x-functions-clientid"]) + assert.same("anything_but_an_API_key", json.headers["x-functions-key"]) + assert.same("and_no_clientid", json.headers["x-functions-clientid"]) end) it("returns server tokens with Via header", function() From 84c8e2bea15caa2a78d500b3b291e21e17012d92 Mon Sep 17 00:00:00 2001 From: windmgc Date: Tue, 31 Oct 2023 13:36:02 +0800 Subject: [PATCH 3/3] remove mock server logs and use different hostname for mock server --- spec/03-plugins/35-azure-functions/01-access_spec.lua | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/spec/03-plugins/35-azure-functions/01-access_spec.lua b/spec/03-plugins/35-azure-functions/01-access_spec.lua index ef2b16ca3da..9907c7e0d0b 100644 --- a/spec/03-plugins/35-azure-functions/01-access_spec.lua +++ b/spec/03-plugins/35-azure-functions/01-access_spec.lua @@ -73,7 +73,6 @@ for _, strategy in helpers.each_strategy() do 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) return _request_uri(self, new_uri, params) end ]] @@ -81,16 +80,13 @@ for _, strategy in helpers.each_strategy() do } } - -- 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 = false, - appname = "mockbin", - hostdomain = "org", + appname = "azure", + hostdomain = "example.com", routeprefix = "request", functionname = "test-func-name", apikey = "anything_but_an_API_key", @@ -103,7 +99,7 @@ for _, strategy in helpers.each_strategy() do } fixtures.dns_mock:A({ - name = "mockbin.org", + name = "azure.example.com", address = "127.0.0.1", })