Skip to content

Commit

Permalink
Merge pull request #52 from dlcs/feature/19/vary_header
Browse files Browse the repository at this point in the history
Add vary heade to flat and hierarchical collection GETs
  • Loading branch information
p-kaczynski authored Sep 26, 2024
2 parents f65ffd0 + 667baa7 commit 0fe302a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/IIIFPresentation/API.Tests/Integration/GetCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public async Task Get_ChildHierarchical_Returns_Child()
var firstItem = (Collection)collection.Items[0];
firstItem.Id.Should().Be("http://localhost/1/first-child/second-child");
}

[Fact]
public async Task Get_ChildHierarchical_Returns_Vary_Header()
{
// Act
var response = await httpClient.GetAsync("1/first-child");

var collection = await response.ReadAsIIIFJsonAsync<Collection>();

// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Headers.Vary.Should().HaveCount(2);
}

[Fact]
public async Task Get_Hierarchical_Redirects_WhenAuthAndShowExtrasHeaders()
Expand Down Expand Up @@ -373,4 +386,20 @@ public async Task Get_ChildFlat_IgnoresOrderBy_WhenCalledWithInvalidOrderBy()
collection.Items!.Count.Should().Be(1);
collection.Items.OfType<Collection>().First().Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
}

[Fact]
public async Task Get_ChildFlat_Returns_Vary_Header()
{
// Arrange
var requestMessage =
HttpRequestMessageBuilder.GetPrivateRequest(HttpMethod.Get,
$"1/collections/{RootCollection.Id}?page=1&pageSize=1&orderByDescending=notValid");

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

// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Headers.Vary.Should().HaveCount(2);
}
}
11 changes: 7 additions & 4 deletions src/IIIFPresentation/API/Features/Storage/StorageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using API.Features.Storage.Validators;
using API.Helpers;
using API.Infrastructure;
using API.Infrastructure.Filters;
using API.Infrastructure.Helpers;
using API.Settings;
using IIIF.Presentation;
using IIIF.Serialisation;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using IIIF.Presentation;
using IIIF.Serialisation;
using Models.API.Collection.Upsert;

namespace API.Features.Storage;
Expand All @@ -23,6 +24,7 @@ public class StorageController(IOptions<ApiSettings> options, IMediator mediator
{
[HttpGet("{*slug}")]
[ETagCaching()]
[VaryHeader]
public async Task<IActionResult> GetHierarchicalCollection(int customerId, string slug = "")
{
var storageRoot = await Mediator.Send(new GetHierarchicalCollection(customerId, slug));
Expand All @@ -40,6 +42,7 @@ public async Task<IActionResult> GetHierarchicalCollection(int customerId, strin

[HttpGet("collections/{id}")]
[ETagCaching]
[VaryHeader]
public async Task<IActionResult> Get(int customerId, string id, int? page = 1, int? pageSize = -1,
string? orderBy = null, string? orderByDescending = null)
{
Expand Down Expand Up @@ -113,8 +116,8 @@ public async Task<IActionResult> Delete(int customerId, string id)

return await HandleDelete(new DeleteCollection(customerId, id));
}
private IActionResult SeeOther(string location)

private StatusCodeResult SeeOther(string location)
{
Response.Headers.Location = location;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Net.Http.Headers;

namespace API.Infrastructure.Filters;

public class VaryHeaderAttribute : ActionFilterAttribute
{
private static readonly string[] VaryHeaders = ["X-IIIF-CS-Show-Extras", "Authorization"];

public override void OnActionExecuted(ActionExecutedContext context)
{
if (context.Result is not null)
context.HttpContext.Response.Headers.Append(HeaderNames.Vary, VaryHeaders);
}
}

0 comments on commit 0fe302a

Please sign in to comment.