Skip to content

Commit 03391c4

Browse files
authored
Merge pull request #19 from stone-payments/release/v0.4.0
[master] Release/v0.4.0
2 parents c4f68cf + b38a07a commit 03391c4

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ 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+
## [0.4.0](https://github.com/stone-payments/kong-plugin-url-rewrite/tree/v0.4.0) - 2018-12-26
9+
### Added
10+
- support to parameter replacing when rewriting URL.
11+
812
## [0.3.0](https://github.com/stone-payments/kong-plugin-url-rewrite/tree/v0.3.0) - 2018-10-30
913
### Added
1014
- Travis CI and deploy stage.
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.3.0-1"
2+
version = "0.4.0-0"
33
source = {
44
url = "git://github.com/stone-payments/kong-plugin-url-rewrite",
55
}

url-rewrite/handler.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,22 @@ function URLRewriter:new()
88
URLRewriter.super.new(self, "url-rewriter")
99
end
1010

11+
function resolveUrlParams(requestParams, url)
12+
for paramValue in requestParams do
13+
local requestParamValue = ngx.ctx.router_matches.uri_captures[paramValue]
14+
url = url:gsub("<" .. paramValue .. ">", requestParamValue)
15+
end
16+
return url
17+
end
18+
19+
function getRequestUrlParams(url)
20+
return string.gmatch(url, "<(.-)>")
21+
end
22+
1123
function URLRewriter:access(config)
1224
URLRewriter.super.access(self)
13-
ngx.var.upstream_uri = config.url
25+
requestParams = getRequestUrlParams(config.url)
26+
ngx.var.upstream_uri = resolveUrlParams(requestParams, config.url)
1427
end
1528

1629
return URLRewriter

url-rewrite/spec/handler_spec.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ local ngx = {
33
log = spy.new(function() end),
44
var = {
55
upstream_uri = "mock"
6+
},
7+
ctx = {
8+
router_matches = {
9+
uri_captures = {}
10+
}
611
}
712
}
13+
814
_G.ngx = ngx
915

1016
local URLRewriter = require('../handler')
@@ -25,4 +31,26 @@ describe("TestHandler", function()
2531
URLRewriter:access(config)
2632
assert.equal('new_url', ngx.var.upstream_uri)
2733
end)
34+
35+
it("should test rewrite of upstream_uri with params", function()
36+
URLRewriter:new()
37+
ngx.ctx.router_matches.uri_captures["code_parameter"] = "123456"
38+
config = {
39+
url = "new_url/<code_parameter>"
40+
}
41+
URLRewriter:access(config)
42+
assert.equal('new_url/123456', ngx.var.upstream_uri)
43+
end)
44+
45+
it("should replace url params", function()
46+
URLRewriter:new()
47+
local mockUrl = "url/<param1>/<param2>"
48+
local iter = getRequestUrlParams(mockUrl)
49+
ngx.ctx.router_matches.uri_captures["param1"] = 123456
50+
ngx.ctx.router_matches.uri_captures["param2"] = "test"
51+
52+
local result = resolveUrlParams(iter, mockUrl)
53+
54+
assert.equal("url/123456/test", result)
55+
end)
2856
end)

0 commit comments

Comments
 (0)