public void DoesNothingForUnknownException()
        {
            var target  = new ApiExceptionFilterAttribute();
            var context = GetContext(new InvalidOperationException("Something weird."));

            target.OnException(context);

            Assert.Null(context.Result);
        }
        public void ReturnsExpectedStatusCodeForKnowExceptions(Exception ex, HttpStatusCode statusCode, string message)
        {
            var target  = new ApiExceptionFilterAttribute();
            var context = GetContext(ex);

            target.OnException(context);

            Assert.NotNull(context.Result);
            var jsonResult = Assert.IsType <JsonResult>(context.Result);

            Assert.Equal(statusCode, (HttpStatusCode)jsonResult.StatusCode);
            var response = Assert.IsType <ErrorResponse>(jsonResult.Value);

            Assert.False(response.Success);
            Assert.Equal(message, response.Message);
        }
Esempio n. 3
0
        public async Task ReturnsExpectedStatusCodeForKnowExceptions(Exception ex, HttpStatusCode statusCode, string message)
        {
            var target  = new ApiExceptionFilterAttribute();
            var context = GetContext(ex);

            target.OnException(context);

            Assert.NotNull(context.Response);
            Assert.Equal(statusCode, context.Response.StatusCode);
            var content = await context.Response.Content.ReadAsStringAsync();

            var json = JObject.Parse(content);

            Assert.Equal(new[] { "Success", "Message" }, json.Properties().Select(x => x.Name).ToArray());
            Assert.Equal(false, json["Success"]);
            Assert.Equal(message, json["Message"]);
        }