From 466b44c7a49ced9cdbd92de2069cf303d3d4d5fa Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 3 Aug 2017 19:02:52 +0200 Subject: [PATCH] tests(plugins) use local upstream mock instead of httpbin/mockbin --- .../01-helpers/00-helpers_spec.lua | 2 +- .../05-proxy/01-router_spec.lua | 4 +- .../05-proxy/06-upstream_timeouts_spec.lua | 2 +- .../05-proxy/07-uri_encoding_spec.lua | 13 +- .../03-plugins/01-tcp-log/01-tcp-log_spec.lua | 45 +- .../03-plugins/02-udp-log/01-udp-log_spec.lua | 52 +- spec/03-plugins/04-file-log/01-log_spec.lua | 18 +- spec/03-plugins/05-syslog/01-log_spec.lua | 52 +- spec/03-plugins/06-statsd/01-log_spec.lua | 288 +++---- spec/03-plugins/07-loggly/01-log_spec.lua | 81 +- spec/03-plugins/08-datadog/01-log_spec.lua | 114 +-- spec/03-plugins/09-galileo/01-alf_spec.lua | 28 +- spec/03-plugins/09-galileo/02-buffer_spec.lua | 38 +- spec/03-plugins/09-galileo/ngx.lua | 4 +- spec/03-plugins/10-key-auth/01-api_spec.lua | 16 +- .../03-plugins/10-key-auth/02-access_spec.lua | 207 +++-- .../10-key-auth/03-invalidations_spec.lua | 20 +- .../11-basic-auth/03-access_spec.lua | 118 +-- .../11-basic-auth/04-invalidations_spec.lua | 22 +- .../12-correlation-id/01-access_spec.lua | 72 +- .../01-access_spec.lua | 12 +- spec/03-plugins/14-cors/01-access_spec.lua | 61 +- .../15-request-transformer/02-access_spec.lua | 192 +++-- .../15-request-transformer/03-api_spec.lua | 10 +- .../16-response-transformer/03-api_spec.lua | 10 +- .../04-filter_spec.lua | 20 +- .../05-big_response_body_spec.lua | 32 +- spec/03-plugins/17-jwt/03-access_spec.lua | 192 +++-- .../17-jwt/04-invalidations_spec.lua | 22 +- .../18-ip-restriction/02-access_spec.lua | 89 ++- spec/03-plugins/19-acl/02-access_spec.lua | 66 +- .../19-acl/03-invalidations_spec.lua | 16 +- .../20-hmac-auth/03-access_spec.lua | 751 +++++++++--------- .../20-hmac-auth/04-invalidations_spec.lua | 26 +- .../21-ldap-auth/01-access_spec.lua | 74 +- .../22-bot-detection/01-access_spec.lua | 36 +- .../02-invalidations_spec.lua | 12 +- .../22-bot-detection/03-api_spec.lua | 16 +- .../23-aws-lambda/01-access_spec.lua | 158 ++-- .../24-rate-limiting/03-api_spec.lua | 10 +- .../24-rate-limiting/04-access_spec.lua | 244 +++--- .../25-response-rate-limiting/03-api_spec.lua | 10 +- .../04-access_spec.lua | 240 +++--- spec/03-plugins/26-oauth2/02-api_spec.lua | 28 +- spec/03-plugins/26-oauth2/03-access_spec.lua | 353 ++++---- .../26-oauth2/04-invalidations_spec.lua | 36 +- .../27-request-termination/02-access_spec.lua | 91 +-- .../03-integration_spec.lua | 18 +- spec/fixtures/custom_nginx.template | 60 +- spec/fixtures/mock_upstream.lua | 175 ++-- spec/helpers.lua | 51 +- 51 files changed, 2232 insertions(+), 2075 deletions(-) diff --git a/spec/02-integration/01-helpers/00-helpers_spec.lua b/spec/02-integration/01-helpers/00-helpers_spec.lua index 93ffc849b1b1..d2491746a551 100644 --- a/spec/02-integration/01-helpers/00-helpers_spec.lua +++ b/spec/02-integration/01-helpers/00-helpers_spec.lua @@ -194,7 +194,7 @@ describe("helpers: assertions and modifiers", function() path = "/request", body = { hello = "world" }, headers = { - host = "mock_upstream", + host = "mock_upstream", ["Content-Type"] = "application/json", }, }) diff --git a/spec/02-integration/05-proxy/01-router_spec.lua b/spec/02-integration/05-proxy/01-router_spec.lua index 74ead71dad07..d1dc5ba4fb0b 100644 --- a/spec/02-integration/05-proxy/01-router_spec.lua +++ b/spec/02-integration/05-proxy/01-router_spec.lua @@ -214,8 +214,8 @@ describe("Router", function() local body = assert.res_status(200, res) local json = cjson.decode(body) - assert.equal("bar", json.args.foo) - assert.equal("world", json.args.hello) + assert.equal("bar", json.uri_args.foo) + assert.equal("world", json.uri_args.hello) end) it("does proxy an empty querystring if URI does not contain arguments", function() diff --git a/spec/02-integration/05-proxy/06-upstream_timeouts_spec.lua b/spec/02-integration/05-proxy/06-upstream_timeouts_spec.lua index ff99f7642ea7..63dc0907bec7 100644 --- a/spec/02-integration/05-proxy/06-upstream_timeouts_spec.lua +++ b/spec/02-integration/05-proxy/06-upstream_timeouts_spec.lua @@ -104,7 +104,7 @@ dao_helpers.for_each_dao(function(kong_config) }) -- do *not* use assert.res_status() here in case of - -- failure to avoid a 1MB long error log + -- failure to avoid a very large error log assert.equal(504, res.status) end) end) diff --git a/spec/02-integration/05-proxy/07-uri_encoding_spec.lua b/spec/02-integration/05-proxy/07-uri_encoding_spec.lua index 6fd052680846..e472b9752fa1 100644 --- a/spec/02-integration/05-proxy/07-uri_encoding_spec.lua +++ b/spec/02-integration/05-proxy/07-uri_encoding_spec.lua @@ -56,8 +56,8 @@ describe("URI encoding", function() local body = assert.res_status(200, res) local json = cjson.decode(body) - assert.equal("25", json.args.limit) - assert.equal([[{"or":[{"name":{"like":"%bac%"}}]}]], json.args.where) + assert.equal("25", json.uri_args.limit) + assert.equal([[{"or":[{"name":{"like":"%bac%"}}]}]], json.uri_args.where) end) it("issue #1480 does not percent-encode args unecessarily", function() @@ -76,8 +76,7 @@ describe("URI encoding", function() local body = assert.res_status(200, res) local json = cjson.decode(body) - assert.equal(helpers.mock_upstream_protocol .. "://" .. - helpers.mock_upstream_host .. "/request?param=1.2.3", json.url) + assert.equal(helpers.mock_upstream_url .. "/request?param=1.2.3", json.url) end) it("issue #749 does not decode percent-encoded args", function() @@ -94,8 +93,7 @@ describe("URI encoding", function() local body = assert.res_status(200, res) local json = cjson.decode(body) - assert.equal(helpers.mock_upstream_protocol .. "://" .. - helpers.mock_upstream_host .. "/request?param=abc%7Cdef", json.url) + assert.equal(helpers.mock_upstream_url .. "/request?param=abc%7Cdef", json.url) end) it("issue #688 does not percent-decode proxied URLs", function() @@ -112,8 +110,7 @@ describe("URI encoding", function() local body = assert.res_status(200, res) local json = cjson.decode(body) - assert.equal(helpers.mock_upstream_protocol .. "://" .. - helpers.mock_upstream_host .. "/request/foo%2Fbar", json.url) + assert.equal(helpers.mock_upstream_url .. "/request/foo%2Fbar", json.url) end) it("issue #2512 does not double percent-encode upstream URLs", function() diff --git a/spec/03-plugins/01-tcp-log/01-tcp-log_spec.lua b/spec/03-plugins/01-tcp-log/01-tcp-log_spec.lua index edc48e6c8a51..8035dae3aa3c 100644 --- a/spec/03-plugins/01-tcp-log/01-tcp-log_spec.lua +++ b/spec/03-plugins/01-tcp-log/01-tcp-log_spec.lua @@ -2,7 +2,6 @@ local cjson = require "cjson" local helpers = require "spec.helpers" local TCP_PORT = 35001 -local HTTP_DELAY_PORT = 35003 describe("Plugin: tcp-log (log)", function() local client @@ -11,34 +10,23 @@ describe("Plugin: tcp-log (log)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "tcp_logging.com" }, - upstream_url = "http://mockbin.com", - }) - local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "tcp_logging2.com" }, - upstream_url = "http://127.0.0.1:" .. HTTP_DELAY_PORT, + name = "api-1", + hosts = { "tcp_logging.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { api_id = api1.id, - name = "tcp-log", - config = { - host = "127.0.0.1", - port = TCP_PORT - }, - }) - assert(helpers.dao.plugins:insert { - api_id = api2.id, - name = "tcp-log", + name = "tcp-log", config = { host = "127.0.0.1", - port = TCP_PORT + port = TCP_PORT, }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) teardown(function() @@ -51,10 +39,10 @@ describe("Plugin: tcp-log (log)", function() -- Making the request local r = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { - host = "tcp_logging.com" + host = "tcp_logging.com", }, }) assert.response(r).has.status(200) @@ -70,16 +58,15 @@ describe("Plugin: tcp-log (log)", function() end) it("logs proper latencies", function() - local http_thread = helpers.http_server(HTTP_DELAY_PORT) -- Starting the mock TCP server local tcp_thread = helpers.tcp_server(TCP_PORT) -- Starting the mock TCP server -- Making the request local r = assert(client:send { - method = "GET", - path = "/request/delay", + method = "GET", + path = "/delay/2", headers = { - host = "tcp_logging2.com" - } + host = "tcp_logging.com", + }, }) assert.response(r).has.status(200) @@ -93,7 +80,5 @@ describe("Plugin: tcp-log (log)", function() assert.True(log_message.latencies.proxy < 3000) assert.True(log_message.latencies.request >= log_message.latencies.kong + log_message.latencies.proxy) - - http_thread:join() end) end) diff --git a/spec/03-plugins/02-udp-log/01-udp-log_spec.lua b/spec/03-plugins/02-udp-log/01-udp-log_spec.lua index 94c7d03b6162..05e52166ed2a 100644 --- a/spec/03-plugins/02-udp-log/01-udp-log_spec.lua +++ b/spec/03-plugins/02-udp-log/01-udp-log_spec.lua @@ -1,9 +1,7 @@ local cjson = require "cjson" local helpers = require "spec.helpers" -local TCP_PORT = 35002 local UDP_PORT = 35001 -local HTTP_DELAY_PORT = 35004 describe("Plugin: udp-log (log)", function() local client @@ -11,35 +9,24 @@ describe("Plugin: udp-log (log)", function() setup(function() helpers.run_migrations() - local api2 = assert(helpers.dao.apis:insert { - name = "tests-udp-logging2", - hosts = { "udp_logging2.com" }, - upstream_url = "http://127.0.0.1:" .. HTTP_DELAY_PORT, - }) - local api3 = assert(helpers.dao.apis:insert { - name = "tests-udp-logging", - hosts = { "udp_logging.com" }, - upstream_url = "http://mockbin.com", + local api1 = assert(helpers.dao.apis:insert { + name = "tests-udp-logging", + hosts = { "udp_logging.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - api_id = api2.id, - name = "udp-log", - config = { - host = "127.0.0.1", - port = TCP_PORT - }, - }) - assert(helpers.dao.plugins:insert { - api_id = api3.id, - name = "udp-log", + api_id = api1.id, + name = "udp-log", config = { host = "127.0.0.1", port = UDP_PORT }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) @@ -49,16 +36,15 @@ describe("Plugin: udp-log (log)", function() end) it("logs proper latencies", function() - local http_thread = helpers.http_server(HTTP_DELAY_PORT) - local udp_thread = helpers.udp_server(TCP_PORT) + local udp_thread = helpers.udp_server(UDP_PORT) -- Making the request local r = assert(client:send { - method = "GET", - path = "/request/delay", + method = "GET", + path = "/delay/2", headers = { - host = "udp_logging2.com" - } + host = "udp_logging.com", + }, }) assert.response(r).has.status(200) @@ -72,8 +58,6 @@ describe("Plugin: udp-log (log)", function() assert.True(log_message.latencies.proxy < 3000) assert.True(log_message.latencies.request >= log_message.latencies.kong + log_message.latencies.proxy) - - http_thread:join() end) it("logs to UDP", function() @@ -81,11 +65,11 @@ describe("Plugin: udp-log (log)", function() -- Making the request local res = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { - host = "udp_logging.com" - } + host = "udp_logging.com", + }, }) assert.response(res).has.status(200) diff --git a/spec/03-plugins/04-file-log/01-log_spec.lua b/spec/03-plugins/04-file-log/01-log_spec.lua index af3d64a3eb06..2ef4572000ac 100644 --- a/spec/03-plugins/04-file-log/01-log_spec.lua +++ b/spec/03-plugins/04-file-log/01-log_spec.lua @@ -13,21 +13,23 @@ describe("Plugin: file-log (log)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "file_logging.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "file_logging.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { api_id = api1.id, - name = "file-log", + name = "file-log", config = { - path = FILE_LOG_PATH, + path = FILE_LOG_PATH, reopen = true, - } + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() helpers.stop_kong() @@ -65,7 +67,7 @@ describe("Plugin: file-log (log)", function() assert.same("127.0.0.1", log_message.client_ip) assert.same(uuid, log_message.request.headers["file-log-uuid"]) end) - + it("reopens file on each request", function() local uuid1 = utils.uuid() diff --git a/spec/03-plugins/05-syslog/01-log_spec.lua b/spec/03-plugins/05-syslog/01-log_spec.lua index 4ae35ab34406..58ef979e74a7 100644 --- a/spec/03-plugins/05-syslog/01-log_spec.lua +++ b/spec/03-plugins/05-syslog/01-log_spec.lua @@ -9,57 +9,59 @@ describe("#ci Plugin: syslog (log)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "logging.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "logging.com" }, + upstream_url = helpers.mock_upstream_url, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "logging2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "logging2.com" }, + upstream_url = helpers.mock_upstream_url, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "logging3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "logging3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { api_id = api1.id, - name = "syslog", + name = "syslog", config = { - log_level = "info", - successful_severity = "warning", + log_level = "info", + successful_severity = "warning", client_errors_severity = "warning", - server_errors_severity = "warning" - } + server_errors_severity = "warning", + }, }) assert(helpers.dao.plugins:insert { api_id = api2.id, - name = "syslog", + name = "syslog", config = { - log_level = "err", - successful_severity = "warning", + log_level = "err", + successful_severity = "warning", client_errors_severity = "warning", - server_errors_severity = "warning" - } + server_errors_severity = "warning", + }, }) assert(helpers.dao.plugins:insert { api_id = api3.id, - name = "syslog", + name = "syslog", config = { - log_level = "warning", - successful_severity = "warning", + log_level = "warning", + successful_severity = "warning", client_errors_severity = "warning", - server_errors_severity = "warning" - } + server_errors_severity = "warning", + }, }) local ok, _, stdout = helpers.execute("uname") assert(ok, "failed to retrieve platform name") platform = pl_stringx.strip(stdout) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() helpers.stop_kong() diff --git a/spec/03-plugins/06-statsd/01-log_spec.lua b/spec/03-plugins/06-statsd/01-log_spec.lua index 424af490a415..b4aafb608ae7 100644 --- a/spec/03-plugins/06-statsd/01-log_spec.lua +++ b/spec/03-plugins/06-statsd/01-log_spec.lua @@ -7,268 +7,270 @@ describe("Plugin: statsd (log)", function() helpers.run_migrations() local consumer1 = assert(helpers.dao.consumers:insert { - username = "bob", - custom_id = "robert" + username = "bob", + custom_id = "robert", }) assert(helpers.dao.keyauth_credentials:insert { - key = "kong", - consumer_id = consumer1.id + key = "kong", + consumer_id = consumer1.id, }) local api1 = assert(helpers.dao.apis:insert { - name = "stastd1", - hosts = { "logging1.com" }, - upstream_url = "http://mockbin.com" + name = "stastd1", + hosts = { "logging1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api1.id + name = "key-auth", + api_id = api1.id, }) local api2 = assert(helpers.dao.apis:insert { - name = "stastd2", - hosts = { "logging2.com" }, - upstream_url = "http://mockbin.com" + name = "stastd2", + hosts = { "logging2.com" }, + upstream_url = helpers.mock_upstream_url, }) local api3 = assert(helpers.dao.apis:insert { - name = "stastd3", - hosts = { "logging3.com" }, - upstream_url = "http://mockbin.com" + name = "stastd3", + hosts = { "logging3.com" }, + upstream_url = helpers.mock_upstream_url, }) local api4 = assert(helpers.dao.apis:insert { - name = "stastd4", - hosts = { "logging4.com" }, - upstream_url = "http://mockbin.com" + name = "stastd4", + hosts = { "logging4.com" }, + upstream_url = helpers.mock_upstream_url, }) local api5 = assert(helpers.dao.apis:insert { - name = "stastd5", - hosts = { "logging5.com" }, - upstream_url = "http://mockbin.com" + name = "stastd5", + hosts = { "logging5.com" }, + upstream_url = helpers.mock_upstream_url, }) local api6 = assert(helpers.dao.apis:insert { - name = "stastd6", - hosts = { "logging6.com" }, - upstream_url = "http://mockbin.com" + name = "stastd6", + hosts = { "logging6.com" }, + upstream_url = helpers.mock_upstream_url, }) local api7 = assert(helpers.dao.apis:insert { - name = "stastd7", - hosts = { "logging7.com" }, - upstream_url = "http://mockbin.com" + name = "stastd7", + hosts = { "logging7.com" }, + upstream_url = helpers.mock_upstream_url, }) local api8 = assert(helpers.dao.apis:insert { - name = "stastd8", - hosts = { "logging8.com" }, - upstream_url = "http://mockbin.com" + name = "stastd8", + hosts = { "logging8.com" }, + upstream_url = helpers.mock_upstream_url, }) local api9 = assert(helpers.dao.apis:insert { - name = "stastd9", - hosts = { "logging9.com" }, - upstream_url = "http://mockbin.com" + name = "stastd9", + hosts = { "logging9.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api9.id + name = "key-auth", + api_id = api9.id, }) local api10 = assert(helpers.dao.apis:insert { - name = "stastd10", - hosts = { "logging10.com" }, - upstream_url = "http://mockbin.com" + name = "stastd10", + hosts = { "logging10.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api10.id + name = "key-auth", + api_id = api10.id, }) local api11 = assert(helpers.dao.apis:insert { - name = "stastd11", - hosts = { "logging11.com" }, - upstream_url = "http://mockbin.com" + name = "stastd11", + hosts = { "logging11.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api11.id + name = "key-auth", + api_id = api11.id, }) local api12 = assert(helpers.dao.apis:insert { - name = "stastd12", - hosts = { "logging12.com" }, - upstream_url = "http://mockbin.com" + name = "stastd12", + hosts = { "logging12.com" }, + upstream_url = helpers.mock_upstream_url, }) local api13 = assert(helpers.dao.apis:insert { - name = "stastd13", - hosts = { "logging13.com" }, - upstream_url = "http://mockbin.com" + name = "stastd13", + hosts = { "logging13.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api12.id + name = "key-auth", + api_id = api12.id, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api13.id + name = "key-auth", + api_id = api13.id, }) assert(helpers.dao.plugins:insert { api_id = api1.id, - name = "statsd", + name = "statsd", config = { host = "127.0.0.1", - port = UDP_PORT - } + port = UDP_PORT, + }, }) assert(helpers.dao.plugins:insert { api_id = api2.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "latency", + name = "latency", stat_type = "timer" - }} - } + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api3.id, - name = "statsd", - config = { - host = "127.0.0.1", - port = UDP_PORT, + name = "statsd", + config = { + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "status_count", - stat_type = "counter", - sample_rate = 1 - }} - } + name = "status_count", + stat_type = "counter", + sample_rate = 1, + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api4.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "request_size", - stat_type = "timer" - }} - } + name = "request_size", + stat_type = "timer", + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api5.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "request_count", - stat_type = "counter", - sample_rate = 1 + name = "request_count", + stat_type = "counter", + sample_rate = 1, }} } }) assert(helpers.dao.plugins:insert { api_id = api6.id, - name = "statsd", - config = { - host = "127.0.0.1", - port = UDP_PORT, + name = "statsd", + config = { + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "response_size", - stat_type = "timer" - }} - } + name = "response_size", + stat_type = "timer", + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api7.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "upstream_latency", - stat_type = "timer" - }} - } + name = "upstream_latency", + stat_type = "timer", + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api8.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "kong_latency", - stat_type = "timer" - }} + name = "kong_latency", + stat_type = "timer", + }}, } }) assert(helpers.dao.plugins:insert { api_id = api9.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "unique_users", - stat_type = "set", - consumer_identifier = "custom_id" - }} - } + name = "unique_users", + stat_type = "set", + consumer_identifier = "custom_id", + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api10.id, - name = "statsd", + name = "statsd", config = { host = "127.0.0.1", port = UDP_PORT, metrics = {{ - name = "status_count_per_user", - stat_type = "counter", + name = "status_count_per_user", + stat_type = "counter", consumer_identifier = "custom_id", - sample_rate = 1 - }} - } + sample_rate = 1, + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api11.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "request_per_user", - stat_type = "counter", + name = "request_per_user", + stat_type = "counter", consumer_identifier = "username", - sample_rate = 1 - }} - } + sample_rate = 1, + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api12.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, + host = "127.0.0.1", + port = UDP_PORT, metrics = {{ - name = "latency", - stat_type = "gauge", - sample_rate = 1 - }} - } + name = "latency", + stat_type = "gauge", + sample_rate = 1, + }}, + }, }) assert(helpers.dao.plugins:insert { api_id = api13.id, - name = "statsd", + name = "statsd", config = { - host = "127.0.0.1", - port = UDP_PORT, - prefix = "prefix" - } + host = "127.0.0.1", + port = UDP_PORT, + prefix = "prefix", + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) diff --git a/spec/03-plugins/07-loggly/01-log_spec.lua b/spec/03-plugins/07-loggly/01-log_spec.lua index 9fc5c00f264d..d0dc89b33175 100644 --- a/spec/03-plugins/07-loggly/01-log_spec.lua +++ b/spec/03-plugins/07-loggly/01-log_spec.lua @@ -9,72 +9,75 @@ describe("Plugin: loggly (log)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "logging.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "logging.com" }, + upstream_url = helpers.mock_upstream_url, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "logging1.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "logging1.com" }, + upstream_url = helpers.mock_upstream_url, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "logging2.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "logging2.com" }, + upstream_url = helpers.mock_upstream_url, }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "logging3.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "logging3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { api_id = api1.id, - name = "loggly", + name = "loggly", config = { - host = "127.0.0.1", - port = UDP_PORT, - key = "123456789", - log_level = "info", + host = "127.0.0.1", + port = UDP_PORT, + key = "123456789", + log_level = "info", successful_severity = "warning" } }) - assert(helpers.dao.plugins:insert { + + assert(helpers.dao.plugins:insert { api_id = api2.id, - name = "loggly", + name = "loggly", config = { - host = "127.0.0.1", - port = UDP_PORT, - key = "123456789", - log_level = "debug", - timeout = 2000, - successful_severity = "info" + host = "127.0.0.1", + port = UDP_PORT, + key = "123456789", + log_level = "debug", + timeout = 2000, + successful_severity = "info", } }) - assert(helpers.dao.plugins:insert { + assert(helpers.dao.plugins:insert { api_id = api3.id, - name = "loggly", + name = "loggly", config = { - host = "127.0.0.1", - port = UDP_PORT, - key = "123456789", - log_level = "crit", - successful_severity = "crit", - client_errors_severity = "warning" + host = "127.0.0.1", + port = UDP_PORT, + key = "123456789", + log_level = "crit", + successful_severity = "crit", + client_errors_severity = "warning", } }) - assert(helpers.dao.plugins:insert { + assert(helpers.dao.plugins:insert { api_id = api4.id, - name = "loggly", + name = "loggly", config = { host = "127.0.0.1", port = UDP_PORT, - key = "123456789" + key = "123456789" } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() helpers.stop_kong() @@ -164,7 +167,7 @@ describe("Plugin: loggly (log)", function() it("logs to UDP when severity and log level are default values and response status is 401", function() local pri, message = run({ method = "GET", - path = "/status/401/", + path = "/status/401", headers = { host = "logging3.com" } @@ -175,7 +178,7 @@ describe("Plugin: loggly (log)", function() it("logs to UDP when severity and log level are default values and response status is 500", function() local pri, message = run({ method = "GET", - path = "/status/500/", + path = "/status/500", headers = { host = "logging3.com" } diff --git a/spec/03-plugins/08-datadog/01-log_spec.lua b/spec/03-plugins/08-datadog/01-log_spec.lua index c35ea5c74429..70cc7365c615 100644 --- a/spec/03-plugins/08-datadog/01-log_spec.lua +++ b/spec/03-plugins/08-datadog/01-log_spec.lua @@ -17,100 +17,102 @@ describe("Plugin: datadog (log)", function() }) local api1 = assert(helpers.dao.apis:insert { - name = "dd1", - hosts = { "datadog1.com" }, - upstream_url = "http://mockbin.com" + name = "dd1", + hosts = { "datadog1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api1.id }) - local api2 = assert(helpers.dao.apis:insert { - name = "dd2", - hosts = { "datadog2.com" }, - upstream_url = "http://mockbin.com" + local api2 = assert(helpers.dao.apis:insert { + name = "dd2", + hosts = { "datadog2.com" }, + upstream_url = helpers.mock_upstream_url, }) - local api3 = assert(helpers.dao.apis:insert { - name = "dd3", - hosts = { "datadog3.com" }, - upstream_url = "http://mockbin.com" + local api3 = assert(helpers.dao.apis:insert { + name = "dd3", + hosts = { "datadog3.com" }, + upstream_url = helpers.mock_upstream_url, }) - local api4 = assert(helpers.dao.apis:insert { - name = "dd4", - hosts = { "datadog4.com" }, - upstream_url = "http://mockbin.com" + local api4 = assert(helpers.dao.apis:insert { + name = "dd4", + hosts = { "datadog4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api4.id }) assert(helpers.dao.plugins:insert { - name = "datadog", + name = "datadog", api_id = api1.id, config = { host = "127.0.0.1", - port = 9999 - } + port = 9999, + }, }) assert(helpers.dao.plugins:insert { - name = "datadog", + name = "datadog", api_id = api2.id, config = { - host = "127.0.0.1", - port = 9999, + host = "127.0.0.1", + port = 9999, metrics = { { - name = "status_count", - stat_type = "counter", - sample_rate = 1 + name = "status_count", + stat_type = "counter", + sample_rate = 1, }, { - name = "request_count", - stat_type = "counter", - sample_rate = 1 - } - } - } + name = "request_count", + stat_type = "counter", + sample_rate = 1, + }, + }, + }, }) assert(helpers.dao.plugins:insert { - name = "datadog", + name = "datadog", api_id = api3.id, config = { - host = "127.0.0.1", - port = 9999, + host = "127.0.0.1", + port = 9999, metrics = { { - name = "status_count", - stat_type = "counter", + name = "status_count", + stat_type = "counter", sample_rate = 1, - tags = {"T1:V1"}, + tags = {"T1:V1"}, }, { - name = "request_count", - stat_type = "counter", + name = "request_count", + stat_type = "counter", sample_rate = 1, - tags = {"T2:V2,T3:V3,T4"}, + tags = {"T2:V2,T3:V3,T4"}, }, { - name = "latency", - stat_type = "gauge", + name = "latency", + stat_type = "gauge", sample_rate = 1, - tags = {"T2:V2:V3,T4"} - } - } - } + tags = {"T2:V2:V3,T4"}, + }, + }, + }, }) assert(helpers.dao.plugins:insert { - name = "datadog", + name = "datadog", api_id = api4.id, config = { - host = "127.0.0.1", - port = 9999, - prefix = "prefix" - } + host = "127.0.0.1", + port = 9999, + prefix = "prefix", + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) teardown(function() @@ -138,7 +140,7 @@ describe("Plugin: datadog (log)", function() local res = assert(client:send { method = "GET", - path = "/status/200/?apikey=kong", + path = "/status/200?apikey=kong", headers = { ["Host"] = "datadog1.com" } @@ -184,7 +186,7 @@ describe("Plugin: datadog (log)", function() local res = assert(client:send { method = "GET", - path = "/status/200/?apikey=kong", + path = "/status/200?apikey=kong", headers = { ["Host"] = "datadog4.com" } @@ -305,7 +307,7 @@ describe("Plugin: datadog (log)", function() }) assert.res_status(404, res) - + local err_log = pl_file.read(helpers.test_conf.nginx_err_logs) assert.not_matches("attempt to index field 'api' (a nil value)", err_log, nil, true) end) diff --git a/spec/03-plugins/09-galileo/01-alf_spec.lua b/spec/03-plugins/09-galileo/01-alf_spec.lua index a4deae98e070..a096c460e80e 100644 --- a/spec/03-plugins/09-galileo/01-alf_spec.lua +++ b/spec/03-plugins/09-galileo/01-alf_spec.lua @@ -49,19 +49,19 @@ describe("ALF serializer", function() before_each(function() _ngx = { status = 200, - var = { + var = { server_protocol = "HTTP/1.1", - scheme = "https", - host = "mockbin.com", - request_uri = "/request/path", - request_length = 32, - remote_addr = "127.0.0.1" + scheme = "https", + host = "example.com", + request_uri = "/request/path", + request_length = 32, + remote_addr = "127.0.0.1", }, - ctx = { + ctx = { KONG_PROXY_LATENCY = 3, - KONG_WAITING_TIME = 15, - KONG_RECEIVE_TIME = 25 - } + KONG_WAITING_TIME = 15, + KONG_RECEIVE_TIME = 25, + }, } end) it("sanity", function() @@ -107,7 +107,7 @@ describe("ALF serializer", function() local entry = assert(alf:add_entry(_ngx)) assert.equal("HTTP/1.1", entry.request.httpVersion) assert.equal("GET", entry.request.method) - assert.equal("https://mockbin.com/request/path", entry.request.url) + assert.equal("https://example.com/request/path", entry.request.url) assert.is_table(entry.request.headers) assert.is_table(entry.request.queryString) --assert.is_table(entry.request.postData) -- none by default @@ -125,7 +125,7 @@ describe("ALF serializer", function() local alf = alf_serializer.new() local entry = assert(alf:add_entry(_ngx)) assert.contains({name = "accept", value = "application/json"}, entry.request.headers) - assert.contains({name = "host", value = "mockbin.com"}, entry.request.headers) + assert.contains({name = "host", value = "example.com"}, entry.request.headers) assert.equal(118, entry.request.headersSize) end) it("handles headers with multiple values", function() @@ -141,7 +141,7 @@ describe("ALF serializer", function() }, entry.request.headers) assert.contains({ name = "host", - value = "mockbin.com" + value = "example.com" }, entry.request.headers) end) end) @@ -614,7 +614,7 @@ describe("ALF serializer", function() local json_encoded_alf = assert(alf:serialize("abcd", "test")) assert.matches([["value":"application/json"]], json_encoded_alf, nil, true) assert.matches([["httpVersion":"HTTP/1.1"]], json_encoded_alf, nil, true) - assert.matches([["url":"https://mockbin.com/request/path"]], json_encoded_alf, nil, true) + assert.matches([["url":"https://example.com/request/path"]], json_encoded_alf, nil, true) end) end) diff --git a/spec/03-plugins/09-galileo/02-buffer_spec.lua b/spec/03-plugins/09-galileo/02-buffer_spec.lua index 42bfce3ee9a4..45f4e5fe9013 100644 --- a/spec/03-plugins/09-galileo/02-buffer_spec.lua +++ b/spec/03-plugins/09-galileo/02-buffer_spec.lua @@ -14,33 +14,33 @@ describe("ALF Buffer", function() reload_buffer() conf = { - server_addr = "10.10.10.10", - service_token = "abcd", - environment = "test", - log_bodies = false, - retry_count = 0, + server_addr = "10.10.10.10", + service_token = "abcd", + environment = "test", + log_bodies = false, + retry_count = 0, connection_timeout = 30, - flush_timeout = 2, - queue_size = 1000, - host = "collector.galileo.mashape.com", - port = 443 + flush_timeout = 2, + queue_size = 1000, + host = "collector.galileo.mashape.com", + port = 443, } _ngx = { status = 200, - var = { - scheme = "https", - host = "mockbin.com", - request_uri = "/request/path", + var = { + scheme = "https", + host = "example.com", + request_uri = "/request/path", request_length = 32, - remote_addr = "127.0.0.1", - server_addr = "10.10.10.10" + remote_addr = "127.0.0.1", + server_addr = "10.10.10.10", }, - ctx = { + ctx = { KONG_PROXY_LATENCY = 3, - KONG_WAITING_TIME = 15, - KONG_RECEIVE_TIME = 25 - } + KONG_WAITING_TIME = 15, + KONG_RECEIVE_TIME = 25, + }, } end) diff --git a/spec/03-plugins/09-galileo/ngx.lua b/spec/03-plugins/09-galileo/ngx.lua index 5a7f855fe3bb..25d50fbb0680 100644 --- a/spec/03-plugins/09-galileo/ngx.lua +++ b/spec/03-plugins/09-galileo/ngx.lua @@ -14,14 +14,14 @@ return { http_version = function() return 1.1 end, raw_header = function () return "GET /request/path HTTP/1.1\r\n".. - "Host: mockbin.com\r\n".. + "Host: example.com\r\n".. "Accept: application/json\r\n".. "Accept: application/x-www-form-urlencoded\r\n\r\n" end, get_headers = function() return { accept = {"application/json", "application/x-www-form-urlencoded"}, - host = "mockbin.com" + host = "example.com" } end, get_uri_args = function() diff --git a/spec/03-plugins/10-key-auth/01-api_spec.lua b/spec/03-plugins/10-key-auth/01-api_spec.lua index 4cb19a42be2b..91dbf5a007d1 100644 --- a/spec/03-plugins/10-key-auth/01-api_spec.lua +++ b/spec/03-plugins/10-key-auth/01-api_spec.lua @@ -8,19 +8,21 @@ describe("Plugin: key-auth (API)", function() helpers.run_migrations() assert(helpers.dao.apis:insert { - name = "keyauth1", - upstream_url = "http://mockbin.com", - hosts = { "keyauth1.test" }, + name = "keyauth1", + upstream_url = helpers.mock_upstream_url, + hosts = { "keyauth1.test" }, }) assert(helpers.dao.apis:insert { - name = "keyauth2", - upstream_url = "http://mockbin.com", - hosts = { "keyauth2.test" }, + name = "keyauth2", + upstream_url = helpers.mock_upstream_url, + hosts = { "keyauth2.test" }, }) consumer = assert(helpers.dao.consumers:insert { username = "bob" }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) admin_client = helpers.admin_client() end) teardown(function() diff --git a/spec/03-plugins/10-key-auth/02-access_spec.lua b/spec/03-plugins/10-key-auth/02-access_spec.lua index 377f4308bd24..93560043b6af 100644 --- a/spec/03-plugins/10-key-auth/02-access_spec.lua +++ b/spec/03-plugins/10-key-auth/02-access_spec.lua @@ -9,94 +9,96 @@ describe("Plugin: key-auth (access)", function() helpers.run_migrations() local anonymous_user = assert(helpers.dao.consumers:insert { - username = "no-body" + username = "no-body", }) - + local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "key-auth1.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "key-auth1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api1.id + name = "key-auth", + api_id = api1.id, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "key-auth2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "key-auth2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api2.id, config = { - hide_credentials = true - } + hide_credentials = true, + }, }) local consumer1 = assert(helpers.dao.consumers:insert { username = "bob" }) assert(helpers.dao.keyauth_credentials:insert { - key = "kong", - consumer_id = consumer1.id + key = "kong", + consumer_id = consumer1.id, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "key-auth3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "key-auth3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api3.id, config = { - anonymous = anonymous_user.id - } + anonymous = anonymous_user.id, + }, }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "key-auth4.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "key-auth4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api4.id, config = { - anonymous = utils.uuid() -- unknown consumer - } + anonymous = utils.uuid(), -- unknown consumer + }, }) local api5 = assert(helpers.dao.apis:insert { - name = "api-5", - hosts = { "key-auth5.com" }, - upstream_url = "http://mockbin.com" + name = "api-5", + hosts = { "key-auth5.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api5.id, config = { key_in_body = true, - } + }, }) local api6 = assert(helpers.dao.apis:insert { - name = "api-6", - hosts = { "key-auth6.com" }, - upstream_url = "http://mockbin.com" + name = "api-6", + hosts = { "key-auth6.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "key-auth", api_id = api6.id, config = { - key_in_body = true, + key_in_body = true, hide_credentials = true, - } + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) teardown(function() @@ -168,7 +170,7 @@ describe("Plugin: key-auth (access)", function() end) describe("key in request body", function() - for _, type in pairs({ "application/www-form-urlencoded", "application/json", "multipart/form-data" }) do + for _, type in pairs({ "application/x-www-form-urlencoded", "application/json", "multipart/form-data" }) do describe(type, function() it("authenticates valid credentials", function() local res = assert(client:send { @@ -268,79 +270,60 @@ describe("Plugin: key-auth (access)", function() describe("config.hide_credentials", function() local harness = { - queryString = { + uri_args = { -- query string { headers = { Host = "key-auth1.com" }, - path = "/request?apikey=kong", - method = "GET", + path = "/request?apikey=kong", + method = "GET", }, { headers = { Host = "key-auth2.com" }, - path = "/request?apikey=kong", - method = "GET", + path = "/request?apikey=kong", + method = "GET", } }, headers = { { headers = { Host = "key-auth1.com", ["apikey"] = "kong" }, - path = "/request", - method = "GET", + path = "/request", + method = "GET", }, { headers = { Host = "key-auth2.com", ["apikey"] = "kong" }, - path = "/request", - method = "GET", - } + path = "/request", + method = "GET", + }, }, - postData = { + ["post_data.params"] = { { - headers = { ["Host"] = "key-auth5.com", ["Content-Type"] = "application/www-form-urlencoded" }, - body = { apikey = "kong" }, - method = "POST", - path = "/request", + headers = { ["Host"] = "key-auth5.com", ["Content-Type"] = "application/x-www-form-urlencoded" }, + body = { apikey = "kong" }, + method = "POST", + path = "/request", }, { - headers = { ["Host"] = "key-auth6.com", ["Content-Type"] = "application/www-form-urlencoded" }, - body = { apikey = "kong" }, - method = "POST", - path = "/request", - } + headers = { ["Host"] = "key-auth6.com", ["Content-Type"] = "application/x-www-form-urlencoded" }, + body = { apikey = "kong" }, + method = "POST", + path = "/request", + }, } } for type, _ in pairs(harness) do describe(type, function() it("false sends key to upstream", function() - local res = assert(client:send(harness[type][1])) - local body = assert.res_status(200, res) - local json = cjson.decode(body) - - -- small workaround for how mockbin sends body data - local field - if type == "postData" then - local t = json[type].text:sub(8) - field = { apikey = t ~= "" and t or nil } - - else - field = json[type] - end - + local res = assert(client:send(harness[type][1])) + local body = assert.res_status(200, res) + local json = cjson.decode(body) + local field = type == "post_data.params" and json.post_data.params or json[type] assert.equal("kong", field.apikey) end) it("true doesn't send key to upstream", function() - local res = assert(client:send(harness[type][2])) - local body = assert.res_status(200, res) - local json = cjson.decode(body) - - local field - if type == "postData" then - local t = json[type].text:sub(8) - field = { apikey = t ~= "" and t or nil } - - else - field = json[type] - end - + local res = assert(client:send(harness[type][2])) + local body = assert.res_status(200, res) + local json = cjson.decode(body) + local field = type == "post_data.params" and json.post_data.params or json[type] assert.is_nil(field.apikey) end) end) @@ -392,60 +375,62 @@ describe("Plugin: key-auth (access)", function() setup(function() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "logical-and.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-1", + hosts = { "logical-and.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { - name = "basic-auth", - api_id = api1.id + name = "basic-auth", + api_id = api1.id, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api1.id + name = "key-auth", + api_id = api1.id, }) anonymous = assert(helpers.dao.consumers:insert { - username = "Anonymous" + username = "Anonymous", }) user1 = assert(helpers.dao.consumers:insert { - username = "Mickey" + username = "Mickey", }) user2 = assert(helpers.dao.consumers:insert { - username = "Aladdin" + username = "Aladdin", }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "logical-or.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-2", + hosts = { "logical-or.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { - name = "basic-auth", + name = "basic-auth", api_id = api2.id, config = { - anonymous = anonymous.id - } + anonymous = anonymous.id, + }, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api2.id, config = { - anonymous = anonymous.id - } + anonymous = anonymous.id, + }, }) assert(helpers.dao.keyauth_credentials:insert { - key = "Mouse", - consumer_id = user1.id + key = "Mouse", + consumer_id = user1.id, }) assert(helpers.dao.basicauth_credentials:insert { - username = "Aladdin", - password = "OpenSesame", - consumer_id = user2.id + username = "Aladdin", + password = "OpenSesame", + consumer_id = user2.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) diff --git a/spec/03-plugins/10-key-auth/03-invalidations_spec.lua b/spec/03-plugins/10-key-auth/03-invalidations_spec.lua index 32b16fa81c94..79ffc675083c 100644 --- a/spec/03-plugins/10-key-auth/03-invalidations_spec.lua +++ b/spec/03-plugins/10-key-auth/03-invalidations_spec.lua @@ -8,24 +8,26 @@ describe("Plugin: key-auth (invalidations)", function() helpers.dao:truncate_tables() helpers.run_migrations() local api = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "key-auth.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "key-auth.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api.id + name = "key-auth", + api_id = api.id, }) local consumer = assert(helpers.dao.consumers:insert { - username = "bob" + username = "bob", }) assert(helpers.dao.keyauth_credentials:insert { - key = "kong", - consumer_id = consumer.id + key = "kong", + consumer_id = consumer.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) proxy_client = helpers.proxy_client() admin_client = helpers.admin_client() end) diff --git a/spec/03-plugins/11-basic-auth/03-access_spec.lua b/spec/03-plugins/11-basic-auth/03-access_spec.lua index f787e8302a99..ab36697c1e69 100644 --- a/spec/03-plugins/11-basic-auth/03-access_spec.lua +++ b/spec/03-plugins/11-basic-auth/03-access_spec.lua @@ -11,72 +11,74 @@ describe("Plugin: basic-auth (access)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "basic-auth1.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "basic-auth1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "basic-auth", - api_id = api1.id + name = "basic-auth", + api_id = api1.id, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "basic-auth2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "basic-auth2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "basic-auth", + name = "basic-auth", api_id = api2.id, config = { - hide_credentials = true - } + hide_credentials = true, + }, }) local consumer = assert(helpers.dao.consumers:insert { - username = "bob" + username = "bob", }) local anonymous_user = assert(helpers.dao.consumers:insert { - username = "no-body" + username = "no-body", }) assert(helpers.dao.basicauth_credentials:insert { - username = "bob", - password = "kong", - consumer_id = consumer.id + username = "bob", + password = "kong", + consumer_id = consumer.id, }) assert(helpers.dao.basicauth_credentials:insert { - username = "user123", - password = "password123", - consumer_id = consumer.id + username = "user123", + password = "password123", + consumer_id = consumer.id, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "basic-auth3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "basic-auth3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "basic-auth", + name = "basic-auth", api_id = api3.id, config = { - anonymous = anonymous_user.id - } + anonymous = anonymous_user.id, + }, }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "basic-auth4.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "basic-auth4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "basic-auth", + name = "basic-auth", api_id = api4.id, config = { - anonymous = utils.uuid() -- a non-existing consumer id - } + anonymous = utils.uuid(), -- a non-existing consumer id + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) @@ -332,60 +334,62 @@ describe("Plugin: basic-auth (access)", function() setup(function() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "logical-and.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-1", + hosts = { "logical-and.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { - name = "basic-auth", - api_id = api1.id + name = "basic-auth", + api_id = api1.id, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api1.id + name = "key-auth", + api_id = api1.id, }) anonymous = assert(helpers.dao.consumers:insert { - username = "Anonymous" + username = "Anonymous", }) user1 = assert(helpers.dao.consumers:insert { - username = "Mickey" + username = "Mickey", }) user2 = assert(helpers.dao.consumers:insert { - username = "Aladdin" + username = "Aladdin", }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "logical-or.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-2", + hosts = { "logical-or.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { - name = "basic-auth", + name = "basic-auth", api_id = api2.id, config = { - anonymous = anonymous.id - } + anonymous = anonymous.id, + }, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api2.id, config = { - anonymous = anonymous.id - } + anonymous = anonymous.id, + }, }) assert(helpers.dao.keyauth_credentials:insert { - key = "Mouse", - consumer_id = user1.id + key = "Mouse", + consumer_id = user1.id, }) assert(helpers.dao.basicauth_credentials:insert { - username = "Aladdin", - password = "OpenSesame", - consumer_id = user2.id + username = "Aladdin", + password = "OpenSesame", + consumer_id = user2.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) diff --git a/spec/03-plugins/11-basic-auth/04-invalidations_spec.lua b/spec/03-plugins/11-basic-auth/04-invalidations_spec.lua index ba3e3c173694..c5b8f327698c 100644 --- a/spec/03-plugins/11-basic-auth/04-invalidations_spec.lua +++ b/spec/03-plugins/11-basic-auth/04-invalidations_spec.lua @@ -8,25 +8,27 @@ describe("Plugin: basic-auth (invalidations)", function() helpers.dao:truncate_tables() helpers.run_migrations() local api = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "basic-auth.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "basic-auth.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "basic-auth", - api_id = api.id + name = "basic-auth", + api_id = api.id, }) local consumer = assert(helpers.dao.consumers:insert { - username = "bob" + username = "bob", }) assert(helpers.dao.basicauth_credentials:insert { - username = "bob", - password = "kong", - consumer_id = consumer.id + username = "bob", + password = "kong", + consumer_id = consumer.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) proxy_client = helpers.proxy_client() admin_client = helpers.admin_client() end) diff --git a/spec/03-plugins/12-correlation-id/01-access_spec.lua b/spec/03-plugins/12-correlation-id/01-access_spec.lua index 0c61eebed776..4f98370f4468 100644 --- a/spec/03-plugins/12-correlation-id/01-access_spec.lua +++ b/spec/03-plugins/12-correlation-id/01-access_spec.lua @@ -10,55 +10,57 @@ describe("Plugin: correlation-id (access)", function() setup(function() helpers.run_migrations() - local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "correlation1.com" }, - upstream_url = "http://mockbin.com" + local api1 = assert(helpers.dao.apis:insert { + name = "api-1", + hosts = { "correlation1.com" }, + upstream_url = helpers.mock_upstream_url, }) - local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "correlation2.com" }, - upstream_url = "http://mockbin.com" + local api2 = assert(helpers.dao.apis:insert { + name = "api-2", + hosts = { "correlation2.com" }, + upstream_url = helpers.mock_upstream_url, }) - local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "correlation3.com" }, - upstream_url = "http://mockbin.com" + local api3 = assert(helpers.dao.apis:insert { + name = "api-3", + hosts = { "correlation3.com" }, + upstream_url = helpers.mock_upstream_url, }) - local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "correlation-tracker.com" }, - upstream_url = "http://mockbin.com" + local api4 = assert(helpers.dao.apis:insert { + name = "api-4", + hosts = { "correlation-tracker.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "correlation-id", - api_id = api1.id + name = "correlation-id", + api_id = api1.id, }) assert(helpers.dao.plugins:insert { - name = "correlation-id", + name = "correlation-id", api_id = api2.id, config = { - header_name = "Foo-Bar-Id" - } + header_name = "Foo-Bar-Id", + }, }) assert(helpers.dao.plugins:insert { - name = "correlation-id", + name = "correlation-id", api_id = api3.id, config = { - generator = "uuid", - echo_downstream = true - } + generator = "uuid", + echo_downstream = true, + }, }) assert(helpers.dao.plugins:insert { - name = "correlation-id", + name = "correlation-id", api_id = api4.id, config = { - generator = "tracker" - } + generator = "tracker", + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) @@ -78,7 +80,7 @@ describe("Plugin: correlation-id (access)", function() }) local body = assert.res_status(200, res) local json = cjson.decode(body) - local id1 = json.headers["kong-request-id"] -- header received by upstream (mockbin) + local id1 = json.headers["kong-request-id"] -- header received by upstream (mock_upstream) assert.matches(UUID_COUNTER_PATTERN, id1) res = assert(client:send { @@ -90,7 +92,7 @@ describe("Plugin: correlation-id (access)", function() }) body = assert.res_status(200, res) json = cjson.decode(body) - local id2 = json.headers["kong-request-id"] -- header received by upstream (mockbin) + local id2 = json.headers["kong-request-id"] -- header received by upstream (mock_upstream) assert.matches(UUID_COUNTER_PATTERN, id2) assert.not_equal(id1, id2) @@ -115,7 +117,7 @@ describe("Plugin: correlation-id (access)", function() }) local body = assert.res_status(200, res) local json = cjson.decode(body) - local id1 = json.headers["kong-request-id"] -- header received by upstream (mockbin) + local id1 = json.headers["kong-request-id"] -- header received by upstream (mock_upstream) assert.matches(UUID_PATTERN, id1) res = assert(client:send { @@ -127,7 +129,7 @@ describe("Plugin: correlation-id (access)", function() }) body = assert.res_status(200, res) json = cjson.decode(body) - local id2 = json.headers["kong-request-id"] -- header received by upstream (mockbin) + local id2 = json.headers["kong-request-id"] -- header received by upstream (mock_upstream) assert.matches(UUID_PATTERN, id2) assert.not_equal(id1, id2) end) @@ -173,7 +175,7 @@ describe("Plugin: correlation-id (access)", function() }) local body = assert.res_status(200, res) local json = cjson.decode(body) - local upstream_id = json.headers["kong-request-id"] -- header received by upstream (mockbin) + local upstream_id = json.headers["kong-request-id"] -- header received by upstream (mock_upstream) local downstream_id = res.headers["kong-request-id"] -- header received by downstream (client) assert.matches(UUID_PATTERN, upstream_id) assert.equal(upstream_id, downstream_id) @@ -199,7 +201,7 @@ describe("Plugin: correlation-id (access)", function() }) local body = assert.res_status(200, res) local json = cjson.decode(body) - local id = json.headers["foo-bar-id"] -- header received by upstream (mockbin) + local id = json.headers["foo-bar-id"] -- header received by upstream (mock_upstream) assert.matches(UUID_PATTERN, id) end) end) diff --git a/spec/03-plugins/13-request-size-limiting/01-access_spec.lua b/spec/03-plugins/13-request-size-limiting/01-access_spec.lua index 5ec6c585739e..1ef805359fe3 100644 --- a/spec/03-plugins/13-request-size-limiting/01-access_spec.lua +++ b/spec/03-plugins/13-request-size-limiting/01-access_spec.lua @@ -11,19 +11,21 @@ describe("Plugin: request-size-limiting (access)", function() helpers.run_migrations() local api = assert(helpers.dao.apis:insert { - name = "limit.com", - hosts = { "limit.com" }, - upstream_url = "http://mockbin.com" + name = "limit.com", + hosts = { "limit.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "request-size-limiting", + name = "request-size-limiting", api_id = api.id, config = { allowed_payload_size = TEST_SIZE } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) diff --git a/spec/03-plugins/14-cors/01-access_spec.lua b/spec/03-plugins/14-cors/01-access_spec.lua index b043fbb010a0..e871378b2841 100644 --- a/spec/03-plugins/14-cors/01-access_spec.lua +++ b/spec/03-plugins/14-cors/01-access_spec.lua @@ -8,49 +8,49 @@ describe("Plugin: cors (access)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "cors1.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "cors1.com" }, + upstream_url = helpers.mock_upstream_url, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "cors2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "cors2.com" }, + upstream_url = helpers.mock_upstream_url, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "cors3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "cors3.com" }, + upstream_url = helpers.mock_upstream_url, }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "cors4.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "cors4.com" }, + upstream_url = helpers.mock_upstream_url, }) local api5 = assert(helpers.dao.apis:insert { - name = "api-5", - hosts = { "cors5.com" }, - upstream_url = "http://mockbin.com" + name = "api-5", + hosts = { "cors5.com" }, + upstream_url = helpers.mock_upstream_url, }) local api6 = assert(helpers.dao.apis:insert { - name = "api-6", - hosts = { "cors6.com" }, - upstream_url = "http://mockbin.com" + name = "api-6", + hosts = { "cors6.com" }, + upstream_url = helpers.mock_upstream_url, }) local api7 = assert(helpers.dao.apis:insert { - name = "api-7", - hosts = { "cors7.com" }, - upstream_url = "http://mockbin.com" + name = "api-7", + hosts = { "cors7.com" }, + upstream_url = helpers.mock_upstream_url, }) local api8 = assert(helpers.dao.apis:insert { - name = "api-8", - hosts = { "cors-empty-origins.com" }, - upstream_url = "http://mockbin.com" + name = "api-8", + hosts = { "cors-empty-origins.com" }, + upstream_url = helpers.mock_upstream_url, }) local api9 = assert(helpers.dao.apis:insert { - name = "api-9", - hosts = { "cors9.com" }, - upstream_url = "http://mockbin.com" + name = "api-9", + hosts = { "cors9.com" }, + upstream_url = helpers.mock_upstream_url, }) @@ -141,7 +141,9 @@ describe("Plugin: cors (access)", function() } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) @@ -231,8 +233,7 @@ describe("Plugin: cors (access)", function() }) local body = assert.res_status(201, res) local json = cjson.decode(body) - assert.equal("201", json.code) - assert.equal("OK", json.message) + assert.equal(201, json.code) end) it("replies with request-headers if present in request", function() diff --git a/spec/03-plugins/15-request-transformer/02-access_spec.lua b/spec/03-plugins/15-request-transformer/02-access_spec.lua index 24151505e4fc..ae76e3fecf25 100644 --- a/spec/03-plugins/15-request-transformer/02-access_spec.lua +++ b/spec/03-plugins/15-request-transformer/02-access_spec.lua @@ -1,4 +1,5 @@ local helpers = require "spec.helpers" +local cjson = require "cjson" describe("Plugin: request-transformer (access)", function() local client @@ -6,15 +7,15 @@ describe("Plugin: request-transformer (access)", function() setup(function() helpers.run_migrations() - local api1 = assert(helpers.dao.apis:insert { name = "api-1", hosts = { "test1.com" }, upstream_url = "http://mockbin.com"}) - local api2 = assert(helpers.dao.apis:insert { name = "api-2", hosts = { "test2.com" }, upstream_url = "http://httpbin.org"}) - local api3 = assert(helpers.dao.apis:insert { name = "api-3", hosts = { "test3.com" }, upstream_url = "http://mockbin.com"}) - local api4 = assert(helpers.dao.apis:insert { name = "api-4", hosts = { "test4.com" }, upstream_url = "http://mockbin.com"}) - local api5 = assert(helpers.dao.apis:insert { name = "api-5", hosts = { "test5.com" }, upstream_url = "http://mockbin.com"}) - local api6 = assert(helpers.dao.apis:insert { name = "api-6", hosts = { "test6.com" }, upstream_url = "http://mockbin.com"}) - local api7 = assert(helpers.dao.apis:insert { name = "api-7", hosts = { "test7.com" }, upstream_url = "http://mockbin.com"}) - local api8 = assert(helpers.dao.apis:insert { name = "api-8", hosts = { "test8.com" }, upstream_url = "http://mockbin.com"}) - local api9 = assert(helpers.dao.apis:insert { name = "api-9", hosts = { "test9.com" }, upstream_url = "http://mockbin.com"}) + local api1 = assert(helpers.dao.apis:insert { name = "api-1", hosts = { "test1.com" }, upstream_url = helpers.mock_upstream_url}) + local api2 = assert(helpers.dao.apis:insert { name = "api-2", hosts = { "test2.com" }, upstream_url = helpers.mock_upstream_url}) + local api3 = assert(helpers.dao.apis:insert { name = "api-3", hosts = { "test3.com" }, upstream_url = helpers.mock_upstream_url}) + local api4 = assert(helpers.dao.apis:insert { name = "api-4", hosts = { "test4.com" }, upstream_url = helpers.mock_upstream_url}) + local api5 = assert(helpers.dao.apis:insert { name = "api-5", hosts = { "test5.com" }, upstream_url = helpers.mock_upstream_url}) + local api6 = assert(helpers.dao.apis:insert { name = "api-6", hosts = { "test6.com" }, upstream_url = helpers.mock_upstream_url}) + local api7 = assert(helpers.dao.apis:insert { name = "api-7", hosts = { "test7.com" }, upstream_url = helpers.mock_upstream_url}) + local api8 = assert(helpers.dao.apis:insert { name = "api-8", hosts = { "test8.com" }, upstream_url = helpers.mock_upstream_url}) + local api9 = assert(helpers.dao.apis:insert { name = "api-9", hosts = { "test9.com" }, upstream_url = helpers.mock_upstream_url}) assert(helpers.dao.plugins:insert { api_id = api1.id, @@ -119,7 +120,9 @@ describe("Plugin: request-transformer (access)", function() } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() @@ -145,9 +148,9 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equal("POST", json.method) - assert.equal("world", json.queryString.hello) - assert.equal("marco", json.queryString.name) + assert.equal("POST", json.vars.request_method) + assert.equal("world", json.uri_args.hello) + assert.equal("marco", json.uri_args.name) end) it("changes the HTTP method from POST to GET", function() local r = assert(client:send { @@ -163,10 +166,9 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equal("GET", json.method) - assert.equal("marco", json.postData.params.name) - assert.equal("world", json.queryString.hello) - assert.equal("marco", json.queryString.name) + assert.equal("GET", json.vars.request_method) + assert.equal("world", json.uri_args.hello) + assert.equal("marco", json.uri_args.name) end) end) describe("remove", function() @@ -220,8 +222,8 @@ describe("Plugin: request-transformer (access)", function() assert.response(r).has.status(200) assert.response(r).has.jsonbody() local json = assert.request(r).has.jsonbody() - assert.is_nil(json["toremoveform"]) - assert.equals("yes", json["nottoremove"]) + assert.is_nil(json.params["toremoveform"]) + assert.equals("yes", json.params["nottoremove"]) end) it("does not fail if JSON body is malformed in POST", function() local r = assert(client:send { @@ -235,7 +237,8 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equals("malformed json body", json.postData.text) + assert.equals("json (error)", json.post_data.kind) + assert.not_nil(json.post_data.error) end) it("does not fail if body is empty and content type is application/json in POST", function() local r = assert(client:send { @@ -249,21 +252,22 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equals('{}', json.postData.text) + assert.equals('{}', json.post_data.text) assert.equals("2", json.headers["content-length"]) end) it("does not fail if body is empty in POST", function() local r = assert(client:send { method = "POST", path = "/request", + body = "", headers = { host = "test4.com" } }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.same({}, json.postData.params) - assert.equal('', json.postData.text) + assert.same(cjson.null, json.post_data.params) + assert.equal('', json.post_data.text) local value = assert.request(r).has.header("content-length") assert.equal("0", value) end) @@ -281,11 +285,12 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.is_nil(json.postData.params["toremoveform"]) - assert.equals("yes", json.postData.params["nottoremove"]) + assert.response(r).has.jsonbody() + assert.request(r).has.no.formparam("toremoveform") + local value = assert.request(r).has.formparam("nottoremove") + assert.equals("yes", value) end) - it("queryString on GET if it exist", function() + it("args on GET if it exist", function() local r = assert( client:send { method = "GET", path = "/request", @@ -393,10 +398,10 @@ describe("Plugin: request-transformer (access)", function() assert.response(r).has.status(200) assert.response(r).has.jsonbody() local json = assert.request(r).has.jsonbody() - assert.is_nil(json["originalparam"]) - assert.is_not_nil(json["renamedparam"]) - assert.equals("yes", json["renamedparam"]) - assert.equals("yes", json["nottorename"]) + assert.is_nil(json.params["originalparam"]) + assert.is_not_nil(json.params["renamedparam"]) + assert.equals("yes", json.params["renamedparam"]) + assert.equals("yes", json.params["nottorename"]) end) it("does not fail if JSON body is malformed in POST", function() local r = assert(client:send { @@ -410,7 +415,8 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equals("malformed json body", json.postData.text) + assert.equals("json (error)", json.post_data.kind) + assert.is_not_nil(json.post_data.error) end) it("parameters on multipart POST", function() local r = assert(client:send { @@ -426,13 +432,14 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.is_nil(json.postData.params["originalparam"]) - assert.is_not_nil(json.postData.params["renamedparam"]) - assert.equals("yes", json.postData.params["renamedparam"]) - assert.equals("yes", json.postData.params["nottorename"]) + assert.response(r).has.jsonbody() + assert.request(r).has.no.formparam("originalparam") + local value = assert.request(r).has.formparam("renamedparam") + assert.equals("yes", value) + local value2 = assert.request(r).has.formparam("nottorename") + assert.equals("yes", value2) end) - it("queryString on GET if it exists", function() + it("args on GET if it exists", function() local r = assert( client:send { method = "GET", path = "/request", @@ -539,10 +546,10 @@ describe("Plugin: request-transformer (access)", function() assert.response(r).has.status(200) assert.response(r).has.jsonbody() local json = assert.request(r).has.jsonbody() - assert.is_nil(json["originalparam"]) - assert.is_not_nil(json["renamedparam"]) - assert.equals("yes", json["renamedparam"]) - assert.equals("yes", json["nottorename"]) + assert.is_nil(json.params["originalparam"]) + assert.is_not_nil(json.params["renamedparam"]) + assert.equals("yes", json.params["renamedparam"]) + assert.equals("yes", json.params["nottorename"]) end) it("does not fail if JSON body is malformed in POST", function() local r = assert(client:send { @@ -556,7 +563,8 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equals("malformed json body", json.postData.text) + assert.equals("json (error)", json.post_data.kind) + assert.is_not_nil(json.post_data.error) end) it("parameters on multipart POST", function() local r = assert(client:send { @@ -572,13 +580,14 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.is_nil(json.postData.params["originalparam"]) - assert.is_not_nil(json.postData.params["renamedparam"]) - assert.equals("yes", json.postData.params["renamedparam"]) - assert.equals("yes", json.postData.params["nottorename"]) + assert.response(r).has.jsonbody() + assert.request(r).has.no.formparam("originalparam") + local value = assert.request(r).has.formparam("renamedparam") + assert.equals("yes", value) + local value2 = assert.request(r).has.formparam("nottorename") + assert.equals("yes", value2) end) - it("queryString on GET if it exists", function() + it("args on GET if it exists", function() local r = assert( client:send { method = "GET", path = "/request", @@ -687,8 +696,8 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.request(r).has.jsonbody() - assert.equals("v1", json.p1) - assert.equals("v1", json.p2) + assert.equals("v1", json.params.p1) + assert.equals("v1", json.params.p2) end) it("does not fail if JSON body is malformed in POST", function() local r = assert(client:send { @@ -702,7 +711,8 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equal("malformed json body", json.postData.text) + assert.equal("json (error)", json.post_data.kind) + assert.is_not_nil(json.post_data.error) end) it("does not add as new parameter in json if parameter does not exist on POST", function() local r = assert(client:send { @@ -718,8 +728,8 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.request(r).has.jsonbody() - assert.is_nil(json.p1) - assert.equals("v1", json.p2) + assert.is_nil(json.params.p1) + assert.equals("v1", json.params.p2) end) it("specified parameters on multipart POST", function() local r = assert(client:send { @@ -735,9 +745,11 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.equals("v1", json.postData.params.p1) - assert.equals("v1", json.postData.params.p2) + assert.response(r).has.jsonbody() + local value = assert.request(r).has.formparam("p1") + assert.equals("v1", value) + local value2 = assert.request(r).has.formparam("p2") + assert.equals("v1", value2) end) it("does not add as new parameter if parameter does not exist on multipart POST", function() local r = assert(client:send { @@ -752,11 +764,14 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.is_nil(json.postData.params.p1) - assert.equals("v1", json.postData.params.p2) + assert.response(r).has.jsonbody() + + assert.request(r).has.no.formparam("p1") + + local value = assert.request(r).has.formparam("p2") + assert.equals("v1", value) end) - it("queryString on POST if it exist", function() + it("args on POST if it exist", function() local r = assert(client:send { method = "POST", path = "/request", @@ -778,7 +793,7 @@ describe("Plugin: request-transformer (access)", function() local value = assert.request(r).has.queryparam("q2") assert.equals("v2", value) end) - it("does not add new queryString on POST if it does not exist", function() + it("does not add new args on POST if it does not exist", function() local r = assert(client:send { method = "POST", path = "/request", @@ -882,7 +897,7 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local params = assert.request(r).has.jsonbody() + local params = assert.request(r).has.jsonbody().params assert.equals("world", params.hello) assert.equals("v1", params.p1) end) @@ -900,7 +915,7 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local params = assert.request(r).has.jsonbody() + local params = assert.request(r).has.jsonbody().params assert.equals("world", params.hello) assert.equals("this should not change", params.p1) end) @@ -916,7 +931,8 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equal("malformed json body", json.postData.text) + assert.equal("json (error)", json.post_data.kind) + assert.is_not_nil(json.post_data.error) end) it("new parameter on multipart POST", function() local r = assert(client:send { @@ -929,8 +945,9 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.equals("v1", json.postData.params.p1) + assert.response(r).has.jsonbody() + local value = assert.request(r).has.formparam("p1") + assert.equals("v1", value) end) it("does not change or append value to parameter on multipart POST when parameter exists", function() local r = assert(client:send { @@ -946,9 +963,12 @@ describe("Plugin: request-transformer (access)", function() }, }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.equals("this should not change", json.postData.params.p1) - assert.equals("world", json.postData.params.hello) + assert.response(r).has.jsonbody() + local value = assert.request(r).has.formparam("p1") + assert.equals("this should not change", value) + + local value2 = assert.request(r).has.formparam("hello") + assert.equals("world", value2) end) it("new querystring on GET", function() local r = assert( client:send { @@ -996,7 +1016,7 @@ describe("Plugin: request-transformer (access)", function() assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() local value = assert.has.header("host", json) - assert.equals("httpbin.org", value) + assert.equals(helpers.mock_upstream_host .. ':' .. helpers.mock_upstream_port, value) end) end) @@ -1025,7 +1045,7 @@ describe("Plugin: request-transformer (access)", function() assert.response(r).has.status(200) assert.response(r).has.jsonbody() local h_h1 = assert.request(r).has.header("h1") - assert.equals("v1, v2", h_h1) + assert.same({"v1", "v2"}, h_h1) end) it("new querystring if querystring does not exists", function() local r = assert(client:send { @@ -1066,9 +1086,12 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.are.same({"v1", "v2"}, json.postData.params.p1) - assert.are.same("value:1", json.postData.params.p2) + assert.response(r).has.jsonbody() + local value = assert.request(r).has.formparam("p1") + assert.same({"v1", "v2"}, value) + + local value2 = assert.request(r).has.formparam("p2") + assert.same("value:1", value2) end) it("values to existing parameter in url encoded body if parameter already exist on POST", function() local r = assert(client:send { @@ -1083,9 +1106,12 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.are.same({"v0", "v1", "v2"}, json.postData.params.p1) - assert.are.same("value:1", json.postData.params.p2) + assert.response(r).has.jsonbody() + local value = assert.request(r).has.formparam("p1") + assert.same({"v0", "v1", "v2"}, value) + + local value2 = assert.request(r).has.formparam("p2") + assert.are.same("value:1", value2) end) it("does not fail if JSON body is malformed in POST", function() local r = assert(client:send { @@ -1099,7 +1125,8 @@ describe("Plugin: request-transformer (access)", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.equal("malformed json body", json.postData.text) + assert.equal("json (error)", json.post_data.kind) + assert.is_not_nil(json.post_data.error) end) it("does not change or append value to parameter on multipart POST", function() local r = assert(client:send { @@ -1114,8 +1141,9 @@ describe("Plugin: request-transformer (access)", function() } }) assert.response(r).has.status(200) - local json = assert.response(r).has.jsonbody() - assert.are.same("This should not change", json.postData.params.p1) + assert.response(r).has.jsonbody() + local value = assert.request(r).has.formparam("p1") + assert.equals("This should not change", value) end) end) @@ -1197,7 +1225,7 @@ describe("Plugin: request-transformer (access)", function() assert.response(r).has.status(200) assert.response(r).has.jsonbody() local hval = assert.request(r).has.header("x-added") - assert.equals("a1, a2, a3", hval) + assert.same({"a1", "a2", "a3"}, hval) end) it("adds new parameters on POST when query string key missing", function() local r = assert(client:send { diff --git a/spec/03-plugins/15-request-transformer/03-api_spec.lua b/spec/03-plugins/15-request-transformer/03-api_spec.lua index b673be342458..570140eb4c06 100644 --- a/spec/03-plugins/15-request-transformer/03-api_spec.lua +++ b/spec/03-plugins/15-request-transformer/03-api_spec.lua @@ -17,12 +17,14 @@ describe("Plugin: request-transformer (API)", function() helpers.run_migrations() assert(helpers.dao.apis:insert { - name = "test", - hosts = { "test1.com" }, - upstream_url = "http://mockbin.com" + name = "test", + hosts = { "test1.com" }, + upstream_url = helpers.mock_upstream_url, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) admin_client = helpers.admin_client() end) diff --git a/spec/03-plugins/16-response-transformer/03-api_spec.lua b/spec/03-plugins/16-response-transformer/03-api_spec.lua index 85f0037d300b..9d68c88334f5 100644 --- a/spec/03-plugins/16-response-transformer/03-api_spec.lua +++ b/spec/03-plugins/16-response-transformer/03-api_spec.lua @@ -16,12 +16,14 @@ describe("Plugin: response-transformer (API)", function() helpers.run_migrations() assert(helpers.dao.apis:insert { - name = "test", - hosts = { "test1.com" }, - upstream_url = "http://mockbin.com" + name = "test", + hosts = { "test1.com" }, + upstream_url = helpers.mock_upstream_url, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) admin_client = helpers.admin_client() end) diff --git a/spec/03-plugins/16-response-transformer/04-filter_spec.lua b/spec/03-plugins/16-response-transformer/04-filter_spec.lua index 93d90e1581dd..96dc7362a08c 100644 --- a/spec/03-plugins/16-response-transformer/04-filter_spec.lua +++ b/spec/03-plugins/16-response-transformer/04-filter_spec.lua @@ -7,14 +7,14 @@ describe("Plugin: response-transformer (filter)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "tests-response-transformer", - hosts = { "response.com" }, - upstream_url = "http://httpbin.org" + name = "tests-response-transformer", + hosts = { "response.com" }, + upstream_url = helpers.mock_upstream_url, }) local api2 = assert(helpers.dao.apis:insert { - name = "tests-response-transformer-2", - hosts = { "response2.com" }, - upstream_url = "http://httpbin.org" + name = "tests-response-transformer-2", + hosts = { "response2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { @@ -32,12 +32,14 @@ describe("Plugin: response-transformer (filter)", function() name = "response-transformer", config = { replace = { - json = {"headers:/hello/world", "args:this is a / test", "url:\"wot\""} + json = {"headers:/hello/world", "uri_args:this is a / test", "url:\"wot\""} } } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() @@ -88,8 +90,8 @@ describe("Plugin: response-transformer (filter)", function() assert.response(r).status(200) local json = assert.response(r).has.jsonbody() assert.equals([[/hello/world]], json.headers) - assert.equals([[this is a / test]], json.args) assert.equals([["wot"]], json.url) + assert.equals([[this is a / test]], json.uri_args) end) end) end) diff --git a/spec/03-plugins/16-response-transformer/05-big_response_body_spec.lua b/spec/03-plugins/16-response-transformer/05-big_response_body_spec.lua index 56b0df7a5661..a21bd330a750 100644 --- a/spec/03-plugins/16-response-transformer/05-big_response_body_spec.lua +++ b/spec/03-plugins/16-response-transformer/05-big_response_body_spec.lua @@ -1,9 +1,11 @@ local helpers = require "spec.helpers" local function create_big_data(size) - return string.format([[ - {"mock_json":{"big_field":"%s"}} - ]], string.rep("*", size)) + return { + mock_json = { + big_field = string.rep("*", size), + }, + } end describe("Plugin: response-transformer", function() @@ -11,24 +13,26 @@ describe("Plugin: response-transformer", function() setup(function() local api = assert(helpers.dao.apis:insert { - name = "tests-response-transformer", - hosts = { "response.com" }, - upstream_url = "http://httpbin.org", + name = "tests-response-transformer", + hosts = { "response.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { api_id = api.id, - name = "response-transformer", + name = "response-transformer", config = { - add = { + add = { json = {"p1:v1"}, }, remove = { - json = {"json"}, + json = {"params"}, } - } + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) @@ -48,7 +52,7 @@ describe("Plugin: response-transformer", function() local r = assert(client:send { method = "POST", path = "/post", - body = {create_big_data(1 * 1024 * 1024)}, + body = create_big_data(1024 * 1024), headers = { host = "response.com", ["content-type"] = "application/json", @@ -62,7 +66,7 @@ describe("Plugin: response-transformer", function() local r = assert(client:send { method = "POST", path = "/post", - body = {create_big_data(1 * 1024 * 1024)}, + body = create_big_data(1024 * 1024), headers = { host = "response.com", ["content-type"] = "application/json", @@ -70,6 +74,6 @@ describe("Plugin: response-transformer", function() }) assert.response(r).has.status(200) local json = assert.response(r).has.jsonbody() - assert.is_nil(json.json) + assert.is_nil(json.params) end) end) diff --git a/spec/03-plugins/17-jwt/03-access_spec.lua b/spec/03-plugins/17-jwt/03-access_spec.lua index 7eea32a3e516..c7e9eded8622 100644 --- a/spec/03-plugins/17-jwt/03-access_spec.lua +++ b/spec/03-plugins/17-jwt/03-access_spec.lua @@ -18,28 +18,53 @@ describe("Plugin: jwt (access)", function() setup(function() helpers.run_migrations() - local api1 = assert(helpers.dao.apis:insert {name = "tests-jwt1", hosts = { "jwt.com" }, upstream_url = "http://mockbin.com"}) - local api2 = assert(helpers.dao.apis:insert {name = "tests-jwt2", hosts = { "jwt2.com" }, upstream_url = "http://mockbin.com"}) - local api3 = assert(helpers.dao.apis:insert {name = "tests-jwt3", hosts = { "jwt3.com" }, upstream_url = "http://mockbin.com"}) - local api4 = assert(helpers.dao.apis:insert {name = "tests-jwt4", hosts = { "jwt4.com" }, upstream_url = "http://mockbin.com"}) - local api5 = assert(helpers.dao.apis:insert {name = "tests-jwt5", hosts = { "jwt5.com" }, upstream_url = "http://mockbin.com"}) - local api6 = assert(helpers.dao.apis:insert {name = "tests-jwt6", hosts = { "jwt6.com" }, upstream_url = "http://mockbin.com"}) - local api7 = assert(helpers.dao.apis:insert {name = "tests-jwt7", hosts = { "jwt7.com" }, upstream_url = "http://mockbin.com"}) - - local consumer1 = assert(helpers.dao.consumers:insert {username = "jwt_tests_consumer"}) - local consumer2 = assert(helpers.dao.consumers:insert {username = "jwt_tests_base64_consumer"}) - local consumer3 = assert(helpers.dao.consumers:insert {username = "jwt_tests_rsa_consumer_1"}) - local consumer4 = assert(helpers.dao.consumers:insert {username = "jwt_tests_rsa_consumer_2"}) - local consumer5 = assert(helpers.dao.consumers:insert {username = "jwt_tests_rsa_consumer_5"}) - local anonymous_user = assert(helpers.dao.consumers:insert {username = "no-body"}) - - assert(helpers.dao.plugins:insert {name = "jwt", config = {}, api_id = api1.id}) - assert(helpers.dao.plugins:insert {name = "jwt", config = {uri_param_names = {"token", "jwt"}}, api_id = api2.id}) - assert(helpers.dao.plugins:insert {name = "jwt", config = {claims_to_verify = {"nbf", "exp"}}, api_id = api3.id}) - assert(helpers.dao.plugins:insert {name = "jwt", config = {key_claim_name = "aud"}, api_id = api4.id}) - assert(helpers.dao.plugins:insert {name = "jwt", config = {secret_is_base64 = true}, api_id = api5.id}) - assert(helpers.dao.plugins:insert {name = "jwt", config = {anonymous = anonymous_user.id}, api_id = api6.id}) - assert(helpers.dao.plugins:insert {name = "jwt", config = {anonymous = utils.uuid()}, api_id = api7.id}) + local apis = {} + + for i = 1, 7 do + apis[i] = assert(helpers.dao.apis:insert({ + name = "tests-jwt" .. i, + hosts = { "jwt" .. i .. ".com" }, + upstream_url = helpers.mock_upstream_url, + })) + end + + local cdao = helpers.dao.consumers + local consumer1 = assert(cdao:insert({ username = "jwt_tests_consumer" })) + local consumer2 = assert(cdao:insert({ username = "jwt_tests_base64_consumer" })) + local consumer3 = assert(cdao:insert({ username = "jwt_tests_rsa_consumer_1" })) + local consumer4 = assert(cdao:insert({ username = "jwt_tests_rsa_consumer_2" })) + local consumer5 = assert(cdao:insert({ username = "jwt_tests_rsa_consumer_5" })) + local anonymous_user = assert(cdao:insert({ username = "no-body" })) + + local pdao = helpers.dao.plugins + assert(pdao:insert({ name = "jwt", + api_id = apis[1].id, + config = {}, + })) + assert(pdao:insert({ name = "jwt", + api_id = apis[2].id, + config = { uri_param_names = { "token", "jwt" } }, + })) + assert(pdao:insert({ name = "jwt", + api_id = apis[3].id, + config = { claims_to_verify = {"nbf", "exp"} }, + })) + assert(pdao:insert({ name = "jwt", + api_id = apis[4].id, + config = { key_claim_name = "aud" }, + })) + assert(pdao:insert({ name = "jwt", + api_id = apis[5].id, + config = { secret_is_base64 = true }, + })) + assert(pdao:insert({ name = "jwt", + api_id = apis[6].id, + config = { anonymous = anonymous_user.id }, + })) + assert(pdao:insert({ name = "jwt", + api_id = apis[7].id, + config = { anonymous = utils.uuid() }, + })) jwt_secret = assert(helpers.dao.jwt_secrets:insert {consumer_id = consumer1.id}) base64_jwt_secret = assert(helpers.dao.jwt_secrets:insert {consumer_id = consumer2.id}) @@ -63,6 +88,7 @@ describe("Plugin: jwt (access)", function() real_ip_header = "X-Forwarded-For", real_ip_recursive = "on", trusted_ips = "0.0.0.0/0, ::/0", + nginx_conf = "spec/fixtures/custom_nginx.template", }) proxy_client = helpers.proxy_client() admin_client = helpers.admin_client() @@ -77,10 +103,10 @@ describe("Plugin: jwt (access)", function() describe("refusals", function() it("returns 401 Unauthorized if no JWT is found in the request", function() local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com", } }) assert.res_status(401, res) @@ -89,11 +115,11 @@ describe("Plugin: jwt (access)", function() local jwt = jwt_encoder.encode(PAYLOAD, "foo") local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com", } }) local body = assert.res_status(401, res) @@ -105,11 +131,11 @@ describe("Plugin: jwt (access)", function() local jwt = jwt_encoder.encode(PAYLOAD, jwt_secret.secret) local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com", } }) local body = assert.res_status(403, res) @@ -121,11 +147,11 @@ describe("Plugin: jwt (access)", function() local jwt = jwt_encoder.encode(PAYLOAD, "foo") local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com", } }) local body = assert.res_status(403, res) @@ -137,11 +163,11 @@ describe("Plugin: jwt (access)", function() local jwt = jwt_encoder.encode(PAYLOAD, jwt_secret.secret, 'HS256', header) local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com", } }) local body = assert.res_status(403, res) @@ -156,11 +182,11 @@ describe("Plugin: jwt (access)", function() local jwt = jwt_encoder.encode(PAYLOAD, jwt_secret.secret) local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com", } }) local body = cjson.decode(assert.res_status(200, res)) @@ -218,10 +244,10 @@ describe("Plugin: jwt (access)", function() PAYLOAD.iss = jwt_secret.key local jwt = jwt_encoder.encode(PAYLOAD, jwt_secret.secret) local res = assert(proxy_client:send { - method = "GET", - path = "/request/?jwt=" .. jwt, + method = "GET", + path = "/request/?jwt=" .. jwt, headers = { - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com", } }) assert.res_status(200, res) @@ -230,10 +256,10 @@ describe("Plugin: jwt (access)", function() PAYLOAD.iss = jwt_secret.key local jwt = jwt_encoder.encode(PAYLOAD, jwt_secret.secret) local res = assert(proxy_client:send { - method = "GET", - path = "/request/?token=" .. jwt, + method = "GET", + path = "/request/?token=" .. jwt, headers = { - ["Host"] = "jwt2.com" + ["Host"] = "jwt2.com", } }) assert.res_status(200, res) @@ -246,11 +272,11 @@ describe("Plugin: jwt (access)", function() local jwt = jwt_encoder.encode(PAYLOAD, fixtures.rs256_private_key, 'RS256') local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com" } }) local body = cjson.decode(assert.res_status(200, res)) @@ -262,11 +288,11 @@ describe("Plugin: jwt (access)", function() local jwt = jwt_encoder.encode(PAYLOAD, fixtures.rs256_private_key, 'RS256') local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com" + ["Host"] = "jwt1.com" } }) local body = cjson.decode(assert.res_status(200, res)) @@ -281,11 +307,11 @@ describe("RS512", function() local jwt = jwt_encoder.encode(PAYLOAD, fixtures.rs512_private_key, "RS512") local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com", + ["Host"] = "jwt1.com", } }) local body = cjson.decode(assert.res_status(200, res)) @@ -297,11 +323,11 @@ describe("RS512", function() local jwt = jwt_encoder.encode(PAYLOAD, fixtures.rs512_private_key, "RS512") local authorization = "Bearer " .. jwt local res = assert(proxy_client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Authorization"] = authorization, - ["Host"] = "jwt.com", + ["Host"] = "jwt1.com", } }) local body = cjson.decode(assert.res_status(200, res)) @@ -411,61 +437,63 @@ describe("Plugin: jwt (access)", function() setup(function() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "logical-and.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-1", + hosts = { "logical-and.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { - name = "jwt", - api_id = api1.id + name = "jwt", + api_id = api1.id, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api1.id + name = "key-auth", + api_id = api1.id, }) anonymous = assert(helpers.dao.consumers:insert { - username = "Anonymous" + username = "Anonymous", }) user1 = assert(helpers.dao.consumers:insert { - username = "Mickey" + username = "Mickey", }) user2 = assert(helpers.dao.consumers:insert { - username = "Aladdin" + username = "Aladdin", }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "logical-or.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-2", + hosts = { "logical-or.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { - name = "jwt", + name = "jwt", api_id = api2.id, config = { - anonymous = anonymous.id - } + anonymous = anonymous.id, + }, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api2.id, config = { - anonymous = anonymous.id - } + anonymous = anonymous.id, + }, }) assert(helpers.dao.keyauth_credentials:insert { - key = "Mouse", - consumer_id = user1.id + key = "Mouse", + consumer_id = user1.id, }) local jwt_secret = assert(helpers.dao.jwt_secrets:insert { - consumer_id = user2.id + consumer_id = user2.id, }) PAYLOAD.iss = jwt_secret.key - jwt_token = "Bearer " .. jwt_encoder.encode(PAYLOAD, jwt_secret.secret) + jwt_token = "Bearer " .. jwt_encoder.encode(PAYLOAD, jwt_secret.secret) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) diff --git a/spec/03-plugins/17-jwt/04-invalidations_spec.lua b/spec/03-plugins/17-jwt/04-invalidations_spec.lua index 1b0ed833155d..9c97be72199a 100644 --- a/spec/03-plugins/17-jwt/04-invalidations_spec.lua +++ b/spec/03-plugins/17-jwt/04-invalidations_spec.lua @@ -10,26 +10,28 @@ describe("Plugin: jwt (invalidations)", function() helpers.run_migrations() api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "jwt.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "jwt.com" }, + upstream_url = helpers.mock_upstream_url, }) consumer1 = assert(helpers.dao.consumers:insert { - username = "consumer1" + username = "consumer1", }) assert(helpers.dao.plugins:insert { - name = "jwt", + name = "jwt", config = {}, - api_id = api1.id + api_id = api1.id, }) assert(helpers.dao.jwt_secrets:insert { - key = "key123", - secret = "secret123", - consumer_id = consumer1.id + key = "key123", + secret = "secret123", + consumer_id = consumer1.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) admin_client = helpers.admin_client() proxy_client = helpers.proxy_client() end) diff --git a/spec/03-plugins/18-ip-restriction/02-access_spec.lua b/spec/03-plugins/18-ip-restriction/02-access_spec.lua index e5799d9550fc..88be9d83f504 100644 --- a/spec/03-plugins/18-ip-restriction/02-access_spec.lua +++ b/spec/03-plugins/18-ip-restriction/02-access_spec.lua @@ -9,99 +9,100 @@ describe("Plugin: ip-restriction (access)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "ip-restriction1.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "ip-restriction1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "ip-restriction", + name = "ip-restriction", api_id = api1.id, config = { blacklist = {"127.0.0.1", "127.0.0.2"} - } + }, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "ip-restriction2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "ip-restriction2.com" }, + upstream_url = helpers.mock_upstream_url, }) plugin_config = assert(helpers.dao.plugins:insert { - name = "ip-restriction", + name = "ip-restriction", api_id = api2.id, config = { - blacklist = {"127.0.0.2"} - } + blacklist = {"127.0.0.2"}, + }, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "ip-restriction3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "ip-restriction3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "ip-restriction", + name = "ip-restriction", api_id = api3.id, config = { - whitelist = {"127.0.0.2"} - } + whitelist = {"127.0.0.2"}, + }, }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "ip-restriction4.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "ip-restriction4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "ip-restriction", + name = "ip-restriction", api_id = api4.id, config = { - whitelist = {"127.0.0.1"} - } + whitelist = {"127.0.0.1"}, + }, }) local api5 = assert(helpers.dao.apis:insert { - name = "api-5", - hosts = { "ip-restriction5.com" }, - upstream_url = "http://mockbin.com" + name = "api-5", + hosts = { "ip-restriction5.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "ip-restriction", + name = "ip-restriction", api_id = api5.id, config = { - blacklist = {"127.0.0.0/24"} - } + blacklist = {"127.0.0.0/24"}, + }, }) local api6 = assert(helpers.dao.apis:insert { - name = "api-6", - hosts = { "ip-restriction6.com" }, - upstream_url = "http://mockbin.com" + name = "api-6", + hosts = { "ip-restriction6.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "ip-restriction", + name = "ip-restriction", api_id = api6.id, config = { - whitelist = {"127.0.0.4"} - } + whitelist = {"127.0.0.4"}, + }, }) local api7 = assert(helpers.dao.apis:insert { - name = "api-7", - hosts = { "ip-restriction7.com" }, - upstream_url = "http://mockbin.com" + name = "api-7", + hosts = { "ip-restriction7.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "ip-restriction", + name = "ip-restriction", api_id = api7.id, config = { - blacklist = {"127.0.0.4"} - } + blacklist = {"127.0.0.4"}, + }, }) assert(helpers.start_kong { real_ip_header = "X-Forwarded-For", real_ip_recursive = "on", trusted_ips = "0.0.0.0/0, ::/0", + nginx_conf = "spec/fixtures/custom_nginx.template", }) client = helpers.proxy_client() admin_client = helpers.admin_client() @@ -138,7 +139,7 @@ describe("Plugin: ip-restriction (access)", function() }) local body = assert.res_status(200, res) local json = cjson.decode(body) - assert.equal("127.0.0.1", json.clientIPAddress) + assert.equal("127.0.0.1", json.vars.remote_addr) end) it("blocks IP with CIDR", function() local res = assert(client:send { @@ -164,7 +165,7 @@ describe("Plugin: ip-restriction (access)", function() }) local body = assert.res_status(200, res) local json = cjson.decode(body) - assert.equal("127.0.0.1", json.clientIPAddress) + assert.equal("127.0.0.1", json.vars.remote_addr) end) it("allows with allowed X-Forwarded-For header", function() local res = assert(client:send { @@ -177,7 +178,7 @@ describe("Plugin: ip-restriction (access)", function() }) local body = assert.res_status(200, res) local json = cjson.decode(body) - assert.equal("127.0.0.3", json.clientIPAddress) + assert.equal("127.0.0.3", json.vars.remote_addr) end) it("blocks with not allowed X-Forwarded-For header", function() local res = assert(client:send { diff --git a/spec/03-plugins/19-acl/02-access_spec.lua b/spec/03-plugins/19-acl/02-access_spec.lua index d32db9dd07ab..00da42649291 100644 --- a/spec/03-plugins/19-acl/02-access_spec.lua +++ b/spec/03-plugins/19-acl/02-access_spec.lua @@ -69,9 +69,9 @@ describe("Plugin: ACL (access)", function() }) local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "acl1.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "acl1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "acl", @@ -82,9 +82,9 @@ describe("Plugin: ACL (access)", function() }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "acl2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "acl2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "acl", @@ -100,9 +100,9 @@ describe("Plugin: ACL (access)", function() }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "acl3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "acl3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "acl", @@ -118,9 +118,9 @@ describe("Plugin: ACL (access)", function() }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "acl4.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "acl4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "acl", @@ -136,9 +136,9 @@ describe("Plugin: ACL (access)", function() }) local api5 = assert(helpers.dao.apis:insert { - name = "api-5", - hosts = { "acl5.com" }, - upstream_url = "http://mockbin.com" + name = "api-5", + hosts = { "acl5.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "acl", @@ -154,9 +154,9 @@ describe("Plugin: ACL (access)", function() }) local api6 = assert(helpers.dao.apis:insert { - name = "api-6", - hosts = { "acl6.com" }, - upstream_url = "http://mockbin.com" + name = "api-6", + hosts = { "acl6.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "acl", @@ -172,9 +172,9 @@ describe("Plugin: ACL (access)", function() }) local api7 = assert(helpers.dao.apis:insert { - name = "api-7", - hosts = { "acl7.com" }, - upstream_url = "http://mockbin.com" + name = "api-7", + hosts = { "acl7.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "acl", @@ -190,9 +190,9 @@ describe("Plugin: ACL (access)", function() }) local api8 = assert(helpers.dao.apis:insert { - name = "api-8", - hosts = { "acl8.com" }, - upstream_url = "http://mockbin.com" + name = "api-8", + hosts = { "acl8.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { @@ -211,7 +211,9 @@ describe("Plugin: ACL (access)", function() } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) before_each(function() @@ -442,16 +444,16 @@ describe("Plugin: ACL (access)", function() for i = 1, 3 do -- Create API local res = assert(api_client:send { - method = "POST", - path = "/apis/", + method = "POST", + path = "/apis/", headers = { ["Content-Type"] = "application/json" }, - body = { - name = "acl_test" .. i, - hosts = { "acl_test" .. i .. ".com" }, - upstream_url = "http://mockbin.com" - } + body = { + name = "acl_test" .. i, + hosts = { "acl_test" .. i .. ".com" }, + upstream_url = helpers.mock_upstream_url, + }, }) assert.res_status(201, res) diff --git a/spec/03-plugins/19-acl/03-invalidations_spec.lua b/spec/03-plugins/19-acl/03-invalidations_spec.lua index 7c71b5a74c65..4fb5252a4c14 100644 --- a/spec/03-plugins/19-acl/03-invalidations_spec.lua +++ b/spec/03-plugins/19-acl/03-invalidations_spec.lua @@ -37,9 +37,9 @@ describe("Plugin: ACL (invalidations)", function() }) local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "acl1.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "acl1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "key-auth", @@ -54,9 +54,9 @@ describe("Plugin: ACL (invalidations)", function() }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "acl2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "acl2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "key-auth", @@ -70,7 +70,9 @@ describe("Plugin: ACL (invalidations)", function() } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) proxy_client = helpers.proxy_client() admin_client = helpers.admin_client() end) diff --git a/spec/03-plugins/20-hmac-auth/03-access_spec.lua b/spec/03-plugins/20-hmac-auth/03-access_spec.lua index a98426ecd3a4..2d17c071d622 100644 --- a/spec/03-plugins/20-hmac-auth/03-access_spec.lua +++ b/spec/03-plugins/20-hmac-auth/03-access_spec.lua @@ -17,9 +17,9 @@ describe("Plugin: hmac-auth (access)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "hmacauth.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "hmacauth.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "hmac-auth", @@ -43,9 +43,9 @@ describe("Plugin: hmac-auth (access)", function() username = "no-body" }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "hmacauth2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "hmacauth2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "hmac-auth", @@ -57,9 +57,9 @@ describe("Plugin: hmac-auth (access)", function() }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "hmacauth3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "hmacauth3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "hmac-auth", @@ -71,9 +71,9 @@ describe("Plugin: hmac-auth (access)", function() }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "hmacauth4.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "hmacauth4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "hmac-auth", @@ -85,9 +85,9 @@ describe("Plugin: hmac-auth (access)", function() }) local api5 = assert(helpers.dao.apis:insert { - name = "api-5", - hosts = { "hmacauth5.com" }, - upstream_url = "http://mockbin.com" + name = "api-5", + hosts = { "hmacauth5.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "hmac-auth", @@ -100,9 +100,9 @@ describe("Plugin: hmac-auth (access)", function() }) local api6 = assert(helpers.dao.apis:insert { - name = "api-6", - hosts = { "hmacauth6.com" }, - upstream_url = "http://mockbin.com" + name = "api-6", + hosts = { "hmacauth6.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "hmac-auth", @@ -119,6 +119,7 @@ describe("Plugin: hmac-auth (access)", function() real_ip_header = "X-Forwarded-For", real_ip_recursive = "on", trusted_ips = "0.0.0.0/0, ::/0", + nginx_conf = "spec/fixtures/custom_nginx.template", }) client = helpers.proxy_client() end) @@ -296,14 +297,14 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[headers="date",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, - authorization = hmacAuth - } + ["HOST"] = "hmacauth.com", + date = date, + authorization = hmacAuth, + }, }) local body = assert.res_status(200, res) body = cjson.decode(body) @@ -316,14 +317,14 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[headers="date",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, - ["proxy-authorization"] = hmacAuth - } + ["HOST"] = "hmacauth.com", + date = date, + ["proxy-authorization"] = hmacAuth, + }, }) assert.res_status(200, res) end) @@ -334,14 +335,14 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[headers="date",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "POST", - path = "/requests", - body = {}, + method = "POST", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, - authorization = hmacAuth - } + ["HOST"] = "hmacauth.com", + date = date, + authorization = hmacAuth, + }, }) local body = assert.res_status(200, res) body = cjson.decode(body) @@ -354,15 +355,15 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[headers="date",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "POST", - path = "/requests", - body = {}, + method = "POST", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = "hmac username", - authorization = hmacAuth - } + authorization = hmacAuth, + }, }) local body = assert.res_status(200, res) body = cjson.decode(body) @@ -376,15 +377,15 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[headers="date",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "POST", - path = "/requests", - body = {}, + method = "POST", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello" - } + authorization = "hello", + }, }) assert.res_status(200, res) end) @@ -396,16 +397,16 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[headers="date content-md5",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -414,21 +415,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " - .. date .. "\n" .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. date .. "\n" .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha1", ]] .. [[headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -437,22 +438,22 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bobb", algorithm="hmac-sha1", ]] .. [[headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, - headers = { - ["HOST"] = "hmacauth.com", - date = date, - ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } - }) + method = "GET", + path = "/request", + body = {}, + headers = { + ["HOST"] = "hmacauth.com", + date = date, + ["proxy-authorization"] = hmacAuth, + authorization = "hello", + ["content-md5"] = "md5", + }, + }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -463,21 +464,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", - "date: " .. date .. "\n" .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + "date: " .. date .. "\n" .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="", algorithm="hmac-sha1",]] .. [[ headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) local body = assert.res_status(403, res) @@ -490,21 +491,21 @@ describe("Plugin: hmac-auth (access)", function() local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " .. date .. "\n" .. "content-md5: md5" - .. "\nGET /requests HTTP/1.1")) + .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac algorithm="hmac-sha1", ]] .. [[headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) local body = assert.res_status(403, res) @@ -516,21 +517,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha1", ]] .. [[wrong_header="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -541,21 +542,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha1",]] .. [[ headers="date content-md5 request-line", wrong_signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -566,21 +567,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha1"]] .. [[ headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -596,16 +597,16 @@ describe("Plugin: hmac-auth (access)", function() .. [[headers=" date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, - headers = { - ["HOST"] = "hmacauth.com", - date = date, + method = "GET", + path = "/request", + body = {}, + headers = { + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -616,21 +617,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[ headers="date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -639,21 +640,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( crypto.hmac.digest("sha256","date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1", "secret", true)) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1", "secret", true)) local hmacAuth = [[hmac username="bob",algorithm="hmac-sha",]] .. [[ headers="date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) assert.res_status(403, res) end) @@ -662,22 +663,22 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( crypto.hmac.digest("sha256","date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1", + .. "content-md5: md5" .. "\nGET /request HTTP/1.1", "secret", true)) local hmacAuth = [[hmac username="bob",algorithm="hmac-sha256",]] .. [[ headers="date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - date = date, + ["HOST"] = "hmacauth.com", + date = date, ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) local body = assert.res_status(200, res) local parsed_body = cjson.decode(body) @@ -691,20 +692,20 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "x-date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[ headers="x-date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - ["x-date"] = date, - authorization = hmacAuth, - ["content-md5"] = "md5" - } + ["HOST"] = "hmacauth.com", + ["x-date"] = date, + authorization = hmacAuth, + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -717,15 +718,15 @@ describe("Plugin: hmac-auth (access)", function() .. [[ headers="content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", + ["HOST"] = "hmacauth.com", ["proxy-authorization"] = hmacAuth, - authorization = "hello", - ["content-md5"] = "md5" - } + authorization = "hello", + ["content-md5"] = "md5", + }, }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -742,15 +743,15 @@ describe("Plugin: hmac-auth (access)", function() .. [[ headers="x-date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - ["x-date"] = "wrong date", - authorization = hmacAuth, - ["content-md5"] = "md5" - } + ["HOST"] = "hmacauth.com", + ["x-date"] = "wrong date", + authorization = hmacAuth, + ["content-md5"] = "md5", + }, }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -762,21 +763,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "content-md5: md5" - .. "\nGET /requests HTTP/1.1")) + .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[ headers="content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - ["x-date"] = "wrong date", - date = date, - authorization = hmacAuth, - ["content-md5"] = "md5" - } + ["HOST"] = "hmacauth.com", + ["x-date"] = "wrong date", + date = date, + authorization = hmacAuth, + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -785,21 +786,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[ headers="date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - ["x-date"] = "wrong date", - date = date, - authorization = hmacAuth, - ["content-md5"] = "md5" - } + ["HOST"] = "hmacauth.com", + ["x-date"] = "wrong date", + date = date, + authorization = hmacAuth, + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -808,21 +809,21 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "x-date: " .. "wrong date" .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[ headers="x-date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - ["x-date"] = "wrong date", - date = date, - authorization = hmacAuth, - ["content-md5"] = "md5" - } + ["HOST"] = "hmacauth.com", + ["x-date"] = "wrong date", + date = date, + authorization = hmacAuth, + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -831,20 +832,20 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " .. "wrong date" .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[ headers="date content-md5 request-line",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth.com", - ["x-date"] = date, - date = "wrong date", - authorization = hmacAuth, - ["content-md5"] = "md5" + ["HOST"] = "hmacauth.com", + ["x-date"] = date, + date = "wrong date", + authorization = hmacAuth, + ["content-md5"] = "md5", } }) assert.res_status(200, res) @@ -856,14 +857,14 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [[hmac username="bob",algorithm="hmac-sha1",]] .. [[headers="date",signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/request", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth2.com", - date = date, - authorization = hmacAuth - } + ["HOST"] = "hmacauth2.com", + date = date, + authorization = hmacAuth, + }, }) local body = assert.res_status(200, res) body = cjson.decode(body) @@ -874,12 +875,12 @@ describe("Plugin: hmac-auth (access)", function() it("should pass with invalid credentials and anonymous", function() local res = assert(client:send { - method = "GET", - path = "/request", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth2.com" - } + ["HOST"] = "hmacauth2.com", + }, }) local body = assert.res_status(200, res) body = cjson.decode(body) @@ -893,10 +894,10 @@ describe("Plugin: hmac-auth (access)", function() local res = assert(client:send { method = "GET", - path = "/request", + path = "/request", headers = { - ["Host"] = "hmacauth3.com" - } + ["Host"] = "hmacauth3.com", + }, }) assert.response(res).has.status(500) end) @@ -907,14 +908,14 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [["hmac username="bob",algorithm="hmac-sha1",]] ..[[headers="date",signature="]]..encodedSignature..[["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth4.com", - date = date, - authorization = hmacAuth - } + ["HOST"] = "hmacauth4.com", + date = date, + authorization = hmacAuth, + }, }) assert.res_status(200, res) end) @@ -931,15 +932,15 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [["hmac username="bob",algorithm="hmac-sha1",]] ..[[headers="date digest",signature="]]..encodedSignature..[["]] local res = assert(client:send { - method = "POST", - path = "/requests", - body = postBody, + method = "POST", + path = "/request", + body = postBody, headers = { - ["HOST"] = "hmacauth4.com", - date = date, - digest = digest, - authorization = hmacAuth - } + ["HOST"] = "hmacauth4.com", + date = date, + digest = digest, + authorization = hmacAuth, + }, }) assert.res_status(200, res) end) @@ -956,15 +957,15 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [["hmac username="bob",algorithm="hmac-sha1",]] ..[[headers="date digest",signature="]]..encodedSignature..[["]] local res = assert(client:send { - method = "POST", - path = "/requests", - body = postBody, + method = "POST", + path = "/request", + body = postBody, headers = { - ["HOST"] = "hmacauth4.com", - date = date, - digest = digest, - authorization = hmacAuth - } + ["HOST"] = "hmacauth4.com", + date = date, + digest = digest, + authorization = hmacAuth, + }, }) assert.res_status(200, res) end) @@ -981,14 +982,14 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [["hmac username="bob",algorithm="hmac-sha1",]] ..[[headers="date digest",signature="]]..encodedSignature..[["]] local res = assert(client:send { - method = "POST", - path = "/requests", - body = postBody, + method = "POST", + path = "/request", + body = postBody, headers = { - ["HOST"] = "hmacauth4.com", - date = date, + ["HOST"] = "hmacauth4.com", + date = date, authorization = hmacAuth, - } + }, }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -1007,15 +1008,15 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [["hmac username="bob",algorithm="hmac-sha1",]] ..[[headers="date digest",signature="]]..encodedSignature..[["]] local res = assert(client:send { - method = "POST", - path = "/requests", - body = "abc", + method = "POST", + path = "/request", + body = "abc", headers = { - ["HOST"] = "hmacauth4.com", - date = date, - digest = digest, - authorization = hmacAuth - } + ["HOST"] = "hmacauth4.com", + date = date, + digest = digest, + authorization = hmacAuth, + }, }) local body = assert.res_status(403, res) body = cjson.decode(body) @@ -1034,14 +1035,14 @@ describe("Plugin: hmac-auth (access)", function() local hmacAuth = [["hmac username="bob",algorithm="hmac-sha1",]] ..[[headers="date digest",signature="]]..encodedSignature..[["]] local res = assert(client:send { - method = "POST", - path = "/requests", - body = postBody, + method = "POST", + path = "/request", + body = postBody, headers = { - ["HOST"] = "hmacauth4.com", - date = date, - digest = digest .. "spoofed", - authorization = hmacAuth + ["HOST"] = "hmacauth4.com", + date = date, + digest = digest .. "spoofed", + authorization = hmacAuth, } }) local body = assert.res_status(403, res) @@ -1053,20 +1054,20 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( hmac_sha1_binary("secret", "date: " - .. date .. "\n" .. "content-md5: md5" .. "\nGET /requests HTTP/1.1")) + .. date .. "\n" .. "content-md5: md5" .. "\nGET /request HTTP/1.1")) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha1", ]] .. [[headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth5.com", - date = date, + ["HOST"] = "hmacauth5.com", + date = date, ["proxy-authorization"] = hmacAuth, - ["content-md5"] = "md5" - } + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -1080,15 +1081,15 @@ describe("Plugin: hmac-auth (access)", function() .. [[headers="date content-md5", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth5.com", - date = date, + ["HOST"] = "hmacauth5.com", + date = date, ["proxy-authorization"] = hmacAuth, - ["content-md5"] = "md5" - } + ["content-md5"] = "md5", + }, }) assert.res_status(403, res) end) @@ -1097,20 +1098,20 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( crypto.hmac.digest("sha384","date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1", "secret", true)) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1", "secret", true)) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha384", ]] .. [[headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth5.com", - date = date, + ["HOST"] = "hmacauth5.com", + date = date, ["proxy-authorization"] = hmacAuth, - ["content-md5"] = "md5" - } + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -1119,20 +1120,20 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( crypto.hmac.digest("sha512","date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1", "secret", true)) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1", "secret", true)) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha512", ]] .. [[headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth5.com", - date = date, + ["HOST"] = "hmacauth5.com", + date = date, ["proxy-authorization"] = hmacAuth, - ["content-md5"] = "md5" - } + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -1141,20 +1142,20 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( crypto.hmac.digest("sha512","date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1", "secret", true)) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1", "secret", true)) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha512", ]] .. [[headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth6.com", - date = date, + ["HOST"] = "hmacauth6.com", + date = date, ["proxy-authorization"] = hmacAuth, - ["content-md5"] = "md5" - } + ["content-md5"] = "md5", + }, }) assert.res_status(403, res) end) @@ -1163,20 +1164,20 @@ describe("Plugin: hmac-auth (access)", function() local date = os.date("!%a, %d %b %Y %H:%M:%S GMT") local encodedSignature = ngx.encode_base64( crypto.hmac.digest("sha1","date: " .. date .. "\n" - .. "content-md5: md5" .. "\nGET /requests HTTP/1.1", "secret", true)) + .. "content-md5: md5" .. "\nGET /request HTTP/1.1", "secret", true)) local hmacAuth = [[hmac username="bob", algorithm="hmac-sha1", ]] .. [[headers="date content-md5 request-line", signature="]] .. encodedSignature .. [["]] local res = assert(client:send { - method = "GET", - path = "/requests", - body = {}, + method = "GET", + path = "/request", + body = {}, headers = { - ["HOST"] = "hmacauth6.com", - date = date, + ["HOST"] = "hmacauth6.com", + date = date, ["proxy-authorization"] = hmacAuth, - ["content-md5"] = "md5" - } + ["content-md5"] = "md5", + }, }) assert.res_status(200, res) end) @@ -1190,9 +1191,9 @@ describe("Plugin: hmac-auth (access)", function() setup(function() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "logical-and.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-1", + hosts = { "logical-and.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { name = "hmac-auth", @@ -1214,9 +1215,9 @@ describe("Plugin: hmac-auth (access)", function() }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "logical-or.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-2", + hosts = { "logical-or.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { name = "hmac-auth", @@ -1247,7 +1248,9 @@ describe("Plugin: hmac-auth (access)", function() hmacAuth = [[hmac username="]] .. credential.username .. [[",algorithm="hmac-sha1",]] .. [[headers="date",signature="]] .. encodedSignature .. [["]] - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) @@ -1261,14 +1264,14 @@ describe("Plugin: hmac-auth (access)", function() it("passes with all credentials provided", function() local res = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { - ["Host"] = "logical-and.com", - ["apikey"] = "Mouse", + ["Host"] = "logical-and.com", + ["apikey"] = "Mouse", ["Authorization"] = hmacAuth, - ["date"] = hmacDate, - } + ["date"] = hmacDate, + }, }) assert.response(res).has.status(200) assert.request(res).has.no.header("x-anonymous-consumer") @@ -1279,12 +1282,12 @@ describe("Plugin: hmac-auth (access)", function() it("fails 401, with only the first credential provided", function() local res = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { - ["Host"] = "logical-and.com", + ["Host"] = "logical-and.com", ["apikey"] = "Mouse", - } + }, }) assert.response(res).has.status(401) end) @@ -1292,23 +1295,23 @@ describe("Plugin: hmac-auth (access)", function() it("fails 401, with only the second credential provided", function() local res = assert(client:send { method = "GET", - path = "/request", + path = "/request", headers = { - ["Host"] = "logical-and.com", + ["Host"] = "logical-and.com", ["Authorization"] = hmacAuth, - ["date"] = hmacDate, - } + ["date"] = hmacDate, + }, }) assert.response(res).has.status(401) end) it("fails 401, with no credential provided", function() local res = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Host"] = "logical-and.com", - } + }, }) assert.response(res).has.status(401) end) @@ -1319,14 +1322,14 @@ describe("Plugin: hmac-auth (access)", function() it("passes with all credentials provided", function() local res = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { - ["Host"] = "logical-or.com", - ["apikey"] = "Mouse", + ["Host"] = "logical-or.com", + ["apikey"] = "Mouse", ["Authorization"] = hmacAuth, - ["date"] = hmacDate, - } + ["date"] = hmacDate, + }, }) assert.response(res).has.status(200) assert.request(res).has.no.header("x-anonymous-consumer") @@ -1337,12 +1340,12 @@ describe("Plugin: hmac-auth (access)", function() it("passes with only the first credential provided", function() local res = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { - ["Host"] = "logical-or.com", + ["Host"] = "logical-or.com", ["apikey"] = "Mouse", - } + }, }) assert.response(res).has.status(200) assert.request(res).has.no.header("x-anonymous-consumer") @@ -1353,13 +1356,13 @@ describe("Plugin: hmac-auth (access)", function() it("passes with only the second credential provided", function() local res = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { - ["Host"] = "logical-or.com", + ["Host"] = "logical-or.com", ["Authorization"] = hmacAuth, - ["date"] = hmacDate, - } + ["date"] = hmacDate, + }, }) assert.response(res).has.status(200) assert.request(res).has.no.header("x-anonymous-consumer") @@ -1370,11 +1373,11 @@ describe("Plugin: hmac-auth (access)", function() it("passes with no credential provided", function() local res = assert(client:send { - method = "GET", - path = "/request", + method = "GET", + path = "/request", headers = { ["Host"] = "logical-or.com", - } + }, }) assert.response(res).has.status(200) assert.request(res).has.header("x-anonymous-consumer") diff --git a/spec/03-plugins/20-hmac-auth/04-invalidations_spec.lua b/spec/03-plugins/20-hmac-auth/04-invalidations_spec.lua index 78ec51f4ef59..835ba9587cc7 100644 --- a/spec/03-plugins/20-hmac-auth/04-invalidations_spec.lua +++ b/spec/03-plugins/20-hmac-auth/04-invalidations_spec.lua @@ -10,29 +10,31 @@ describe("Plugin: hmac-auth (invalidations)", function() helpers.run_migrations() local api = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "hmacauth.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "hmacauth.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "hmac-auth", + name = "hmac-auth", api_id = api.id, config = { - clock_skew = 3000 - } + clock_skew = 3000, + }, }) consumer = assert(helpers.dao.consumers:insert { - username = "consumer1", - custom_id = "1234" + username = "consumer1", + custom_id = "1234", }) credential = assert(helpers.dao["hmacauth_credentials"]:insert { - username = "bob", - secret = "secret", - consumer_id = consumer.id + username = "bob", + secret = "secret", + consumer_id = consumer.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client_proxy = helpers.proxy_client() client_admin = helpers.admin_client() end) diff --git a/spec/03-plugins/21-ldap-auth/01-access_spec.lua b/spec/03-plugins/21-ldap-auth/01-access_spec.lua index 0dd6c6afd181..b669361ab1dd 100644 --- a/spec/03-plugins/21-ldap-auth/01-access_spec.lua +++ b/spec/03-plugins/21-ldap-auth/01-access_spec.lua @@ -13,24 +13,24 @@ describe("Plugin: ldap-auth (access)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "test-ldap", - hosts = { "ldap.com" }, - upstream_url = "http://mockbin.com" + name = "test-ldap", + hosts = { "ldap.com" }, + upstream_url = helpers.mock_upstream_url, }) api2 = assert(helpers.dao.apis:insert { - name = "test-ldap2", - hosts = { "ldap2.com" }, - upstream_url = "http://mockbin.com" + name = "test-ldap2", + hosts = { "ldap2.com" }, + upstream_url = helpers.mock_upstream_url, }) local api3 = assert(helpers.dao.apis:insert { - name = "test-ldap3", - hosts = { "ldap3.com" }, - upstream_url = "http://mockbin.com" + name = "test-ldap3", + hosts = { "ldap3.com" }, + upstream_url = helpers.mock_upstream_url, }) local api4 = assert(helpers.dao.apis:insert { - name = "test-ldap4", - hosts = { "ldap4.com" }, - upstream_url = "http://mockbin.com" + name = "test-ldap4", + hosts = { "ldap4.com" }, + upstream_url = helpers.mock_upstream_url, }) local anonymous_user = assert(helpers.dao.consumers:insert { @@ -88,7 +88,9 @@ describe("Plugin: ldap-auth (access)", function() } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() @@ -352,64 +354,66 @@ describe("Plugin: ldap-auth (access)", function() setup(function() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "logical-and.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-1", + hosts = { "logical-and.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { api_id = api1.id, - name = "ldap-auth", + name = "ldap-auth", config = { ldap_host = ldap_host_aws, ldap_port = "389", start_tls = false, - base_dn = "ou=scientists,dc=ldap,dc=mashape,dc=com", + base_dn = "ou=scientists,dc=ldap,dc=mashape,dc=com", attribute = "uid", - } + }, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api1.id + name = "key-auth", + api_id = api1.id, }) anonymous = assert(helpers.dao.consumers:insert { - username = "Anonymous" + username = "Anonymous", }) user1 = assert(helpers.dao.consumers:insert { - username = "Mickey" + username = "Mickey", }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "logical-or.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-2", + hosts = { "logical-or.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { api_id = api2.id, - name = "ldap-auth", + name = "ldap-auth", config = { ldap_host = ldap_host_aws, ldap_port = "389", start_tls = false, - base_dn = "ou=scientists,dc=ldap,dc=mashape,dc=com", + base_dn = "ou=scientists,dc=ldap,dc=mashape,dc=com", attribute = "uid", anonymous = anonymous.id, - } + }, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api2.id, config = { - anonymous = anonymous.id - } + anonymous = anonymous.id, + }, }) assert(helpers.dao.keyauth_credentials:insert { - key = "Mouse", - consumer_id = user1.id + key = "Mouse", + consumer_id = user1.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) diff --git a/spec/03-plugins/22-bot-detection/01-access_spec.lua b/spec/03-plugins/22-bot-detection/01-access_spec.lua index a86804c5e754..edbfb755801d 100644 --- a/spec/03-plugins/22-bot-detection/01-access_spec.lua +++ b/spec/03-plugins/22-bot-detection/01-access_spec.lua @@ -9,45 +9,47 @@ describe("Plugin: bot-detection (access)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "bot.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "bot.com" }, + upstream_url = helpers.mock_upstream_url, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "bot2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "bot2.com" }, + upstream_url = helpers.mock_upstream_url, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "bot3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "bot3.com" }, + upstream_url = helpers.mock_upstream_url, }) -- plugin 1 assert(helpers.dao.plugins:insert { api_id = api1.id, - name = "bot-detection", + name = "bot-detection", config = {}, }) -- plugin 2 assert(helpers.dao.plugins:insert { api_id = api2.id, - name = "bot-detection", + name = "bot-detection", config = { - blacklist = HELLOWORLD - } + blacklist = HELLOWORLD, + }, }) -- plugin 3 assert(helpers.dao.plugins:insert { api_id = api3.id, - name = "bot-detection", + name = "bot-detection", config = { - whitelist = FACEBOOK - } + whitelist = FACEBOOK, + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() diff --git a/spec/03-plugins/22-bot-detection/02-invalidations_spec.lua b/spec/03-plugins/22-bot-detection/02-invalidations_spec.lua index 8dfc88b74304..5099265a47c6 100644 --- a/spec/03-plugins/22-bot-detection/02-invalidations_spec.lua +++ b/spec/03-plugins/22-bot-detection/02-invalidations_spec.lua @@ -7,17 +7,19 @@ describe("Plugin: bot-detection (hooks)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "bot.com", - hosts = { "bot.com" }, - upstream_url = "http://mockbin.com" + name = "bot.com", + hosts = { "bot.com" }, + upstream_url = helpers.mock_upstream_url, }) plugin = assert(helpers.dao.plugins:insert { api_id = api1.id, - name = "bot-detection", + name = "bot-detection", config = {}, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() diff --git a/spec/03-plugins/22-bot-detection/03-api_spec.lua b/spec/03-plugins/22-bot-detection/03-api_spec.lua index 362cea62c04c..b63e00fddc4f 100644 --- a/spec/03-plugins/22-bot-detection/03-api_spec.lua +++ b/spec/03-plugins/22-bot-detection/03-api_spec.lua @@ -9,17 +9,19 @@ describe("Plugin: bot-detection (API)", function() helpers.run_migrations() assert(helpers.dao.apis:insert { - name = "bot1.com", - hosts = { "bot1.com" }, - upstream_url = "http://mockbin.com" + name = "bot1.com", + hosts = { "bot1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.apis:insert { - name = "bot2.com", - hosts = { "bot2.com" }, - upstream_url = "http://mockbin.com" + name = "bot2.com", + hosts = { "bot2.com" }, + upstream_url = helpers.mock_upstream_url, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() diff --git a/spec/03-plugins/23-aws-lambda/01-access_spec.lua b/spec/03-plugins/23-aws-lambda/01-access_spec.lua index 44ff90186f05..c830581c06f6 100644 --- a/spec/03-plugins/23-aws-lambda/01-access_spec.lua +++ b/spec/03-plugins/23-aws-lambda/01-access_spec.lua @@ -7,153 +7,153 @@ describe("Plugin: AWS Lambda (access)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "lambda.com", - hosts = { "lambda.com" } , - upstream_url = "http://httpbin.org" + name = "lambda.com", + hosts = { "lambda.com" }, + upstream_url = helpers.mock_upstream_url, }) local api2 = assert(helpers.dao.apis:insert { - name = "lambda2.com", - hosts = { "lambda2.com" }, - upstream_url = "http://httpbin.org" + name = "lambda2.com", + hosts = { "lambda2.com" }, + upstream_url = helpers.mock_upstream_url, }) local api3 = assert(helpers.dao.apis:insert { - name = "lambda3.com", - hosts = { "lambda3.com" }, - upstream_url = "http://httpbin.org" + name = "lambda3.com", + hosts = { "lambda3.com" }, + upstream_url = helpers.mock_upstream_url, }) local api4 = assert(helpers.dao.apis:insert { - name = "lambda4.com", - hosts = { "lambda4.com" }, - upstream_url = "http://httpbin.org" + name = "lambda4.com", + hosts = { "lambda4.com" }, + upstream_url = helpers.mock_upstream_url, }) local api5 = assert(helpers.dao.apis:insert { - name = "lambda5.com", - hosts = { "lambda5.com" }, - upstream_url = "http://httpbin.org" + name = "lambda5.com", + hosts = { "lambda5.com" }, + upstream_url = helpers.mock_upstream_url, }) local api6 = assert(helpers.dao.apis:insert { - name = "lambda6.com", - hosts = { "lambda6.com" }, - upstream_url = "http://httpbin.org" + name = "lambda6.com", + hosts = { "lambda6.com" }, + upstream_url = helpers.mock_upstream_url, }) local api7 = assert(helpers.dao.apis:insert { - name = "lambda7.com", - hosts = { "lambda7.com" }, - upstream_url = "http://httpbin.org" + name = "lambda7.com", + hosts = { "lambda7.com" }, + upstream_url = helpers.mock_upstream_url, }) local api8 = assert(helpers.dao.apis:insert { - name = "lambda8.com", - hosts = { "lambda8.com" }, - upstream_url = "http://httpbin.org" + name = "lambda8.com", + hosts = { "lambda8.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "aws-lambda", + name = "aws-lambda", api_id = api1.id, config = { - port = 10001, - aws_key = "mock-key", - aws_secret = "mock-secret", - aws_region = "us-east-1", - function_name = "kongLambdaTest" - } + port = 10001, + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", + function_name = "kongLambdaTest", + }, }) assert(helpers.dao.plugins:insert { - name = "aws-lambda", + name = "aws-lambda", api_id = api2.id, config = { - port = 10001, - aws_key = "mock-key", - aws_secret = "mock-secret", - aws_region = "us-east-1", - function_name = "kongLambdaTest", - invocation_type = "Event" - } + port = 10001, + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", + function_name = "kongLambdaTest", + invocation_type = "Event", + }, }) assert(helpers.dao.plugins:insert { - name = "aws-lambda", + name = "aws-lambda", api_id = api3.id, config = { - port = 10001, - aws_key = "mock-key", - aws_secret = "mock-secret", - aws_region = "us-east-1", - function_name = "kongLambdaTest", - invocation_type = "DryRun" - } + port = 10001, + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", + function_name = "kongLambdaTest", + invocation_type = "DryRun", + }, }) assert(helpers.dao.plugins:insert { - name = "aws-lambda", + name = "aws-lambda", api_id = api4.id, config = { port = 10001, - aws_key = "mock-key", - aws_secret = "mock-secret", - aws_region = "us-east-1", + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", function_name = "kongLambdaTest", - timeout = 100, - } + timeout = 100, + }, }) assert(helpers.dao.plugins:insert { - name = "aws-lambda", + name = "aws-lambda", api_id = api5.id, config = { - port = 10001, - aws_key = "mock-key", - aws_secret = "mock-secret", - aws_region = "us-east-1", + port = 10001, + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", function_name = "functionWithUnhandledError", - } + }, }) assert(helpers.dao.plugins:insert { - name = "aws-lambda", + name = "aws-lambda", api_id = api6.id, config = { - port = 10001, - aws_key = "mock-key", - aws_secret = "mock-secret", - aws_region = "us-east-1", - function_name = "functionWithUnhandledError", + port = 10001, + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", + function_name = "functionWithUnhandledError", invocation_type = "Event", - } + }, }) assert(helpers.dao.plugins:insert { - name = "aws-lambda", + name = "aws-lambda", api_id = api7.id, config = { port = 10001, - aws_key = "mock-key", - aws_secret = "mock-secret", - aws_region = "us-east-1", - function_name = "functionWithUnhandledError", + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", + function_name = "functionWithUnhandledError", invocation_type = "DryRun", - } + }, }) assert(helpers.dao.plugins:insert { - name = "aws-lambda", + name = "aws-lambda", api_id = api8.id, config = { - port = 10001, - aws_key = "mock-key", - aws_secret = "mock-secret", - aws_region = "us-east-1", - function_name = "functionWithUnhandledError", + port = 10001, + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", + function_name = "functionWithUnhandledError", unhandled_status = 412, - } + }, }) assert(helpers.start_kong{ diff --git a/spec/03-plugins/24-rate-limiting/03-api_spec.lua b/spec/03-plugins/24-rate-limiting/03-api_spec.lua index 123a66e7c9a0..fa51d25db00b 100644 --- a/spec/03-plugins/24-rate-limiting/03-api_spec.lua +++ b/spec/03-plugins/24-rate-limiting/03-api_spec.lua @@ -16,12 +16,14 @@ describe("Plugin: rate-limiting (API)", function() describe("POST", function() setup(function() assert(helpers.dao.apis:insert { - name = "test", - hosts = { "test1.com" }, - upstream_url = "http://mockbin.com" + name = "test", + hosts = { "test1.com" }, + upstream_url = helpers.mock_upstream_url, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) admin_client = helpers.admin_client() end) diff --git a/spec/03-plugins/24-rate-limiting/04-access_spec.lua b/spec/03-plugins/24-rate-limiting/04-access_spec.lua index 7b944d96cb76..5526dd14f2d4 100644 --- a/spec/03-plugins/24-rate-limiting/04-access_spec.lua +++ b/spec/03-plugins/24-rate-limiting/04-access_spec.lua @@ -55,85 +55,85 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do helpers.run_migrations() local consumer1 = assert(helpers.dao.consumers:insert { - custom_id = "provider_123" + custom_id = "provider_123", }) assert(helpers.dao.keyauth_credentials:insert { - key = "apikey122", - consumer_id = consumer1.id + key = "apikey122", + consumer_id = consumer1.id, }) local consumer2 = assert(helpers.dao.consumers:insert { - custom_id = "provider_124" + custom_id = "provider_124", }) assert(helpers.dao.keyauth_credentials:insert { - key = "apikey123", - consumer_id = consumer2.id + key = "apikey123", + consumer_id = consumer2.id, }) assert(helpers.dao.keyauth_credentials:insert { - key = "apikey333", - consumer_id = consumer2.id + key = "apikey333", + consumer_id = consumer2.id, }) local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "test1.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "test1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api1.id, config = { - policy = policy, - minute = 6, + policy = policy, + minute = 6, fault_tolerant = false, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, - redis_database = REDIS_DATABASE + redis_database = REDIS_DATABASE, } }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "test2.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "test2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api2.id, config = { - minute = 3, - hour = 5, + minute = 3, + hour = 5, fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, - redis_database = REDIS_DATABASE + redis_database = REDIS_DATABASE, } }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "test3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "test3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api3.id + name = "key-auth", + api_id = api3.id, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api3.id, config = { - minute = 6, - limit_by = "credential", + minute = 6, + limit_by = "credential", fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, - redis_database = REDIS_DATABASE + redis_database = REDIS_DATABASE, } }) assert(helpers.dao.plugins:insert { @@ -152,50 +152,52 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "test4.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "test4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api4.id + name = "key-auth", + api_id = api4.id, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", - api_id = api4.id, + name = "rate-limiting", + api_id = api4.id, consumer_id = consumer1.id, - config = { - minute = 6, + config = { + minute = 6, fault_tolerant = true, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, - redis_database = REDIS_DATABASE - } + redis_database = REDIS_DATABASE, + }, }) local api5 = assert(helpers.dao.apis:insert { - name = "api-5", - hosts = { "test5.com" }, - upstream_url = "http://mockbin.com" + name = "api-5", + hosts = { "test5.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api5.id, config = { - policy = policy, - minute = 6, + policy = policy, + minute = 6, hide_client_headers = true, - fault_tolerant = false, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, - redis_password = REDIS_PASSWORD, - redis_database = REDIS_DATABASE - } + fault_tolerant = false, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, + redis_password = REDIS_PASSWORD, + redis_database = REDIS_DATABASE, + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() @@ -219,7 +221,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do for i = 1, 6 do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "test1.com" } @@ -235,7 +237,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do -- Additonal request, while limit is 6/minute local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "test1.com" } @@ -254,7 +256,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do for i = 1, 3 do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "test2.com" } @@ -271,7 +273,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "test2.com" } @@ -289,7 +291,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do for i = 1, 6 do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/?apikey=apikey123", + path = "/status/200?apikey=apikey123", headers = { ["Host"] = "test3.com" } @@ -305,7 +307,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do -- Third query, while limit is 2/minute local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/?apikey=apikey123", + path = "/status/200?apikey=apikey123", headers = { ["Host"] = "test3.com" } @@ -317,7 +319,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do -- Using a different key of the same consumer works local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/?apikey=apikey333", + path = "/status/200?apikey=apikey333", headers = { ["Host"] = "test3.com" } @@ -330,7 +332,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do for i = 1, 8 do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/?apikey=apikey122", + path = "/status/200?apikey=apikey122", headers = { ["Host"] = "test3.com" } @@ -345,7 +347,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/?apikey=apikey122", + path = "/status/200?apikey=apikey122", headers = { ["Host"] = "test3.com" } @@ -358,7 +360,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do for i = 1, 6 do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/?apikey=apikey122", + path = "/status/200?apikey=apikey122", headers = { ["Host"] = "test4.com" } @@ -373,7 +375,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/?apikey=apikey122", + path = "/status/200?apikey=apikey122", headers = { ["Host"] = "test4.com" } @@ -389,7 +391,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do it("does not send rate-limit headers when hide_client_headers==true", function() local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "test5.com" } @@ -410,28 +412,30 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "failtest1_com", - hosts = { "failtest1.com" }, - upstream_url = "http://mockbin.com" + name = "failtest1_com", + hosts = { "failtest1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api1.id, config = { minute = 6, fault_tolerant = false } }) local api2 = assert(helpers.dao.apis:insert { - name = "failtest2_com", - hosts = { "failtest2.com" }, - upstream_url = "http://mockbin.com" + name = "failtest2_com", + hosts = { "failtest2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api2.id, - config = { minute = 6, fault_tolerant = true } + config = { minute = 6, fault_tolerant = true }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() @@ -443,7 +447,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do it("does not work if an error occurs", function() local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "failtest1.com" } @@ -458,7 +462,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do -- Make another request local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "failtest1.com" } @@ -470,7 +474,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do it("keeps working if an error occurs", function() local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "failtest2.com" } @@ -485,7 +489,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do -- Make another request local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "failtest2.com" } @@ -503,35 +507,37 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do helpers.kill_all() local api1 = assert(helpers.dao.apis:insert { - name = "failtest3_com", - hosts = { "failtest3.com" }, - upstream_url = "http://mockbin.com" + name = "failtest3_com", + hosts = { "failtest3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api1.id, - config = { minute = 6, policy = policy, redis_host = "5.5.5.5", fault_tolerant = false } + config = { minute = 6, policy = policy, redis_host = "5.5.5.5", fault_tolerant = false }, }) local api2 = assert(helpers.dao.apis:insert { - name = "failtest4_com", - hosts = { "failtest4.com" }, - upstream_url = "http://mockbin.com" + name = "failtest4_com", + hosts = { "failtest4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api2.id, - config = { minute = 6, policy = policy, redis_host = "5.5.5.5", fault_tolerant = true } + config = { minute = 6, policy = policy, redis_host = "5.5.5.5", fault_tolerant = true }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) it("does not work if an error occurs", function() -- Make another request local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "failtest3.com" } @@ -544,7 +550,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do -- Make another request local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "failtest4.com" } @@ -564,31 +570,33 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do helpers.run_migrations() api = assert(helpers.dao.apis:insert { - name = "expire1_com", - hosts = { "expire1.com" }, - upstream_url = "http://mockbin.com" + name = "expire1_com", + hosts = { "expire1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "rate-limiting", + name = "rate-limiting", api_id = api.id, config = { - minute = 6, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + minute = 6, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, fault_tolerant = false, - redis_database = REDIS_DATABASE - } + redis_database = REDIS_DATABASE, + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) describe("expires a counter", function() local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "expire1.com" } @@ -604,7 +612,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "expire1.com" } diff --git a/spec/03-plugins/25-response-rate-limiting/03-api_spec.lua b/spec/03-plugins/25-response-rate-limiting/03-api_spec.lua index 7529030765d7..eba106801d68 100644 --- a/spec/03-plugins/25-response-rate-limiting/03-api_spec.lua +++ b/spec/03-plugins/25-response-rate-limiting/03-api_spec.lua @@ -14,12 +14,14 @@ describe("Plugin: response-rate-limiting (API)", function() describe("POST", function() setup(function() assert(helpers.dao.apis:insert { - name = "test", - hosts = { "test1.com" }, - upstream_url = "http://mockbin.com" + name = "test", + hosts = { "test1.com" }, + upstream_url = helpers.mock_upstream_url, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) admin_client = helpers.admin_client() end) diff --git a/spec/03-plugins/25-response-rate-limiting/04-access_spec.lua b/spec/03-plugins/25-response-rate-limiting/04-access_spec.lua index 8767e854748d..f3d25e4f2e6d 100644 --- a/spec/03-plugins/25-response-rate-limiting/04-access_spec.lua +++ b/spec/03-plugins/25-response-rate-limiting/04-access_spec.lua @@ -72,159 +72,163 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do }) local api = assert(helpers.dao.apis:insert { - name = "test1_com", - hosts = { "test1.com" }, - upstream_url = "http://httpbin.org" + name = "test1_com", + hosts = { "test1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api.id, config = { fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, redis_database = REDIS_DATABASE, - limits = {video = {minute = 6}} + limits = { video = { minute = 6 } }, } }) api = assert(helpers.dao.apis:insert { - name = "test2_com", - hosts = { "test2.com" }, - upstream_url = "http://httpbin.org" + name = "test2_com", + hosts = { "test2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api.id, config = { fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, redis_database = REDIS_DATABASE, - limits = {video = {minute = 6, hour = 10}, image = {minute = 4}} + limits = { video = { minute = 6, hour = 10 }, + image = { minute = 4 } }, } }) api = assert(helpers.dao.apis:insert { - name = "test3_com", - hosts = { "test3.com" }, - upstream_url = "http://httpbin.org" + name = "test3_com", + hosts = { "test3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api.id + name = "key-auth", + api_id = api.id, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api.id, - config = {limits = {video = {minute = 6}}} + config = { limits = { video = { minute = 6 } } }, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", - api_id = api.id, + name = "response-ratelimiting", + api_id = api.id, consumer_id = consumer1.id, - config = { + config = { fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, redis_database = REDIS_DATABASE, - limits = {video = {minute = 2}} + limits = { video = { minute = 2 } }, } }) api = assert(helpers.dao.apis:insert { - name = "test4_com", - hosts = { "test4.com" }, - upstream_url = "http://httpbin.org" + name = "test4_com", + hosts = { "test4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api.id, config = { fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, redis_database = REDIS_DATABASE, - limits = {video = {minute = 6}, image = {minute = 4}} + limits = { video = { minute = 6 }, image = { minute = 4 } }, } }) api = assert(helpers.dao.apis:insert { - name = "test7_com", - hosts = { "test7.com" }, - upstream_url = "http://httpbin.org" + name = "test7_com", + hosts = { "test7.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api.id, config = { fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, redis_database = REDIS_DATABASE, block_on_first_violation = true, - limits = { + limits = { video = { minute = 6, - hour = 10 + hour = 10, }, image = { - minute = 4 - } - } + minute = 4, + }, + }, } }) api = assert(helpers.dao.apis:insert { - name = "test8_com", - hosts = { "test8.com" }, - upstream_url = "http://httpbin.org" + name = "test8_com", + hosts = { "test8.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api.id, config = { fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, redis_database = REDIS_DATABASE, - limits = {video = {minute = 6, hour = 10}, image = {minute = 4}} + limits = { video = { minute = 6, hour = 10 }, + image = { minute = 4 } }, } }) api = assert(helpers.dao.apis:insert { - name = "test9_com", - hosts = { "test9.com" }, - upstream_url = "http://httpbin.org" + name = "test9_com", + hosts = { "test9.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api.id, config = { - fault_tolerant = false, - policy = policy, + fault_tolerant = false, + policy = policy, hide_client_headers = true, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, - redis_password = REDIS_PASSWORD, - redis_database = REDIS_DATABASE, - limits = {video = {minute = 6}} + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, + redis_password = REDIS_PASSWORD, + redis_database = REDIS_DATABASE, + limits = { video = { minute = 6 } }, } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() @@ -412,9 +416,9 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do ["Host"] = "test8.com" } }) - local body = cjson.decode(assert.res_status(200, res)) - assert.equal(4, tonumber(body.headers["X-Ratelimit-Remaining-Image"])) - assert.equal(6, tonumber(body.headers["X-Ratelimit-Remaining-Video"])) + local json = cjson.decode(assert.res_status(200, res)) + assert.equal(4, tonumber(json.headers["x-ratelimit-remaining-image"])) + assert.equal(6, tonumber(json.headers["x-ratelimit-remaining-video"])) -- Actually consume the limits local res = assert(helpers.proxy_client():send { @@ -436,8 +440,8 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do } }) local body = cjson.decode(assert.res_status(200, res)) - assert.equal(3, tonumber(body.headers["X-Ratelimit-Remaining-Image"])) - assert.equal(4, tonumber(body.headers["X-Ratelimit-Remaining-Video"])) + assert.equal(3, tonumber(body.headers["x-ratelimit-remaining-image"])) + assert.equal(4, tonumber(body.headers["x-ratelimit-remaining-video"])) end) it("combines multiple x-kong-limit headers from upstream", function() @@ -523,42 +527,44 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "failtest1_com", - hosts = { "failtest1.com" }, - upstream_url = "http://httpbin.org" + name = "failtest1_com", + hosts = { "failtest1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api1.id, config = { fault_tolerant = false, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, - limits = {video = {minute = 6}} + limits = { video = { minute = 6} }, } }) local api2 = assert(helpers.dao.apis:insert { - name = "failtest2_com", - hosts = { "failtest2.com" }, - upstream_url = "http://httpbin.org" + name = "failtest2_com", + hosts = { "failtest2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "response-ratelimiting", api_id = api2.id, config = { fault_tolerant = true, - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, - limits = {video = {minute = 6}} + limits = { video = {minute = 6} } } }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) end) teardown(function() @@ -629,34 +635,34 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do before_each(function() local api1 = assert(helpers.dao.apis:insert { - name = "failtest3_com", - hosts = { "failtest3.com" }, - upstream_url = "http://mockbin.com" + name = "failtest3_com", + hosts = { "failtest3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api1.id, config = { fault_tolerant = false, - policy = policy, - redis_host = "5.5.5.5", - limits = {video = {minute = 6}} + policy = policy, + redis_host = "5.5.5.5", + limits = { video = { minute = 6 } }, } }) local api2 = assert(helpers.dao.apis:insert { - name = "failtest4_com", - hosts = { "failtest4.com" }, - upstream_url = "http://mockbin.com" + name = "failtest4_com", + hosts = { "failtest4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api2.id, config = { fault_tolerant = true, - policy = policy, - redis_host = "5.5.5.5", - limits = {video = {minute = 6}} + policy = policy, + redis_host = "5.5.5.5", + limits = { video = { minute = 6 } }, } }) end) @@ -665,7 +671,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do -- Make another request local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "failtest3.com" } @@ -678,7 +684,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do -- Make another request local res = assert(helpers.proxy_client():send { method = "GET", - path = "/status/200/", + path = "/status/200", headers = { ["Host"] = "failtest4.com" } @@ -696,23 +702,25 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do helpers.stop_kong() helpers.dao:drop_schema() helpers.run_migrations() - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) api = assert(helpers.dao.apis:insert { - name = "expire1_com", - hosts = { "expire1.com" }, - upstream_url = "http://httpbin.org" + name = "expire1_com", + hosts = { "expire1.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "response-ratelimiting", + name = "response-ratelimiting", api_id = api.id, config = { - policy = policy, - redis_host = REDIS_HOST, - redis_port = REDIS_PORT, + policy = policy, + redis_host = REDIS_HOST, + redis_port = REDIS_PORT, redis_password = REDIS_PASSWORD, fault_tolerant = false, - limits = {video = {minute = 6}} + limits = { video = { minute = 6 } }, } }) end) diff --git a/spec/03-plugins/26-oauth2/02-api_spec.lua b/spec/03-plugins/26-oauth2/02-api_spec.lua index 2e8fe2bb87be..48741a51ca27 100644 --- a/spec/03-plugins/26-oauth2/02-api_spec.lua +++ b/spec/03-plugins/26-oauth2/02-api_spec.lua @@ -7,7 +7,9 @@ describe("Plugin: oauth (API)", function() helpers.run_migrations() helpers.prepare_prefix() - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) admin_client = helpers.admin_client() end) @@ -20,9 +22,9 @@ describe("Plugin: oauth (API)", function() describe("/consumers/:consumer/oauth2/", function() setup(function() api = assert(helpers.dao.apis:insert { - name = "oauth2_token.com", - hosts = { "oauth2_token.com" }, - upstream_url = "http://mockbin.com" + name = "oauth2_token.com", + hosts = { "oauth2_token.com" }, + upstream_url = helpers.mock_upstream_url, }) consumer = assert(helpers.dao.consumers:insert { username = "bob" @@ -215,9 +217,9 @@ describe("Plugin: oauth (API)", function() setup(function() for i = 1, 3 do assert(helpers.dao.oauth2_credentials:insert { - name = "app" .. i, - redirect_uri = "https://mockbin.org", - consumer_id = consumer.id + name = "app" .. i, + redirect_uri = helpers.mock_upstream_ssl_url, + consumer_id = consumer.id, }) end end) @@ -243,9 +245,9 @@ describe("Plugin: oauth (API)", function() before_each(function() helpers.dao:truncate_table("oauth2_credentials") credential = assert(helpers.dao.oauth2_credentials:insert { - name = "test app", - redirect_uri = "https://mockbin.org", - consumer_id = consumer.id + name = "test app", + redirect_uri = helpers.mock_upstream_ssl_url, + consumer_id = consumer.id, }) end) describe("GET", function() @@ -384,9 +386,9 @@ describe("Plugin: oauth (API)", function() local oauth2_credential setup(function() oauth2_credential = assert(helpers.dao.oauth2_credentials:insert { - name = "Test APP", - redirect_uri = "https://mockin.com", - consumer_id = consumer.id + name = "Test APP", + redirect_uri = helpers.mock_upstream_ssl_url, + consumer_id = consumer.id, }) end) after_each(function() diff --git a/spec/03-plugins/26-oauth2/03-access_spec.lua b/spec/03-plugins/26-oauth2/03-access_spec.lua index 2e7bdec07658..684bfde659b5 100644 --- a/spec/03-plugins/26-oauth2/03-access_spec.lua +++ b/spec/03-plugins/26-oauth2/03-access_spec.lua @@ -39,9 +39,9 @@ local function provision_token(host, extra_headers, client_id, client_secret) local res = assert(request_client:send { method = "POST", path = "/oauth2/token", - body = { code = code, - client_id = client_id or "clientid123", - client_secret = client_secret or "secret123", + body = { code = code, + client_id = client_id or "clientid123", + client_secret = client_secret or "secret123", grant_type = "authorization_code" }, headers = utils.table_merge({ ["Host"] = host or "oauth2.com", @@ -98,215 +98,216 @@ describe("Plugin: oauth2 (access)", function() }) local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "oauth2.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "oauth2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api1.id, config = { - scopes = { "email", "profile", "user.email" }, + scopes = { "email", "profile", "user.email" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + }, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "mockbin-path.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "example-path.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api2.id, config = { - scopes = { "email", "profile" }, + scopes = { "email", "profile" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, } }) local api2bis = assert(helpers.dao.apis:insert { - name = "api-2-bis", - uris = { "/somepath" }, - upstream_url = "http://mockbin.com" + name = "api-2-bis", + uris = { "/somepath" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api2bis.id, config = { - scopes = { "email", "profile" }, + scopes = { "email", "profile" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + }, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "oauth2_3.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "oauth2_3.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api3.id, config = { - scopes = { "email", "profile" }, + scopes = { "email", "profile" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true, - hide_credentials = true - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + hide_credentials = true, + }, }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "oauth2_4.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "oauth2_4.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api4.id, config = { - scopes = { "email", "profile" }, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, + scopes = { "email", "profile" }, + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, enable_client_credentials = true, - enable_authorization_code = false - } + enable_authorization_code = false, + }, }) local api5 = assert(helpers.dao.apis:insert { - name = "api-5", - hosts = { "oauth2_5.com" }, - upstream_url = "http://mockbin.com" + name = "api-5", + hosts = { "oauth2_5.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api5.id, config = { - scopes = { "email", "profile" }, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_password_grant = true, - enable_authorization_code = false - } + scopes = { "email", "profile" }, + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_password_grant = true, + enable_authorization_code = false, + }, }) local api6 = assert(helpers.dao.apis:insert { - name = "api-6", - hosts = { "oauth2_6.com" }, - upstream_url = "http://mockbin.com" + name = "api-6", + hosts = { "oauth2_6.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api6.id, config = { - scopes = { "email", "profile", "user.email" }, - enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true, - accept_http_if_already_terminated = true - } + scopes = { "email", "profile", "user.email" }, + enable_authorization_code = true, + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + accept_http_if_already_terminated = true, + }, }) local api7 = assert(helpers.dao.apis:insert { - name = "api-7", - hosts = { "oauth2_7.com" }, - upstream_url = "http://mockbin.com" + name = "api-7", + hosts = { "oauth2_7.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api7.id, config = { - scopes = { "email", "profile", "user.email" }, + scopes = { "email", "profile", "user.email" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true, - anonymous = anonymous_user.id, - global_credentials = false - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + anonymous = anonymous_user.id, + global_credentials = false, + }, }) local api8 = assert(helpers.dao.apis:insert { - name = "oauth2_8.com", - hosts = { "oauth2_8.com" }, - upstream_url = "http://mockbin.com" + name = "oauth2_8.com", + hosts = { "oauth2_8.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api8.id, config = { - scopes = { "email", "profile", "user.email" }, + scopes = { "email", "profile", "user.email" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true, - global_credentials = true - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + global_credentials = true, + }, }) local api9 = assert(helpers.dao.apis:insert { - name = "oauth2_9.com", - hosts = { "oauth2_9.com" }, - upstream_url = "http://mockbin.com" + name = "oauth2_9.com", + hosts = { "oauth2_9.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api9.id, config = { - scopes = { "email", "profile", "user.email" }, + scopes = { "email", "profile", "user.email" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true, - global_credentials = true - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + global_credentials = true, + }, }) local api10 = assert(helpers.dao.apis:insert { - name = "oauth2_10.com", - hosts = { "oauth2_10.com" }, - upstream_url = "http://mockbin.com" + name = "oauth2_10.com", + hosts = { "oauth2_10.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api10.id, config = { - scopes = { "email", "profile", "user.email" }, + scopes = { "email", "profile", "user.email" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true, - global_credentials = true, - anonymous = utils.uuid(), -- a non existing consumer - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + global_credentials = true, + anonymous = utils.uuid(), -- a non existing consumer + }, }) assert(helpers.start_kong({ trusted_ips = "127.0.0.1", + nginx_conf = "spec/fixtures/custom_nginx.template", })) - proxy_client = helpers.proxy_client() + proxy_client = helpers.proxy_client() proxy_ssl_client = helpers.proxy_ssl_client() end) teardown(function() @@ -592,19 +593,19 @@ describe("Plugin: oauth2 (access)", function() end) it("fails with a path when using the DNS", function() local res = assert(proxy_ssl_client:send { - method = "POST", - path = "/oauth2/authorize", - body = { - provision_key = "provision123a", + method = "POST", + path = "/oauth2/authorize", + body = { + provision_key = "provision123a", authenticated_userid = "id123", - client_id = "clientid123", - scope = "email", - response_type = "code" + client_id = "clientid123", + scope = "email", + response_type = "code", }, headers = { - ["Host"] = "mockbin-path.com", - ["Content-Type"] = "application/json" - } + ["Host"] = "example-path.com", + ["Content-Type"] = "application/json", + }, }) local body = assert.res_status(400, res) local json = cjson.decode(body) @@ -2078,7 +2079,7 @@ describe("Plugin: oauth2 (access)", function() } }) local body = cjson.decode(assert.res_status(200, res)) - assert.equal(token.access_token, body.postData.params.access_token) + assert.equal(token.access_token, body.post_data.params.access_token) end) it("hides credentials in the body", function() local token = provision_token("oauth2_3.com") @@ -2095,7 +2096,7 @@ describe("Plugin: oauth2 (access)", function() } }) local body = cjson.decode(assert.res_status(200, res)) - assert.is_nil(body.postData.params.access_token) + assert.is_nil(body.post_data.params.access_token) end) it("does not hide credentials in the querystring", function() local token = provision_token() @@ -2108,7 +2109,7 @@ describe("Plugin: oauth2 (access)", function() } }) local body = cjson.decode(assert.res_status(200, res)) - assert.equal(token.access_token, body.queryString.access_token) + assert.equal(token.access_token, body.uri_args.access_token) end) it("hides credentials in the querystring", function() local token = provision_token("oauth2_3.com") @@ -2121,7 +2122,7 @@ describe("Plugin: oauth2 (access)", function() } }) local body = cjson.decode(assert.res_status(200, res)) - assert.is_nil(body.queryString.access_token) + assert.is_nil(body.uri_args.access_token) end) it("does not hide credentials in the header", function() local token = provision_token() @@ -2177,79 +2178,81 @@ describe("Plugin: oauth2 (access)", function() setup(function() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "logical-and.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-1", + hosts = { "logical-and.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api1.id, config = { - scopes = { "email", "profile", "user.email" }, + scopes = { "email", "profile", "user.email" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true, - global_credentials = false, - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + global_credentials = false, + }, }) assert(helpers.dao.plugins:insert { - name = "key-auth", - api_id = api1.id + name = "key-auth", + api_id = api1.id, }) anonymous = assert(helpers.dao.consumers:insert { - username = "Anonymous" + username = "Anonymous", }) user1 = assert(helpers.dao.consumers:insert { - username = "Mickey" + username = "Mickey", }) user2 = assert(helpers.dao.consumers:insert { - username = "Aladdin" + username = "Aladdin", }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "logical-or.com" }, - upstream_url = "http://mockbin.org/request" + name = "api-2", + hosts = { "logical-or.com" }, + upstream_url = helpers.mock_upstream_url .. "/request", }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api2.id, config = { - scopes = { "email", "profile", "user.email" }, + scopes = { "email", "profile", "user.email" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true, - global_credentials = false, - anonymous = anonymous.id, - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + global_credentials = false, + anonymous = anonymous.id, + }, }) assert(helpers.dao.plugins:insert { - name = "key-auth", + name = "key-auth", api_id = api2.id, config = { - anonymous = anonymous.id - } + anonymous = anonymous.id, + }, }) assert(helpers.dao.keyauth_credentials:insert { - key = "Mouse", - consumer_id = user1.id + key = "Mouse", + consumer_id = user1.id, }) assert(helpers.dao.oauth2_credentials:insert { - client_id = "clientid123", + client_id = "clientid123", client_secret = "secret123", - redirect_uri = "http://google.com/kong", - name = "testapp", - consumer_id = user2.id + redirect_uri = "http://google.com/kong", + name = "testapp", + consumer_id = user2.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() end) diff --git a/spec/03-plugins/26-oauth2/04-invalidations_spec.lua b/spec/03-plugins/26-oauth2/04-invalidations_spec.lua index 159f28975129..837b392ed709 100644 --- a/spec/03-plugins/26-oauth2/04-invalidations_spec.lua +++ b/spec/03-plugins/26-oauth2/04-invalidations_spec.lua @@ -8,36 +8,38 @@ describe("Plugin: oauth2 (invalidations)", function() helpers.dao:truncate_tables() local api = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "oauth2.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "oauth2.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "oauth2", + name = "oauth2", api_id = api.id, config = { - scopes = { "email", "profile" }, + scopes = { "email", "profile" }, enable_authorization_code = true, - mandatory_scope = true, - provision_key = "provision123", - token_expiration = 5, - enable_implicit_grant = true - } + mandatory_scope = true, + provision_key = "provision123", + token_expiration = 5, + enable_implicit_grant = true, + }, }) local consumer = assert(helpers.dao.consumers:insert { - username = "bob" + username = "bob", }) assert(helpers.dao.oauth2_credentials:insert { - client_id = "clientid123", + client_id = "clientid123", client_secret = "secret123", - redirect_uri = "http://google.com/kong", - name = "testapp", - consumer_id = consumer.id + redirect_uri = "http://google.com/kong", + name = "testapp", + consumer_id = consumer.id, }) - assert(helpers.start_kong()) - admin_client = helpers.admin_client() + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) + admin_client = helpers.admin_client() proxy_ssl_client = helpers.proxy_ssl_client() end) diff --git a/spec/03-plugins/27-request-termination/02-access_spec.lua b/spec/03-plugins/27-request-termination/02-access_spec.lua index 0ed62f2c8fc9..9880759f6047 100644 --- a/spec/03-plugins/27-request-termination/02-access_spec.lua +++ b/spec/03-plugins/27-request-termination/02-access_spec.lua @@ -8,83 +8,84 @@ describe("Plugin: request-termination (access)", function() helpers.run_migrations() local api1 = assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "api1.request-termination.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "api1.request-termination.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "request-termination", + name = "request-termination", api_id = api1.id, - config = { - } + config = {}, }) local api2 = assert(helpers.dao.apis:insert { - name = "api-2", - hosts = { "api2.request-termination.com" }, - upstream_url = "http://mockbin.com" + name = "api-2", + hosts = { "api2.request-termination.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "request-termination", + name = "request-termination", api_id = api2.id, config = { - status_code=404 - } + status_code = 404, + }, }) local api3 = assert(helpers.dao.apis:insert { - name = "api-3", - hosts = { "api3.request-termination.com" }, - upstream_url = "http://mockbin.com" + name = "api-3", + hosts = { "api3.request-termination.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "request-termination", + name = "request-termination", api_id = api3.id, config = { - status_code=406, - message="Invalid" - } + status_code = 406, + message = "Invalid", + }, }) local api4 = assert(helpers.dao.apis:insert { - name = "api-4", - hosts = { "api4.request-termination.com" }, - upstream_url = "http://mockbin.com" + name = "api-4", + hosts = { "api4.request-termination.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "request-termination", + name = "request-termination", api_id = api4.id, config = { - body="

Service is down for maintenance

" - } + body = "

Service is down for maintenance

", + }, }) local api5 = assert(helpers.dao.apis:insert { - name = "api-5", - hosts = { "api5.request-termination.com" }, - upstream_url = "http://mockbin.com" + name = "api-5", + hosts = { "api5.request-termination.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "request-termination", + name = "request-termination", api_id = api5.id, config = { - status_code=451, - content_type="text/html", - body="

Service is down due to content infringement

" - } + status_code = 451, + content_type = "text/html", + body = "

Service is down due to content infringement

", + }, }) local api6 = assert(helpers.dao.apis:insert { - name = "api-6", - hosts = { "api6.request-termination.com" }, - upstream_url = "http://mockbin.com" + name = "api-6", + hosts = { "api6.request-termination.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { - name = "request-termination", + name = "request-termination", api_id = api6.id, config = { - status_code=503, - body='{"code": 1, "message": "Service unavailable"}' - } + status_code = 503, + body = '{"code": 1, "message": "Service unavailable"}', + }, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() admin_client = helpers.admin_client() end) @@ -110,7 +111,7 @@ describe("Plugin: request-termination (access)", function() local json = cjson.decode(body) assert.same({ message = "Service unavailable" }, json) end) - + it("status code with default message", function() local res = assert(client:send { method = "GET", @@ -123,7 +124,7 @@ describe("Plugin: request-termination (access)", function() local json = cjson.decode(body) assert.same({ message = "Not found" }, json) end) - + it("status code with custom message", function() local res = assert(client:send { method = "GET", @@ -151,7 +152,7 @@ describe("Plugin: request-termination (access)", function() local body = assert.res_status(503, res) assert.equal([[

Service is down for maintenance

]], body) end) - + it("status code with default message", function() local res = assert(client:send { method = "GET", @@ -163,7 +164,7 @@ describe("Plugin: request-termination (access)", function() local body = assert.res_status(451, res) assert.equal([[

Service is down due to content infringement

]], body) end) - + it("status code with custom message", function() local res = assert(client:send { method = "GET", diff --git a/spec/03-plugins/27-request-termination/03-integration_spec.lua b/spec/03-plugins/27-request-termination/03-integration_spec.lua index b52de211c699..3151206b4af6 100644 --- a/spec/03-plugins/27-request-termination/03-integration_spec.lua +++ b/spec/03-plugins/27-request-termination/03-integration_spec.lua @@ -8,23 +8,25 @@ describe("Plugin: request-termination (integration)", function() helpers.run_migrations() assert(helpers.dao.apis:insert { - name = "api-1", - hosts = { "api1.request-termination.com" }, - upstream_url = "http://mockbin.com" + name = "api-1", + hosts = { "api1.request-termination.com" }, + upstream_url = helpers.mock_upstream_url, }) assert(helpers.dao.plugins:insert { name = "key-auth", }) consumer1 = assert(helpers.dao.consumers:insert { - username = "bob" + username = "bob", }) assert(helpers.dao.keyauth_credentials:insert { - key = "kong", - consumer_id = consumer1.id + key = "kong", + consumer_id = consumer1.id, }) - assert(helpers.start_kong()) + assert(helpers.start_kong({ + nginx_conf = "spec/fixtures/custom_nginx.template", + })) client = helpers.proxy_client() admin_client = helpers.admin_client() end) @@ -52,7 +54,7 @@ describe("Plugin: request-termination (integration)", function() }, }) assert.response(res).has.status(201) - + -- verify access being blocked res = assert(client:send { method = "GET", diff --git a/spec/fixtures/custom_nginx.template b/spec/fixtures/custom_nginx.template index fbf4f279ecfa..eadfa3196096 100644 --- a/spec/fixtures/custom_nginx.template +++ b/spec/fixtures/custom_nginx.template @@ -64,10 +64,6 @@ http { kong.init_worker() } - rewrite_by_lua_block { - kong.rewrite() - } - proxy_next_upstream_tries 999; upstream kong_upstream { @@ -116,6 +112,10 @@ http { set $upstream_x_forwarded_host ''; set $upstream_x_forwarded_port ''; + rewrite_by_lua_block { + kong.rewrite() + } + access_by_lua_block { kong.access() } @@ -247,19 +247,22 @@ http { ssl_certificate_key ${{SSL_CERT_KEY}}; ssl_protocols TLSv1.1 TLSv1.2; + set_real_ip_from 127.0.0.1; + location = / { content_by_lua_block { local mu = require "spec.fixtures.mock_upstream" return mu.send_default_json_response({ valid_routes = { - ["/ws"] = "Websocket echo server", - ["/get"] = "Accepts a GET request and returns it in JSON format", - ["/post"] = "Accepts a POST request and returns it in JSON format", - ["/anything"] = "Accepts any request and returns it in JSON format", - ["/request"] = "Alias to /anything", - ["/delay/:duration"] = "Delay the response for seconds", - ["/basic-auth/:user/:pass"] = "Performs HTTP basic authentication with the given credentials", - ["/status/:code"] = "Returns a response with the specified " + ["/ws"] = "Websocket echo server", + ["/get"] = "Accepts a GET request and returns it in JSON format", + ["/post"] = "Accepts a POST request and returns it in JSON format", + ["/response-headers?:key=:val"] = "Returns given response headers", + ["/anything"] = "Accepts any request and returns it in JSON format", + ["/request"] = "Alias to /anything", + ["/delay/:duration"] = "Delay the response for seconds", + ["/basic-auth/:user/:pass"] = "Performs HTTP basic authentication with the given credentials", + ["/status/:code"] = "Returns a response with the specified " }, }) } @@ -289,11 +292,22 @@ http { return mu.filter_access_by_method("POST") } content_by_lua_block { - local mu = require "spec.fixtures.mock_upstream" + local mu = require "spec.fixtures.mock_upstream" return mu.send_default_json_response() } } + location = /response-headers { + access_by_lua_block { + local mu = require "spec.fixtures.mock_upstream" + return mu.filter_access_by_method("GET") + } + content_by_lua_block { + local mu = require "spec.fixtures.mock_upstream" + return mu.send_default_json_response({}, ngx.req.get_uri_args()) + } + } + location ~ "^/basic-auth/(?[a-zA-Z0-9_]+)/(?.+)$" { access_by_lua_block { local mu = require "spec.fixtures.mock_upstream" @@ -326,7 +340,7 @@ http { ngx.sleep(delay_seconds) - return mu.send_json_response({ + return mu.send_default_json_response({ delay = delay_seconds, }) } @@ -340,20 +354,12 @@ http { return ngx.exit(ngx.HTTP_NOT_FOUND) end ngx.status = code - if code == 418 then - local teapot = [[ - -=[ teapot ]=- - _...._ - .' _ _ `. - | ."` ^ `". _, - \_;`"---"`|// - | ;/ - \_ _/ - `"""`]] - ngx.header["X-More-Info"] = "http://tools.ietf.org/html/rfc2324" - return mu.send_text_response(teapot) - end + return mu.send_default_json_response({ + code = code, + }) } } + + } } diff --git a/spec/fixtures/mock_upstream.lua b/spec/fixtures/mock_upstream.lua index 81d238c429ae..cc56d8bcc3d1 100644 --- a/spec/fixtures/mock_upstream.lua +++ b/spec/fixtures/mock_upstream.lua @@ -1,12 +1,84 @@ local utils = require "kong.tools.utils" local cjson_safe = require "cjson.safe" +local cjson = require "cjson" +local ws_server = require "resty.websocket.server" +local pl_stringx = require "pl.stringx" + + +local function parse_multipart_form_params(body, content_type) + if not content_type then + return nil, 'missing content-type' + end + + local m, err = ngx.re.match(content_type, "boundary=(.+)", "oj") + if not m or not m[1] or err then + return nil, "could not find boundary in content type " .. content_type .. + "error: " .. tostring(err) + end + + local boundary = m[1] + local parts_split = utils.split(body, '--' .. boundary) + local params = {} + local part, from, to, part_value, part_name, part_headers, first_header + for i = 1, #parts_split do + part = pl_stringx.strip(parts_split[i]) + + if part ~= '' and part ~= '--' then + from, to, err = ngx.re.find(part, '^\\r$', 'ojm') + if err or (not from and not to) then + return nil, nil, "could not find part body. Error: " .. tostring(err) + end + + part_value = part:sub(to + 2, #part) -- +2: trim leading line jump + part_headers = part:sub(1, from - 1) + first_header = utils.split(part_headers, '\\n')[1] + if pl_stringx.startswith(first_header:lower(), "content-disposition") then + local m, err = ngx.re.match(first_header, 'name="(.*?)"', "oj") + + if err or not m or not m[1] then + return nil, "could not parse part name. Error: " .. tostring(err) + end + + part_name = m[1] + else + return nil, "could not find part name in: " .. part_headers + end + + params[part_name] = part_value + end + end + + return params +end + + +local function send_text_response(text, content_type, headers) + headers = headers or {} + content_type = content_type or "text/plain" + + text = ngx.req.get_method() == "HEAD" and "" or tostring(text) + + ngx.header["X-Powered-By"] = "mock_upstream" + ngx.header["Content-Length"] = #text + 1 + ngx.header["Content-Type"] = content_type + + for header,value in pairs(headers) do + if type(value) == "table" then + ngx.header[header] = table.concat(value, ", ") + else + ngx.header[header] = value + end + end + + return ngx.say(text) +end + local function filter_access_by_method(method) if ngx.req.get_method() ~= method then ngx.status = ngx.HTTP_NOT_ALLOWED - ngx.header["X-Powered-By"] = "mock_upstream" - ngx.say("The method is not allowed for the requested URL") - return ngx.exit(ngx.HTTP_NOT_ALLOWED) + send_text_response("Method not allowed for the requested URL") + return ngx.exit(ngx.OK) end end @@ -61,18 +133,6 @@ local function filter_access_by_basic_auth(expected_username, end -local function send_text_response(text, content_type) - content_type = content_type or "text/plain" - ngx.header["X-Powered-By"] = "mock_upstream" - - text = ngx.req.get_method() == "HEAD" and "" or tostring(text) - - ngx.header["Content-Length"] = #text + 1 - ngx.header["Content-Type"] = content_type - return ngx.say(text) -end - - local function get_ngx_vars() local var = ngx.var return { @@ -104,6 +164,8 @@ end local function get_body_data() local req = ngx.req + + req.read_body() local data = req.get_body_data() if data then return data @@ -117,58 +179,69 @@ local function get_body_data() return data end - return nil, "could not read body data or body file" + return "" end +local function get_post_data(content_type) + local text = get_body_data() + local kind = "unknown" + local params = cjson_safe.null + local err -local function get_default_json_response() - local req = ngx.req - local headers = req.get_headers(0) - local data, form, params = "", {}, cjson_safe.null - local ct = headers["content-type"] - if ct then - req.read_body() - if string.find(ct, "application/x-www-form-urlencoded", nil, true) then - form = req.get_post_args() - - elseif string.find(ct, "application/json", nil, true) then - local err - data, err = get_body_data() - if not data then - ngx.log(ngx.ERR, "could not read body data: ", err) - return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) - end - -- ignore decoding errors - params = cjson_safe.decode(data) or cjson_safe.null + if type(content_type) == "string" then + if content_type:find("application/x-www-form-urlencoded", nil, true) then + + kind = "form" + params, err = ngx.req.get_post_args() + + elseif content_type:find("multipart/form-data", nil, true) then + kind = "multipart-form" + params, err = parse_multipart_form_params(text, content_type) + + elseif content_type:find("application/json", nil, true) then + kind = "json" + params, err = cjson_safe.decode(text) + end + + params = params or cjson_safe.null + + if err then + kind = kind .. " (error)" + err = tostring(err) end end + return { text = text, kind = kind, params = params, error = err } +end + + +local function get_default_json_response() + local headers = ngx.req.get_headers(0) + local vars = get_ngx_vars() + return { - args = ngx.req.get_uri_args(), - data = data, - form = form, - headers = headers, - params = params, - url = string.format("%s://%s%s", ngx.var.scheme, - ngx.var.host, ngx.var.request_uri), - vars = get_ngx_vars(), + headers = headers, + post_data = get_post_data(headers["Content-Type"]), + url = ("%s://%s:%s%s"):format(vars.scheme, vars.host, + vars.server_port, vars.request_uri), + uri_args = ngx.req.get_uri_args(), + vars = vars, } end -local function send_default_json_response(extra) - local cjson = require "cjson" - local tbl = utils.table_merge(get_default_json_response(), extra) - return send_text_response(cjson.encode(tbl), "application/json") +local function send_default_json_response(extra_fields, response_headers) + local tbl = utils.table_merge(get_default_json_response(), extra_fields) + return send_text_response(cjson.encode(tbl), + "application/json", response_headers) end local function serve_web_sockets() - local server = require "resty.websocket.server" - local wb, err = server:new{ - timeout = 5000, + local wb, err = ws_server:new({ + timeout = 5000, max_payload_len = 65535, - } + }) if not wb then ngx.log(ngx.ERR, "failed to open websocket: ", err) diff --git a/spec/helpers.lua b/spec/helpers.lua index 3d4401050161..ed202e63b0c1 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -470,11 +470,7 @@ local function modifier_request(state, arguments, level) if lookup((res.headers or {}),"X-Powered-By") ~= "mock_upstream" then - if lookup((res.headers or {}),"X-Powered-By") ~= "mockbin" then - if type(body.url) ~= "string" or not body.url:find("//httpbin.org") then - error("Could not determine the response to be from mock_upstream, mockbin or httpbin") - end - end + error("Could not determine the response to be from mock_upstream") end rawset(state, "kong_request", request) @@ -631,8 +627,7 @@ luassert:register("assertion", "res_status", res_status, --- Checks and returns a json body of an http response/request. Only checks -- validity of the json, does not check appropriate headers. Setting the target --- to check can be done through `request` or `response` (requests are only --- supported by mock_upstream and mockbin.com) +-- to check can be done through `request` or `response` -- @name jsonbody -- @return the decoded json as a table -- @usage @@ -655,19 +650,12 @@ local function jsonbody(state, args) else local r = rawget(state, "kong_request") - if r.params then -- mock_upstream - local json = {params = r.params, data = r.data} - return true, {json} - - elseif r.postData and r.postData.text then -- mockbin - local text = r.postData.text - local json, err = cjson.decode(text) - if not json then - table.insert(args, 1, "Error decoding: " .. tostring(err) .. "\nRequest body:" .. text) - args.n = 1 - return false - end - return true, {json} + if r.post_data + and (r.post_data.kind == "json" or r.post_data.kind == "json (error)") + and r.post_data.params + then + local pd = r.post_data + return true, { { params = pd.params, data = pd.text, error = pd.error, kind = pd.kind } } else error("No json data found in the request") @@ -734,11 +722,8 @@ local function req_query_param(state, args) local req = rawget(state, "kong_request") assert(req, "'queryparam' assertion only works with a request object") local params - if type(req.args) == "table" then -- mock_upstream && httpbin - params = req.args - - elseif type(req.queryString) == "table" then -- mockbin - params = req.queryString + if type(req.uri_args) == "table" then + params = req.uri_args else error("No query parameters found in request object") @@ -769,7 +754,7 @@ luassert:register("assertion", "queryparam", req_query_param, "assertion.req_query_param.positive") --- --- Adds an assertion to look for a urlencoded form parameter in a mockbin request. +-- Adds an assertion to look for a urlencoded form parameter in a request. -- Parameter name comparison is done case-insensitive. Use the `request` modifier to set -- the request to operate on. -- @name formparam @@ -778,17 +763,15 @@ luassert:register("assertion", "queryparam", req_query_param, local function req_form_param(state, args) local param = args[1] local req = rawget(state, "kong_request") - assert(req, "'formparam' assertion can only be used with a mock_upstream/mockbin/httpbin request object") + assert(req, "'formparam' assertion can only be used with a mock_upstream request object") local value - if req.form then -- mock_upstream / httpbin - value = lookup(req.form or {}, param) - - elseif req.postData then -- mockbin - value = lookup((req.postData or {}).params, param) - + if req.post_data + and (req.post_data.kind == "form" or req.post_data.kind == "multipart-form") + then + value = lookup(req.post_data.params or {}, param) else - error("Could not determine the request to be from either mock_upstream, mockbin.com or httpbin.org") + error("Could not determine the request to be from either mock_upstream") end table.insert(args, 1, req)