Skip to content

Commit 17c3e92

Browse files
author
Pedro Fadel
authored
Merge pull request #27 from stone-payments/release/v0.5.0
[master] Release/v0.5.0
2 parents 03391c4 + fbcef3b commit 17c3e92

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ All changes made on any release of this project should be commented on high leve
55
Document model based on [Semantic Versioning](http://semver.org/).
66
Examples of how to use this _markdown_ cand be found here [Keep a CHANGELOG](http://keepachangelog.com/).
77

8+
9+
## [0.5.0](https://github.com/stone-payments/kong-plugin-url-rewrite/tree/v0.5.0) - 2020-03-05
10+
### Added
11+
- Support to querystring field.
12+
813
## [0.4.0](https://github.com/stone-payments/kong-plugin-url-rewrite/tree/v0.4.0) - 2018-12-26
914
### Added
10-
- support to parameter replacing when rewriting URL.
15+
- Support to parameter replacing when rewriting URL.
1116

1217
## [0.3.0](https://github.com/stone-payments/kong-plugin-url-rewrite/tree/v0.3.0) - 2018-10-30
1318
### Added
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "kong-plugin-url-rewrite"
2-
version = "0.4.0-0"
2+
version = "0.5.0-0"
33
source = {
44
url = "git://github.com/stone-payments/kong-plugin-url-rewrite",
55
}

url-rewrite/handler.lua

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ local URLRewriter = BasePlugin:extend()
44

55
URLRewriter.PRIORITY = 700
66

7+
function split(s, delimiter)
8+
result = {}
9+
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
10+
table.insert(result, match)
11+
end
12+
return result
13+
end
14+
715
function URLRewriter:new()
816
URLRewriter.super.new(self, "url-rewriter")
917
end
@@ -22,8 +30,22 @@ end
2230

2331
function URLRewriter:access(config)
2432
URLRewriter.super.access(self)
33+
34+
if config.query_string ~= nil then
35+
local args = ngx.req.get_uri_args()
36+
for k, queryString in ipairs(config.query_string) do
37+
local splitted = split(queryString, '=')
38+
local key, value = splitted[1], splitted[2]
39+
local queryParams = getRequestUrlParams(value)
40+
local resolvedParams = resolveUrlParams(queryParams, value)
41+
args[key] = resolvedParams
42+
end
43+
ngx.req.set_uri_args(args)
44+
end
45+
2546
requestParams = getRequestUrlParams(config.url)
26-
ngx.var.upstream_uri = resolveUrlParams(requestParams, config.url)
47+
local url = resolveUrlParams(requestParams, config.url)
48+
ngx.var.upstream_uri = url
2749
end
2850

2951
return URLRewriter

url-rewrite/schema.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
return {
22
no_consumer = true,
33
fields = {
4-
url = {required = true, type = "string"}
4+
url = {required = true, type = "string"},
5+
query_string = {required = false, type = "table"}
56
}
67
}

url-rewrite/spec/handler_spec.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ local ngx = {
88
router_matches = {
99
uri_captures = {}
1010
}
11+
},
12+
req = {
13+
get_uri_args = spy.new(function() return {} end),
14+
set_uri_args = spy.new(function() end)
1115
}
1216
}
1317

@@ -53,4 +57,22 @@ describe("TestHandler", function()
5357

5458
assert.equal("url/123456/test", result)
5559
end)
60+
61+
it("should add querystring params when schema has query_string field", function()
62+
URLRewriter:new()
63+
ngx.ctx.router_matches.uri_captures["code_parameter"] = "123456"
64+
config = {
65+
url = "new_url",
66+
query_string = {
67+
"code_parameter=<code_parameter>",
68+
"parameter2=abcdef",
69+
}
70+
}
71+
URLRewriter:access(config)
72+
local expected = {
73+
code_parameter = "123456",
74+
parameter2 = "abcdef",
75+
}
76+
assert.spy(ngx.req.set_uri_args).was_called_with(expected)
77+
end)
5678
end)

0 commit comments

Comments
 (0)