From a7dfa4ff283c3f1273196a654a2de5cc8a2abc13 Mon Sep 17 00:00:00 2001 From: Matt Bird Date: Wed, 15 Jun 2016 18:48:29 +0100 Subject: [PATCH] Fix for #111 and #116 (#118) Fix for #111 (500 error for non-API endpoints) and #116 (PreprocessingDelegatingHandler breaks Swagger UI). --- Saule/Http/PreprocessingDelegatingHandler.cs | 8 ++++++-- Tests/Controllers/StaticController.cs | 16 ++++++++++++++++ Tests/Helpers/Paths.cs | 1 + Tests/Integration/ContentNegotiationTests.cs | 13 +++++++++++++ Tests/Tests.csproj | 1 + 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 Tests/Controllers/StaticController.cs diff --git a/Saule/Http/PreprocessingDelegatingHandler.cs b/Saule/Http/PreprocessingDelegatingHandler.cs index db2d07f..17089fa 100644 --- a/Saule/Http/PreprocessingDelegatingHandler.cs +++ b/Saule/Http/PreprocessingDelegatingHandler.cs @@ -11,6 +11,8 @@ namespace Saule.Http { + using System.Linq; + /// /// Processes JSON API responses to enable filtering, pagination and sorting. /// @@ -61,10 +63,12 @@ internal static PreprocessResult PreprocessRequest( protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var result = await base.SendAsync(request, cancellationToken); + var hasMediaType = request.Headers.Accept.Any(x => x.MediaType == Constants.MediaType); var statusCode = (int)result.StatusCode; - if (statusCode >= 400 && statusCode < 500) - { // probably malformed request or not found + if (!hasMediaType || (statusCode >= 400 && statusCode < 500)) + { + // probably malformed request or not found return result; } diff --git a/Tests/Controllers/StaticController.cs b/Tests/Controllers/StaticController.cs new file mode 100644 index 0000000..f9baa2c --- /dev/null +++ b/Tests/Controllers/StaticController.cs @@ -0,0 +1,16 @@ +namespace Tests.Controllers +{ + using System.Net; + using System.Web.Http; + + [RoutePrefix("static")] + public class StaticController : ApiController + { + [HttpGet] + [Route("text")] + public IHttpActionResult GetStaticFile() + { + return this.Content(HttpStatusCode.OK, "This is static content."); + } + } +} diff --git a/Tests/Helpers/Paths.cs b/Tests/Helpers/Paths.cs index 43a624f..c458f37 100644 --- a/Tests/Helpers/Paths.cs +++ b/Tests/Helpers/Paths.cs @@ -2,6 +2,7 @@ { public static class Paths { + public const string StaticText = "static/text"; public const string SingleResource = "api/people/123/"; public const string ResourceCollection = "api/people/"; public const string NonExistingPath = "api/wrong/"; diff --git a/Tests/Integration/ContentNegotiationTests.cs b/Tests/Integration/ContentNegotiationTests.cs index b30911d..1f5688f 100644 --- a/Tests/Integration/ContentNegotiationTests.cs +++ b/Tests/Integration/ContentNegotiationTests.cs @@ -162,6 +162,19 @@ public async Task MustReturn200OkForOneValidAccept() Assert.Equal(HttpStatusCode.OK, result.StatusCode); } + + + [Fact(DisplayName = "Should return OK for a static content request that does not have media type parameters")] + public async Task MustReturn200OkForStaticContent() + { + var target = _server.GetClient(); + + target.DefaultRequestHeaders.Accept.Clear(); + + var result = await target.GetAsync(Paths.StaticText); + + Assert.Equal(HttpStatusCode.OK, result.StatusCode); + } } } } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 6500500..1ba6cb6 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -106,6 +106,7 @@ +