Skip to content

Commit 5852bc4

Browse files
authored
Merge pull request #641 from 3scale/fix-bug-respect-manifest-url-rewriting
Fix bug in URL rewriting policy that makes it ignore a field of its own manifest [THREESCALE-731]
2 parents 7cdca78 + fa5cb61 commit 5852bc4

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Error loading policy chain configuration JSON with null value [PR #626](https://github.com/3scale/apicast/pull/626)
1717
- Splitted `resolv.conf` in lines,to avoid commented lines [PR #618](https://github.com/3scale/apicast/pull/618)
1818
- Avoid `nameserver` repetion from `RESOLVER` variable and `resolv.conf` file [PR #636](https://github.com/3scale/apicast/pull/636)
19+
- Bug in URL rewriting policy that ignored the `commands` attribute in the policy manifest [PR #641](https://github.com/3scale/apicast/pull/641)
1920

2021
## Added
2122

gateway/src/apicast/policy/url_rewriting/url_rewriting.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ end
5858
-- the URL, it will be the last command applied.
5959
function _M.new(config)
6060
local self = new()
61-
self.config = config or {}
61+
self.commands = (config and config.commands) or {}
6262
return self
6363
end
6464

6565
function _M:rewrite()
66-
for _, command in ipairs(self.config) do
66+
for _, command in ipairs(self.commands) do
6767
local rewritten = apply_rewrite_command(command)
6868

6969
if rewritten and command['break'] then

spec/policy/url_rewriting/url_rewriting_spec.lua

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ describe('URL rewriting policy', function()
1212

1313
it('can rewrite URLs using sub', function()
1414
local config_with_sub = {
15-
{ op = 'sub', regex = 'to_be_replaced', replace = 'new' }
15+
commands = {
16+
{ op = 'sub', regex = 'to_be_replaced', replace = 'new' }
17+
}
1618
}
1719
local url_rewriting = URLRewriting.new(config_with_sub)
1820

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

2527
it('can rewrite URLs using gsub', function()
2628
local config_with_gsub = {
27-
{ op = 'gsub', regex = 'to_be_replaced', replace = 'new' }
29+
commands = {
30+
{ op = 'gsub', regex = 'to_be_replaced', replace = 'new' }
31+
}
2832
}
2933
local url_rewriting = URLRewriting.new(config_with_gsub)
3034

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

3640
it('applies the commands in order', function()
3741
local config_with_several_ops = {
38-
{ op = 'gsub', regex = 'to_be_replaced', replace = 'abc' },
39-
{ op = 'gsub', regex = 'abc', replace = 'def' },
40-
{ op = 'gsub', regex = 'def', replace = 'ghi' }
42+
commands = {
43+
{ op = 'gsub', regex = 'to_be_replaced', replace = 'abc' },
44+
{ op = 'gsub', regex = 'abc', replace = 'def' },
45+
{ op = 'gsub', regex = 'def', replace = 'ghi' }
46+
}
4147
}
4248
local url_rewriting = URLRewriting.new(config_with_several_ops)
4349

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

4955
it('when there is a break, stops at the first match', function()
5056
local config_with_break = {
51-
{ op = 'gsub', regex = 'to_be_replaced', replace = 'abc', ['break'] = '1' },
52-
{ op = 'gsub', regex = 'abc', replace = 'def' } -- Not applied
57+
commands = {
58+
{ op = 'gsub', regex = 'to_be_replaced', replace = 'abc', ['break'] = '1' },
59+
{ op = 'gsub', regex = 'abc', replace = 'def' } -- Not applied
60+
}
5361
}
5462
local url_rewriting = URLRewriting.new(config_with_break)
5563

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

6169
it('accepts options for the regexes, same as ngx.req.{sub, gsub}', function()
6270
local config_with_regex_opts = {
63-
{ op = 'gsub', regex = 'TO_BE_REPLACED', replace = 'new', options = 'i' }
71+
commands = {
72+
{ op = 'gsub', regex = 'TO_BE_REPLACED', replace = 'new', options = 'i' }
73+
}
6474
}
6575
local url_rewriting = URLRewriting.new(config_with_regex_opts)
6676

t/apicast-policy-url-rewriting.t

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ __DATA__
2929
{ "name": "apicast.policy.apicast" },
3030
{
3131
"name": "apicast.policy.url_rewriting",
32-
"configuration":
33-
[
32+
"configuration": {
33+
"commands": [
3434
{ "op": "sub", "regex": "original", "replace": "new" }
3535
]
36+
}
3637
}
3738
]
3839
}
@@ -78,10 +79,11 @@ yay, api backend
7879
{ "name": "apicast.policy.apicast" },
7980
{
8081
"name": "apicast.policy.url_rewriting",
81-
"configuration":
82-
[
82+
"configuration": {
83+
"commands": [
8384
{ "op": "gsub", "regex": "original", "replace": "new" }
8485
]
86+
}
8587
}
8688
]
8789
}
@@ -128,12 +130,13 @@ Substitutions are applied in the order specified.
128130
{ "name": "apicast.policy.apicast" },
129131
{
130132
"name": "apicast.policy.url_rewriting",
131-
"configuration":
132-
[
133+
"configuration": {
134+
"commands": [
133135
{ "op": "gsub", "regex": "aaa", "replace": "bbb", "options": "i" },
134136
{ "op": "sub", "regex": "bbb", "replace": "ccc" },
135137
{ "op": "sub", "regex": "ccc", "replace": "ddd" }
136138
]
139+
}
137140
}
138141
]
139142
}
@@ -182,13 +185,14 @@ We need to test 2 things:
182185
{ "name": "apicast.policy.apicast" },
183186
{
184187
"name": "apicast.policy.url_rewriting",
185-
"configuration":
186-
[
188+
"configuration": {
189+
"commands": [
187190
{ "op": "sub", "regex": "does_not_match", "replace": "a", "break": true },
188191
{ "op": "sub", "regex": "aaa", "replace": "bbb" },
189192
{ "op": "sub", "regex": "bbb", "replace": "ccc", "break": true },
190193
{ "op": "sub", "regex": "ccc", "replace": "ddd" }
191194
]
195+
}
192196
}
193197
]
194198
}
@@ -236,10 +240,11 @@ rules.
236240
"policy_chain": [
237241
{
238242
"name": "apicast.policy.url_rewriting",
239-
"configuration":
240-
[
243+
"configuration": {
244+
"commands": [
241245
{ "op": "sub", "regex": "original", "replace": "new" }
242246
]
247+
}
243248
},
244249
{ "name": "apicast.policy.apicast" }
245250
]

0 commit comments

Comments
 (0)