Skip to content

Commit 62fefff

Browse files
committed
add middleware tests
1 parent 9c0a716 commit 62fefff

File tree

9 files changed

+99
-11
lines changed

9 files changed

+99
-11
lines changed

JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using AutoMapper;
33
using JsonApiDotNetCore.Configuration;
44
using JsonApiDotNetCore.Routing;
5-
using JsonApiDotNetCore.Services;
65
using Microsoft.Extensions.DependencyInjection;
76

87
namespace JsonApiDotNetCore.Extensions
@@ -18,7 +17,6 @@ public static void AddJsonApi(this IServiceCollection services, Action<IJsonApiM
1817
{
1918
config.ResourceMapper = new MapperConfiguration(cfg => {}).CreateMapper();
2019
}
21-
2220
services.AddSingleton(_ => new Router(config));
2321
}
2422
}

JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33
using JsonApiDotNetCore.Routing;
44
using Microsoft.AspNetCore.Http;
55
using Microsoft.Extensions.Logging;
6-
using JsonApiDotNetCore.Services;
76

87
namespace JsonApiDotNetCore.Middleware
98
{
109
public class JsonApiMiddleware
1110
{
1211
private readonly RequestDelegate _next;
1312
private readonly ILogger _logger;
14-
private readonly Router _jsonApiService;
13+
private readonly IRouter _router;
1514
private readonly IServiceProvider _serviceProvider;
1615

1716
public JsonApiMiddleware(RequestDelegate next,
1817
ILogger<JsonApiMiddleware> logger,
19-
Router jsonApiService,
18+
IRouter router,
2019
IServiceProvider serviceProvider)
2120
{
2221
_next = next;
2322
_logger = logger;
24-
_jsonApiService = jsonApiService;
23+
_router = router;
2524
_serviceProvider = serviceProvider;
2625
}
2726

@@ -30,7 +29,7 @@ public async Task Invoke(HttpContext context)
3029
_logger.LogInformation("Passing request to JsonApiService: " + context.Request.Path);
3130

3231
if(context.Request.ContentType == "application/vnd.api+json") {
33-
var wasHandled = _jsonApiService.HandleJsonApiRoute(context, _serviceProvider);
32+
_router.HandleJsonApiRoute(context, _serviceProvider);
3433
}
3534
else
3635
{

JsonApiDotNetCore/Routing/IRouter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using Microsoft.AspNetCore.Http;
3+
4+
namespace JsonApiDotNetCore.Routing
5+
{
6+
public interface IRouter
7+
{
8+
bool HandleJsonApiRoute(HttpContext context, IServiceProvider serviceProvider);
9+
}
10+
}

JsonApiDotNetCore/Routing/Router.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace JsonApiDotNetCore.Routing
1010
{
11-
public class Router
11+
public class Router : IRouter
1212
{
1313
private readonly JsonApiModelConfiguration _jsonApiModelConfiguration;
1414
private IServiceProvider _serviceProvider;

JsonApiDotNetCoreTests/Extensions/IServiceCollectionExtensionsTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ public void AddJsonApi_AddsRouterToServiceCollection()
2121
// assert
2222
Assert.True(serviceCollection.ContainsType(typeof(Router)));
2323
}
24-
2524
}
2625
}

JsonApiDotNetCoreTests/Helpers/ServiceCollection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Linq;
32
using System.Collections;
43
using System.Collections.Generic;
54
using Microsoft.Extensions.DependencyInjection;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using JsonApiDotNetCore.Routing;
3+
using Microsoft.AspNetCore.Http;
4+
5+
namespace JsonApiDotNetCoreTests.Helpers
6+
{
7+
public class TestRouter : IRouter
8+
{
9+
public bool DidHandleRoute { get; set; }
10+
public bool HandleJsonApiRoute(HttpContext context, IServiceProvider serviceProvider)
11+
{
12+
DidHandleRoute = true;
13+
return true;
14+
}
15+
}
16+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Xunit;
2+
using Microsoft.AspNetCore.Http;
3+
using JsonApiDotNetCore.Middleware;
4+
using JsonApiDotNetCoreTests.Helpers;
5+
using Microsoft.Extensions.Logging;
6+
using Moq;
7+
using System.IO;
8+
9+
namespace JsonApiDotNetCoreTests.Middleware.UnitTests
10+
{
11+
// see example explanation on xUnit.net website:
12+
// https://xunit.github.io/docs/getting-started-dotnet-core.html
13+
public class JsonApiMiddlewareTests
14+
{
15+
[Fact]
16+
public async void Invoke_CallsHandleJsonApiRequest_OnRouter()
17+
{
18+
// arrange
19+
var httpRequestMock = new Mock<HttpRequest>();
20+
httpRequestMock.Setup(r => r.Path).Returns(new PathString(""));
21+
httpRequestMock.Setup(r => r.ContentType).Returns("application/vnd.api+json");
22+
23+
var httpContextMock = new Mock<HttpContext>();
24+
httpContextMock.Setup(c => c.Request).Returns(httpRequestMock.Object);
25+
26+
var router = new TestRouter();
27+
var loggerMock = new Mock<ILogger<JsonApiMiddleware>>();
28+
var middleware = new JsonApiMiddleware(null, loggerMock.Object, router, null);
29+
30+
// act
31+
await middleware.Invoke(httpContextMock.Object);
32+
33+
// assert
34+
Assert.True(router.DidHandleRoute);
35+
}
36+
37+
[Fact]
38+
public async void Invoke_SetsStatusCode_To415_ForInvalidContentType()
39+
{
40+
// arrange
41+
var httpRequestMock = new Mock<HttpRequest>();
42+
httpRequestMock.Setup(r => r.Path).Returns(new PathString(""));
43+
httpRequestMock.Setup(r => r.ContentType).Returns("");
44+
45+
var httpResponsMock = new Mock<HttpResponse>();
46+
httpResponsMock.SetupAllProperties();
47+
httpResponsMock.Setup(r => r.Body).Returns(new MemoryStream());
48+
49+
var httpContextMock = new Mock<HttpContext>();
50+
httpContextMock.Setup(c => c.Request).Returns(httpRequestMock.Object);
51+
httpContextMock.Setup(c => c.Response).Returns(httpResponsMock.Object);
52+
53+
var requestDelegateMock = new Mock<RequestDelegate>();
54+
55+
var router = new TestRouter();
56+
var loggerMock = new Mock<ILogger<JsonApiMiddleware>>();
57+
var middleware = new JsonApiMiddleware(requestDelegateMock.Object, loggerMock.Object, router, null);
58+
59+
// act
60+
await middleware.Invoke(httpContextMock.Object);
61+
62+
// assert
63+
Assert.Equal(415, httpResponsMock.Object.StatusCode);
64+
}
65+
}
66+
}

JsonApiDotNetCoreTests/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"dependencies": {
55
"JsonApiDotNetCore": "1.0.0",
66
"dotnet-test-xunit": "2.2.0-preview2-build1029",
7-
"xunit": "2.2.0-beta2-build3300"
7+
"xunit": "2.2.0-beta2-build3300",
8+
"moq": "4.6.38-alpha"
89
},
910
"frameworks": {
1011
"netcoreapp1.0": {

0 commit comments

Comments
 (0)