Skip to content

Commit 22b91ad

Browse files
author
Bart Koelman
authored
Minor fixes in content negotiation (#897)
* Converted existing content negotiation tests to new syntax. Fixed: Integration tests used to send request body "null" on GET requests, while they should not send a body at all. * Fixed: allow the use of quality factor in Accept header (content negotiation)
1 parent ccaff6d commit 22b91ad

File tree

8 files changed

+426
-315
lines changed

8 files changed

+426
-315
lines changed

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace JsonApiDotNetCore.Middleware
2323
/// </summary>
2424
public sealed class JsonApiMiddleware
2525
{
26+
private static readonly MediaTypeHeaderValue _mediaType = MediaTypeHeaderValue.Parse(HeaderConstants.MediaType);
27+
2628
private readonly RequestDelegate _next;
2729

2830
public JsonApiMiddleware(RequestDelegate next)
@@ -96,7 +98,7 @@ private static async Task<bool> ValidateContentTypeHeaderAsync(HttpContext httpC
9698
private static async Task<bool> ValidateAcceptHeaderAsync(HttpContext httpContext, JsonSerializerSettings serializerSettings)
9799
{
98100
StringValues acceptHeaders = httpContext.Request.Headers["Accept"];
99-
if (!acceptHeaders.Any() || acceptHeaders == HeaderConstants.MediaType)
101+
if (!acceptHeaders.Any())
100102
{
101103
return true;
102104
}
@@ -105,15 +107,17 @@ private static async Task<bool> ValidateAcceptHeaderAsync(HttpContext httpContex
105107

106108
foreach (var acceptHeader in acceptHeaders)
107109
{
108-
if (MediaTypeHeaderValue.TryParse(acceptHeader, out var headerValue))
110+
if (MediaTypeWithQualityHeaderValue.TryParse(acceptHeader, out var headerValue))
109111
{
112+
headerValue.Quality = null;
113+
110114
if (headerValue.MediaType == "*/*" || headerValue.MediaType == "application/*")
111115
{
112116
seenCompatibleMediaType = true;
113117
break;
114118
}
115119

116-
if (headerValue.MediaType == HeaderConstants.MediaType && !headerValue.Parameters.Any())
120+
if (_mediaType.Equals(headerValue))
117121
{
118122
seenCompatibleMediaType = true;
119123
break;
@@ -126,7 +130,7 @@ private static async Task<bool> ValidateAcceptHeaderAsync(HttpContext httpContex
126130
await FlushResponseAsync(httpContext.Response, serializerSettings, new Error(HttpStatusCode.NotAcceptable)
127131
{
128132
Title = "The specified Accept header value does not contain any supported media types.",
129-
Detail = $"Please include '{HeaderConstants.MediaType}' in the Accept header values."
133+
Detail = $"Please include '{_mediaType}' in the Accept header values."
130134
});
131135
return false;
132136
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/ContentNegotiationTests.cs

Lines changed: 0 additions & 296 deletions
This file was deleted.

0 commit comments

Comments
 (0)