|
| 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 | +} |
0 commit comments