Пример #1
0
        public void AffinitizedRequest_AffinityKeyIsNotExtracted_SetKeyOnResponse()
        {
            var provider = new CookieSessionAffinityProvider(
                Options.Create(_defaultProviderOptions),
                AffinityTestHelper.GetDataProtector().Object,
                AffinityTestHelper.GetLogger <CookieSessionAffinityProvider>().Object);
            var context = new DefaultHttpContext();

            provider.AffinitizeRequest(context, _defaultOptions, _destinations[1]);

            var affinityCookieHeader = context.Response.Headers["Set-Cookie"];

            Assert.Equal(".Yarp.ReverseProxy.Affinity=ZGVzdC1C; path=/; httponly", affinityCookieHeader);
        }
Пример #2
0
        public void FindAffinitizedDestination_AffinityKeyIsNotSetOnRequest_ReturnKeyNotSet()
        {
            var provider = new CustomHeaderSessionAffinityProvider(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityProvider>().Object);

            Assert.Equal(SessionAffinityConstants.Modes.CustomHeader, provider.Mode);

            var context = new DefaultHttpContext();

            context.Request.Headers["SomeHeader"] = new[] { "SomeValue" };

            var affinityResult = provider.FindAffinitizedDestinations(context, _destinations, "cluster-1", _defaultOptions);

            Assert.Equal(AffinityStatus.AffinityKeyNotSet, affinityResult.Status);
            Assert.Null(affinityResult.Destinations);
        }
Пример #3
0
        public void FindAffinitizedDestination_AffinityKeyIsSetOnRequest_Success()
        {
            var provider = new CustomHeaderSessionAffinityProvider(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityProvider>().Object);
            var context  = new DefaultHttpContext();

            context.Request.Headers["SomeHeader"] = new[] { "SomeValue" };
            var affinitizedDestination = _destinations[1];

            context.Request.Headers[AffinityHeaderName] = new[] { affinitizedDestination.DestinationId.ToUTF8BytesInBase64() };

            var affinityResult = provider.FindAffinitizedDestinations(context, _destinations, "cluster-1", _defaultOptions);

            Assert.Equal(AffinityStatus.OK, affinityResult.Status);
            Assert.Equal(1, affinityResult.Destinations.Count);
            Assert.Same(affinitizedDestination, affinityResult.Destinations[0]);
        }
Пример #4
0
        public void FindAffinitizedDestination_AffinityKeyIsSetOnRequest_Success()
        {
            var provider = new CookieSessionAffinityProvider(
                Options.Create(_defaultProviderOptions),
                AffinityTestHelper.GetDataProtector().Object,
                AffinityTestHelper.GetLogger <CookieSessionAffinityProvider>().Object);
            var context = new DefaultHttpContext();
            var affinitizedDestination = _destinations[1];

            context.Request.Headers["Cookie"] = GetCookieWithAffinity(affinitizedDestination);

            var affinityResult = provider.FindAffinitizedDestinations(context, _destinations, "cluster-1", _defaultOptions);

            Assert.Equal(AffinityStatus.OK, affinityResult.Status);
            Assert.Equal(1, affinityResult.Destinations.Count);
            Assert.Same(affinitizedDestination, affinityResult.Destinations[0]);
        }
Пример #5
0
        public void AffinitizedRequest_AffinityKeyIsExtracted_DoNothing()
        {
            var provider = new CustomHeaderSessionAffinityProvider(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityProvider>().Object);
            var context  = new DefaultHttpContext();

            context.Request.Headers["SomeHeader"] = new[] { "SomeValue" };
            var affinitizedDestination = _destinations[1];

            context.Request.Headers[AffinityHeaderName] = new[] { affinitizedDestination.DestinationId.ToUTF8BytesInBase64() };

            var affinityResult = provider.FindAffinitizedDestinations(context, _destinations, "cluster-1", _defaultOptions);

            Assert.Equal(AffinityStatus.OK, affinityResult.Status);

            provider.AffinitizeRequest(context, _defaultOptions, affinitizedDestination);

            Assert.False(context.Response.Headers.ContainsKey(AffinityHeaderName));
        }
Пример #6
0
        public void AffinitizedRequest_AffinityKeyIsExtracted_DoNothing()
        {
            var provider = new CookieSessionAffinityProvider(
                Options.Create(_defaultProviderOptions),
                AffinityTestHelper.GetDataProtector().Object,
                AffinityTestHelper.GetLogger <CookieSessionAffinityProvider>().Object);
            var context = new DefaultHttpContext();
            var affinitizedDestination = _destinations[0];

            context.Request.Headers["Cookie"] = GetCookieWithAffinity(affinitizedDestination);

            var affinityResult = provider.FindAffinitizedDestinations(context, _destinations, "cluster-1", _defaultOptions);

            Assert.Equal(AffinityStatus.OK, affinityResult.Status);

            provider.AffinitizeRequest(context, _defaultOptions, affinitizedDestination);

            Assert.False(context.Response.Headers.ContainsKey("Cookie"));
        }
Пример #7
0
        public void FindAffinitizedDestination_CustomHeaderNameIsNotSpecified_UseDefaultName(Dictionary <string, string> settings)
        {
            var options = new SessionAffinityOptions
            {
                Enabled       = true,
                Mode          = "CustomHeader",
                FailurePolicy = "Return503",
                Settings      = settings,
            };
            var provider = new CustomHeaderSessionAffinityProvider(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityProvider>().Object);
            var context  = new DefaultHttpContext();
            var affinitizedDestination = _destinations[1];

            context.Request.Headers[CustomHeaderSessionAffinityProvider.DefaultCustomHeaderName] = new[] { affinitizedDestination.DestinationId.ToUTF8BytesInBase64() };

            var affinityResult = provider.FindAffinitizedDestinations(context, _destinations, "cluster-1", options);

            Assert.Equal(AffinityStatus.OK, affinityResult.Status);
            Assert.Equal(1, affinityResult.Destinations.Count);
            Assert.Same(affinitizedDestination, affinityResult.Destinations[0]);
        }
Пример #8
0
        public void AffinitizeRequest_CookieBuilderSettingsChanged_UseNewSettings()
        {
            var providerOptions = new CookieSessionAffinityProviderOptions();

            providerOptions.Cookie.Domain       = "mydomain.my";
            providerOptions.Cookie.HttpOnly     = false;
            providerOptions.Cookie.IsEssential  = true;
            providerOptions.Cookie.MaxAge       = TimeSpan.FromHours(1);
            providerOptions.Cookie.Name         = "My.Affinity";
            providerOptions.Cookie.Path         = "/some";
            providerOptions.Cookie.SameSite     = SameSiteMode.Lax;
            providerOptions.Cookie.SecurePolicy = CookieSecurePolicy.Always;
            var provider = new CookieSessionAffinityProvider(
                Options.Create(providerOptions),
                AffinityTestHelper.GetDataProtector().Object,
                AffinityTestHelper.GetLogger <CookieSessionAffinityProvider>().Object);
            var context = new DefaultHttpContext();

            provider.AffinitizeRequest(context, _defaultOptions, _destinations[1]);

            var affinityCookieHeader = context.Response.Headers["Set-Cookie"];

            Assert.Equal("My.Affinity=ZGVzdC1C; max-age=3600; domain=mydomain.my; path=/some; secure; samesite=lax", affinityCookieHeader);
        }
Пример #9
0
        public void AffinitizedRequest_AffinityKeyIsNotExtracted_SetKeyOnResponse()
        {
            var provider                    = new CustomHeaderSessionAffinityProvider(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityProvider>().Object);
            var context                     = new DefaultHttpContext();
            var chosenDestination           = _destinations[1];
            var expectedAffinityHeaderValue = chosenDestination.DestinationId.ToUTF8BytesInBase64();

            provider.AffinitizeRequest(context, _defaultOptions, chosenDestination);

            Assert.True(context.Response.Headers.ContainsKey(AffinityHeaderName));
            Assert.Equal(expectedAffinityHeaderValue, context.Response.Headers[AffinityHeaderName]);
        }