Skip to content

Commit

Permalink
Merge pull request #219 from dlcs/bugfix/195/trailing_slash_non_get
Browse files Browse the repository at this point in the history
Fix #195 - Encapsulate and customize the trailing slash redirection
  • Loading branch information
p-kaczynski authored Jan 14, 2025
2 parents 42a318b + 7c0062c commit d03f049
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ public async Task DeleteCollection_DeletesCollection_WhenAllValuesProvided()
fromDatabaseHierarchy.Should().BeNull();
}

[Fact]
public async Task DeleteCollection_DeletesCollection_TrailingSlash()
{
// Arrange
var dbCollection = (await dbContext.Collections.AddTestCollection()).Entity;

await dbContext.Collections.AddAsync(dbCollection);
await dbContext.SaveChangesAsync();

var deleteRequestMessage = HttpRequestMessageBuilder.GetPrivateRequest(HttpMethod.Delete,
$"{Customer}/collections/{dbCollection.Id}/");

// Act
var response = await httpClient.AsCustomer().SendAsync(deleteRequestMessage);

var fromDatabase = dbContext.Collections.FirstOrDefault(c => c.Id == dbCollection.Id);
var fromDatabaseHierarchy = dbContext.Hierarchy.FirstOrDefault(c => c.CollectionId == dbCollection.Id);

// Assert
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
fromDatabase.Should().BeNull();
fromDatabaseHierarchy.Should().BeNull();
}

[Fact]
public async Task DeleteCollection_FailsToDeleteCollection_WhenNotFound()
{
Expand Down Expand Up @@ -184,4 +208,4 @@ public async Task DeleteIiifCollection_DeletesInS3()
await new Func<Task>(async () => await amazonS3.GetObjectAsync(LocalStackFixture.StorageBucketName,
$"{Customer}/collections/{id}")).Should().ThrowAsync<AmazonS3Exception>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Rewrite;

namespace API.Infrastructure.Http.Redirect;

/// <summary>
/// This application specific rules about redirecting trailing slashes.
/// Encapsulates the built-in RedirectRule, but only allows execution
/// for GET requests - it was causing issues with PUT/POST/DELETE etc.
/// </summary>
public class TrailingSlashRedirectRule : IRule
{
// Use built-in logic, it's internal so this is the way
private static readonly IRule BaseRedirectRule
= new RewriteOptions().AddRedirect("(.*)/$", "$1").Rules.Single();

#region Implementation of IRule

public void ApplyRule(RewriteContext context)
{
// Only for GET requests
if ("GET".Equals(context.HttpContext.Request.Method, StringComparison.OrdinalIgnoreCase))
BaseRedirectRule.ApplyRule(context);
}

#endregion
}
3 changes: 2 additions & 1 deletion src/IIIFPresentation/API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using API.Infrastructure.Helpers;
using API.Infrastructure.Http;
using API.Infrastructure.Http.CorrelationId;
using API.Infrastructure.Http.Redirect;
using API.Settings;
using DLCS;
using FluentValidation;
Expand Down Expand Up @@ -92,7 +93,7 @@
IIIFPresentationContextConfiguration.TryRunMigrations(builder.Configuration, app.Logger);

var rewriteOptions = new RewriteOptions()
.AddRedirect("(.*)/$", "$1");
.Add(new TrailingSlashRedirectRule());

app
.UseSwagger()
Expand Down

0 comments on commit d03f049

Please sign in to comment.