private async Task <T> CallHttpClientXmlAsync <T>(ApplicationSitemapModel model) { using (var request = new HttpRequestMessage(HttpMethod.Get, model.SitemapUrl)) { if (!string.IsNullOrWhiteSpace(model.BearerToken)) { request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", model.BearerToken); } request.Headers.Add(HeaderNames.Accept, MediaTypeNames.Application.Xml); var response = await httpClient.SendAsync(request).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var serializer = new XmlSerializer(typeof(T)); using (var reader = new StringReader(responseString)) { using (var xmlReader = XmlReader.Create(reader)) { xmlReader.Read(); return((T)serializer.Deserialize(xmlReader)); } } } }
public async Task GetAsyncReturnsExceptionIfNoSitemapsTextFound() { var sitemapService = new ApplicationSitemapService(defaultHttpClient); var model = new ApplicationSitemapModel { BearerToken = "SomeBearerToken" }; var exceptionResult = await Assert.ThrowsAsync <InvalidOperationException>(async() => await sitemapService.GetAsync(model).ConfigureAwait(false)).ConfigureAwait(false); Assert.StartsWith("An invalid request URI was provided.", exceptionResult.Message, StringComparison.OrdinalIgnoreCase); }
public async Task <IEnumerable <SitemapLocation> > GetAsync(ApplicationSitemapModel model) { if (model == null) { return(null); } var responseTask = await CallHttpClientXmlAsync <Sitemap>(model).ConfigureAwait(false); return(responseTask?.Locations); }
private async Task <T> CallHttpClientXmlAsync <T>(ApplicationSitemapModel model) { using (var request = new HttpRequestMessage(HttpMethod.Get, model.SitemapUrl)) { if (!string.IsNullOrWhiteSpace(model.BearerToken)) { request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", model.BearerToken); } request.Headers.Add(HeaderNames.Accept, MediaTypeNames.Application.Xml); var response = await httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(responseString)) { return(default);
public async Task <IEnumerable <SitemapLocation> > GetAsync(ApplicationSitemapModel model) { if (model == null) { return(null); } try { logger.LogInformation($"Getting Sitemap for: {model.Path}"); var responseTask = await CallHttpClientXmlAsync <Sitemap>(model); return(responseTask?.Locations); } catch (Exception ex) { logger.LogError(ex, $"Exception getting Sitemap for: {model.Path}"); return(null); } }
public async Task GetAsyncReturnsSitemapTextWhenApiReturnsSitemapText() { const string DummySitemapLocation = "http://SomeSitemapLocation"; var responseText = $"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"><url><loc>{DummySitemapLocation}</loc><priority>1</priority></url></urlset>"; var httpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(responseText), }; var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>(); A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponse); var fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender); var logger = A.Fake <ILogger <ApplicationSitemapService> >(); var httpClient = new HttpClient(fakeHttpMessageHandler) { BaseAddress = new Uri("http://SomeDummyCDNUrl") }; var sitemapService = new ApplicationSitemapService(logger, httpClient); var model = new ApplicationSitemapModel { BearerToken = "SomeBearerToken" }; var result = await sitemapService.GetAsync(model); var resultLocations = result.Select(r => r.Url); Assert.Contains(DummySitemapLocation, resultLocations); httpResponse.Dispose(); httpClient.Dispose(); fakeHttpMessageHandler.Dispose(); }
private async Task <List <ApplicationSitemapModel> > CreateApplicationSitemapModelTasksAsync(IList <Models.PathModel> paths) { var bearerToken = User.Identity.IsAuthenticated ? await bearerTokenRetriever.GetToken(HttpContext).ConfigureAwait(false) : null; var applicationSitemapModels = new List <ApplicationSitemapModel>(); foreach (var path in paths) { logger.LogInformation($"{nameof(Action)}: Getting child Sitemap for: {path.Path}"); var applicationSitemapModel = new ApplicationSitemapModel { Path = path.Path, BearerToken = bearerToken, SitemapUrl = path.SitemapURL, }; applicationSitemapModel.RetrievalTask = sitemapService.GetAsync(applicationSitemapModel); applicationSitemapModels.Add(applicationSitemapModel); } return(applicationSitemapModels); }