Skip to content

Commit 2dcceaf

Browse files
committed
test(content-negotiation): clients should not send media type parameters
1 parent 6326917 commit 2dcceaf

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,25 @@ public static class IApplicationBuilderExtensions
66
{
77
public static IApplicationBuilder UseJsonApi(this IApplicationBuilder app)
88
{
9+
app.Use(async (context, next) =>
10+
{
11+
var contentType = context.Request.ContentType;
12+
if (contentType != null)
13+
{
14+
var contentTypeArr = contentType.Split(';');
15+
if (contentTypeArr[0] == "application/vnd.api+json" && contentTypeArr.Length == 2)
16+
{
17+
context.Response.StatusCode = 415;
18+
context.Response.Body.Flush();
19+
return;
20+
}
21+
}
22+
23+
await next.Invoke();
24+
});
25+
926
app.UseMvc();
10-
27+
1128
return app;
1229
}
1330
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/Spec/ContentNegotiation.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
using System;
2+
using System.Diagnostics.Contracts;
3+
using System.Globalization;
14
using System.Net;
25
using System.Net.Http;
6+
using System.Net.Http.Headers;
7+
using System.Text;
38
using System.Threading.Tasks;
49
using DotNetCoreDocs;
510
using DotNetCoreDocs.Models;
611
using DotNetCoreDocs.Writers;
712
using JsonApiDotNetCoreExample;
813
using Microsoft.AspNetCore.Hosting;
14+
using Microsoft.AspNetCore.Mvc.Formatters.Internal;
915
using Microsoft.AspNetCore.TestHost;
1016
using Xunit;
1117

@@ -32,13 +38,36 @@ public async Task Server_Sends_Correct_ContentType_Header()
3238
var server = new TestServer(builder);
3339
var client = server.CreateClient();
3440
var request = new HttpRequestMessage(httpMethod, route);
35-
41+
3642
// act
3743
var response = await client.SendAsync(request);
3844

3945
// assert
4046
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
4147
Assert.Equal("application/vnd.api+json", response.Content.Headers.ContentType.ToString());
4248
}
49+
50+
[Fact]
51+
public async Task Server_Responds_415_With_MediaType_Parameters()
52+
{
53+
// arrange
54+
var builder = new WebHostBuilder()
55+
.UseStartup<Startup>();
56+
var httpMethod = new HttpMethod("GET");
57+
var route = "/api/v1/todo-items";
58+
var description = new RequestProperties("Server responds with 415 if request contains media type parameters");
59+
var server = new TestServer(builder);
60+
var client = server.CreateClient();
61+
var request = new HttpRequestMessage(httpMethod, route);
62+
request.Content = new StringContent(string.Empty);
63+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
64+
request.Content.Headers.ContentType.CharSet="ISO-8859-4";
65+
66+
// act
67+
var response = await client.SendAsync(request);
68+
69+
// assert
70+
Assert.Equal(HttpStatusCode.UnsupportedMediaType, response.StatusCode);
71+
}
4372
}
4473
}

0 commit comments

Comments
 (0)