mirror of https://github.com/Squidex/squidex.git
committed by
GitHub
14 changed files with 676 additions and 5 deletions
@ -0,0 +1,73 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.Extensions.Primitives; |
||||
|
using Squidex.Domain.Apps.Entities.Contents.Commands; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Pipeline.CommandMiddlewares |
||||
|
{ |
||||
|
public class ETagCommandMiddlewareTests |
||||
|
{ |
||||
|
private readonly IHttpContextAccessor httpContextAccessor = A.Fake<IHttpContextAccessor>(); |
||||
|
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
||||
|
private readonly IHeaderDictionary requestHeaders = new HeaderDictionary(); |
||||
|
private readonly ETagCommandMiddleware sut; |
||||
|
|
||||
|
public ETagCommandMiddlewareTests() |
||||
|
{ |
||||
|
A.CallTo(() => httpContextAccessor.HttpContext.Request.Headers) |
||||
|
.Returns(requestHeaders); |
||||
|
|
||||
|
sut = new ETagCommandMiddleware(httpContextAccessor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_do_nothing_when_context_is_null() |
||||
|
{ |
||||
|
A.CallTo(() => httpContextAccessor.HttpContext) |
||||
|
.Returns(null); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Null(command.Actor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_add_expected_version_to_command() |
||||
|
{ |
||||
|
requestHeaders["If-Match"] = "13"; |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(13, context.Command.ExpectedVersion); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_add_etag_header_to_response() |
||||
|
{ |
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
context.Complete(new EntitySavedResult(17)); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(new StringValues("17"), httpContextAccessor.HttpContext.Response.Headers["ETag"]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,109 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Security; |
||||
|
using System.Security.Claims; |
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Squidex.Domain.Apps.Entities.Contents.Commands; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Squidex.Infrastructure.Security; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Pipeline.CommandMiddlewares |
||||
|
{ |
||||
|
public class EnrichWithActorCommandMiddlewareTests |
||||
|
{ |
||||
|
private readonly IHttpContextAccessor httpContextAccessor = A.Fake<IHttpContextAccessor>(); |
||||
|
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
||||
|
private readonly HttpContext httpContext = new DefaultHttpContext(); |
||||
|
private readonly EnrichWithActorCommandMiddleware sut; |
||||
|
|
||||
|
public EnrichWithActorCommandMiddlewareTests() |
||||
|
{ |
||||
|
A.CallTo(() => httpContextAccessor.HttpContext) |
||||
|
.Returns(httpContext); |
||||
|
|
||||
|
sut = new EnrichWithActorCommandMiddleware(httpContextAccessor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_throw_security_exception_when_no_subject_or_client_is_found() |
||||
|
{ |
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await Assert.ThrowsAsync<SecurityException>(() => sut.HandleAsync(context)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_do_nothing_when_context_is_null() |
||||
|
{ |
||||
|
A.CallTo(() => httpContextAccessor.HttpContext) |
||||
|
.Returns(null); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Null(command.Actor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_assign_actor_from_subject() |
||||
|
{ |
||||
|
httpContext.User = CreatePrincipal(OpenIdClaims.Subject, "me"); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(new RefToken("subject", "me"), command.Actor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_assign_actor_from_client() |
||||
|
{ |
||||
|
httpContext.User = CreatePrincipal(OpenIdClaims.ClientId, "my-client"); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(new RefToken("client", "my-client"), command.Actor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_override_actor() |
||||
|
{ |
||||
|
httpContext.User = CreatePrincipal(OpenIdClaims.ClientId, "my-client"); |
||||
|
|
||||
|
var command = new CreateContent { Actor = new RefToken("subject", "me") }; |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(new RefToken("subject", "me"), command.Actor); |
||||
|
} |
||||
|
|
||||
|
private static ClaimsPrincipal CreatePrincipal(string claimType, string claimValue) |
||||
|
{ |
||||
|
var claimsPrincipal = new ClaimsPrincipal(); |
||||
|
var claimsIdentity = new ClaimsIdentity(); |
||||
|
|
||||
|
claimsIdentity.AddClaim(new Claim(claimType, claimValue)); |
||||
|
claimsPrincipal.AddIdentity(claimsIdentity); |
||||
|
|
||||
|
return claimsPrincipal; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,123 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Squidex.Domain.Apps.Entities.Apps; |
||||
|
using Squidex.Domain.Apps.Entities.Apps.Commands; |
||||
|
using Squidex.Domain.Apps.Entities.Contents.Commands; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Pipeline.CommandMiddlewares |
||||
|
{ |
||||
|
public class EnrichWithAppIdCommandMiddlewareTests |
||||
|
{ |
||||
|
private readonly IHttpContextAccessor httpContextAccessor = A.Fake<IHttpContextAccessor>(); |
||||
|
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
||||
|
private readonly HttpContext httpContext = new DefaultHttpContext(); |
||||
|
private readonly EnrichWithAppIdCommandMiddleware sut; |
||||
|
|
||||
|
public EnrichWithAppIdCommandMiddlewareTests() |
||||
|
{ |
||||
|
A.CallTo(() => httpContextAccessor.HttpContext) |
||||
|
.Returns(httpContext); |
||||
|
|
||||
|
sut = new EnrichWithAppIdCommandMiddleware(httpContextAccessor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_throw_exception_if_app_not_found() |
||||
|
{ |
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await Assert.ThrowsAsync<InvalidOperationException>(() => sut.HandleAsync(context)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_do_nothing_when_context_is_null() |
||||
|
{ |
||||
|
A.CallTo(() => httpContextAccessor.HttpContext) |
||||
|
.Returns(null); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Null(command.Actor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_assign_app_id_and_name_to_app_command() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(new NamedId<Guid>(appId, appName), command.AppId); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_assign_app_id_to_app_self_command() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
|
||||
|
var command = new AddPattern(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(appId, command.AppId); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_override_app_id() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
|
||||
|
var command = new AddPattern { AppId = Guid.NewGuid() }; |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.NotEqual(appId, command.AppId); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_override_app_id_and_name() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
|
||||
|
var command = new CreateContent { AppId = new NamedId<Guid>(Guid.NewGuid(), "other-app") }; |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.NotEqual(new NamedId<Guid>(appId, appName), command.AppId); |
||||
|
} |
||||
|
|
||||
|
private void SetupApp(out Guid appId, out string appName) |
||||
|
{ |
||||
|
appId = Guid.NewGuid(); |
||||
|
appName = "my-app"; |
||||
|
|
||||
|
var appEntity = A.Fake<IAppEntity>(); |
||||
|
A.CallTo(() => appEntity.Id).Returns(appId); |
||||
|
A.CallTo(() => appEntity.Name).Returns(appName); |
||||
|
|
||||
|
httpContext.Features.Set<IAppFeature>(new AppApiFilter.AppFeature(appEntity)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,214 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.AspNetCore.Mvc.Infrastructure; |
||||
|
using Microsoft.AspNetCore.Routing; |
||||
|
using Squidex.Domain.Apps.Entities; |
||||
|
using Squidex.Domain.Apps.Entities.Apps; |
||||
|
using Squidex.Domain.Apps.Entities.Contents.Commands; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas.Commands; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Pipeline.CommandMiddlewares |
||||
|
{ |
||||
|
public class EnrichWithSchemaIdCommandMiddlewareTests |
||||
|
{ |
||||
|
private readonly IActionContextAccessor actionContextAccessor = A.Fake<IActionContextAccessor>(); |
||||
|
private readonly IAppProvider appProvider = A.Fake<IAppProvider>(); |
||||
|
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
||||
|
private readonly HttpContext httpContext = new DefaultHttpContext(); |
||||
|
private readonly ActionContext actionContext = new ActionContext(); |
||||
|
private readonly EnrichWithSchemaIdCommandMiddleware sut; |
||||
|
|
||||
|
public EnrichWithSchemaIdCommandMiddlewareTests() |
||||
|
{ |
||||
|
actionContext.RouteData = new RouteData(); |
||||
|
actionContext.HttpContext = httpContext; |
||||
|
|
||||
|
A.CallTo(() => actionContextAccessor.ActionContext) |
||||
|
.Returns(actionContext); |
||||
|
|
||||
|
sut = new EnrichWithSchemaIdCommandMiddleware(appProvider, actionContextAccessor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_throw_exception_if_app_not_found() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
SetupSchema(appId, out var schemaId, out var schemaName); |
||||
|
|
||||
|
A.CallTo(() => appProvider.GetSchemaAsync(appId, "other-schema")) |
||||
|
.Returns(Task.FromResult<ISchemaEntity>(null)); |
||||
|
|
||||
|
actionContext.RouteData.Values["name"] = "other-schema"; |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await Assert.ThrowsAsync<DomainObjectNotFoundException>(() => sut.HandleAsync(context)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_do_nothing_when_context_is_null() |
||||
|
{ |
||||
|
A.CallTo(() => actionContextAccessor.ActionContext) |
||||
|
.Returns(null); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Null(command.Actor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_do_nothing_when_route_has_no_parameter() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
SetupSchema(appId, out var schemaId, out var schemaName); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Null(command.Actor); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_assign_schema_id_and_name_from_name() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
SetupSchema(appId, out var schemaId, out var schemaName); |
||||
|
|
||||
|
actionContext.RouteData.Values["name"] = schemaName; |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(new NamedId<Guid>(schemaId, schemaName), command.SchemaId); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_assign_schema_id_and_name_from_id() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
SetupSchema(appId, out var schemaId, out var schemaName); |
||||
|
|
||||
|
actionContext.RouteData.Values["name"] = schemaId.ToString(); |
||||
|
|
||||
|
var command = new CreateContent(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(new NamedId<Guid>(schemaId, schemaName), command.SchemaId); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_assign_schema_id_from_id() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
SetupSchema(appId, out var schemaId, out var schemaName); |
||||
|
|
||||
|
actionContext.RouteData.Values["name"] = schemaId.ToString(); |
||||
|
|
||||
|
var command = new UpdateSchema(); |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(schemaId, command.SchemaId); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_use_app_id_from_command() |
||||
|
{ |
||||
|
var appId = new NamedId<Guid>(Guid.NewGuid(), "my-app"); |
||||
|
|
||||
|
SetupSchema(appId.Id, out var schemaId, out var schemaName); |
||||
|
|
||||
|
actionContext.RouteData.Values["name"] = schemaId.ToString(); |
||||
|
|
||||
|
var command = new CreateContent { AppId = appId }; |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.Equal(new NamedId<Guid>(schemaId, schemaName), command.SchemaId); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_override_schema_id() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
SetupSchema(appId, out var schemaId, out var schemaName); |
||||
|
|
||||
|
var command = new CreateSchema { SchemaId = Guid.NewGuid() }; |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.NotEqual(schemaId, command.SchemaId); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_override_schema_id_and_name() |
||||
|
{ |
||||
|
SetupApp(out var appId, out var appName); |
||||
|
SetupSchema(appId, out var schemaId, out var schemaName); |
||||
|
|
||||
|
var command = new CreateContent { SchemaId = new NamedId<Guid>(Guid.NewGuid(), "other-schema") }; |
||||
|
var context = new CommandContext(command, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
Assert.NotEqual(new NamedId<Guid>(appId, appName), command.AppId); |
||||
|
} |
||||
|
|
||||
|
private void SetupSchema(Guid appId, out Guid schemaId, out string schemaName) |
||||
|
{ |
||||
|
schemaId = Guid.NewGuid(); |
||||
|
schemaName = "my-schema"; |
||||
|
|
||||
|
var schemaEntity = A.Fake<ISchemaEntity>(); |
||||
|
A.CallTo(() => schemaEntity.Id).Returns(schemaId); |
||||
|
A.CallTo(() => schemaEntity.Name).Returns(schemaName); |
||||
|
|
||||
|
var temp1 = schemaName; |
||||
|
var temp2 = schemaId; |
||||
|
|
||||
|
A.CallTo(() => appProvider.GetSchemaAsync(appId, temp1)) |
||||
|
.Returns(schemaEntity); |
||||
|
A.CallTo(() => appProvider.GetSchemaAsync(appId, temp2, false)) |
||||
|
.Returns(schemaEntity); |
||||
|
} |
||||
|
|
||||
|
private void SetupApp(out Guid appId, out string appName) |
||||
|
{ |
||||
|
appId = Guid.NewGuid(); |
||||
|
appName = "my-app"; |
||||
|
|
||||
|
var appEntity = A.Fake<IAppEntity>(); |
||||
|
A.CallTo(() => appEntity.Id).Returns(appId); |
||||
|
A.CallTo(() => appEntity.Name).Returns(appName); |
||||
|
|
||||
|
httpContext.Features.Set<IAppFeature>(new AppApiFilter.AppFeature(appEntity)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,83 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Squidex.Config; |
||||
|
using Squidex.Infrastructure.Tasks; |
||||
|
using Xunit; |
||||
|
using Options = Microsoft.Extensions.Options.Options; |
||||
|
|
||||
|
namespace Squidex.Pipeline |
||||
|
{ |
||||
|
public class EnforceHttpsMiddlewareTests |
||||
|
{ |
||||
|
private bool isNextCalled; |
||||
|
private RequestDelegate next; |
||||
|
|
||||
|
public EnforceHttpsMiddlewareTests() |
||||
|
{ |
||||
|
next = (context) => |
||||
|
{ |
||||
|
isNextCalled = true; |
||||
|
|
||||
|
return TaskHelper.Done; |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_make_permanent_redirect_if_redirect_is_required() |
||||
|
{ |
||||
|
var httpContext = CreateHttpContext(); |
||||
|
|
||||
|
var sut = new EnforceHttpsMiddleware(next, Options.Create(new MyUrlsOptions { EnforceHTTPS = true })); |
||||
|
|
||||
|
await sut.Invoke(httpContext); |
||||
|
|
||||
|
Assert.False(isNextCalled); |
||||
|
Assert.Equal("https://squidex.local/path?query=1", httpContext.Response.Headers["Location"]); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_redirect_if_already_on_https() |
||||
|
{ |
||||
|
var httpContext = CreateHttpContext("https"); |
||||
|
|
||||
|
var sut = new EnforceHttpsMiddleware(next, Options.Create(new MyUrlsOptions { EnforceHTTPS = true })); |
||||
|
|
||||
|
await sut.Invoke(httpContext); |
||||
|
|
||||
|
Assert.True(isNextCalled); |
||||
|
Assert.Null((string)httpContext.Response.Headers["Location"]); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_redirect_if_not_required() |
||||
|
{ |
||||
|
var httpContext = CreateHttpContext("http"); |
||||
|
|
||||
|
var sut = new EnforceHttpsMiddleware(next, Options.Create(new MyUrlsOptions { EnforceHTTPS = false })); |
||||
|
|
||||
|
await sut.Invoke(httpContext); |
||||
|
|
||||
|
Assert.True(isNextCalled); |
||||
|
Assert.Null((string)httpContext.Response.Headers["Location"]); |
||||
|
} |
||||
|
|
||||
|
private static DefaultHttpContext CreateHttpContext(string scheme = "http") |
||||
|
{ |
||||
|
var httpContext = new DefaultHttpContext(); |
||||
|
|
||||
|
httpContext.Request.QueryString = new QueryString("?query=1"); |
||||
|
httpContext.Request.Host = new HostString("squidex.local"); |
||||
|
httpContext.Request.Path = new PathString("/path"); |
||||
|
httpContext.Request.Scheme = scheme; |
||||
|
|
||||
|
return httpContext; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
<PropertyGroup> |
||||
|
<OutputType>Exe</OutputType> |
||||
|
<TargetFramework>netcoreapp2.0</TargetFramework> |
||||
|
<RootNamespace>Squidex</RootNamespace> |
||||
|
</PropertyGroup> |
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Squidex.Domain.Users\Squidex.Domain.Users.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Squidex\Squidex.csproj" /> |
||||
|
</ItemGroup> |
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="FakeItEasy" Version="4.5.1" /> |
||||
|
<PackageReference Include="IdentityServer4" Version="2.1.2" /> |
||||
|
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="2.1.0" /> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" /> |
||||
|
<PackageReference Include="NJsonSchema" Version="9.10.35" /> |
||||
|
<PackageReference Include="RefactoringEssentials" Version="5.6.0" /> |
||||
|
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" /> |
||||
|
<PackageReference Include="xunit" Version="2.3.1" /> |
||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> |
||||
|
</ItemGroup> |
||||
|
<ItemGroup> |
||||
|
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" /> |
||||
|
</ItemGroup> |
||||
|
<PropertyGroup> |
||||
|
<CodeAnalysisRuleSet>..\..\Squidex.ruleset</CodeAnalysisRuleSet> |
||||
|
</PropertyGroup> |
||||
|
<ItemGroup> |
||||
|
<AdditionalFiles Include="..\..\stylecop.json" Link="stylecop.json" /> |
||||
|
</ItemGroup> |
||||
|
</Project> |
||||
Loading…
Reference in new issue