Skip to content

Commit

Permalink
Fix for #111 and #116 (#118)
Browse files Browse the repository at this point in the history
Fix for #111 (500 error for non-API endpoints) and #116 (PreprocessingDelegatingHandler breaks Swagger UI).
  • Loading branch information
nukefusion authored and Jouke van der Maas committed Jun 15, 2016
1 parent 5e16259 commit a7dfa4f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Saule/Http/PreprocessingDelegatingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Saule.Http
{
using System.Linq;

/// <summary>
/// Processes JSON API responses to enable filtering, pagination and sorting.
/// </summary>
Expand Down Expand Up @@ -61,10 +63,12 @@ internal static PreprocessResult PreprocessRequest(
protected override async Task<HttpResponseMessage> 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;
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/Controllers/StaticController.cs
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
1 change: 1 addition & 0 deletions Tests/Helpers/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down
13 changes: 13 additions & 0 deletions Tests/Integration/ContentNegotiationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="ApiResourceTests.cs" />
<Compile Include="Controllers\BrokenController.cs" />
<Compile Include="Controllers\CompaniesController.cs" />
<Compile Include="Controllers\StaticController.cs" />
<Compile Include="Helpers\Get.cs" />
<Compile Include="Helpers\HttpClientExtensions.cs" />
<Compile Include="Helpers\NewSetupJsonApiServer.cs" />
Expand Down

0 comments on commit a7dfa4f

Please sign in to comment.