Tests for backend url override support (#350)
* Tests for backend url override support
* Add EOL
diff --git a/scripts/lua/policies/backendRouting.lua b/scripts/lua/policies/backendRouting.lua
index 8ef79e5..c1efbad 100644
--- a/scripts/lua/policies/backendRouting.lua
+++ b/scripts/lua/policies/backendRouting.lua
@@ -28,6 +28,10 @@
--- Set upstream based on the backendUrl
function _M.setRoute(backendUrl, gatewayPath)
+ _M.setRouteWithOverride(backendUrl, gatewayPath, backendOverride)
+end
+
+function _M.setRouteWithOverride(backendUrl, gatewayPath, override)
local u = url.parse(backendUrl)
if u.scheme == nil then
u = url.parse(utils.concatStrings({'http://', backendUrl}))
@@ -42,8 +46,8 @@
ngx.var.backendUrl = backendUrl
-- if there is a backend override then use that instead of actual backend from swagger
- if backendOverride ~= nil then
- local bou = url.parse(backendOverride)
+ if override ~= nil then
+ local bou = url.parse(override)
u.scheme = bou.scheme
u:setAuthority(bou.authority)
diff --git a/tests/scripts/lua/policies/backendRouting.lua b/tests/scripts/lua/policies/backendRouting.lua
new file mode 100644
index 0000000..06b812f
--- /dev/null
+++ b/tests/scripts/lua/policies/backendRouting.lua
@@ -0,0 +1,44 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local backendRouting = require 'policies/backendRouting'
+local fakengx = require 'fakengx'
+
+describe('Testing backend routing module', function()
+ before_each(function()
+ _G.ngx = fakengx.new()
+ ngx.var.backendUrl = ''
+ ngx.var.upstream = ''
+ ngx.req.set_uri = function(uri)
+ end
+ 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")
+ 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)
+end)