mirror of https://github.com/Squidex/squidex.git
2 changed files with 147 additions and 0 deletions
@ -0,0 +1,77 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Moq; |
||||
|
using Squidex.Config; |
||||
|
using Squidex.Pipeline; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Tests.Pipeline |
||||
|
{ |
||||
|
public class EnforceHttpsMiddlewareTests |
||||
|
{ |
||||
|
private readonly Mock<RequestDelegate> next = new Mock<RequestDelegate>(); |
||||
|
private readonly Mock<HttpContext> httpContextMock = new Mock<HttpContext>(); |
||||
|
private readonly Mock<HttpRequest> requestMock = new Mock<HttpRequest>(); |
||||
|
private readonly Mock<HttpResponse> responseMock = new Mock<HttpResponse>(); |
||||
|
private IOptions<MyUrlsOptions> urls; |
||||
|
private EnforceHttpsMiddleware sut; |
||||
|
|
||||
|
public EnforceHttpsMiddlewareTests() |
||||
|
{ |
||||
|
requestMock.Setup(x => x.Host).Returns(new HostString("test.squidex.com")); |
||||
|
requestMock.Setup(x => x.Scheme).Returns("https"); |
||||
|
httpContextMock.Setup(x => x.Request).Returns(requestMock.Object); |
||||
|
httpContextMock.Setup(x => x.Response).Returns(responseMock.Object); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Continue_EnforceHTTPS_Is_False_Then_Return() |
||||
|
{ |
||||
|
urls = new OptionsManager<MyUrlsOptions>(new OptionsFactory<MyUrlsOptions>( |
||||
|
new List<IConfigureOptions<MyUrlsOptions>>(), new List<IPostConfigureOptions<MyUrlsOptions>>())); |
||||
|
urls.Value.EnforceHTTPS = false; |
||||
|
|
||||
|
sut = new EnforceHttpsMiddleware(next.Object, urls); |
||||
|
await sut.Invoke(httpContextMock.Object); |
||||
|
|
||||
|
next.Verify(x => x(It.IsAny<HttpContext>()), Times.Once); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Call_Next_If_EnforceHTTPS_Is_True_And_Request_Scheme_Is_Https() |
||||
|
{ |
||||
|
urls = new OptionsManager<MyUrlsOptions>(new OptionsFactory<MyUrlsOptions>( |
||||
|
new List<IConfigureOptions<MyUrlsOptions>>(), new List<IPostConfigureOptions<MyUrlsOptions>>())); |
||||
|
urls.Value.EnforceHTTPS = true; |
||||
|
|
||||
|
sut = new EnforceHttpsMiddleware(next.Object, urls); |
||||
|
await sut.Invoke(httpContextMock.Object); |
||||
|
|
||||
|
next.Verify(x => x(It.IsAny<HttpContext>()), Times.Once); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Call_Next_If_EnforceHTTPS_Is_False_Then_Return() |
||||
|
{ |
||||
|
urls = new OptionsManager<MyUrlsOptions>(new OptionsFactory<MyUrlsOptions>( |
||||
|
new List<IConfigureOptions<MyUrlsOptions>>(), new List<IPostConfigureOptions<MyUrlsOptions>>())); |
||||
|
urls.Value.EnforceHTTPS = true; |
||||
|
|
||||
|
requestMock.Setup(x => x.Scheme).Returns("http"); |
||||
|
sut = new EnforceHttpsMiddleware(next.Object, urls); |
||||
|
await sut.Invoke(httpContextMock.Object); |
||||
|
|
||||
|
next.Verify(x => x(It.IsAny<HttpContext>()), Times.Never); |
||||
|
responseMock.Verify(x => x.Redirect(It.IsAny<string>(), true), Times.Once); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,70 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.IO; |
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.AspNetCore.Mvc.Abstractions; |
||||
|
using Microsoft.AspNetCore.Routing; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Moq; |
||||
|
using Squidex.Pipeline; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Tests.Pipeline |
||||
|
{ |
||||
|
public class FileCallbackResultTests |
||||
|
{ |
||||
|
private readonly ILoggerFactory loggerFactory = A.Fake<ILoggerFactory>(); |
||||
|
private readonly ActionContext context; |
||||
|
private readonly Mock<ActionDescriptor> actionDescriptor = new Mock<ActionDescriptor>(); |
||||
|
private readonly Mock<HttpContext> httpContext = new Mock<HttpContext>(); |
||||
|
private readonly Mock<HttpRequest> requestMock = new Mock<HttpRequest>(); |
||||
|
private readonly Mock<HttpResponse> responseMock = new Mock<HttpResponse>(); |
||||
|
private readonly Mock<IServiceProvider> serviceProvider = new Mock<IServiceProvider>(); |
||||
|
private readonly Func<Stream, Task> callback; |
||||
|
private readonly FileCallbackResult sut; |
||||
|
private readonly FileCallbackResultExecutor callbackExecutor; |
||||
|
private bool callbackWasCalled; |
||||
|
|
||||
|
public FileCallbackResultTests() |
||||
|
{ |
||||
|
requestMock.Setup(x => x.Headers).Returns(new HeaderDictionary()); |
||||
|
responseMock.Setup(x => x.Headers).Returns(new HeaderDictionary()); |
||||
|
httpContext.Setup(x => x.RequestServices).Returns(serviceProvider.Object); |
||||
|
httpContext.Setup(x => x.Request).Returns(requestMock.Object); |
||||
|
httpContext.Setup(x => x.Response).Returns(responseMock.Object); |
||||
|
|
||||
|
context = new ActionContext(httpContext.Object, new RouteData(), actionDescriptor.Object); |
||||
|
callback = async bodyStream => { callbackWasCalled = true; }; |
||||
|
callbackExecutor = new FileCallbackResultExecutor(loggerFactory); |
||||
|
sut = new FileCallbackResult("text/plain", "test.txt", callback); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Execute_Callback_Function() |
||||
|
{ |
||||
|
serviceProvider.Setup(x => x.GetService(It.IsAny<Type>())).Returns(callbackExecutor); |
||||
|
await sut.ExecuteResultAsync(context); |
||||
|
|
||||
|
Assert.True(callbackWasCalled); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Not_Call_Callback_If_Exception_Thrown_While_Logging() |
||||
|
{ |
||||
|
httpContext.Setup(x => x.Request).Returns((HttpRequest)null); |
||||
|
serviceProvider.Setup(x => x.GetService(It.IsAny<Type>())).Returns(callbackExecutor); |
||||
|
await sut.ExecuteResultAsync(context); |
||||
|
|
||||
|
Assert.False(callbackWasCalled); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue