Skip to content

Commit

Permalink
Use pathBase in fallback default, if present
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldgray committed Oct 27, 2023
1 parent 1612c1b commit 40bc0f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ public void GetGesturePostbackRelativePath_HandlesNoConfiguredDefault()
result.ToString().Should().Be("/access/123/gesture");
}

[Fact]
public void GetGesturePostbackRelativePath_HandlesNoConfiguredDefault_WithPathBase()
{
// Arrange
var gestureTemplates = new Dictionary<string, string>
{
[OtherHost] = "/access/specific-host"
};
var sut = GetSut(CurrentHost, gestureTemplates, "auth/v2/");

// Act
var result = sut.GetGesturePostbackRelativePath(123);

// Asset
result.IsAbsoluteUri.Should().BeFalse();
result.ToString().Should().Be("auth/v2/access/123/gesture");
}

[Fact]
public void GetGesturePostbackRelativePath_UsesConfiguredDefault()
{
Expand Down Expand Up @@ -67,7 +85,7 @@ public void GetGesturePostbackRelativePath_UsesSpecifiedHost_IfFound()
result.ToString().Should().Be("/123/access/gesture");
}

private UrlPathProvider GetSut(string host, Dictionary<string, string> gestureTemplates)
private UrlPathProvider GetSut(string host, Dictionary<string, string> gestureTemplates, string? pathBase = null)
{
var context = new DefaultHttpContext();
var request = context.Request;
Expand All @@ -77,7 +95,7 @@ private UrlPathProvider GetSut(string host, Dictionary<string, string> gestureTe
A.CallTo(() => contextAccessor.HttpContext).Returns(context);

var authSettings = new AuthSettings { GesturePathTemplateForDomain = gestureTemplates };
var apiSettings = Options.Create(new ApiSettings { Auth = authSettings });
var apiSettings = Options.Create(new ApiSettings { Auth = authSettings, PathBase = pathBase });

return new UrlPathProvider(contextAccessor, apiSettings);
}
Expand Down
14 changes: 10 additions & 4 deletions src/IIIFAuth2/IIIFAuth2.API/Infrastructure/Web/UrlPathProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using IIIFAuth2.API.Data.Entities;
using System.Text.RegularExpressions;
using IIIFAuth2.API.Data.Entities;
using IIIFAuth2.API.Models.Domain;
using IIIFAuth2.API.Settings;
using IIIFAuth2.API.Utils;
Expand Down Expand Up @@ -114,17 +115,22 @@ private string GetPopulatedTemplate(string host, int customerId)
var template = GetTemplate(host);
return template.Replace("{customerId}", customerId.ToString());
}

private string GetTemplate(string host)
{
const string defaultPathTemplate = "/access/{customerId}/gesture";
const string defaultKey = "Default";

var pathTemplates = apiSettings.Auth.GesturePathTemplateForDomain;

if (pathTemplates.TryGetValue(host, out var hostTemplate)) return hostTemplate;
if (pathTemplates.TryGetValue(defaultKey, out var pathTemplate)) return pathTemplate;
return defaultPathTemplate;
if (apiSettings.PathBase.IsNullOrEmpty()) return defaultPathTemplate;

// Replace any duplicate slashes after joining path elements
var candidate = $"{apiSettings.PathBase}/{defaultPathTemplate}";
var duplicateSlashRegex = new Regex("(/)+", RegexOptions.Compiled);
return duplicateSlashRegex.Replace(candidate, "$1");
}

private string GetCurrentBaseUrl() =>
Expand Down

0 comments on commit 40bc0f9

Please sign in to comment.