Ejemplo n.º 1
0
        public void AffinitizedRequest_AffinityKeyIsNotExtracted_SetKeyOnResponse()
        {
            var policy                      = new CustomHeaderSessionAffinityPolicy(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityPolicy>().Object);
            var context                     = new DefaultHttpContext();
            var chosenDestination           = _destinations[1];
            var expectedAffinityHeaderValue = chosenDestination.DestinationId.ToUTF8BytesInBase64();

            policy.AffinitizeResponse(context, new ClusterState("cluster"), _defaultOptions, chosenDestination);

            Assert.True(context.Response.Headers.ContainsKey(AffinityHeaderName));
            Assert.Equal(expectedAffinityHeaderValue, context.Response.Headers[AffinityHeaderName]);
        }
        public void AffinitizeRequest_CookieConfigSpecified_UseIt()
        {
            var policy = new CookieSessionAffinityPolicy(
                AffinityTestHelper.GetDataProtector().Object,
                new ManualClock(),
                AffinityTestHelper.GetLogger <CookieSessionAffinityPolicy>().Object);
            var context = new DefaultHttpContext();

            policy.AffinitizeResponse(context, new ClusterState("cluster"), _config, _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);
        }
Ejemplo n.º 3
0
        public void FindAffinitizedDestination_AffinityKeyIsNotSetOnRequest_ReturnKeyNotSet()
        {
            var policy = new CustomHeaderSessionAffinityPolicy(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityPolicy>().Object);

            Assert.Equal(SessionAffinityConstants.Policies.CustomHeader, policy.Name);

            var context = new DefaultHttpContext();

            context.Request.Headers["SomeHeader"] = new[] { "SomeValue" };
            var cluster = new ClusterState("cluster");

            var affinityResult = policy.FindAffinitizedDestinations(context, cluster, _defaultOptions, _destinations);

            Assert.Equal(AffinityStatus.AffinityKeyNotSet, affinityResult.Status);
            Assert.Null(affinityResult.Destinations);
        }
Ejemplo n.º 4
0
        public void FindAffinitizedDestination_AffinityKeyIsSetOnRequest_Success()
        {
            var policy  = new CustomHeaderSessionAffinityPolicy(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityPolicy>().Object);
            var context = new DefaultHttpContext();

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

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

            var affinityResult = policy.FindAffinitizedDestinations(context, cluster, _defaultOptions, _destinations);

            Assert.Equal(AffinityStatus.OK, affinityResult.Status);
            Assert.Equal(1, affinityResult.Destinations.Count);
            Assert.Same(affinitizedDestination, affinityResult.Destinations[0]);
        }
        public void FindAffinitizedDestination_AffinityKeyIsSetOnRequest_Success()
        {
            var policy = new CookieSessionAffinityPolicy(
                AffinityTestHelper.GetDataProtector().Object,
                new ManualClock(),
                AffinityTestHelper.GetLogger <CookieSessionAffinityPolicy>().Object);
            var context = new DefaultHttpContext();
            var affinitizedDestination = _destinations[1];

            context.Request.Headers["Cookie"] = GetCookieWithAffinity(affinitizedDestination);
            var cluster = new ClusterState("cluster");

            var affinityResult = policy.FindAffinitizedDestinations(context, cluster, _config, _destinations);

            Assert.Equal(AffinityStatus.OK, affinityResult.Status);
            Assert.Equal(1, affinityResult.Destinations.Count);
            Assert.Same(affinitizedDestination, affinityResult.Destinations[0]);
        }
        public void FindAffinitizedDestination_AffinityKeyIsNotSetOnRequest_ReturnKeyNotSet()
        {
            var policy = new CookieSessionAffinityPolicy(
                AffinityTestHelper.GetDataProtector().Object,
                new ManualClock(),
                AffinityTestHelper.GetLogger <CookieSessionAffinityPolicy>().Object);

            Assert.Equal(SessionAffinityConstants.Policies.Cookie, policy.Name);

            var context = new DefaultHttpContext();

            context.Request.Headers["Cookie"] = new[] { $"Some-Cookie=ZZZ" };
            var cluster = new ClusterState("cluster");

            var affinityResult = policy.FindAffinitizedDestinations(context, cluster, _config, _destinations);

            Assert.Equal(AffinityStatus.AffinityKeyNotSet, affinityResult.Status);
            Assert.Null(affinityResult.Destinations);
        }
Ejemplo n.º 7
0
        public void AffinitizedRequest_AffinityKeyIsExtracted_DoNothing()
        {
            var policy  = new CustomHeaderSessionAffinityPolicy(AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CustomHeaderSessionAffinityPolicy>().Object);
            var context = new DefaultHttpContext();

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

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

            var affinityResult = policy.FindAffinitizedDestinations(context, cluster, _defaultOptions, _destinations);

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

            policy.AffinitizeResponse(context, cluster, _defaultOptions, affinitizedDestination);

            Assert.False(context.Response.Headers.ContainsKey(AffinityHeaderName));
        }
        public void AffinitizedRequest_AffinityKeyIsExtracted_DoNothing()
        {
            var policy = new CookieSessionAffinityPolicy(
                AffinityTestHelper.GetDataProtector().Object,
                new ManualClock(),
                AffinityTestHelper.GetLogger <CookieSessionAffinityPolicy>().Object);
            var context = new DefaultHttpContext();
            var affinitizedDestination = _destinations[0];

            context.Request.Headers["Cookie"] = GetCookieWithAffinity(affinitizedDestination);
            var cluster = new ClusterState("cluster");

            var affinityResult = policy.FindAffinitizedDestinations(context, cluster, _config, _destinations);

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

            policy.AffinitizeResponse(context, cluster, _config, affinitizedDestination);

            Assert.False(context.Response.Headers.ContainsKey("Cookie"));
        }