Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,14 @@ internal static void ParseAuthorityIfNecessary(MergedOptions mergedOptions)
{
if (string.IsNullOrEmpty(mergedOptions.TenantId) && string.IsNullOrEmpty(mergedOptions.Instance) && !string.IsNullOrEmpty(mergedOptions.Authority))
{
const int StartingIndex = 8; // length of "https://"
ReadOnlySpan<char> doubleSlash = "//".AsSpan();
ReadOnlySpan<char> authoritySpan = mergedOptions.Authority.AsSpan().TrimEnd('/');
int doubleSlashIndex = authoritySpan.IndexOf(doubleSlash);
int startingIndex = doubleSlashIndex == -1 ? 0 : doubleSlashIndex + doubleSlash.Length;

int indexTenant = authoritySpan.Slice(StartingIndex).IndexOf('/') + StartingIndex;
if (indexTenant >= 0)
// Gets position of first '/' after the "https://" prefix, will return -1 if not found, this checks for presence of instance/tenantId
int indexTenant = authoritySpan.Slice(startingIndex).IndexOf('/') + startingIndex;
if (indexTenant >= startingIndex)
{
int indexVersion = authoritySpan.Slice(indexTenant + 1).IndexOf('/');
int indexEndOfTenant = indexVersion == -1 ? authoritySpan.Length : indexVersion + indexTenant + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,45 @@ public void TestParseAuthorityIfNecessary_V2Authority()
Assert.Equal(TC.TenantIdAsGuid, mergedOptions.TenantId);
}

[Fact]
public void TestParseAuthorityIfNecessary_NoUriScheme()
{
var tenantId = TC.TenantIdAsGuid.ToString();
var instance = "myauthorityisbetter.fyi";
var authority = $"{instance}/{tenantId}/v2.0";

MergedOptions mergedOptions = new()
{
Authority = authority,
PreserveAuthority = false
};

MergedOptions.ParseAuthorityIfNecessary(mergedOptions);

Assert.Equal(authority, mergedOptions.Authority);
Assert.Equal(tenantId, mergedOptions.TenantId);
Assert.Equal(instance, mergedOptions.Instance);
}

[Fact]
public void TestParseAuthorityIfNecessary_NoTenantId()
{
var instance = "myauthorityisbetter.fyi";
var authority = $"https://{instance}";

MergedOptions mergedOptions = new()
{
Authority = authority,
PreserveAuthority = false
};

MergedOptions.ParseAuthorityIfNecessary(mergedOptions);

Assert.Equal(authority, mergedOptions.Authority);
Assert.Null(mergedOptions.TenantId);
Assert.Null(mergedOptions.Instance);
}

[Fact]
public void MergeExtraQueryParametersTest()
{
Expand Down
Loading