private async Task <object?> CallControllerAsync( IReadOnlyList <object?> arguments, MethodInfo method) { if (_methodValidation != null) { _methodValidation(method); } var callId = Guid.NewGuid(); var(url, actionInfo) = await _getInfo.GetInfo <T>(arguments.ToList(), method, _actionInfoTransformersCaller); using var request = HttpRequestFactory.Create( actionInfo.HttpMethod, url, actionInfo.Body, callId, actionInfo.BodyFormat, actionInfo.Headers, _serializer); CallDataDictionary.InsertEmptyDataToIndicateTestCall(callId); var result = await _webCaller.Call(request, actionInfo, new InvocationInfo(arguments, method)); return(await _resultFactory.Create <T>(result, callId.ToString(), method)); }
public async Task WhenControllerCallerIsTurnedOffMiddlewareIsNotHit() { CallDataDictionary.Clear(); using (var application = ApplicationBuilder.CreateApplication()) { var response = await application.HttpClient.GetAsync($"{nameof(GeneralPageModel)}?handler=ThrowsInvalidOperationException"); response.IsSuccessStatusCode.Should().BeFalse(); CallDataDictionary.IsEmpty().Should().BeTrue(); } }
public async Task WhenControllerCallerIsTurnedOffEverythingWorksNormally() { CallDataDictionary.Clear(); using var application = ApplicationBuilder.CreateApplication(); var response = await application.HttpClient.GetAsync($"{nameof(GeneralPageModel)}"); var content = await response.Content.ReadAsStringAsync(); content.Should().Contain("body"); CallDataDictionary.IsEmpty().Should().BeTrue(); }
public async Task Invoke( HttpContext context) { if (!CallDataDictionary.IsTestCall(context)) { await _next(context); return; } try { await _next(context); } catch (Exception ex) { CallDataDictionary.InsertException(context, ex); throw; } }
public async Task <object> Create <T>( HttpResponseMessage httpResponseMessage, string callId, MethodInfo methodInfo) { var actionReturnType = GeneralHelpers.GetReturnTypeOrGenericArgumentOfTask(methodInfo); var resultString = await httpResponseMessage.Content.ReadAsStringAsync(); CallDataDto callDataDto = CallDataDictionary.GetData(callId); if (callDataDto.Exception != null) { ExceptionDispatchInfo.Capture(callDataDto.Exception).Throw(); throw new InvalidOperationException("This is never thrown"); // this line is never reached } if (actionReturnType == typeof(ActionResult)) { return(new ControllerCallResult(httpResponseMessage, resultString, httpResponseMessage.StatusCode)); } if (GeneralHelpers.IsOrImplements(actionReturnType, typeof(ActionResult <>))) { var genericTypeOfActionResult = actionReturnType.GenericTypeArguments.First(); var ridgeResult = GeneralHelpers.CreateInstance( typeof(ControllerCallResult <>).MakeGenericType(genericTypeOfActionResult), httpResponseMessage, resultString, httpResponseMessage.StatusCode, _serializer); var actionResult = GeneralHelpers.CreateInstance(actionReturnType, ridgeResult); return(actionResult); } if (GeneralHelpers.IsOrImplements(actionReturnType, typeof(IActionResult))) { return(new ControllerCallResult(httpResponseMessage, resultString, httpResponseMessage.StatusCode)); } throw new InvalidOperationException($"Controller method must return {nameof(ActionResult)} or {nameof(ActionResult)}<T> or {nameof(IActionResult)}"); }
public async Task <object> Create <T>( HttpResponseMessage httpResponseMessage, string callId, MethodInfo methodInfo) { var resultString = await httpResponseMessage.Content.ReadAsStringAsync(); CallDataDto callDataDto = CallDataDictionary.GetData(callId); if (callDataDto.Exception != null) { ExceptionDispatchInfo.Capture(callDataDto.Exception).Throw(); throw new InvalidOperationException("This is never thrown"); // this line is never reached } if (callDataDto.PageModel == null) { throw new InvalidOperationException("Call data are null."); } return(new PageResult <T>(httpResponseMessage, (T)callDataDto.PageModel, resultString, httpResponseMessage.StatusCode)); }