public async Task PostContentWhenBrokenCircuitExceptionThrownAndHealthCheckIsNotRequiredThenReturnOfflineHtml() { const string OfflineHtml = "<p>Offline HTML</p>"; var model = new RegionModel { IsHealthy = true, OfflineHtml = OfflineHtml, }; var fakeLogger = A.Fake <ILogger <ContentRetriever> >(); A.CallTo(() => fakeLogger.Log(LogLevel.Information, 0, A <IReadOnlyList <KeyValuePair <string, object> > > .Ignored, A <Exception> .Ignored, A <Func <object, Exception, string> > .Ignored)) .Throws <BrokenCircuitException>(); var fakeRegionService = A.Fake <IAppRegistryDataService>(); using var memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); var service = new ContentRetriever(httpClientFactory, fakeLogger, fakeRegionService, httpResponseMessageHandler, markupMessages, memoryCache, passOnHeaderSettings); var result = await service.PostContent("http://someUrl", "path", model, defaultFormPostParams, "http://baseUrl"); A.CallTo(() => fakeRegionService.SetRegionHealthState(A <string> .Ignored, A <PageRegion> .Ignored, false)) .MustNotHaveHappened(); Assert.Equal(OfflineHtml, result.Html); }
public async Task WhenPostContentIssuesARedirectThenRedirectExceptionIsThrown() { var baseUrl = "https://base/baseurl"; var postUrl = "https://base/posturl"; var redirectUrl = "https://child1/redirecturl"; var httpResponseMessage = new HttpResponseMessage() { StatusCode = HttpStatusCode.Redirect, }; httpResponseMessage.Headers.Location = new Uri(redirectUrl); var model = new RegionModel { IsHealthy = true, }; var httpHandler = new MockHttpMessageHandler(); httpHandler.When(HttpMethod.Post, postUrl).Respond(x => httpResponseMessage); var localHttpClientFactory = A.Fake <IUriSpecifcHttpClientFactory>(); A.CallTo(() => localHttpClientFactory.GetClientForRegionEndpoint(A <string> .Ignored)).Returns(httpHandler.ToHttpClient()); using var memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); var contentRetriever = new ContentRetriever(localHttpClientFactory, logger, appRegistryDataService, httpResponseMessageHandler, markupMessages, memoryCache, passOnHeaderSettings); var ex = await Assert.ThrowsAsync <RedirectException>(async() => await contentRetriever.PostContent(postUrl, "path", model, defaultFormPostParams, baseUrl)); Assert.Equal("https://base/baseurl/redirecturl", ex.Location.AbsoluteUri); Assert.Equal(postUrl, ex.OldLocation.AbsoluteUri); httpHandler.Dispose(); httpResponseMessage.Dispose(); }
public async Task GetContentWhenRegionReturnsRedirectResponseThenThrowRedirectException() { var redirectHttpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.Redirect, Content = new StringContent(DummyChildAppContent), }; redirectHttpResponse.Headers.Location = new Uri("http://SomeLocation"); var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(redirectHttpResponse); var fakeRedirectHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); var redirectHttpClient = new HttpClient(fakeRedirectHttpMessageHandler) { BaseAddress = new Uri("http://SomeRegionBaseAddress"), }; var model = new RegionModel { IsHealthy = true, }; var service = new ContentRetriever(redirectHttpClient, logger, appRegistryDataService, httpResponseMessageHandler, markupMessages); await Assert.ThrowsAnyAsync <RedirectException>(async() => await service.GetContent("http://someUrl", "path", model, false, "http://baseUrl").ConfigureAwait(false)).ConfigureAwait(false); fakeRedirectHttpMessageHandler.Dispose(); redirectHttpResponse.Dispose(); redirectHttpClient.Dispose(); }
public async Task PostContentWhenResponseIsStatusFoundThrowRedirectException() { var model = new RegionModel { IsHealthy = true, RegionEndpoint = "SomeRegionEndpoint", PageRegion = PageRegion.Body, }; var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.Found, Content = new StringContent(DummyChildAppContent), }; httpResponseMessage.Headers.Location = new Uri("http://SomeLocation"); var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponseMessage); var fakeRedirectHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); var postHttpClient = new HttpClient(fakeRedirectHttpMessageHandler) { BaseAddress = new Uri("http://SomeRegionBaseAddress"), }; var service = new ContentRetriever(postHttpClient, logger, regionService, httpResponseMessageHandler); await Assert.ThrowsAnyAsync <RedirectException>(async() => await service.PostContent("http://someUrl", model, defaultFormPostParams, "http://baseUrl").ConfigureAwait(false)).ConfigureAwait(false); httpResponseMessage.Dispose(); fakeRedirectHttpMessageHandler.Dispose(); postHttpClient.Dispose(); }
public ContentRetrieverTests() { httpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(DummyChildAppContent), }; var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponse); fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); httpClient = new HttpClient(fakeHttpMessageHandler) { BaseAddress = new Uri("http://SomeRegionBaseAddress"), }; defaultFormPostParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("formParam1", "testvalue") }; logger = A.Fake <ILogger <ContentRetriever> >(); regionService = A.Fake <IRegionService>(); httpResponseMessageHandler = A.Fake <IHttpResponseMessageHandler>(); defaultService = new ContentRetriever(httpClient, logger, regionService, httpResponseMessageHandler); }
public async Task GetContentWhenBrokenCircuitExceptionThrownAndHealthCheckIsNotRequiredThenReturnOfflineHtml() { const string OfflineHtml = "<p>Offline HTML</p>"; var model = new RegionModel { IsHealthy = true, OfflineHtml = OfflineHtml, }; var fakeRedirectHttpMessageHandler = A.Fake <IHttpResponseMessageHandler>(); A.CallTo(() => fakeRedirectHttpMessageHandler.Process(A <HttpResponseMessage> .Ignored)) .Throws <BrokenCircuitException>(); var fakeRegionService = A.Fake <IAppRegistryDataService>(); using var memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); var service = new ContentRetriever(httpClientFactory, logger, fakeRegionService, fakeRedirectHttpMessageHandler, markupMessages, memoryCache, passOnHeaderSettings); var result = await service.GetContent("someUrl", "path", model, true, "baseUrl", new HeaderDictionary()); A.CallTo(() => fakeRegionService.SetRegionHealthState(A <string> .Ignored, A <PageRegion> .Ignored, false)) .MustNotHaveHappened(); Assert.Equal(OfflineHtml, result); }
public async Task WhenPostContentIssuesARedirectThenRedirectExceptionIsThrown() { var baseUrl = "https://base/baseurl"; var postUrl = "https://base/posturl"; var redirectUrl = "https://child1/redirecturl"; var httpResponseMessage = new HttpResponseMessage() { StatusCode = HttpStatusCode.Redirect, }; httpResponseMessage.Headers.Location = new Uri(redirectUrl); var model = new RegionModel { IsHealthy = true, }; var httpHandler = new MockHttpMessageHandler(); httpHandler.When(HttpMethod.Post, postUrl).Respond(x => httpResponseMessage); var contentRetriever = new ContentRetriever(httpHandler.ToHttpClient(), logger, appRegistryDataService, httpResponseMessageHandler, markupMessages); var ex = await Assert.ThrowsAsync <RedirectException>(async() => await contentRetriever.PostContent(postUrl, "path", model, defaultFormPostParams, baseUrl).ConfigureAwait(false)).ConfigureAwait(false); Assert.Equal("https://base/baseurl/redirecturl", ex.Location.AbsoluteUri); Assert.Equal(postUrl, ex.OldLocation.AbsoluteUri); httpHandler.Dispose(); httpResponseMessage.Dispose(); }
public async Task GetContentWhenContentRetrievedFromChildAppWithHeadersThatShouldBePassedOnThenRequestMadeWithHeaders() { var model = new RegionModel { IsHealthy = true, RegionEndpoint = "SomeRegionEndpoint", PageRegion = PageRegion.Body, }; var headerSettings = Options.Create(new PassOnHeaderSettings { SupportedHeaders = new List <string> { "referer", }, }); var service = new ContentRetriever(httpClientFactory, logger, appRegistryDataService, httpResponseMessageHandler, markupMessages, memoryCache, headerSettings); var result = await service.GetContent("someUrl", "path", model, true, "baseUrl", new HeaderDictionary { new KeyValuePair <string, StringValues>("Referer", new StringValues("job-profiles")), }); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .That.Matches(x => x.Headers.Any(x => x.Key == "Referer" && x.Value.Contains("job-profiles"))))).MustHaveHappened(); }
public async Task WhenPostContentIssuesARedirectThenHandlerProcessesRequest() { var baseUrl = "https://base/baseurl"; var postUrl = "https://base/posturl"; var redirectUrl = "https://child1/redirecturl"; var httpResponseMessage = new HttpResponseMessage() { StatusCode = HttpStatusCode.Redirect, }; httpResponseMessage.Headers.Location = new Uri(redirectUrl); var model = new RegionModel { IsHealthy = true, }; var httpHandler = new MockHttpMessageHandler(); httpHandler.When(HttpMethod.Post, postUrl).Respond(x => httpResponseMessage); var contentRetriever = new ContentRetriever(httpHandler.ToHttpClient(), logger, appRegistryDataService, httpResponseMessageHandler, markupMessages); await Assert.ThrowsAsync <RedirectException>(async() => await contentRetriever.PostContent(postUrl, "path", model, defaultFormPostParams, baseUrl).ConfigureAwait(false)).ConfigureAwait(false); A.CallTo(() => httpResponseMessageHandler.Process(A <HttpResponseMessage> .That.Matches(x => x.StatusCode == HttpStatusCode.Redirect))).MustHaveHappenedOnceExactly(); httpHandler.Dispose(); httpResponseMessage.Dispose(); }
public async Task PostContentWhenBrokenCircuitExceptionThrownAndHealthCheckIsNotRequiredThenReturnOfflineHTML() { const string offlineHTML = "<p>Offline HTML</p>"; var model = new RegionModel { IsHealthy = true, OfflineHTML = offlineHTML, }; var fakeLogger = A.Fake <ILogger <ContentRetriever> >(); A.CallTo(() => fakeLogger.Log(LogLevel.Information, 0, A <IReadOnlyList <KeyValuePair <string, object> > > .Ignored, A <Exception> .Ignored, A <Func <object, Exception, string> > .Ignored)) .Throws <BrokenCircuitException>(); var fakeRegionService = A.Fake <IRegionService>(); var service = new ContentRetriever(httpClient, fakeLogger, fakeRegionService, httpResponseMessageHandler); var result = await service.PostContent("http://someUrl", model, defaultFormPostParams, "http://baseUrl").ConfigureAwait(false); A.CallTo(() => fakeRegionService.SetRegionHealthState(A <string> .Ignored, A <PageRegion> .Ignored, false)) .MustNotHaveHappened(); Assert.Equal(offlineHTML, result); }
public ContentRetrieverTests() { httpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(DummyChildAppContent), }; var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponse); fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); httpClient = new HttpClient(fakeHttpMessageHandler) { BaseAddress = new Uri("http://SomeRegionBaseAddress"), }; defaultFormPostParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("formParam1", "test value") }; logger = A.Fake <ILogger <ContentRetriever> >(); appRegistryDataService = A.Fake <IAppRegistryDataService>(); httpResponseMessageHandler = A.Fake <IHttpResponseMessageHandler>(); markupMessages = new MarkupMessages { AppOfflineHtml = "<h3>App offline</h3>", RegionOfflineHtml = new Dictionary <PageRegion, string> { { PageRegion.Head, "<h3>Head Region is offline</h3>" }, { PageRegion.Breadcrumb, "<h3>Breadcrumb Region is offline</h3>" }, { PageRegion.BodyTop, "<h3>BodyTop Region is offline</h3>" }, { PageRegion.Body, "<h3>Body Region is offline</h3>" }, { PageRegion.SidebarRight, "<h3>SidebarRight Region is offline</h3>" }, { PageRegion.SidebarLeft, "<h3>SidebarLeft Region is offline</h3>" }, { PageRegion.BodyFooter, "<h3>BodyFooter Region is offline</h3>" }, { PageRegion.HeroBanner, "<h3>HeroBanner Region is offline</h3>" }, }, }; defaultService = new ContentRetriever(httpClient, logger, appRegistryDataService, httpResponseMessageHandler, markupMessages); }
public async Task GetContentWhenBrokenCircuitExceptionThrownAndHealthCheckIsNotRequiredThenReturnOfflineHTML() { const string offlineHTML = "<p>Offline HTML</p>"; var model = new RegionModel { IsHealthy = true, OfflineHTML = offlineHTML, }; var fakeRedirectHttpMessageHandler = A.Fake <IHttpResponseMessageHandler>(); A.CallTo(() => fakeRedirectHttpMessageHandler.Process(A <HttpResponseMessage> .Ignored)) .Throws <BrokenCircuitException>(); var fakeRegionService = A.Fake <IRegionService>(); var service = new ContentRetriever(httpClient, logger, fakeRegionService, fakeRedirectHttpMessageHandler); var result = await service.GetContent("someUrl", model, true, "baseUrl").ConfigureAwait(false); A.CallTo(() => fakeRegionService.SetRegionHealthState(A <string> .Ignored, A <PageRegion> .Ignored, false)) .MustNotHaveHappened(); Assert.Equal(offlineHTML, result); }
public async Task GetContentWhenRegionReturnsRedirectResponseThenFollowRedirect() { var redirectHttpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.Redirect, Content = new StringContent(DummyChildAppContent), }; redirectHttpResponse.Headers.Location = new Uri("http://SomeLocation"); var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(redirectHttpResponse); var fakeRedirectHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); var redirectHttpClient = new HttpClient(fakeRedirectHttpMessageHandler) { BaseAddress = new Uri("http://SomeRegionBaseAddress"), }; var model = new RegionModel { IsHealthy = true, }; var service = new ContentRetriever(redirectHttpClient, logger, regionService, httpResponseMessageHandler); await service.GetContent("someUrl", model, true, "baseUrl").ConfigureAwait(false); A.CallTo(() => httpResponseMessageHandler.Process(null)).MustHaveHappened(); fakeRedirectHttpMessageHandler.Dispose(); redirectHttpResponse.Dispose(); redirectHttpClient.Dispose(); }
public ContentRetrieverWithSourceProperty(ContentRetriever <TSource, TKey> contentRetriever, PropertyInfo property) { if (contentRetriever == null) { throw new ArgumentNullException("contentRetriever"); } if (property == null) { throw new ArgumentNullException("property"); } ContentRetriever = contentRetriever; Property = property; }
public async Task PostContentWhenResponseIsStatusFoundThrowRedirectException() { var model = new RegionModel { IsHealthy = true, RegionEndpoint = "SomeRegionEndpoint", PageRegion = PageRegion.Body, }; var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.Found, Content = new StringContent(DummyChildAppContent), }; httpResponseMessage.Headers.Location = new Uri("http://SomeLocation"); var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponseMessage); var fakeRedirectHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); var postHttpClient = new HttpClient(fakeRedirectHttpMessageHandler) { BaseAddress = new Uri("http://SomeRegionBaseAddress"), }; using var httpHandler = new MockHttpMessageHandler(); httpHandler.When(HttpMethod.Post, "http://someUrl").Respond(_ => httpResponseMessage); var localHttpClientFactory = A.Fake <IUriSpecifcHttpClientFactory>(); A.CallTo(() => localHttpClientFactory.GetClientForRegionEndpoint(A <string> .Ignored)).Returns(httpHandler.ToHttpClient()); using var memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); var service = new ContentRetriever(localHttpClientFactory, logger, appRegistryDataService, httpResponseMessageHandler, markupMessages, memoryCache, passOnHeaderSettings); await Assert.ThrowsAnyAsync <RedirectException>(async() => await service.PostContent("http://someUrl", "path", model, defaultFormPostParams, "http://baseUrl")); httpResponseMessage.Dispose(); fakeRedirectHttpMessageHandler.Dispose(); postHttpClient.Dispose(); }
public async Task PostContentWhenBrokenCircuitExceptionThrownAndHealthCheckIsRequiredThenRegionStateUpdated() { var model = new RegionModel { IsHealthy = true, HealthCheckRequired = true, }; var fakeLogger = A.Fake <ILogger <ContentRetriever> >(); A.CallTo(() => fakeLogger.Log(LogLevel.Information, 0, A <IReadOnlyList <KeyValuePair <string, object> > > .Ignored, A <Exception> .Ignored, A <Func <object, Exception, string> > .Ignored)) .Throws <BrokenCircuitException>(); var fakeRegionService = A.Fake <IAppRegistryDataService>(); var service = new ContentRetriever(httpClient, fakeLogger, fakeRegionService, httpResponseMessageHandler, markupMessages); await service.PostContent("http://someUrl", "path", model, defaultFormPostParams, "http://baseUrl").ConfigureAwait(false); A.CallTo(() => fakeRegionService.SetRegionHealthState(A <string> .Ignored, A <PageRegion> .Ignored, false)) .MustHaveHappenedOnceExactly(); }
public async Task GetContentWhenBrokenCircuitExceptionThrownAndHealthCheckIsRequiredThenRegionStateUpdated() { var model = new RegionModel { IsHealthy = true, HealthCheckRequired = true, }; var fakeRedirectHttpMessageHandler = A.Fake <IHttpResponseMessageHandler>(); A.CallTo(() => fakeRedirectHttpMessageHandler.Process(A <HttpResponseMessage> .Ignored)) .Throws <BrokenCircuitException>(); var fakeRegionService = A.Fake <IRegionService>(); var service = new ContentRetriever(httpClient, logger, fakeRegionService, fakeRedirectHttpMessageHandler); await service.GetContent("someUrl", model, true, "baseUrl").ConfigureAwait(false); A.CallTo(() => fakeRegionService.SetRegionHealthState(A <string> .Ignored, A <PageRegion> .Ignored, false)) .MustHaveHappenedOnceExactly(); }
public async Task PostContentWhenResponseIsStatusOKReturnsContent() { var model = new RegionModel { IsHealthy = true, RegionEndpoint = "SomeRegionEndpoint", PageRegion = PageRegion.Body, }; var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(DummyChildAppContent), }; httpResponseMessage.Headers.Location = new Uri("http://SomeLocation"); var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponseMessage); var fakeRedirectHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); var postHttpClient = new HttpClient(fakeRedirectHttpMessageHandler) { BaseAddress = new Uri("http://SomeRegionBaseAddress"), }; using var memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); var service = new ContentRetriever(httpClientFactory, logger, appRegistryDataService, httpResponseMessageHandler, markupMessages, memoryCache, passOnHeaderSettings); var result = await service.PostContent("http://someUrl", "path", model, defaultFormPostParams, "http://baseUrl"); Assert.Equal(DummyChildAppContent, result.Html); httpResponseMessage.Dispose(); fakeRedirectHttpMessageHandler.Dispose(); postHttpClient.Dispose(); }
public async Task GetContentWhenRegionReturnsRedirectResponseThenFollowRedirect() { using var redirectHttpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.Redirect, Content = new StringContent(DummyChildAppContent), }; redirectHttpResponse.Headers.Location = new Uri("http://someUrl"); var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(redirectHttpResponse); using var fakeRedirectHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); using var redirectHttpClient = new HttpClient(fakeRedirectHttpMessageHandler) { BaseAddress = new Uri("http://SomeRegionBaseAddress"), }; var model = new RegionModel { IsHealthy = true, }; using var httpHandler = new MockHttpMessageHandler(); httpHandler.When(HttpMethod.Get, "http://someUrl").Respond(_ => redirectHttpResponse); var localHttpClientFactory = A.Fake <IUriSpecifcHttpClientFactory>(); A.CallTo(() => localHttpClientFactory.GetClientForRegionEndpoint(A <string> .Ignored)).Returns(httpHandler.ToHttpClient()); var service = new ContentRetriever(localHttpClientFactory, logger, appRegistryDataService, httpResponseMessageHandler, markupMessages, memoryCache, passOnHeaderSettings); await service.GetContent("http://someUrl", "path", model, true, baseUri, new HeaderDictionary()); A.CallTo(() => httpResponseMessageHandler.Process(null)).MustHaveHappened(); }