public ApplicationControllerTests() { defaultMapper = new ApplicationToPageModelMapper(); defaultLogger = A.Fake <ILogger <ApplicationController> >(); defaultApplicationService = A.Fake <IApplicationService>(); defaultVersionedFiles = A.Fake <IVersionedFiles>(); defaultConfiguration = A.Fake <IConfiguration>(); defaultBaseUrlService = A.Fake <IBaseUrlService>(); defaultApplicationModel = new ApplicationModel { Path = new PathModel { Path = ChildAppPath }, Regions = new List <RegionModel> { new RegionModel { Path = ChildAppPath, IsHealthy = true, PageRegion = PageRegion.Body, RegionEndpoint = "http://childApp/bodyRegion", }, }, }; A.CallTo(() => defaultApplicationService.GetApplicationAsync(ChildAppPath)).Returns(defaultApplicationModel); var fakeHttpContext = new DefaultHttpContext { Request = { QueryString = QueryString.Create("test", "testvalue") } }; defaultPostRequestViewModel = new ActionPostRequestModel { Path = ChildAppPath, FormCollection = new FormCollection(new Dictionary <string, StringValues> { { "someKey", "someFormValue" }, }), }; defaultGetController = new ApplicationController(defaultMapper, defaultLogger, defaultApplicationService, defaultVersionedFiles, defaultConfiguration, defaultBaseUrlService) { ControllerContext = new ControllerContext { HttpContext = fakeHttpContext, }, }; defaultPostController = new ApplicationController(defaultMapper, defaultLogger, defaultApplicationService, defaultVersionedFiles, defaultConfiguration, defaultBaseUrlService) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Request = { Method = "POST" }, }, }, }; }
public async Task <IActionResult> Action(ActionPostRequestModel requestViewModel) { var viewModel = versionedFiles.BuildDefaultPageViewModel(configuration); if (requestViewModel != null) { requestViewModel.Path = requestViewModel.Path?.ToLowerInvariant(); requestViewModel.Data = requestViewModel.Data?.ToLowerInvariant(); bool postFirstRequest = true; var errorRequestViewModel = new ActionPostRequestModel { Path = AlertPathName, Data = $"{(int)HttpStatusCode.NotFound}", }; var requestItems = new[] { requestViewModel, errorRequestViewModel, new ActionPostRequestModel { Path = AlertPathName, Data = $"{(int)HttpStatusCode.InternalServerError}", }, }; foreach (var requestItem in requestItems) { try { logger.LogInformation($"{nameof(Action)}: Getting child response for: {requestItem.Path}/{requestItem.Data}"); var application = await applicationService.GetApplicationAsync(requestItem).ConfigureAwait(false); if (application?.AppRegistrationModel == null) { var errorString = $"The path '{requestItem.Path}' is not registered"; logger.LogWarning($"{nameof(Action)}: {errorString}"); Response.StatusCode = (int)HttpStatusCode.NotFound; } else { await mapper.Map(application, viewModel).ConfigureAwait(false); applicationService.RequestBaseUrl = baseUrlService.GetBaseUrl(Request, Url); KeyValuePair <string, string>[] formParameters = null; if (requestItem.FormCollection != null && requestItem.FormCollection.Any()) { formParameters = (from a in requestItem.FormCollection select new KeyValuePair <string, string>(a.Key, a.Value)).ToArray(); } if (postFirstRequest) { postFirstRequest = false; await applicationService.PostMarkupAsync(application, formParameters, viewModel).ConfigureAwait(false); } else { await applicationService.GetMarkupAsync(application, viewModel, string.Empty).ConfigureAwait(false); } logger.LogInformation($"{nameof(Action)}: Received child response for: {application.AppRegistrationModel.Path}/{application.Article}"); if (string.Compare(application.AppRegistrationModel.Path, AlertPathName, true, CultureInfo.InvariantCulture) == 0) { if (int.TryParse(application.Article, out var statusCode)) { Response.StatusCode = statusCode; } } break; } } catch (EnhancedHttpException ex) { var errorString = $"The content {ex.Url} responded with {ex.StatusCode}"; logger.LogWarning($"{nameof(Action)}: {errorString}"); Response.StatusCode = (int)ex.StatusCode; errorRequestViewModel.Data = $"{Response.StatusCode}"; } catch (RedirectException ex) { string redirectTo = ex.Location?.OriginalString; logger.LogInformation(ex, $"{nameof(Action)}: Redirecting from: {ex.OldLocation?.ToString()} to: {redirectTo}"); Response.Redirect(redirectTo, ex.IsPermenant); break; } } } return(View(MainRenderViewName, Map(viewModel))); }
public ApplicationControllerTests() { defaultAppRegistryDataService = A.Fake <IAppRegistryDataService>(); defaultMapper = new ApplicationToPageModelMapper(defaultAppRegistryDataService); defaultLogger = A.Fake <ILogger <ApplicationController> >(); defaultApplicationService = A.Fake <IApplicationService>(); defaultVersionedFiles = A.Fake <IVersionedFiles>(); defaultConfiguration = A.Fake <IConfiguration>(); defaultBaseUrlService = A.Fake <IBaseUrlService>(); neo4JService = A.Fake <INeo4JService>(); defaultApplicationModel = new ApplicationModel { AppRegistrationModel = new AppRegistrationModel { Path = ChildAppPath, Regions = new List <RegionModel> { new RegionModel { IsHealthy = true, PageRegion = PageRegion.Body, RegionEndpoint = "http://childApp/bodyRegion", }, }, }, }; defaultPostRequestViewModel = new ActionPostRequestModel { Path = ChildAppPath, Data = ChildAppData, FormCollection = new FormCollection(new Dictionary <string, StringValues> { { "someKey", "someFormValue" }, }), }; childAppActionGetRequestModel = defaultPostRequestViewModel; A.CallTo(() => defaultApplicationService.GetApplicationAsync(childAppActionGetRequestModel)).Returns(defaultApplicationModel); var fakeHttpContext = new DefaultHttpContext { Request = { QueryString = QueryString.Create("test", "testvalue") } }; defaultGetController = new ApplicationController(defaultMapper, defaultLogger, defaultApplicationService, defaultVersionedFiles, defaultConfiguration, defaultBaseUrlService, neo4JService) { ControllerContext = new ControllerContext { HttpContext = fakeHttpContext, }, }; defaultPostController = new ApplicationController(defaultMapper, defaultLogger, defaultApplicationService, defaultVersionedFiles, defaultConfiguration, defaultBaseUrlService, neo4JService) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Request = { Method = "POST" }, }, }, }; bearerTokenController = new ApplicationController(defaultMapper, defaultLogger, defaultApplicationService, defaultVersionedFiles, defaultConfiguration, defaultBaseUrlService, neo4JService) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim("bearer", "test") }, "mock")) }, }, }; postBearerTokenController = new ApplicationController(defaultMapper, defaultLogger, defaultApplicationService, defaultVersionedFiles, defaultConfiguration, defaultBaseUrlService, neo4JService) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim("bearer", "test") }, "mock")) }, }, }; }