public void AffinitizeRequest_AffinitiDisabled_DoNothing() { var dataProtector = GetDataProtector(); var provider = new ProviderStub(dataProtector.Object, AffinityTestHelper.GetLogger <BaseSessionAffinityProvider <string> >().Object); Assert.Throws <InvalidOperationException>(() => provider.AffinitizeRequest(new DefaultHttpContext(), new SessionAffinityOptions(), new DestinationInfo("id"))); }
public void AffinitizeRequest_AffinityOptionSettingNotFound_Throw() { var provider = new ProviderStub(GetDataProtector().Object, AffinityTestHelper.GetLogger <BaseSessionAffinityProvider <string> >().Object); var options = GetOptionsWithUnknownSetting(); Assert.Throws <ArgumentException>(() => provider.AffinitizeRequest(new DefaultHttpContext(), options, new DestinationInfo("dest-A"))); }
public void FindAffinitizedDestination_AffinityDisabledOnCluster_ReturnsAffinityDisabled() { var provider = new ProviderStub(GetDataProtector().Object, AffinityTestHelper.GetLogger <BaseSessionAffinityProvider <string> >().Object); var options = new ClusterSessionAffinityOptions(false, _defaultOptions.Mode, _defaultOptions.FailurePolicy, _defaultOptions.Settings); Assert.Throws <InvalidOperationException>(() => provider.FindAffinitizedDestinations(new DefaultHttpContext(), new[] { new DestinationInfo("1") }, "cluster-1", options)); }
public void AffinitizeRequest_RequestIsAffinitized_DoNothing() { var dataProtector = GetDataProtector(); var provider = new ProviderStub(dataProtector.Object, AffinityTestHelper.GetLogger <BaseSessionAffinityProvider <string> >().Object); var context = new DefaultHttpContext(); provider.DirectlySetExtractedKeyOnContext(context, "ExtractedKey"); provider.AffinitizeRequest(context, _defaultOptions, new DestinationInfo("id")); Assert.Null(provider.LastSetEncryptedKey); dataProtector.Verify(p => p.Protect(It.IsAny <byte[]>()), Times.Never); }
public void AffinitizeRequest_RequestIsNotAffinitized_SetAffinityKey() { var dataProtector = GetDataProtector(); var provider = new ProviderStub(dataProtector.Object, AffinityTestHelper.GetLogger <BaseSessionAffinityProvider <string> >().Object); var destination = new DestinationInfo("dest-A"); provider.AffinitizeRequest(new DefaultHttpContext(), _defaultOptions, destination); Assert.Equal("ZGVzdC1B", provider.LastSetEncryptedKey); var keyBytes = Encoding.UTF8.GetBytes(destination.DestinationId); dataProtector.Verify(p => p.Protect(It.Is <byte[]>(b => b.SequenceEqual(keyBytes))), Times.Once); }
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 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]); }
public void FindAffinitizedDestination_AffinityKeyIsNotSetOnRequest_ReturnKeyNotSet() { var provider = new CookieSessionAffinityProvider( Options.Create(_defaultProviderOptions), AffinityTestHelper.GetDataProtector().Object, AffinityTestHelper.GetLogger <CookieSessionAffinityProvider>().Object); Assert.Equal(SessionAffinityConstants.Modes.Cookie, provider.Mode); var context = new DefaultHttpContext(); context.Request.Headers["Cookie"] = new[] { $"Some-Cookie=ZZZ" }; var affinityResult = provider.FindAffinitizedDestinations(context, _destinations, "cluster-1", _defaultOptions); Assert.Equal(AffinityStatus.AffinityKeyNotSet, affinityResult.Status); Assert.Null(affinityResult.Destinations); }
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 Request_FindAffinitizedDestinations( HttpContext context, DestinationInfo[] allDestinations, AffinityStatus expectedStatus, DestinationInfo expectedDestination, byte[] expectedEncryptedKey, bool unprotectCalled, LogLevel?expectedLogLevel, EventId expectedEventId) { var dataProtector = GetDataProtector(); var logger = AffinityTestHelper.GetLogger <BaseSessionAffinityProvider <string> >(); var provider = new ProviderStub(dataProtector.Object, logger.Object); var affinityResult = provider.FindAffinitizedDestinations(context, allDestinations, "cluster-1", _defaultOptions); if (unprotectCalled) { dataProtector.Verify(p => p.Unprotect(It.Is <byte[]>(b => b.SequenceEqual(expectedEncryptedKey))), Times.Once); } Assert.Equal(expectedStatus, affinityResult.Status); Assert.Same(expectedDestination, affinityResult.Destinations?.FirstOrDefault()); if (expectedLogLevel != null) { logger.Verify( l => l.Log(expectedLogLevel.Value, expectedEventId, It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Once); } if (expectedDestination != null) { Assert.Equal(1, affinityResult.Destinations.Count); } else { Assert.Null(affinityResult.Destinations); } }
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); }
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, "backend-1", _defaultOptions); Assert.Equal(AffinityStatus.OK, affinityResult.Status); provider.AffinitizeRequest(context, _defaultOptions, affinitizedDestination); Assert.False(context.Response.Headers.ContainsKey(AffinityHeaderName)); }
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]); }
public void FindAffinitizedDestination_CustomHeaderNameIsNotSpecified_UseDefaultName(Dictionary <string, string> settings) { var options = new BackendConfig.BackendSessionAffinityOptions(true, "CustomHeader", "Return503", 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, "backend-1", options); Assert.Equal(AffinityStatus.OK, affinityResult.Status); Assert.Equal(1, affinityResult.Destinations.Count); Assert.Same(affinitizedDestination, affinityResult.Destinations[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, "backend-1", _defaultOptions); Assert.Equal(AffinityStatus.OK, affinityResult.Status); Assert.Equal(1, affinityResult.Destinations.Count); Assert.Same(affinitizedDestination, affinityResult.Destinations[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, "backend-1", _defaultOptions); Assert.Equal(AffinityStatus.AffinityKeyNotSet, affinityResult.Status); Assert.Null(affinityResult.Destinations); }