Skip to content

V0.1.1 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JsonApiDotNetCore.Routing;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;

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

if(context.Request.ContentType == "application/vnd.api+json") {
if(IsJsonApiRequest(context)) {
var routeWasHandled = _router.HandleJsonApiRoute(context, _serviceProvider);
if(!routeWasHandled)
RespondNotFound(context);
}
else
{
_logger.LogInformation("Content-Type invalid for JsonAPI");

await _next.Invoke(context);

RespondUnsupportedMediaType(context);
}
}

private bool IsJsonApiRequest(HttpContext context)
{
StringValues acceptHeader;
if(context.Request.Headers.TryGetValue("Accept", out acceptHeader) && acceptHeader == "application/vnd.api+json")
{
if(context.Request.ContentLength > 0) {
if(context.Request.ContentType == "application/vnd.api+json") {
return true;
}
_logger.LogInformation("Content-Type invalid for JsonAPI");
return false;
}
return true;
}

_logger.LogInformation("Accept header invalid for JsonAPI");

return false;
}

private void RespondUnsupportedMediaType(HttpContext context)
{
context.Response.StatusCode = 415;
Expand Down
2 changes: 1 addition & 1 deletion JsonApiDotNetCore/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0-alpha-*",
"version": "0.1.1",
"packOptions": {
"licenseUrl": "https://github.com/Research-Institute/json-api-dotnet-core/tree/master/JsonApiDotNetCore/LICENSE",
"projectUrl": "https://github.com/Research-Institute/json-api-dotnet-core/"
Expand Down
2 changes: 1 addition & 1 deletion JsonApiDotNetCoreExample/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"JsonApiDotNetCore": "0.1.0",
"JsonApiDotNetCore": "0.1.1",
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
},
Expand Down
21 changes: 21 additions & 0 deletions JsonApiDotNetCoreTests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true


[*]

# Change these settings to your own preference
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public async void Invoke_CallsHandleJsonApiRequest_OnRouter()
var httpRequestMock = new Mock<HttpRequest>();
httpRequestMock.Setup(r => r.Path).Returns(new PathString(""));
httpRequestMock.Setup(r => r.ContentType).Returns("application/vnd.api+json");
httpRequestMock.Setup(r => r.ContentLength).Returns(0);
var headers = new HeaderDictionary();
headers.Add("Accept","application/vnd.api+json");
httpRequestMock.Setup(r => r.Headers).Returns(headers);

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

[Fact]
public async void Invoke_SetsStatusCode_To415_ForInvalidAcceptType()
{
// arrange
var httpRequestMock = new Mock<HttpRequest>();
httpRequestMock.Setup(r => r.Path).Returns(new PathString(""));
httpRequestMock.Setup(r => r.ContentType).Returns("application/vnd.api+json");
httpRequestMock.Setup(r => r.ContentLength).Returns(0);
var headers = new HeaderDictionary();
headers.Add("Accept","");
httpRequestMock.Setup(r => r.Headers).Returns(headers);

var httpResponsMock = new Mock<HttpResponse>();
httpResponsMock.SetupAllProperties();
httpResponsMock.Setup(r => r.Body).Returns(new MemoryStream());

var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(c => c.Request).Returns(httpRequestMock.Object);
httpContextMock.Setup(c => c.Response).Returns(httpResponsMock.Object);

var requestDelegateMock = new Mock<RequestDelegate>();

var router = new TestRouter();
var loggerMock = new Mock<ILogger<JsonApiMiddleware>>();
var middleware = new JsonApiMiddleware(requestDelegateMock.Object, loggerMock.Object, router, null);

// act
await middleware.Invoke(httpContextMock.Object);

// assert
Assert.Equal(415, httpResponsMock.Object.StatusCode);
}

[Fact]
public async void Invoke_SetsStatusCode_To415_ForInvalidContentType()
{
// arrange
var httpRequestMock = new Mock<HttpRequest>();
httpRequestMock.Setup(r => r.Path).Returns(new PathString(""));
httpRequestMock.Setup(r => r.ContentType).Returns("");
httpRequestMock.Setup(r => r.ContentLength).Returns(1);
var headers = new HeaderDictionary();
headers.Add("Accept","application/vnd.api+json");
httpRequestMock.Setup(r => r.Headers).Returns(headers);

var httpResponsMock = new Mock<HttpResponse>();
httpResponsMock.SetupAllProperties();
Expand Down
2 changes: 1 addition & 1 deletion JsonApiDotNetCoreTests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": "1.0.0-alpha",
"testRunner": "xunit",
"dependencies": {
"JsonApiDotNetCore": "0.1.0",
"JsonApiDotNetCore": "0.1.1",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"xunit": "2.2.0-beta2-build3300",
"moq": "4.6.38-alpha"
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![Build status](https://ci.appveyor.com/api/projects/status/9fvgeoxdikwkom10?svg=true)](https://ci.appveyor.com/project/jaredcnance/json-api-dotnet-core)
[![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)
[![NuGet](https://img.shields.io/nuget/v/JsonApiDotNetCore.svg)](https://www.nuget.org/packages/JsonApiDotNetCore/)
[![MyGet CI](https://img.shields.io/myget/research-institute/v/JsonApiDotNetCore.svg)](https://www.myget.org/feed/research-institute/package/nuget/JsonApiDotNetCore)

JSON API Spec Conformance: **Non Conforming**

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

NuGet packages will be published at v0.1.0.
`Install-Package JsonApiDotNetCore`

```
"JsonApiDotNetCore": "0.1.0"
```

## Usage

Expand Down