Skip to content

Commit

Permalink
Policy: Fixes on Status Codes overwrite policies:
Browse files Browse the repository at this point in the history
- Fix jsonschema format.
- Fix some test typos.
- Add unittest
- Rename policy
- Add Readme file
- Add changelog entry

FIX THREESCALE-6255

Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
  • Loading branch information
eloycoto committed Nov 23, 2020
1 parent 57459d1 commit 3855a5b
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added a new metric when the `worker_process` starts [PR #1228](https://github.com/3scale/APIcast/pull/1228) [THREESCALE-5965](https://issues.redhat.com/browse/THREESCALE-5965)
- Caching policy disable default field [PR #1226](https://github.com/3scale/APIcast/pull/1226) [THREESCALE-1514](https://issues.redhat.com/browse/THREESCALE-1514)
- Add response/request content size limits [PR #1227](https://github.com/3scale/APIcast/pull/1227) [THREESCALE-5244](https://issues.redhat.com/browse/THREESCALE-5244)
- Add HTTP codes policy [PR #1236](https://github.com/3scale/APIcast/pull/1236) [THREESCALE-6255](https://issues.redhat.com/browse/THREESCALE-6255)



Expand Down
22 changes: 22 additions & 0 deletions gateway/src/apicast/policy/statuscode_overwrite/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# StatusCode overwrite

This policy changes the upstream status code with the desired ones.

## Examples

Change status 200 to 201

```
{
"name": "statuscode_overwrite",
"version": "builtin",
"configuration": {
"http_codes": [
{
"upstream": 200,
"apicast": 201
}
]
}
}
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://apicast.io/policy-v1.1/schema#manifest#",
"name": "HTTP Code Overwrite",
"name": "HTTP Status Code Overwrite",
"summary": "Modify the HTTP status code returned by the upstream",
"description":
["Configures a 1-1 mapping for upstream's http codes."],
Expand All @@ -13,22 +13,33 @@
"type": "array",
"items": {
"type": "object",
"required": [
"upstream",
"apicast"
],
"properties": {
"upstream": {
"description": "Upstream HTTP code to replace",
"type": "integer"
"type": "integer",
"minimum": 100,
"maximum": 600
},
"apicast": {
"title": "Return HTTP code",
"description": "HTTP code to return",
"type": "integer"
"type": "integer",
"minimum": 100,
"maximum": 600
}
}
}

}
},
"properties": {
"http_codes": { "$ref": "#/definitions/codes" }
"http_codes": {
"title": "HTTP status codes map",
"$ref": "#/definitions/codes" }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ local new = _M.new
function _M.new(config)
local self = new(config)
self.http_codes = {}
for _, code in ipairs(config.http_codes) do
for _, code in ipairs(config.http_codes or {}) do
self.http_codes[code.upstream] = code.apicast
end

return self
end

function _M:header_filter()
ngx.status = self.http_codes[ngx.status] or ngx.status
end

return _M
return _M
Empty file.
53 changes: 53 additions & 0 deletions spec/policy/statuscode_overwrite/statuscode_overwrite_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local StatusCodeOverwrite = require('apicast.policy.statuscode_overwrite')

describe('IP Check policy', function()
before_each(function()
end)

it('mapped correctly valid status code', function()
local policy = StatusCodeOverwrite.new({
http_codes = {
{ upstream=200, apicast=201 }
}
})

ngx.status = 200
policy:header_filter()
assert.same(ngx.status, 201)

ngx.status = 403
policy:header_filter()
assert.same(ngx.status, 403)

end)

it('invalid code fails correctly', function()
assert.has_error(function()
return StatusCodeOverwrite.new({
http_codes = {
{ upstream=1, apicast=201 }
}})
end)

end)


it('Upstream code is not defined', function()
assert.has_error(function()
return StatusCodeOverwrite.new({
http_codes = {
{ apicast=201 }
}})
end)
end)

it('Apicast code is not defined', function()
assert.has_error(function()
return StatusCodeOverwrite.new({
http_codes = {
{ upstream=201 }
}})
end)
end)

end)
6 changes: 3 additions & 3 deletions t/apicast-http-code.t → t/apicast-statuscode_overwrite.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ __DATA__
"proxy": {
"policy_chain": [
{
"name": "code_overwrite",
"name": "statuscode_overwrite",
"version": "builtin",
"configuration": {
"http_codes": [
Expand Down Expand Up @@ -76,7 +76,7 @@ __DATA__
--- error_code eval
[ 201, 401, 401 ]
=== TEST 1: HTTP CODES policy placed after apicast
=== TEST 2: HTTP CODES policy placed after apicast
--- configuration
{
"services": [
Expand All @@ -91,7 +91,7 @@ __DATA__
"name": "apicast.policy.apicast"
},
{
"name": "code_overwrite",
"name": "statuscode_overwrite",
"version": "builtin",
"configuration": {
"http_codes": [
Expand Down

0 comments on commit 3855a5b

Please sign in to comment.