From f691557904ac29dddcf8b5d780b4b411ee87cfc2 Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Thu, 1 Sep 2016 10:34:59 -0500 Subject: [PATCH 1/3] update readme --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 25f44364d3..36b4d505a4 100644 --- a/README.md +++ b/README.md @@ -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** @@ -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 From bedd9a9b6b2970e4d75bb64101f33a267b3a519d Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Thu, 1 Sep 2016 12:23:15 -0500 Subject: [PATCH 2/3] bump versions --- JsonApiDotNetCore/project.json | 2 +- JsonApiDotNetCoreExample/project.json | 2 +- JsonApiDotNetCoreTests/.editorconfig | 21 +++++++++++++++++++++ JsonApiDotNetCoreTests/project.json | 2 +- 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 JsonApiDotNetCoreTests/.editorconfig diff --git a/JsonApiDotNetCore/project.json b/JsonApiDotNetCore/project.json index 66a3123f23..c385d99a81 100644 --- a/JsonApiDotNetCore/project.json +++ b/JsonApiDotNetCore/project.json @@ -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/" diff --git a/JsonApiDotNetCoreExample/project.json b/JsonApiDotNetCoreExample/project.json index 5d78b38044..b0e071c9d1 100644 --- a/JsonApiDotNetCoreExample/project.json +++ b/JsonApiDotNetCoreExample/project.json @@ -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" }, diff --git a/JsonApiDotNetCoreTests/.editorconfig b/JsonApiDotNetCoreTests/.editorconfig new file mode 100644 index 0000000000..c2cdfb8ada --- /dev/null +++ b/JsonApiDotNetCoreTests/.editorconfig @@ -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 diff --git a/JsonApiDotNetCoreTests/project.json b/JsonApiDotNetCoreTests/project.json index 92e9ee614d..a75cb0e714 100644 --- a/JsonApiDotNetCoreTests/project.json +++ b/JsonApiDotNetCoreTests/project.json @@ -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" From bf7f76b958edd221f4e3363f99c439f29c2fa668 Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Thu, 1 Sep 2016 15:15:30 -0500 Subject: [PATCH 3/3] fix content type and accept header checking --- .../Middleware/JsonApiMiddleware.cs | 25 +++++++++-- .../UnitTests/JsonApiMiddlewareTests.cs | 41 +++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs b/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs index 6cfd048888..c7b9425942 100644 --- a/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs +++ b/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs @@ -3,6 +3,7 @@ using JsonApiDotNetCore.Routing; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Primitives; namespace JsonApiDotNetCore.Middleware { @@ -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; diff --git a/JsonApiDotNetCoreTests/Middleware/UnitTests/JsonApiMiddlewareTests.cs b/JsonApiDotNetCoreTests/Middleware/UnitTests/JsonApiMiddlewareTests.cs index ab77d048ac..b122822747 100644 --- a/JsonApiDotNetCoreTests/Middleware/UnitTests/JsonApiMiddlewareTests.cs +++ b/JsonApiDotNetCoreTests/Middleware/UnitTests/JsonApiMiddlewareTests.cs @@ -17,6 +17,10 @@ public async void Invoke_CallsHandleJsonApiRequest_OnRouter() var httpRequestMock = new Mock(); 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(); httpContextMock.Setup(c => c.Request).Returns(httpRequestMock.Object); @@ -32,6 +36,39 @@ public async void Invoke_CallsHandleJsonApiRequest_OnRouter() Assert.True(router.DidHandleRoute); } + [Fact] + public async void Invoke_SetsStatusCode_To415_ForInvalidAcceptType() + { + // arrange + var httpRequestMock = new Mock(); + 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(); + httpResponsMock.SetupAllProperties(); + httpResponsMock.Setup(r => r.Body).Returns(new MemoryStream()); + + var httpContextMock = new Mock(); + httpContextMock.Setup(c => c.Request).Returns(httpRequestMock.Object); + httpContextMock.Setup(c => c.Response).Returns(httpResponsMock.Object); + + var requestDelegateMock = new Mock(); + + var router = new TestRouter(); + var loggerMock = new Mock>(); + 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() { @@ -39,6 +76,10 @@ public async void Invoke_SetsStatusCode_To415_ForInvalidContentType() var httpRequestMock = new Mock(); 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(); httpResponsMock.SetupAllProperties();