Skip to content

Commit 777b280

Browse files
authored
Merge pull request #12 from Research-Institute/v0.1.1
V0.1.1
2 parents 067dcad + bf7f76b commit 777b280

File tree

7 files changed

+94
-7
lines changed

7 files changed

+94
-7
lines changed

JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

+22-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using JsonApiDotNetCore.Routing;
44
using Microsoft.AspNetCore.Http;
55
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Primitives;
67

78
namespace JsonApiDotNetCore.Middleware
89
{
@@ -28,21 +29,39 @@ public async Task Invoke(HttpContext context)
2829
{
2930
_logger.LogInformation("Passing request to JsonApiService: " + context.Request.Path);
3031

31-
if(context.Request.ContentType == "application/vnd.api+json") {
32+
if(IsJsonApiRequest(context)) {
3233
var routeWasHandled = _router.HandleJsonApiRoute(context, _serviceProvider);
3334
if(!routeWasHandled)
3435
RespondNotFound(context);
3536
}
3637
else
3738
{
38-
_logger.LogInformation("Content-Type invalid for JsonAPI");
39-
4039
await _next.Invoke(context);
4140

4241
RespondUnsupportedMediaType(context);
4342
}
4443
}
4544

45+
private bool IsJsonApiRequest(HttpContext context)
46+
{
47+
StringValues acceptHeader;
48+
if(context.Request.Headers.TryGetValue("Accept", out acceptHeader) && acceptHeader == "application/vnd.api+json")
49+
{
50+
if(context.Request.ContentLength > 0) {
51+
if(context.Request.ContentType == "application/vnd.api+json") {
52+
return true;
53+
}
54+
_logger.LogInformation("Content-Type invalid for JsonAPI");
55+
return false;
56+
}
57+
return true;
58+
}
59+
60+
_logger.LogInformation("Accept header invalid for JsonAPI");
61+
62+
return false;
63+
}
64+
4665
private void RespondUnsupportedMediaType(HttpContext context)
4766
{
4867
context.Response.StatusCode = 415;

JsonApiDotNetCore/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.0-alpha-*",
2+
"version": "0.1.1",
33
"packOptions": {
44
"licenseUrl": "https://github.com/Research-Institute/json-api-dotnet-core/tree/master/JsonApiDotNetCore/LICENSE",
55
"projectUrl": "https://github.com/Research-Institute/json-api-dotnet-core/"

JsonApiDotNetCoreExample/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"Microsoft.Extensions.Logging.Console": "1.0.0",
1212
"Microsoft.Extensions.Logging.Debug": "1.0.0",
1313
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
14-
"JsonApiDotNetCore": "0.1.0",
14+
"JsonApiDotNetCore": "0.1.1",
1515
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0",
1616
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
1717
},

JsonApiDotNetCoreTests/.editorconfig

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
8+
[*]
9+
10+
# Change these settings to your own preference
11+
indent_style = space
12+
indent_size = 2
13+
14+
# We recommend you to keep these unchanged
15+
end_of_line = lf
16+
charset = utf-8
17+
trim_trailing_whitespace = true
18+
insert_final_newline = true
19+
20+
[*.md]
21+
trim_trailing_whitespace = false

JsonApiDotNetCoreTests/Middleware/UnitTests/JsonApiMiddlewareTests.cs

+41
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public async void Invoke_CallsHandleJsonApiRequest_OnRouter()
1717
var httpRequestMock = new Mock<HttpRequest>();
1818
httpRequestMock.Setup(r => r.Path).Returns(new PathString(""));
1919
httpRequestMock.Setup(r => r.ContentType).Returns("application/vnd.api+json");
20+
httpRequestMock.Setup(r => r.ContentLength).Returns(0);
21+
var headers = new HeaderDictionary();
22+
headers.Add("Accept","application/vnd.api+json");
23+
httpRequestMock.Setup(r => r.Headers).Returns(headers);
2024

2125
var httpContextMock = new Mock<HttpContext>();
2226
httpContextMock.Setup(c => c.Request).Returns(httpRequestMock.Object);
@@ -32,13 +36,50 @@ public async void Invoke_CallsHandleJsonApiRequest_OnRouter()
3236
Assert.True(router.DidHandleRoute);
3337
}
3438

39+
[Fact]
40+
public async void Invoke_SetsStatusCode_To415_ForInvalidAcceptType()
41+
{
42+
// arrange
43+
var httpRequestMock = new Mock<HttpRequest>();
44+
httpRequestMock.Setup(r => r.Path).Returns(new PathString(""));
45+
httpRequestMock.Setup(r => r.ContentType).Returns("application/vnd.api+json");
46+
httpRequestMock.Setup(r => r.ContentLength).Returns(0);
47+
var headers = new HeaderDictionary();
48+
headers.Add("Accept","");
49+
httpRequestMock.Setup(r => r.Headers).Returns(headers);
50+
51+
var httpResponsMock = new Mock<HttpResponse>();
52+
httpResponsMock.SetupAllProperties();
53+
httpResponsMock.Setup(r => r.Body).Returns(new MemoryStream());
54+
55+
var httpContextMock = new Mock<HttpContext>();
56+
httpContextMock.Setup(c => c.Request).Returns(httpRequestMock.Object);
57+
httpContextMock.Setup(c => c.Response).Returns(httpResponsMock.Object);
58+
59+
var requestDelegateMock = new Mock<RequestDelegate>();
60+
61+
var router = new TestRouter();
62+
var loggerMock = new Mock<ILogger<JsonApiMiddleware>>();
63+
var middleware = new JsonApiMiddleware(requestDelegateMock.Object, loggerMock.Object, router, null);
64+
65+
// act
66+
await middleware.Invoke(httpContextMock.Object);
67+
68+
// assert
69+
Assert.Equal(415, httpResponsMock.Object.StatusCode);
70+
}
71+
3572
[Fact]
3673
public async void Invoke_SetsStatusCode_To415_ForInvalidContentType()
3774
{
3875
// arrange
3976
var httpRequestMock = new Mock<HttpRequest>();
4077
httpRequestMock.Setup(r => r.Path).Returns(new PathString(""));
4178
httpRequestMock.Setup(r => r.ContentType).Returns("");
79+
httpRequestMock.Setup(r => r.ContentLength).Returns(1);
80+
var headers = new HeaderDictionary();
81+
headers.Add("Accept","application/vnd.api+json");
82+
httpRequestMock.Setup(r => r.Headers).Returns(headers);
4283

4384
var httpResponsMock = new Mock<HttpResponse>();
4485
httpResponsMock.SetupAllProperties();

JsonApiDotNetCoreTests/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "1.0.0-alpha",
33
"testRunner": "xunit",
44
"dependencies": {
5-
"JsonApiDotNetCore": "0.1.0",
5+
"JsonApiDotNetCore": "0.1.1",
66
"dotnet-test-xunit": "2.2.0-preview2-build1029",
77
"xunit": "2.2.0-beta2-build3300",
88
"moq": "4.6.38-alpha"

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![Build status](https://ci.appveyor.com/api/projects/status/9fvgeoxdikwkom10?svg=true)](https://ci.appveyor.com/project/jaredcnance/json-api-dotnet-core)
44
[![Travis](https://img.shields.io/travis/Research-Institute/json-api-dotnet-core.svg?maxAge=3600&label=travis)](https://travis-ci.org/Research-Institute/json-api-dotnet-core)
5+
[![NuGet](https://img.shields.io/nuget/v/JsonApiDotNetCore.svg)](https://www.nuget.org/packages/JsonApiDotNetCore/)
6+
[![MyGet CI](https://img.shields.io/myget/research-institute/v/JsonApiDotNetCore.svg)](https://www.myget.org/feed/research-institute/package/nuget/JsonApiDotNetCore)
57

68
JSON API Spec Conformance: **Non Conforming**
79

@@ -11,7 +13,11 @@ For pre-releases, add the [MyGet](https://www.myget.org/feed/Details/research-in
1113
(https://www.myget.org/F/research-institute/api/v3/index.json)
1214
to your nuget configuration.
1315

14-
NuGet packages will be published at v0.1.0.
16+
`Install-Package JsonApiDotNetCore`
17+
18+
```
19+
"JsonApiDotNetCore": "0.1.0"
20+
```
1521

1622
## Usage
1723

0 commit comments

Comments
 (0)