Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Error loading policy chain configuration JSON with null value [PR #626](https://github.com/3scale/apicast/pull/626)
- Splitted `resolv.conf` in lines,to avoid commented lines [PR #618](https://github.com/3scale/apicast/pull/618)
- Avoid `nameserver` repetion from `RESOLVER` variable and `resolv.conf` file [PR #636](https://github.com/3scale/apicast/pull/636)
- Bug in URL rewriting policy that ignored the `commands` attribute in the policy manifest [PR #641](https://github.com/3scale/apicast/pull/641)

## Added

Expand Down
4 changes: 2 additions & 2 deletions gateway/src/apicast/policy/url_rewriting/url_rewriting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ end
-- the URL, it will be the last command applied.
function _M.new(config)
local self = new()
self.config = config or {}
self.commands = (config and config.commands) or {}
return self
end

function _M:rewrite()
for _, command in ipairs(self.config) do
for _, command in ipairs(self.commands) do
local rewritten = apply_rewrite_command(command)

if rewritten and command['break'] then
Expand Down
26 changes: 18 additions & 8 deletions spec/policy/url_rewriting/url_rewriting_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ describe('URL rewriting policy', function()

it('can rewrite URLs using sub', function()
local config_with_sub = {
{ op = 'sub', regex = 'to_be_replaced', replace = 'new' }
commands = {
{ op = 'sub', regex = 'to_be_replaced', replace = 'new' }
}
}
local url_rewriting = URLRewriting.new(config_with_sub)

Expand All @@ -24,7 +26,9 @@ describe('URL rewriting policy', function()

it('can rewrite URLs using gsub', function()
local config_with_gsub = {
{ op = 'gsub', regex = 'to_be_replaced', replace = 'new' }
commands = {
{ op = 'gsub', regex = 'to_be_replaced', replace = 'new' }
}
}
local url_rewriting = URLRewriting.new(config_with_gsub)

Expand All @@ -35,9 +39,11 @@ describe('URL rewriting policy', function()

it('applies the commands in order', function()
local config_with_several_ops = {
{ op = 'gsub', regex = 'to_be_replaced', replace = 'abc' },
{ op = 'gsub', regex = 'abc', replace = 'def' },
{ op = 'gsub', regex = 'def', replace = 'ghi' }
commands = {
{ op = 'gsub', regex = 'to_be_replaced', replace = 'abc' },
{ op = 'gsub', regex = 'abc', replace = 'def' },
{ op = 'gsub', regex = 'def', replace = 'ghi' }
}
}
local url_rewriting = URLRewriting.new(config_with_several_ops)

Expand All @@ -48,8 +54,10 @@ describe('URL rewriting policy', function()

it('when there is a break, stops at the first match', function()
local config_with_break = {
{ op = 'gsub', regex = 'to_be_replaced', replace = 'abc', ['break'] = '1' },
{ op = 'gsub', regex = 'abc', replace = 'def' } -- Not applied
commands = {
{ op = 'gsub', regex = 'to_be_replaced', replace = 'abc', ['break'] = '1' },
{ op = 'gsub', regex = 'abc', replace = 'def' } -- Not applied
}
}
local url_rewriting = URLRewriting.new(config_with_break)

Expand All @@ -60,7 +68,9 @@ describe('URL rewriting policy', function()

it('accepts options for the regexes, same as ngx.req.{sub, gsub}', function()
local config_with_regex_opts = {
{ op = 'gsub', regex = 'TO_BE_REPLACED', replace = 'new', options = 'i' }
commands = {
{ op = 'gsub', regex = 'TO_BE_REPLACED', replace = 'new', options = 'i' }
}
}
local url_rewriting = URLRewriting.new(config_with_regex_opts)

Expand Down
25 changes: 15 additions & 10 deletions t/apicast-policy-url-rewriting.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ __DATA__
{ "name": "apicast.policy.apicast" },
{
"name": "apicast.policy.url_rewriting",
"configuration":
[
"configuration": {
"commands": [
{ "op": "sub", "regex": "original", "replace": "new" }
]
}
}
]
}
Expand Down Expand Up @@ -78,10 +79,11 @@ yay, api backend
{ "name": "apicast.policy.apicast" },
{
"name": "apicast.policy.url_rewriting",
"configuration":
[
"configuration": {
"commands": [
{ "op": "gsub", "regex": "original", "replace": "new" }
]
}
}
]
}
Expand Down Expand Up @@ -128,12 +130,13 @@ Substitutions are applied in the order specified.
{ "name": "apicast.policy.apicast" },
{
"name": "apicast.policy.url_rewriting",
"configuration":
[
"configuration": {
"commands": [
{ "op": "gsub", "regex": "aaa", "replace": "bbb", "options": "i" },
{ "op": "sub", "regex": "bbb", "replace": "ccc" },
{ "op": "sub", "regex": "ccc", "replace": "ddd" }
]
}
}
]
}
Expand Down Expand Up @@ -182,13 +185,14 @@ We need to test 2 things:
{ "name": "apicast.policy.apicast" },
{
"name": "apicast.policy.url_rewriting",
"configuration":
[
"configuration": {
"commands": [
{ "op": "sub", "regex": "does_not_match", "replace": "a", "break": true },
{ "op": "sub", "regex": "aaa", "replace": "bbb" },
{ "op": "sub", "regex": "bbb", "replace": "ccc", "break": true },
{ "op": "sub", "regex": "ccc", "replace": "ddd" }
]
}
}
]
}
Expand Down Expand Up @@ -236,10 +240,11 @@ rules.
"policy_chain": [
{
"name": "apicast.policy.url_rewriting",
"configuration":
[
"configuration": {
"commands": [
{ "op": "sub", "regex": "original", "replace": "new" }
]
}
},
{ "name": "apicast.policy.apicast" }
]
Expand Down