Fix getUriPath logic to ignore API tenant base path (#363)
diff --git a/README.md b/README.md
index 3b6ff6c..73b2b40 100644
--- a/README.md
+++ b/README.md
@@ -104,11 +104,7 @@
### Testing
- First install the necessary dependencies:
+ To build the test image and run unit tests
```
- make test-build
- ```
- Then, run the unit tests:
- ```
- make test-run
+ make test
```
diff --git a/scripts/lua/policies/backendRouting.lua b/scripts/lua/policies/backendRouting.lua
index c1efbad..646de94 100644
--- a/scripts/lua/policies/backendRouting.lua
+++ b/scripts/lua/policies/backendRouting.lua
@@ -41,7 +41,7 @@
u.path = utils.concatStrings({u.path:sub(1, -16), u.path:sub(-16, -16) == '/' and '' or '/', gatewayPath})
ngx.req.set_uri(u.path)
else
- ngx.req.set_uri(getUriPath(u.path))
+ ngx.req.set_uri(_M.getUriPath(u.path))
end
ngx.var.backendUrl = backendUrl
@@ -74,7 +74,7 @@
u = url.parse(utils.concatStrings({'http://', dynamicBackend}))
end
if utils.tableContains(whitelist, u.host) then
- ngx.req.set_uri(getUriPath(u.path))
+ ngx.req.set_uri(_M.getUriPath(u.path))
-- Split the dynamicBackend url to get the query parameters in the exact order that it was passed in.
-- Don't use u.query here because it returns the parameters in an unordered lua table.
local split = {string.match(dynamicBackend, '([^?]*)?(.*)')}
@@ -91,10 +91,12 @@
end
end
-function getUriPath(backendPath)
+function _M.getUriPath(backendPath)
local gatewayPath = ngx.unescape_uri(ngx.var.gatewayPath)
gatewayPath = gatewayPath:gsub('-', '%%-')
+ local tenant = ngx.var.tenant:gsub('-', '%%-')
local uri = string.gsub(ngx.var.request_uri, '?.*', '')
+ uri = uri:gsub('/api/' .. tenant, '')
local _, j = uri:find(gatewayPath)
local incomingPath = ((j and uri:sub(j + 1)) or nil)
-- Check for backendUrl path
diff --git a/tests/scripts/lua/policies/backendRouting.lua b/tests/scripts/lua/policies/backendRouting.lua
index 06b812f..2f3e10b 100644
--- a/tests/scripts/lua/policies/backendRouting.lua
+++ b/tests/scripts/lua/policies/backendRouting.lua
@@ -28,17 +28,29 @@
end)
it('should work without override', function()
- ngx.var.request_uri = "/api/23bc46b1-71f6-4ed5-8c54-816aa4f8c502/hello/world"
- backendRouting.setRoute("https://localhost:3233/api/v1/web/guest/default/hello2.json", "hello/world")
+ ngx.var.gatewayPath = 'hello/world'
+ ngx.var.tenant = '23bc46b1-71f6-4ed5-8c54-816aa4f8c502'
+ ngx.var.request_uri = '/api/' .. ngx.var.tenant .. '/' .. ngx.var.gatewayPath
+ backendRouting.setRoute("https://localhost:3233/api/v1/web/guest/default/hello2.json", ngx.var.gatewayPath)
assert.are.same(ngx.var.upstream, 'https://localhost:3233')
assert.are.same(ngx.var.backendUrl, 'https://localhost:3233/api/v1/web/guest/default/hello2.json')
end)
it('should work with override', function()
- ngx.var.request_uri = "/api/23bc46b1-71f6-4ed5-8c54-816aa4f8c502/hello/world"
- backendRouting.setRouteWithOverride("https://localhost:3233/api/v1/web/guest/default/hello2.json", "hello/world",
- "http://172.0.0.1:3456")
- assert.are.same(ngx.var.upstream, 'http://172.0.0.1:3456')
- assert.are.same(ngx.var.backendUrl, 'http://172.0.0.1:3456/api/v1/web/guest/default/hello2.json')
- end)
+ ngx.var.gatewayPath = 'hello/world'
+ ngx.var.tenant = '23bc46b1-71f6-4ed5-8c54-816aa4f8c502'
+ ngx.var.request_uri = '/api/' .. ngx.var.tenant .. '/' .. ngx.var.gatewayPath
+ backendRouting.setRouteWithOverride("https://localhost:3233/api/v1/web/guest/default/hello2.json", ngx.var.gatewayPath,
+ "http://172.0.0.1:3456")
+ assert.are.same(ngx.var.upstream, 'http://172.0.0.1:3456')
+ assert.are.same(ngx.var.backendUrl, 'http://172.0.0.1:3456/api/v1/web/guest/default/hello2.json')
+ end)
+
+ it('should match URI properly, ignoring API tenant base path', function()
+ ngx.var.gatewayPath = 'api'
+ ngx.var.tenant = '23bc46b1-71f6-4ed5-8c54-816aa4f8c502'
+ ngx.var.request_uri = '/api/' .. ngx.var.tenant .. '/' .. ngx.var.gatewayPath
+ actual = backendRouting.getUriPath('/api')
+ assert.are.same(actual, '/api')
+ end)
end)