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(".Microsoft.ReverseProxy.Affinity=ZGVzdC1C; path=/; httponly", affinityCookieHeader);
        }
        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"));
        }
        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);
        }