Skip to content

Commit

Permalink
Merge pull request #515 from dlcs/feature/info_json_redirect
Browse files Browse the repository at this point in the history
Ignore query params in info.json redirect
  • Loading branch information
donaldgray authored May 31, 2023
2 parents 08071d2 + 2b43383 commit e039ffe
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ public async Task Parse_ImageRequest_WithCustomerId_ReturnsCorrectRequest()
thumbnailRequest.AssetPath.Should().Be("the-astronaut");
thumbnailRequest.AssetId.Should().Be("the-astronaut");
}

[Theory]
[InlineData("/thumbs/99/1/the-astronaut?foo=bar")]
[InlineData("/thumbs/99/1/the-astronaut?foo=bar&baz=qux")]
[InlineData("/thumbs/99/1/the-astronaut#test")]
[InlineData("/thumbs/99/1/the-astronaut#test?foo=bar")]
[InlineData("/thumbs/99/1/the-astronaut?foo=bar#test")]
public async Task Parse_ImageRequest_WithAdditionalParams_ReturnsCorrectRequest(string path)
{
// Arrange
var customer = new CustomerPathElement(99, "Test-Customer");
A.CallTo(() => pathCustomerRepository.GetCustomerPathElement("99"))
.Returns(customer);

// Act
var thumbnailRequest = await sut.Parse<ImageAssetDeliveryRequest>(path);

// Assert
thumbnailRequest.RoutePrefix.Should().Be("thumbs");
thumbnailRequest.VersionedRoutePrefix.Should().Be("thumbs");
thumbnailRequest.VersionPathValue.Should().BeNull();
thumbnailRequest.CustomerPathValue.Should().Be("99");
thumbnailRequest.Customer.Should().Be(customer);
thumbnailRequest.BasePath.Should().Be("/thumbs/99/1/");
thumbnailRequest.Space.Should().Be(1);
thumbnailRequest.AssetPath.Should().Be("the-astronaut");
thumbnailRequest.AssetId.Should().Be("the-astronaut");
}

[Fact]
public async Task Parse_ImageRequest_WithCustomerName_ReturnsCorrectRequest()
Expand Down
197 changes: 195 additions & 2 deletions src/protagonist/DLCS.Web.Tests/Requests/HttpRequestXTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public void GetFirstQueryParamValue_Finds_Single_Value()
test.Should().Be("aaa");
}


[Fact]
public void GetFirstQueryParamValue_Ignores_Further_Values()
{
Expand All @@ -36,7 +35,6 @@ public void GetFirstQueryParamValue_Ignores_Further_Values()
test.Should().Be("aaa");
}


[Fact]
public void GetFirstQueryParamValueAsInt_Finds_Int()
{
Expand All @@ -50,4 +48,199 @@ public void GetFirstQueryParamValueAsInt_Finds_Int()
// assert
test.Should().Be(12);
}

[Fact]
public void GetDisplayUrl_ReturnsFullUrl_WhenCalledWithDefaultParams()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.Scheme = "https";

const string expected = "https://test.example?foo=bar";

// Act
var result = httpRequest.GetDisplayUrl();

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetDisplayUrl_WithPathBase_ReturnsFullUrl_WhenCalledWithDefaultParams()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.PathBase = new PathString("/v2");
httpRequest.Scheme = "https";

const string expected = "https://test.example/v2?foo=bar";

// Act
var result = httpRequest.GetDisplayUrl();

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetDisplayUrl_ReturnsFullUrl_WhenCalledWithPath()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.Scheme = "https";

const string expected = "https://test.example/my-path/one/two?foo=bar";

// Act
var result = httpRequest.GetDisplayUrl("/my-path/one/two");

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetDisplayUrl_WithPathBase_ReturnsFullUrl_WhenCalledWithWithPath()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.PathBase = new PathString("/v2");
httpRequest.Scheme = "https";

const string expected = "https://test.example/v2/my-path/one/two?foo=bar";

// Act
var result = httpRequest.GetDisplayUrl("/my-path/one/two");

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetDisplayUrl_ReturnsFullUrl_WithoutQueryParam_WhenCalledWithDoNotIncludeQueryParams()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.Scheme = "https";

const string expected = "https://test.example";

// Act
var result = httpRequest.GetDisplayUrl(includeQueryParams: false);

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetDisplayUrl_WithPathBase_ReturnsFullUrl_WithoutQueryParam_WhenCalledWithDoNotIncludeQueryParams()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.PathBase = new PathString("/v2");
httpRequest.Scheme = "https";

const string expected = "https://test.example/v2";

// Act
var result = httpRequest.GetDisplayUrl(includeQueryParams: false);

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetBaseUrl_ReturnsFullUrl_WithoutQueryParam()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.Scheme = "https";

const string expected = "https://test.example";

// Act
var result = httpRequest.GetBaseUrl();

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetBaseUrl_WithPathBase_ReturnsFullUrl_WithoutQueryParam_WhenCalledWithDoNotIncludeQueryParams()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.PathBase = new PathString("/v2");
httpRequest.Scheme = "https";

const string expected = "https://test.example/v2";

// Act
var result = httpRequest.GetBaseUrl();

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetJsonLdId_Correct()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.Scheme = "https";

const string expected = "https://test.example/anything";

// Act
var result = httpRequest.GetJsonLdId();

// Assert
result.Should().Be(expected);
}

[Fact]
public void GetJsonLdId_WithPathBase_Correct()
{
// Arrange
var httpRequest = new DefaultHttpContext().Request;
httpRequest.Path = new PathString("/anything");
httpRequest.QueryString = new QueryString("?foo=bar");
httpRequest.Host = new HostString("test.example");
httpRequest.PathBase = new PathString("/v2");
httpRequest.Scheme = "https";

const string expected = "https://test.example/v2/anything";

// Act
var result = httpRequest.GetJsonLdId();

// Assert
result.Should().Be(expected);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using DLCS.Core;
using DLCS.Model.PathElements;
using DLCS.Web.Requests.AssetDelivery;
Expand Down Expand Up @@ -94,10 +95,10 @@ public void GetFullPathForRequest_OverrideAvailable_ButIgnoredIfNativeFormatRequ
[Theory]
[InlineData("123")]
[InlineData("test-customer")]
public void GetFullPathForRequest_PathGenerator_Default(string customerPathValue)
public void GetFullPathForRequest_Default_QueryParam(string customerPathValue)
{
// Arrange
var sut = GetSut("default.com");
var sut = GetSut("default.com", req => req.QueryString = new QueryString("?foo=bar"));
var request = new BaseAssetRequest
{
Customer = new CustomerPathElement(123, "test-customer"),
Expand All @@ -108,17 +109,10 @@ public void GetFullPathForRequest_PathGenerator_Default(string customerPathValue
RoutePrefix = "thumbs"
};

var expected = $"https://default.com/thumbs/{customerPathValue}/2000/not-asset";
var expected = $"https://default.com/thumbs/{customerPathValue}/10/path/to/asset?foo=bar";

// Act
var actual = sut.GetFullPathForRequest(request,
(assetRequest, template) =>
DlcsPathHelpers.GeneratePathFromTemplate(
template,
assetRequest.RoutePrefix,
assetRequest.CustomerPathValue,
space: "2000",
assetPath: "not-asset"));
var actual = sut.GetFullPathForRequest(request);

// Assert
actual.Should().Be(expected);
Expand All @@ -127,10 +121,10 @@ public void GetFullPathForRequest_PathGenerator_Default(string customerPathValue
[Theory]
[InlineData("123")]
[InlineData("test-customer")]
public void GetFullPathForRequest_PathGenerator_Override(string customerPathValue)
public void GetFullPathForRequest_Default_QueryParam_OmittedIfIncludeFalse(string customerPathValue)
{
// Arrange
var sut = GetSut("test.example.com");
var sut = GetSut("default.com", req => req.QueryString = new QueryString("?foo=bar"));
var request = new BaseAssetRequest
{
Customer = new CustomerPathElement(123, "test-customer"),
Expand All @@ -141,64 +135,24 @@ public void GetFullPathForRequest_PathGenerator_Override(string customerPathValu
RoutePrefix = "thumbs"
};

var expected = "https://test.example.com/thumbs/not-asset";

// Act
var actual = sut.GetFullPathForRequest(request,
(assetRequest, template) =>
DlcsPathHelpers.GeneratePathFromTemplate(
template,
assetRequest.RoutePrefix,
assetRequest.CustomerPathValue,
space: "2000",
assetPath: "not-asset"));
var expected = $"https://default.com/thumbs/{customerPathValue}/10/path/to/asset";

// Assert
actual.Should().Be(expected);
}

[Theory]
[InlineData("123")]
[InlineData("test-customer")]
public void GetFullPathForRequest_PathGenerator_OverrideAvailable_ButIgnoredIfNativeFormatRequested(string customerPathValue)
{
// Arrange
var sut = GetSut("test.example.com");
var request = new BaseAssetRequest
{
Customer = new CustomerPathElement(123, "test-customer"),
CustomerPathValue = customerPathValue,
Space = 10,
AssetPath = "path/to/asset",
BasePath = "thumbs/123/10",
RoutePrefix = "thumbs"
};

var expected = $"https://test.example.com/thumbs/{customerPathValue}/2000/not-asset";

// Act
var actual = sut.GetFullPathForRequest(request,
(assetRequest, template) =>
DlcsPathHelpers.GeneratePathFromTemplate(
template,
assetRequest.RoutePrefix,
assetRequest.CustomerPathValue,
space: "2000",
assetPath: "not-asset"),
true);
var actual = sut.GetFullPathForRequest(request, includeQueryParams: false);

// Assert
actual.Should().Be(expected);
}

private ConfigDrivenAssetPathGenerator GetSut(string host)
private ConfigDrivenAssetPathGenerator GetSut(string host, Action<HttpRequest> requestModifier = null)
{
var context = new DefaultHttpContext();
var request = context.Request;
var contextAccessor = A.Fake<IHttpContextAccessor>();
A.CallTo(() => contextAccessor.HttpContext).Returns(context);
request.Host = new HostString(host);
request.Scheme = "https";
requestModifier?.Invoke(request);

var options = Options.Create(new PathTemplateOptions
{
Expand Down
Loading

0 comments on commit e039ffe

Please sign in to comment.