-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
309 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
return { | ||
default = { | ||
verbose = true, | ||
coverage = false, | ||
coverage = true, | ||
output = "gtest", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
include = { | ||
"%/kong%-plugin%-oasvalidator%/kong%/.+$", | ||
"%/kong%-plugin%/kong%/.+$", | ||
} | ||
|
||
statsfile = "/kong-plugin-oasvalidator/luacov.stats.out" | ||
reportfile = "/kong-plugin-oasvalidator/luacov.report.out" | ||
runreport = true | ||
statsfile = "/kong-plugin/luacov.stats.out" | ||
reportfile = "/kong-plugin/luacov.report.out" | ||
runreport = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
-- Copyright (c) 2023 Muhammad Nawaz | ||
-- Licensed under the MIT License. See LICENSE file for more information. | ||
-- [ END OF LICENSE e2f4afc94de80d2c104519cd5b5e65ca0f4d5b62 ] | ||
|
||
local helpers = require "spec.helpers" | ||
local cjson = require "cjson" | ||
|
||
|
||
local PLUGIN_NAME = "oasvalidator" | ||
|
||
|
||
for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then | ||
describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function() | ||
local client | ||
|
||
lazy_setup(function() | ||
|
||
local bp = helpers.get_db_utils(strategy == "off" and "postgres" or strategy, nil, { PLUGIN_NAME }) | ||
|
||
-- Inject a test route. No need to create a service, there is a default | ||
-- service which will echo the request. | ||
local route1 = bp.routes:insert({ | ||
hosts = { "test1.com" }, | ||
}) | ||
-- add the plugin to test to the route we created | ||
bp.plugins:insert { | ||
name = PLUGIN_NAME, | ||
route = { id = route1.id }, | ||
config = { oas_spec_path = "/data/openAPI_example.json", | ||
validate_request = false, | ||
validate_path_params = true,}, | ||
} | ||
|
||
-- start kong | ||
assert(helpers.start_kong({ | ||
-- set the strategy | ||
database = strategy, | ||
-- use the custom test template to create a local mock server | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
-- make sure our plugin gets loaded | ||
plugins = "bundled," .. PLUGIN_NAME, | ||
-- write & load declarative config, only if 'strategy=off' | ||
declarative_config = strategy == "off" and helpers.make_yaml_file() or nil, | ||
})) | ||
end) | ||
|
||
lazy_teardown(function() | ||
helpers.stop_kong(nil, true) | ||
end) | ||
|
||
before_each(function() | ||
client = helpers.proxy_client() | ||
end) | ||
|
||
after_each(function() | ||
if client then client:close() end | ||
end) | ||
|
||
describe("Invalid:", function() | ||
it("Path param", function() | ||
local r = client:get("/test/integer_simple_true/not_an_integer", { | ||
headers = { | ||
host = "test1.com" | ||
} | ||
}) | ||
-- validate that the request is unsuccessful, response status 400 | ||
assert.response(r).has.status(400) | ||
local body = assert.res_status(400, r) | ||
local json = cjson.decode(body) | ||
assert.equal("INVALID_PATH_PARAM", json["errorCode"]) | ||
end) | ||
end) | ||
|
||
describe("Invalid:", function() | ||
it("Path param", function() | ||
local r = client:get("/test/integer_simple_false/not_an_integer", { | ||
headers = { | ||
host = "test1.com" | ||
} | ||
}) | ||
-- validate that the request is unsuccessful, response status 400 | ||
assert.response(r).has.status(400) | ||
local body = assert.res_status(400, r) | ||
local json = cjson.decode(body) | ||
assert.equal("INVALID_PATH_PARAM", json["errorCode"]) | ||
end) | ||
end) | ||
|
||
describe("Invalid:", function() | ||
it("Path param", function() | ||
local r = client:get("/test/integer_label_true/123", { | ||
headers = { | ||
host = "test1.com" | ||
} | ||
}) | ||
-- validate that the request is unsuccessful, response status 400 | ||
assert.response(r).has.status(400) | ||
local body = assert.res_status(400, r) | ||
local json = cjson.decode(body) | ||
assert.equal("INVALID_PATH_PARAM", json["errorCode"]) | ||
end) | ||
end) | ||
|
||
end) | ||
|
||
end end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
-- Copyright (c) 2023 Muhammad Nawaz | ||
-- Licensed under the MIT License. See LICENSE file for more information. | ||
-- [ END OF LICENSE e2f4afc94de80d2c104519cd5b5e65ca0f4d5b62 ] | ||
|
||
local helpers = require "spec.helpers" | ||
local cjson = require "cjson" | ||
|
||
|
||
local PLUGIN_NAME = "oasvalidator" | ||
|
||
|
||
for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then | ||
describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function() | ||
local client | ||
|
||
lazy_setup(function() | ||
|
||
local bp = helpers.get_db_utils(strategy == "off" and "postgres" or strategy, nil, { PLUGIN_NAME }) | ||
|
||
-- Inject a test route. No need to create a service, there is a default | ||
-- service which will echo the request. | ||
local route1 = bp.routes:insert({ | ||
hosts = { "test1.com" }, | ||
}) | ||
-- add the plugin to test to the route we created | ||
bp.plugins:insert { | ||
name = PLUGIN_NAME, | ||
route = { id = route1.id }, | ||
config = { oas_spec_path = "/data/openAPI_example.json", | ||
validate_request = false, | ||
validate_query_params = true,}, | ||
} | ||
|
||
-- start kong | ||
assert(helpers.start_kong({ | ||
-- set the strategy | ||
database = strategy, | ||
-- use the custom test template to create a local mock server | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
-- make sure our plugin gets loaded | ||
plugins = "bundled," .. PLUGIN_NAME, | ||
-- write & load declarative config, only if 'strategy=off' | ||
declarative_config = strategy == "off" and helpers.make_yaml_file() or nil, | ||
})) | ||
end) | ||
|
||
lazy_teardown(function() | ||
helpers.stop_kong(nil, true) | ||
end) | ||
|
||
before_each(function() | ||
client = helpers.proxy_client() | ||
end) | ||
|
||
after_each(function() | ||
if client then client:close() end | ||
end) | ||
|
||
describe("Invalid:", function() | ||
it("Query param", function() | ||
local r = client:get("/test/query_array_integer_form_true?124.4565", { | ||
headers = { | ||
host = "test1.com" | ||
} | ||
}) | ||
-- validate that the request is unsuccessful, response status 400 | ||
assert.response(r).has.status(400) | ||
local body = assert.res_status(400, r) | ||
local json = cjson.decode(body) | ||
assert.equal("INVALID_QUERY_PARAM", json["errorCode"]) | ||
end) | ||
end) | ||
|
||
describe("Invalid:", function() | ||
it("Query param", function() | ||
local r = client:get("/test/query_array_integer_form_true?not_a_number", { | ||
headers = { | ||
host = "test1.com" | ||
} | ||
}) | ||
-- validate that the request is unsuccessful, response status 400 | ||
assert.response(r).has.status(400) | ||
local body = assert.res_status(400, r) | ||
local json = cjson.decode(body) | ||
assert.equal("INVALID_QUERY_PARAM", json["errorCode"]) | ||
end) | ||
end) | ||
|
||
end) | ||
|
||
end end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
-- Copyright (c) 2023 Muhammad Nawaz | ||
-- Licensed under the MIT License. See LICENSE file for more information. | ||
-- [ END OF LICENSE e2f4afc94de80d2c104519cd5b5e65ca0f4d5b62 ] | ||
|
||
local helpers = require "spec.helpers" | ||
local cjson = require "cjson" | ||
|
||
|
||
local PLUGIN_NAME = "oasvalidator" | ||
|
||
|
||
for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then | ||
describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function() | ||
local client | ||
|
||
lazy_setup(function() | ||
|
||
local bp = helpers.get_db_utils(strategy == "off" and "postgres" or strategy, nil, { PLUGIN_NAME }) | ||
|
||
-- Inject a test route. No need to create a service, there is a default | ||
-- service which will echo the request. | ||
local route1 = bp.routes:insert({ | ||
hosts = { "test1.com" }, | ||
}) | ||
-- add the plugin to test to the route we created | ||
bp.plugins:insert { | ||
name = PLUGIN_NAME, | ||
route = { id = route1.id }, | ||
config = { oas_spec_path = "/data/openAPI_example.json", | ||
validate_request = true }, | ||
} | ||
|
||
-- start kong | ||
assert(helpers.start_kong({ | ||
-- set the strategy | ||
database = strategy, | ||
-- use the custom test template to create a local mock server | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
-- make sure our plugin gets loaded | ||
plugins = "bundled," .. PLUGIN_NAME, | ||
-- write & load declarative config, only if 'strategy=off' | ||
declarative_config = strategy == "off" and helpers.make_yaml_file() or nil, | ||
})) | ||
end) | ||
|
||
lazy_teardown(function() | ||
helpers.stop_kong(nil, true) | ||
end) | ||
|
||
before_each(function() | ||
client = helpers.proxy_client() | ||
end) | ||
|
||
after_each(function() | ||
if client then client:close() end | ||
end) | ||
|
||
-- Valid request | ||
describe("valid request", function() | ||
it("A test valid request", function() | ||
local r = client:get("/test/query_two_integer_form_mixed?param1=123¶m2=6", { | ||
headers = { | ||
host = "test1.com" | ||
} | ||
}) | ||
-- validate that the request succeeded, response status 404 as no valid upstream server present | ||
assert.response(r).has.status(404) | ||
end) | ||
end) | ||
|
||
describe("invalid path", function() | ||
it("An invalid test request", function() | ||
local r = client:get("/invalid/path", { | ||
headers = { | ||
host = "test1.com" | ||
} | ||
}) | ||
-- validate that the request is unsuccessful, response status 400 | ||
assert.response(r).has.status(400) | ||
local body = assert.res_status(400, r) | ||
local json = cjson.decode(body) | ||
assert.equal("INVALID_ROUTE", json["errorCode"]) | ||
end) | ||
end) | ||
|
||
-- Invalid query param | ||
describe("invalid query param", function() | ||
it("An invalid test request", function() | ||
local r = client:get("/test/query_two_integer_form_mixed?param1=123¶m2=not_a_num", { | ||
headers = { | ||
host = "test1.com" | ||
} | ||
}) | ||
-- validate that the request is unsuccessful, response status 400 | ||
assert.response(r).has.status(400) | ||
local body = assert.res_status(400, r) | ||
local json = cjson.decode(body) | ||
assert.equal("INVALID_QUERY_PARAM", json["errorCode"]) | ||
end) | ||
end) | ||
|
||
end) | ||
|
||
end end |