public async Task ThrowingArgumentNullException_FromAction_GetsReturnedToClientParsedAsJson( string actionName ) { string controllerName = "Exception"; string requestUrl = String.Format( "{0}/{1}/{2}", ScenarioHelper.BaseAddress, controllerName, actionName ); await ScenarioHelper.RunTestAsync( controllerName, "/{action}", new HttpRequestMessage(HttpMethod.Post, requestUrl), async (response) => { Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); dynamic json = JToken.Parse(await response.Content.ReadAsStringAsync()); string result = json.ExceptionType; Assert.Equal(typeof(ArgumentNullException).FullName, result); } ); }
public async Task ThrowingOnActionIncludesErrorDetail(bool isLocal, IncludeErrorDetailPolicy includeErrorDetail, bool?customErrors, bool expectErrorDetail) { string controllerName = "Exception"; string requestUrl = String.Format("{0}/{1}/{2}", "http://www.foo.com", controllerName, "ArgumentNull"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl); request.Properties[HttpPropertyKeys.IsLocalKey] = new Lazy <bool>(() => isLocal); if (customErrors != null) { request.Properties[HttpPropertyKeys.IncludeErrorDetailKey] = new Lazy <bool>(() => !(bool)customErrors); } await ScenarioHelper.RunTestAsync( controllerName, "/{action}", request, async (response) => { if (expectErrorDetail) { await AssertResponseIncludesErrorDetailAsync(response); } else { await AssertResponseDoesNotIncludeErrorDetailAsync(response); } }, (config) => { config.IncludeErrorDetailPolicy = includeErrorDetail; } ); }
public async Task Service_ReturnsInternalServerError_WhenMultipleControllersAreFound() { string controllerName = "Duplicate"; string requestUrl = String.Format( "{0}/{1}", ScenarioHelper.BaseAddress, controllerName ); await ScenarioHelper.RunTestAsync( controllerName, "", new HttpRequestMessage(HttpMethod.Post, requestUrl), async (response) => { Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); var result = await response.Content.ReadAsAsync <HttpError>(); Assert.Contains( String.Format( SRResources.DefaultControllerFactory_ControllerNameAmbiguous_WithRouteTemplate, controllerName, "{controller}", String.Empty, Environment.NewLine ), result["ExceptionMessage"] as string ); } ); }
public async Task ThrowingArgumentNullException_FromAction_GetsReturnedToClient( string actionName ) { string controllerName = "Exception"; string requestUrl = String.Format( "{0}/{1}/{2}", ScenarioHelper.BaseAddress, controllerName, actionName ); await ScenarioHelper.RunTestAsync( controllerName, "/{action}", new HttpRequestMessage(HttpMethod.Post, requestUrl), async (response) => { Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); HttpError exception = await response.Content.ReadAsAsync <HttpError>(); Assert.Equal( typeof(ArgumentNullException).FullName, exception["ExceptionType"].ToString() ); } ); }
public async Task Service_ReturnsInternalServerError_WhenMultipleActionsAreFound() { string controllerName = "Exception"; string requestUrl = String.Format( "{0}/{1}", ScenarioHelper.BaseAddress, controllerName ); await ScenarioHelper.RunTestAsync( controllerName, "", new HttpRequestMessage(HttpMethod.Post, requestUrl), async (response) => { Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); var result = await response.Content.ReadAsAsync <HttpError>(); Assert.Contains( String.Format( SRResources.ApiControllerActionSelector_AmbiguousMatch, String.Empty ), result["ExceptionMessage"] as string ); } ); }
public async Task Service_ReturnsMethodNotAllowed_WhenActionsDoesNotSupportTheRequestHttpMethod() { string controllerName = "Exception"; string actionName = "GetString"; HttpMethod requestMethod = HttpMethod.Post; string requestUrl = String.Format( "{0}/{1}/{2}", ScenarioHelper.BaseAddress, controllerName, actionName ); await ScenarioHelper.RunTestAsync( controllerName, "/{action}", new HttpRequestMessage(requestMethod, requestUrl), async (response) => { Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode); var result = await response.Content.ReadAsAsync <HttpError>(); Assert.Equal( String.Format( SRResources.ApiControllerActionSelector_HttpMethodNotSupported, requestMethod.Method ), result.Message ); } ); }
public async Task Service_ReturnsNotFound_WhenActionNameDoesNotExist() { string controllerName = "Exception"; string actionName = "actionNotFound"; string requestUrl = String.Format( "{0}/{1}/{2}", ScenarioHelper.BaseAddress, controllerName, actionName ); await ScenarioHelper.RunTestAsync( controllerName, "/{action}", new HttpRequestMessage(HttpMethod.Post, requestUrl), async (response) => { Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); var result = await response.Content.ReadAsAsync <HttpError>(); Assert.Equal( String.Format( SRResources.ApiControllerActionSelector_ActionNameNotFound, controllerName, actionName ), result["MessageDetail"] ); } ); }
public async Task Service_ReturnsNotFound_WhenControllerNameDoesNotExist() { string controllerName = "randomControllerThatCannotBeFound"; string requestUrl = String.Format( "{0}/{1}", ScenarioHelper.BaseAddress, controllerName ); await ScenarioHelper.RunTestAsync( controllerName, "", new HttpRequestMessage(HttpMethod.Post, requestUrl), async (response) => { Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); var result = await response.Content.ReadAsAsync <HttpError>(); Assert.Equal( String.Format( SRResources.DefaultControllerFactory_ControllerNameNotFound, controllerName ), result["MessageDetail"] ); } ); }
public async Task ThrowingHttpResponseException_FromFilter_GetsReturnedToClient( string actionName, HttpStatusCode responseExceptionStatusCode ) { string controllerName = "Exception"; string requestUrl = String.Format( "{0}/{1}/{2}", ScenarioHelper.BaseAddress, controllerName, actionName ); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl); request.Headers.Add( ExceptionController.ResponseExceptionHeaderKey, responseExceptionStatusCode.ToString() ); await ScenarioHelper.RunTestAsync( controllerName, "/{action}", request, async (response) => { Assert.Equal(responseExceptionStatusCode, response.StatusCode); Assert.Equal( "HttpResponseExceptionMessage", await response.Content.ReadAsAsync <string>() ); } ); }
public async Task ThrowingHttpResponseException_FromAction_GetsReturnedToClient(string actionName) { string controllerName = "Exception"; string requestUrl = String.Format("{0}/{1}/{2}", ScenarioHelper.BaseAddress, controllerName, actionName); await ScenarioHelper.RunTestAsync( controllerName, "/{action}", new HttpRequestMessage(HttpMethod.Post, requestUrl), (response) => { Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode); return(Task.FromResult(0)); } ); }