2020-06-06 15:07:06 +00:00
local sticky_balanced
local sticky_persistent
2018-08-21 18:22:44 +00:00
local cookie = require ( " resty.cookie " )
local util = require ( " util " )
2018-08-23 18:09:08 +00:00
local original_ngx = ngx
2021-07-29 21:23:19 +00:00
local function reset_sticky_balancer ( )
package.loaded [ " balancer.sticky " ] = nil
package.loaded [ " balancer.sticky_balanced " ] = nil
package.loaded [ " balancer.sticky_persistent " ] = nil
sticky_balanced = require ( " balancer.sticky_balanced " )
sticky_persistent = require ( " balancer.sticky_persistent " )
end
local function mock_ngx ( mock , after_mock_set )
2018-08-23 18:02:42 +00:00
local _ngx = mock
2021-07-29 21:23:19 +00:00
setmetatable ( _ngx , { __index = ngx } )
2018-08-23 18:02:42 +00:00
_G.ngx = _ngx
2021-07-29 21:23:19 +00:00
if after_mock_set then
after_mock_set ( )
end
-- Balancer module caches ngx module, must be reset after mocks were configured.
reset_sticky_balancer ( )
2018-08-21 18:22:44 +00:00
end
2018-08-23 18:09:08 +00:00
local function reset_ngx ( )
_G.ngx = original_ngx
2021-07-29 21:23:19 +00:00
-- Ensure balancer cache is reset.
_G.ngx . ctx.balancer = nil
2020-06-06 15:07:06 +00:00
end
2018-08-21 18:22:44 +00:00
function get_mocked_cookie_new ( )
2019-08-30 09:40:29 +00:00
local o = { value = nil }
local mock = {
get = function ( self , n ) return self.value end ,
set = function ( self , c ) self.value = c.value ; return true , nil end
}
setmetatable ( o , mock )
mock.__index = mock
2018-08-23 18:02:42 +00:00
return function ( self )
2019-08-30 09:40:29 +00:00
return o ;
2018-08-23 18:02:42 +00:00
end
2018-08-21 18:22:44 +00:00
end
cookie.new = get_mocked_cookie_new ( )
local function get_test_backend ( )
2018-08-23 18:02:42 +00:00
return {
name = " access-router-production-web-80 " ,
endpoints = {
{ address = " 10.184.7.40 " , port = " 8080 " , maxFails = 0 , failTimeout = 0 } ,
} ,
sessionAffinityConfig = {
name = " cookie " ,
cookieSessionAffinity = { name = " test_name " , hash = " sha1 " }
} ,
}
2018-08-21 18:22:44 +00:00
end
describe ( " Sticky " , function ( )
2018-08-23 18:09:08 +00:00
before_each ( function ( )
2018-12-06 08:01:08 +00:00
mock_ngx ( { var = { location_path = " / " , host = " test.com " } } )
2018-08-23 18:09:08 +00:00
end )
after_each ( function ( )
reset_ngx ( )
end )
2018-09-17 02:52:21 +00:00
2018-08-23 18:02:42 +00:00
local test_backend = get_test_backend ( )
local test_backend_endpoint = test_backend.endpoints [ 1 ] . address .. " : " .. test_backend.endpoints [ 1 ] . port
2021-07-29 21:23:19 +00:00
local legacy_cookie_value = test_backend_endpoint
local function create_current_cookie_value ( backend_key )
return test_backend_endpoint .. " | " .. backend_key
end
2018-08-23 18:02:42 +00:00
describe ( " new(backend) " , function ( )
2021-07-29 21:23:19 +00:00
describe ( " when backend specifies cookie name " , function ( )
local function test_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
2018-08-23 18:02:42 +00:00
local test_backend_cookie_name = test_backend.sessionAffinityConfig . cookieSessionAffinity.name
2019-02-19 22:27:08 +00:00
assert.equal ( sticky_balancer_instance : cookie_name ( ) , test_backend_cookie_name )
2019-08-30 16:07:24 +00:00
end
2021-07-29 21:23:19 +00:00
it ( " returns an instance containing the corresponding cookie name " , function ( ) test_with ( sticky_balanced ) end )
it ( " returns an instance containing the corresponding cookie name " , function ( ) test_with ( sticky_persistent ) end )
2018-08-23 18:02:42 +00:00
end )
2018-08-21 18:22:44 +00:00
2021-07-29 21:23:19 +00:00
describe ( " when backend does not specify cookie name " , function ( )
local function test_with ( sticky_balancer_type )
2018-08-23 18:02:42 +00:00
local temp_backend = util.deepcopy ( test_backend )
temp_backend.sessionAffinityConfig . cookieSessionAffinity.name = nil
2021-07-29 21:23:19 +00:00
local sticky_balancer_instance = sticky_balancer_type : new ( temp_backend )
2018-08-23 18:02:42 +00:00
local default_cookie_name = " route "
2019-02-19 22:27:08 +00:00
assert.equal ( sticky_balancer_instance : cookie_name ( ) , default_cookie_name )
2019-08-30 16:07:24 +00:00
end
2021-07-29 21:23:19 +00:00
it ( " returns an instance with 'route' as cookie name " , function ( ) test_with ( sticky_balanced ) end )
it ( " returns an instance with 'route' as cookie name " , function ( ) test_with ( sticky_persistent ) end )
end )
describe ( " backend_key " , function ( )
local function test_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
assert.is_truthy ( sticky_balancer_instance.backend_key )
end
it ( " calculates at construction time " , function ( ) test_with ( sticky_balanced ) end )
it ( " calculates at construction time " , function ( ) test_with ( sticky_persistent ) end )
2018-08-23 18:02:42 +00:00
end )
end )
2018-08-21 18:22:44 +00:00
2018-08-23 18:02:42 +00:00
describe ( " balance() " , function ( )
local mocked_cookie_new = cookie.new
2018-08-21 18:22:44 +00:00
2018-08-23 18:02:42 +00:00
before_each ( function ( )
2021-07-29 21:23:19 +00:00
reset_sticky_balancer ( )
2018-08-23 18:02:42 +00:00
end )
2018-08-21 18:22:44 +00:00
2018-08-23 18:02:42 +00:00
after_each ( function ( )
cookie.new = mocked_cookie_new
end )
2018-08-21 18:22:44 +00:00
2021-07-29 21:23:19 +00:00
describe ( " when client doesn't have a cookie set and location is in cookie_locations " , function ( )
2019-08-30 16:07:24 +00:00
2021-07-29 21:23:19 +00:00
local function test_pick_endpoint_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
2018-08-23 18:02:42 +00:00
local peer = sticky_balancer_instance : balance ( )
2019-08-30 16:07:24 +00:00
assert.equal ( test_backend_endpoint , peer )
end
2021-07-29 21:23:19 +00:00
it ( " picks an endpoint for the client " , function ( ) test_pick_endpoint_with ( sticky_balanced ) end )
it ( " picks an endpoint for the client " , function ( ) test_pick_endpoint_with ( sticky_persistent ) end )
2018-08-23 18:02:42 +00:00
2021-07-29 21:23:19 +00:00
local function test_set_cookie_with ( sticky_balancer_type )
2018-08-23 18:02:42 +00:00
local s = { }
cookie.new = function ( self )
local cookie_instance = {
set = function ( self , payload )
assert.equal ( payload.key , test_backend.sessionAffinityConfig . cookieSessionAffinity.name )
2018-09-17 02:52:21 +00:00
assert.equal ( payload.path , ngx.var . location_path )
2020-02-08 19:58:52 +00:00
assert.equal ( payload.samesite , nil )
2019-01-12 01:24:45 +00:00
assert.equal ( payload.domain , nil )
2018-08-23 18:02:42 +00:00
assert.equal ( payload.httponly , true )
2018-12-06 08:01:08 +00:00
assert.equal ( payload.secure , false )
2018-09-17 02:52:21 +00:00
return true , nil
2018-08-23 18:02:42 +00:00
end ,
get = function ( k ) return false end ,
}
s = spy.on ( cookie_instance , " set " )
return cookie_instance , false
end
2018-11-19 16:42:12 +00:00
local b = get_test_backend ( )
b.sessionAffinityConfig . cookieSessionAffinity.locations = { }
b.sessionAffinityConfig . cookieSessionAffinity.locations [ " test.com " ] = { " / " }
2021-07-29 21:23:19 +00:00
local sticky_balancer_instance = sticky_balancer_type : new ( b )
2018-08-23 18:02:42 +00:00
assert.has_no . errors ( function ( ) sticky_balancer_instance : balance ( ) end )
assert.spy ( s ) . was_called ( )
2019-08-30 16:07:24 +00:00
end
2018-12-06 08:08:25 +00:00
2021-07-29 21:23:19 +00:00
it ( " sets a cookie on the client " , function ( ) test_set_cookie_with ( sticky_balanced ) end )
it ( " sets a cookie on the client " , function ( ) test_set_cookie_with ( sticky_persistent ) end )
2019-08-30 16:07:24 +00:00
2021-07-29 21:23:19 +00:00
local function test_set_ssl_cookie_with ( sticky_balancer_type )
2018-12-06 08:08:25 +00:00
ngx.var . https = " on "
local s = { }
cookie.new = function ( self )
local cookie_instance = {
set = function ( self , payload )
assert.equal ( payload.key , test_backend.sessionAffinityConfig . cookieSessionAffinity.name )
assert.equal ( payload.path , ngx.var . location_path )
2020-02-08 19:58:52 +00:00
assert.equal ( payload.samesite , nil )
2019-01-12 01:24:45 +00:00
assert.equal ( payload.domain , nil )
2018-12-06 08:08:25 +00:00
assert.equal ( payload.httponly , true )
assert.equal ( payload.secure , true )
return true , nil
end ,
get = function ( k ) return false end ,
}
s = spy.on ( cookie_instance , " set " )
return cookie_instance , false
end
local b = get_test_backend ( )
b.sessionAffinityConfig . cookieSessionAffinity.locations = { }
b.sessionAffinityConfig . cookieSessionAffinity.locations [ " test.com " ] = { " / " }
2021-07-29 21:23:19 +00:00
local sticky_balancer_instance = sticky_balancer_type : new ( b )
2018-12-06 08:08:25 +00:00
assert.has_no . errors ( function ( ) sticky_balancer_instance : balance ( ) end )
assert.spy ( s ) . was_called ( )
2019-08-30 16:07:24 +00:00
end
2021-07-29 21:23:19 +00:00
it ( " sets a secure cookie on the client when being in ssl mode " , function ( ) test_set_ssl_cookie_with ( sticky_balanced ) end )
it ( " sets a secure cookie on the client when being in ssl mode " , function ( ) test_set_ssl_cookie_with ( sticky_persistent ) end )
2018-08-21 18:22:44 +00:00
end )
2021-07-29 21:23:19 +00:00
describe ( " when client doesn't have a cookie set and cookie_locations contains a matching wildcard location " , function ( )
2019-08-26 15:22:07 +00:00
before_each ( function ( )
ngx.var . host = " dev.test.com "
2019-08-21 23:17:42 +00:00
end )
2019-08-26 15:22:07 +00:00
after_each ( function ( )
ngx.var . host = " test.com "
2019-08-21 23:17:42 +00:00
end )
2021-07-29 21:23:19 +00:00
local function test_with ( sticky_balancer_type )
2019-08-21 23:17:42 +00:00
local s = { }
cookie.new = function ( self )
local cookie_instance = {
set = function ( self , payload )
assert.equal ( payload.key , test_backend.sessionAffinityConfig . cookieSessionAffinity.name )
assert.equal ( payload.path , ngx.var . location_path )
2020-02-08 19:58:52 +00:00
assert.equal ( payload.samesite , nil )
2019-08-21 23:17:42 +00:00
assert.equal ( payload.domain , nil )
assert.equal ( payload.httponly , true )
assert.equal ( payload.secure , false )
return true , nil
end ,
get = function ( k ) return false end ,
}
s = spy.on ( cookie_instance , " set " )
return cookie_instance , false
end
local b = get_test_backend ( )
b.sessionAffinityConfig . cookieSessionAffinity.locations = { }
b.sessionAffinityConfig . cookieSessionAffinity.locations [ " *.test.com " ] = { " / " }
2021-07-29 21:23:19 +00:00
local sticky_balancer_instance = sticky_balancer_type : new ( b )
2019-08-21 23:17:42 +00:00
assert.has_no . errors ( function ( ) sticky_balancer_instance : balance ( ) end )
assert.spy ( s ) . was_called ( )
2019-12-28 22:56:13 +00:00
end
2019-08-30 16:07:24 +00:00
2021-07-29 21:23:19 +00:00
it ( " sets a cookie on the client " , function ( ) test_with ( sticky_balanced ) end )
it ( " sets a cookie on the client " , function ( ) test_with ( sticky_persistent ) end )
2019-08-21 23:17:42 +00:00
end )
2021-07-29 21:23:19 +00:00
describe ( " when client doesn't have a cookie set and location not in cookie_locations " , function ( )
2019-08-30 16:07:24 +00:00
2021-07-29 21:23:19 +00:00
local function test_pick_endpoint_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
2019-08-21 23:17:42 +00:00
local peer = sticky_balancer_instance : balance ( )
assert.equal ( peer , test_backend_endpoint )
2019-08-30 16:07:24 +00:00
end
2019-08-21 23:17:42 +00:00
2021-07-29 21:23:19 +00:00
it ( " picks an endpoint for the client " , function ( ) test_pick_endpoint_with ( sticky_balanced ) end )
it ( " picks an endpoint for the client " , function ( ) test_pick_endpoint_with ( sticky_persistent ) end )
2019-08-30 16:07:24 +00:00
2021-07-29 21:23:19 +00:00
local function test_no_cookie_with ( sticky_balancer_type )
2018-11-19 16:42:12 +00:00
local s = { }
cookie.new = function ( self )
local cookie_instance = {
set = function ( self , payload )
assert.equal ( payload.key , test_backend.sessionAffinityConfig . cookieSessionAffinity.name )
assert.equal ( payload.path , ngx.var . location_path )
assert.equal ( payload.domain , ngx.var . host )
assert.equal ( payload.httponly , true )
2020-02-08 19:58:52 +00:00
assert.equal ( payload.samesite , nil )
2018-11-19 16:42:12 +00:00
return true , nil
end ,
get = function ( k ) return false end ,
}
s = spy.on ( cookie_instance , " set " )
return cookie_instance , false
end
2021-07-29 21:23:19 +00:00
local sticky_balancer_instance = sticky_balancer_type : new ( get_test_backend ( ) )
2018-11-19 16:42:12 +00:00
assert.has_no . errors ( function ( ) sticky_balancer_instance : balance ( ) end )
assert.spy ( s ) . was_not_called ( )
2019-08-30 16:07:24 +00:00
end
2021-07-29 21:23:19 +00:00
it ( " does not set a cookie on the client " , function ( ) test_no_cookie_with ( sticky_balanced ) end )
it ( " does not set a cookie on the client " , function ( ) test_no_cookie_with ( sticky_persistent ) end )
2018-11-19 16:42:12 +00:00
end )
2021-07-29 21:23:19 +00:00
describe ( " when client has a cookie set " , function ( )
2019-08-30 16:07:24 +00:00
2021-07-29 21:23:19 +00:00
local function test_no_cookie_with ( sticky_balancer_type )
2018-08-23 18:02:42 +00:00
local s = { }
2018-09-17 02:52:21 +00:00
cookie.new = function ( self )
2018-08-23 18:02:42 +00:00
local return_obj = {
set = function ( v ) return false , nil end ,
2021-07-29 21:23:19 +00:00
get = function ( k ) return legacy_cookie_value end ,
2018-08-23 18:02:42 +00:00
}
s = spy.on ( return_obj , " set " )
return return_obj , false
end
2021-07-29 21:23:19 +00:00
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
2018-08-23 18:02:42 +00:00
assert.has_no . errors ( function ( ) sticky_balancer_instance : balance ( ) end )
assert.spy ( s ) . was_not_called ( )
2019-08-30 16:07:24 +00:00
end
2018-08-23 18:02:42 +00:00
2021-07-29 21:23:19 +00:00
it ( " does not set a cookie " , function ( ) test_no_cookie_with ( sticky_balanced ) end )
it ( " does not set a cookie " , function ( ) test_no_cookie_with ( sticky_persistent ) end )
2019-08-30 16:07:24 +00:00
local function test_correct_endpoint ( sticky )
2018-08-23 18:02:42 +00:00
local sticky_balancer_instance = sticky : new ( test_backend )
local peer = sticky_balancer_instance : balance ( )
assert.equal ( peer , test_backend_endpoint )
2019-08-30 16:07:24 +00:00
end
it ( " returns the correct endpoint for the client " , function ( ) test_correct_endpoint ( sticky_balanced ) end )
it ( " returns the correct endpoint for the client " , function ( ) test_correct_endpoint ( sticky_persistent ) end )
2018-08-23 18:02:42 +00:00
end )
end )
2019-04-29 13:20:30 +00:00
2019-08-30 16:07:24 +00:00
local function get_several_test_backends ( change_on_failure )
2019-04-29 13:20:30 +00:00
return {
name = " access-router-production-web-80 " ,
endpoints = {
{ address = " 10.184.7.40 " , port = " 8080 " , maxFails = 0 , failTimeout = 0 } ,
{ address = " 10.184.7.41 " , port = " 8080 " , maxFails = 0 , failTimeout = 0 } ,
} ,
sessionAffinityConfig = {
name = " cookie " ,
2019-08-30 09:40:29 +00:00
cookieSessionAffinity = {
name = " test_name " ,
hash = " sha1 " ,
2019-08-30 16:07:24 +00:00
change_on_failure = change_on_failure ,
2019-08-30 09:40:29 +00:00
locations = { [ ' test.com ' ] = { ' / ' } }
}
2019-04-29 13:20:30 +00:00
} ,
}
end
describe ( " balance() after error " , function ( )
local mocked_cookie_new = cookie.new
before_each ( function ( )
2019-08-30 16:07:24 +00:00
mock_ngx ( { var = { location_path = " / " , host = " test.com " } } )
2019-04-29 13:20:30 +00:00
end )
after_each ( function ( )
2019-08-30 16:07:24 +00:00
reset_ngx ( )
2019-04-29 13:20:30 +00:00
end )
2021-07-29 21:23:19 +00:00
describe ( " when request to upstream fails " , function ( )
2019-04-29 13:20:30 +00:00
2021-07-29 21:23:19 +00:00
local function test_with ( sticky_balancer_type , change_on_failure )
local sticky_balancer_instance = sticky_balancer_type : new ( get_several_test_backends ( change_on_failure ) )
2019-04-29 13:20:30 +00:00
2019-08-30 16:07:24 +00:00
local old_upstream = sticky_balancer_instance : balance ( )
assert.is . Not.Nil ( old_upstream )
for _ = 1 , 100 do
-- make sure upstream doesn't change on subsequent calls of balance()
assert.equal ( old_upstream , sticky_balancer_instance : balance ( ) )
end
2019-04-29 13:20:30 +00:00
2019-08-30 16:07:24 +00:00
-- simulate request failure
sticky_balancer_instance.get_last_failure = function ( )
return " failed "
end
_G.ngx . var.upstream_addr = old_upstream
for _ = 1 , 100 do
local new_upstream = sticky_balancer_instance : balance ( )
if change_on_failure == false then
2024-09-06 14:59:43 +00:00
-- upstream should be the same in spite of error, if change_on_failure option is false
2019-08-30 16:07:24 +00:00
assert.equal ( new_upstream , old_upstream )
else
-- upstream should change after error, if change_on_failure option is true
assert.not_equal ( new_upstream , old_upstream )
2019-04-29 13:20:30 +00:00
end
end
2019-08-30 16:07:24 +00:00
end
2021-07-29 21:23:19 +00:00
it ( " changes upstream when change_on_failure option is true " , function ( ) test_with ( sticky_balanced , true ) end )
it ( " changes upstream when change_on_failure option is true " , function ( ) test_with ( sticky_persistent , true ) end )
it ( " changes upstream when change_on_failure option is false " , function ( ) test_with ( sticky_balanced , false ) end )
it ( " changes upstream when change_on_failure option is false " , function ( ) test_with ( sticky_persistent , false ) end )
2019-04-29 13:20:30 +00:00
end )
end )
2019-12-28 22:56:13 +00:00
2021-07-29 21:23:19 +00:00
describe ( " when client doesn't have a cookie set and no host header, matching default server '_' " , function ( )
2019-12-28 22:56:13 +00:00
before_each ( function ( )
ngx.var . host = " not-default-server "
ngx.var . server_name = " _ "
end )
2021-07-29 21:23:19 +00:00
local function test_with ( sticky_balancer_type )
2019-12-28 22:56:13 +00:00
local s = { }
cookie.new = function ( self )
local cookie_instance = {
set = function ( self , payload )
assert.equal ( payload.key , test_backend.sessionAffinityConfig . cookieSessionAffinity.name )
assert.equal ( payload.path , ngx.var . location_path )
2020-02-08 19:58:52 +00:00
assert.equal ( payload.samesite , nil )
2019-12-28 22:56:13 +00:00
assert.equal ( payload.domain , nil )
assert.equal ( payload.httponly , true )
assert.equal ( payload.secure , false )
return true , nil
end ,
get = function ( k ) return false end ,
}
s = spy.on ( cookie_instance , " set " )
return cookie_instance , false
end
local b = get_test_backend ( )
b.sessionAffinityConfig . cookieSessionAffinity.locations = { }
b.sessionAffinityConfig . cookieSessionAffinity.locations [ " _ " ] = { " / " }
2021-07-29 21:23:19 +00:00
local sticky_balancer_instance = sticky_balancer_type : new ( b )
2019-12-28 22:56:13 +00:00
assert.has_no . errors ( function ( ) sticky_balancer_instance : balance ( ) end )
assert.spy ( s ) . was_called ( )
end
2021-07-29 21:23:19 +00:00
it ( " sets a cookie on the client " , function ( ) test_with ( sticky_balanced ) end )
it ( " sets a cookie on the client " , function ( ) test_with ( sticky_persistent ) end )
2019-12-28 22:56:13 +00:00
end )
2020-01-22 20:19:16 +00:00
describe ( " SameSite settings " , function ( )
local mocked_cookie_new = cookie.new
before_each ( function ( )
2021-07-29 21:23:19 +00:00
reset_sticky_balancer ( )
2020-01-22 20:19:16 +00:00
end )
after_each ( function ( )
cookie.new = mocked_cookie_new
end )
2021-09-01 22:23:40 +00:00
local function test_set_cookie_with ( sticky_balancer_type , samesite , conditional_samesite_none , expected_path , expected_samesite , secure , expected_secure )
2020-01-22 20:19:16 +00:00
local s = { }
cookie.new = function ( self )
local cookie_instance = {
set = function ( self , payload )
assert.equal ( payload.key , test_backend.sessionAffinityConfig . cookieSessionAffinity.name )
assert.equal ( payload.path , expected_path )
2020-02-08 19:58:52 +00:00
assert.equal ( payload.samesite , expected_samesite )
2020-01-22 20:19:16 +00:00
assert.equal ( payload.domain , nil )
assert.equal ( payload.httponly , true )
2021-09-01 22:23:40 +00:00
assert.equal ( payload.secure , expected_secure )
2020-01-22 20:19:16 +00:00
return true , nil
end ,
get = function ( k ) return false end ,
}
s = spy.on ( cookie_instance , " set " )
return cookie_instance , false
end
local b = get_test_backend ( )
b.sessionAffinityConfig . cookieSessionAffinity.locations = { }
b.sessionAffinityConfig . cookieSessionAffinity.locations [ " test.com " ] = { " / " }
b.sessionAffinityConfig . cookieSessionAffinity.samesite = samesite
b.sessionAffinityConfig . cookieSessionAffinity.conditional_samesite_none = conditional_samesite_none
2021-09-01 22:23:40 +00:00
b.sessionAffinityConfig . cookieSessionAffinity.secure = secure
2021-07-29 21:23:19 +00:00
local sticky_balancer_instance = sticky_balancer_type : new ( b )
2020-01-22 20:19:16 +00:00
assert.has_no . errors ( function ( ) sticky_balancer_instance : balance ( ) end )
assert.spy ( s ) . was_called ( )
end
2021-09-01 22:23:40 +00:00
it ( " returns a secure cookie with SameSite=Strict when user specifies samesite strict and secure=true " , function ( )
test_set_cookie_with ( sticky_balanced , " Lax " , false , " / " , " Lax " , true , true )
2020-01-22 20:19:16 +00:00
end )
it ( " returns a cookie with SameSite=Strict when user specifies samesite strict and conditional samesite none " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_balanced , " Strict " , true , " / " , " Strict " , nil , false )
2020-01-22 20:19:16 +00:00
end )
it ( " returns a cookie with SameSite=Lax when user specifies samesite lax " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_balanced , " Lax " , false , " / " , " Lax " , nil , false )
2020-01-22 20:19:16 +00:00
end )
it ( " returns a cookie with SameSite=Lax when user specifies samesite lax and conditional samesite none " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_balanced , " Lax " , true , " / " , " Lax " , nil , false )
2020-01-22 20:19:16 +00:00
end )
it ( " returns a cookie with SameSite=None when user specifies samesite None " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_balanced , " None " , false , " / " , " None " , nil , false )
2020-01-22 20:19:16 +00:00
end )
it ( " returns a cookie with SameSite=None when user specifies samesite None and conditional samesite none with supported user agent " , function ( )
mock_ngx ( { var = { location_path = " / " , host = " test.com " , http_user_agent = " Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.2704.103 Safari/537.36 " } } )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_balanced , " None " , true , " / " , " None " , nil , false )
2020-01-22 20:19:16 +00:00
end )
it ( " returns a cookie without SameSite=None when user specifies samesite None and conditional samesite none with unsupported user agent " , function ( )
mock_ngx ( { var = { location_path = " / " , host = " test.com " , http_user_agent = " Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 " } } )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_balanced , " None " , true , " / " , nil , nil , false )
2021-07-29 21:23:19 +00:00
end )
2021-09-01 22:23:40 +00:00
it ( " returns a secure cookie with SameSite=Strict when user specifies samesite strict and secure=true " , function ( )
test_set_cookie_with ( sticky_persistent , " Lax " , false , " / " , " Lax " , true , true )
end )
2021-07-29 21:23:19 +00:00
it ( " returns a cookie with SameSite=Strict when user specifies samesite strict " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_persistent , " Strict " , false , " / " , " Strict " , nil , false )
2021-07-29 21:23:19 +00:00
end )
it ( " returns a cookie with SameSite=Strict when user specifies samesite strict and conditional samesite none " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_persistent , " Strict " , true , " / " , " Strict " , nil , false )
2021-07-29 21:23:19 +00:00
end )
it ( " returns a cookie with SameSite=Lax when user specifies samesite lax " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_persistent , " Lax " , false , " / " , " Lax " , nil , false )
2021-07-29 21:23:19 +00:00
end )
it ( " returns a cookie with SameSite=Lax when user specifies samesite lax and conditional samesite none " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_persistent , " Lax " , true , " / " , " Lax " , nil , false )
2020-01-22 20:19:16 +00:00
end )
2021-07-29 21:23:19 +00:00
it ( " returns a cookie with SameSite=None when user specifies samesite None " , function ( )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_persistent , " None " , false , " / " , " None " , nil , false )
2021-07-29 21:23:19 +00:00
end )
it ( " returns a cookie with SameSite=None when user specifies samesite None and conditional samesite none with supported user agent " , function ( )
mock_ngx ( { var = { location_path = " / " , host = " test.com " , http_user_agent = " Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.2704.103 Safari/537.36 " } } )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_persistent , " None " , true , " / " , " None " , nil , false )
2021-07-29 21:23:19 +00:00
end )
it ( " returns a cookie without SameSite=None when user specifies samesite None and conditional samesite none with unsupported user agent " , function ( )
mock_ngx ( { var = { location_path = " / " , host = " test.com " , http_user_agent = " Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 " } } )
2021-09-01 22:23:40 +00:00
test_set_cookie_with ( sticky_persistent , " None " , true , " / " , nil , nil , false )
2021-07-29 21:23:19 +00:00
end )
end )
describe ( " get_cookie() " , function ( )
describe ( " legacy cookie value " , function ( )
local function test_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
cookie.new = function ( self )
local return_obj = {
set = function ( v ) return false , nil end ,
get = function ( k ) return legacy_cookie_value end ,
}
return return_obj , false
end
assert.equal ( test_backend_endpoint , sticky_balancer_instance.get_cookie ( sticky_balancer_instance ) )
end
it ( " retrieves upstream key value " , function ( ) test_with ( sticky_balanced ) end )
it ( " retrieves upstream key value " , function ( ) test_with ( sticky_persistent ) end )
end )
describe ( " current cookie value " , function ( )
local function test_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
cookie.new = function ( self )
local return_obj = {
set = function ( v ) return false , nil end ,
get = function ( k ) return create_current_cookie_value ( sticky_balancer_instance.backend_key ) end ,
}
return return_obj , false
end
assert.equal ( test_backend_endpoint , sticky_balancer_instance.get_cookie ( sticky_balancer_instance ) )
end
it ( " retrieves upstream key value " , function ( ) test_with ( sticky_balanced ) end )
it ( " retrieves upstream key value " , function ( ) test_with ( sticky_persistent ) end )
end )
end )
describe ( " get_cookie_parsed() " , function ( )
describe ( " legacy cookie value " , function ( )
local function test_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
cookie.new = function ( self )
local return_obj = {
set = function ( v ) return false , nil end ,
get = function ( k ) return legacy_cookie_value end ,
}
return return_obj , false
end
local parsed_cookie = sticky_balancer_instance.get_cookie_parsed ( sticky_balancer_instance )
assert.is_truthy ( parsed_cookie )
assert.equal ( test_backend_endpoint , parsed_cookie.upstream_key )
assert.is_falsy ( parsed_cookie.backend_key )
end
it ( " retrieves upstream key value " , function ( ) test_with ( sticky_balanced ) end )
it ( " retrieves upstream key value " , function ( ) test_with ( sticky_persistent ) end )
end )
describe ( " current cookie value " , function ( )
local function test_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
cookie.new = function ( self )
local return_obj = {
set = function ( v ) return false , nil end ,
get = function ( k ) return create_current_cookie_value ( sticky_balancer_instance.backend_key ) end ,
}
return return_obj , false
end
local parsed_cookie = sticky_balancer_instance.get_cookie_parsed ( sticky_balancer_instance )
assert.is_truthy ( parsed_cookie )
assert.equal ( test_backend_endpoint , parsed_cookie.upstream_key )
assert.equal ( sticky_balancer_instance.backend_key , parsed_cookie.backend_key )
end
it ( " retrieves all supported values " , function ( ) test_with ( sticky_balanced ) end )
it ( " retrieves all supported values " , function ( ) test_with ( sticky_persistent ) end )
end )
end )
describe ( " set_cookie() " , function ( )
local function test_with ( sticky_balancer_type )
local sticky_balancer_instance = sticky_balancer_type : new ( test_backend )
local cookieSetSpy = { }
cookie.new = function ( self )
local return_obj = {
set = function ( self , payload )
assert.equal ( create_current_cookie_value ( sticky_balancer_instance.backend_key ) , payload.value )
return true , nil
end ,
get = function ( k ) return nil end ,
}
cookieSetSpy = spy.on ( return_obj , " set " )
return return_obj , false
end
sticky_balancer_instance.set_cookie ( sticky_balancer_instance , test_backend_endpoint )
assert.spy ( cookieSetSpy ) . was_called ( )
end
it ( " constructs correct cookie value " , function ( ) test_with ( sticky_balanced ) end )
it ( " constructs correct cookie value " , function ( ) test_with ( sticky_persistent ) end )
2020-01-22 20:19:16 +00:00
end )
2018-09-17 02:52:21 +00:00
end )