-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/plugin request termination #2051
Closed
pauldaustin
wants to merge
23
commits into
Kong:next
from
pauldaustin:feature/plugin-request-termination
Closed
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f3cd113
feat(schema): regex for numbers
pauldaustin fb608ba
feat(tools): HTTP Response 503 Service unavailable
pauldaustin ff4e2b8
Code style changes
pauldaustin 97a24d5
Implemented tests and updated comments
pauldaustin 330ab87
Revert "feat(schema): regex for numbers"
pauldaustin 1dc3467
feat(plugin): request-termination
pauldaustin 55d74be
feat(schema): regex for numbers
pauldaustin c2bfe7e
feat(tools): HTTP Response 503 Service unavailable
pauldaustin 4d0802c
Code style changes
pauldaustin 1ea22c5
Implemented tests and updated comments
pauldaustin 6ee52b5
Revert "feat(schema): regex for numbers"
pauldaustin a641fd3
feat(plugin): request-termination
pauldaustin 651a669
Merge branch 'feature/plugin-request-termination' of
pauldaustin 8ada13f
Removed custom table definitions as the config is stored directly in
pauldaustin 4b5f437
in-line default vakue for status_code
pauldaustin c2f9133
removed links to request-termination lua files that were no longer
pauldaustin cf8ad0b
Duplicate imports
pauldaustin 43a9c0c
Merge remote-tracking branch 'kong/next' into feature/plugin-request-…
pauldaustin 174e9ce
Changed the list of built in plugins to be one per line. This makes it
pauldaustin 26476d7
Implemented changes as requested in pull request
pauldaustin 69c59f0
Removed accidental commit of kong.conf
pauldaustin 759fcd8
Renamed variable error to err
pauldaustin d918411
Merge remote-tracking branch 'upstream/next' into feature/plugin-requ…
pauldaustin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
local responses = require "kong.tools.responses" | ||
local meta = require "kong.meta" | ||
|
||
local server_header = meta._NAME.."/"..meta._VERSION | ||
|
||
local RequestTerminationHandler = BasePlugin:extend() | ||
|
||
RequestTerminationHandler.PRIORITY = 1 | ||
|
||
function RequestTerminationHandler:new() | ||
RequestTerminationHandler.super.new(self, "request-termination") | ||
end | ||
|
||
function RequestTerminationHandler:access(conf) | ||
RequestTerminationHandler.super.access(self) | ||
|
||
local status_code = conf.status_code | ||
local content_type = conf.content_type | ||
local body = conf.body | ||
local message = conf.message | ||
if body then | ||
ngx.status = status_code | ||
|
||
if not content_type then | ||
content_type = "application/json; charset=utf-8"; | ||
end | ||
ngx.header["Content-Type"] = content_type | ||
ngx.header["Server"] = server_header | ||
|
||
ngx.say(body) | ||
|
||
return ngx.exit(status_code) | ||
else | ||
return responses.send(status_code, message) | ||
end | ||
end | ||
|
||
return RequestTerminationHandler |
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,31 @@ | ||
local Errors = require "kong.dao.errors" | ||
local utils = require "kong.tools.utils" | ||
|
||
return { | ||
no_consumer = true, | ||
fields = { | ||
status_code = { type = "number", default = 503 }, | ||
message = { type = "string" }, | ||
content_type = { type = "string" }, | ||
body = { type = "string" }, | ||
}, | ||
self_check = function(schema, plugin_t, dao, is_updating) | ||
if plugin_t.status_code then | ||
if plugin_t.status_code < 100 or plugin_t.status_code > 599 then | ||
return false, Errors.schema("status_code must be between 100..599") | ||
end | ||
end | ||
|
||
if plugin_t.message then | ||
if plugin_t.content_type or plugin_t.body then | ||
return false, Errors.schema("message cannot be used with content_type or body") | ||
end | ||
else | ||
if plugin_t.content_type and not plugin_t.body then | ||
return false, Errors.schema("content_type requires a body") | ||
end | ||
end | ||
|
||
return true | ||
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
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
local schemas_validation = require "kong.dao.schemas_validation" | ||
local schema = require "kong.plugins.request-termination.schema" | ||
|
||
local v = schemas_validation.validate_entity | ||
|
||
describe("Plugin: request-termination (schema)", function() | ||
it("should accept a valid status_code", function() | ||
assert(v({status_code = 404}, schema)) | ||
end) | ||
it("should accept a valid message", function() | ||
assert(v({message = "Not found"}, schema)) | ||
end) | ||
it("should accept a valid content_type", function() | ||
assert(v({content_type = "text/html",body = "<body><h1>Not found</h1>"}, schema)) | ||
end) | ||
it("should accept a valid body", function() | ||
assert(v({body = "<body><h1>Not found</h1>"}, schema)) | ||
end) | ||
|
||
describe("errors", function() | ||
it("status_code should only accept numbers", function() | ||
local ok, error = v({status_code = "abcd"}, schema) | ||
assert.same({status_code = "status_code is not a number"}, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please do not use |
||
assert.False(ok) | ||
end) | ||
it("status_code < 100", function() | ||
local ok, _, error = v({status_code = "99"}, schema) | ||
assert.False(ok) | ||
assert.same("status_code must be between 100..599", error.message) | ||
end) | ||
it("status_code > 599", function() | ||
local ok, _, error = v({status_code = "600"}, schema) | ||
assert.False(ok) | ||
assert.same("status_code must be between 100..599", error.message) | ||
end) | ||
it("message with body", function() | ||
local ok, _, error = v({message = "error", body = "test"}, schema) | ||
assert.False(ok) | ||
assert.same("message cannot be used with content_type or body", error.message) | ||
end) | ||
it("message with body and content_type", function() | ||
local ok, _, error = v({message = "error", content_type="text/html", body = "test"}, schema) | ||
assert.False(ok) | ||
assert.same("message cannot be used with content_type or body", error.message) | ||
end) | ||
it("message with content_type", function() | ||
local ok, _, error = v({message = "error", content_type="text/html"}, schema) | ||
assert.False(ok) | ||
assert.same("message cannot be used with content_type or body", error.message) | ||
end) | ||
it("content_type without body", function() | ||
local ok, _, error = v({content_type="text/html"}, schema) | ||
assert.False(ok) | ||
assert.same("content_type requires a body", error.message) | ||
end) | ||
end) | ||
end) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just combine those 2 lines