Skip to content

Commit

Permalink
Address PR and add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
bgavrilMS committed Aug 13, 2024
1 parent 37d11e0 commit 61af76a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Reflection;
using System.Text.RegularExpressions;

namespace Microsoft.Identity.Web
namespace Microsoft.Identity.Web.Diagnostics
{
internal static class IdHelper
{
Expand Down Expand Up @@ -37,13 +37,13 @@ internal static class IdHelper
});

public static string GetIdWebVersion()
{
{
return s_idWebVersion.Value;
}

public static string CreateTelemetryInfo()
{
return string.Format(CultureInfo.InvariantCulture, IDWebSku + s_idWebVersion.Value);
return string.Format(CultureInfo.InvariantCulture, IDWebSku + GetIdWebVersion());
}
}
}
15 changes: 8 additions & 7 deletions src/Microsoft.Identity.Web.DownstreamApi/DownstreamApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Extensions.Options;
using Microsoft.Identity.Abstractions;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web.Diagnostics;

namespace Microsoft.Identity.Web
{
Expand Down Expand Up @@ -306,7 +307,6 @@ private async Task<HttpResponseMessage> CallApiInternalAsync(
{
// Downstream API URI
string apiUrl = effectiveOptions.GetApiUrl();
AddCallerSDKTelemetry(effectiveOptions);

// Create an HTTP request message
using HttpRequestMessage httpRequestMessage = new(
Expand Down Expand Up @@ -340,14 +340,16 @@ private async Task<HttpResponseMessage> CallApiInternalAsync(
return downstreamApiResult;
}

internal async Task UpdateRequestAsync(
internal /* internal for test */ async Task UpdateRequestAsync(
HttpRequestMessage httpRequestMessage,
HttpContent? content,
DownstreamApiOptions effectiveOptions,
bool appToken,
ClaimsPrincipal? user,
CancellationToken cancellationToken)
{
AddCallerSDKTelemetry(effectiveOptions);

if (content != null)
{
httpRequestMessage.Content = content;
Expand Down Expand Up @@ -379,8 +381,7 @@ internal async Task UpdateRequestAsync(
effectiveOptions.CustomizeHttpRequestMessage?.Invoke(httpRequestMessage);
}


private static readonly Dictionary<string, string> s_callerSDKDetails = new()
internal /* for test */ static Dictionary<string, string> CallerSDKDetails { get; } = new()
{
{ "caller-sdk-id", "1" }, // 1 = Downstream API SDK ID
{ "caller-sdk-ver", IdHelper.GetIdWebVersion() }
Expand All @@ -390,14 +391,14 @@ private static void AddCallerSDKTelemetry(DownstreamApiOptions effectiveOptions)
{
if (effectiveOptions.AcquireTokenOptions.ExtraQueryParameters == null)
{
effectiveOptions.AcquireTokenOptions.ExtraQueryParameters = s_callerSDKDetails;
effectiveOptions.AcquireTokenOptions.ExtraQueryParameters = CallerSDKDetails;
}
else
{
effectiveOptions.AcquireTokenOptions.ExtraQueryParameters["caller-sdk-id"] =
s_callerSDKDetails["caller-sdk-id"];
CallerSDKDetails["caller-sdk-id"];
effectiveOptions.AcquireTokenOptions.ExtraQueryParameters["caller-sdk-ver"] =
s_callerSDKDetails["caller-sdk-ver"];
CallerSDKDetails["caller-sdk-ver"];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Microsoft.Identity.Web.TokenAcquisition\IdHelper.cs" Link="IdHelper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\README.md">
<Pack>True</Pack>
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Identity.Web.OWIN/AppBuilderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Owin.Security.Jwt;
using Microsoft.Owin.Security.OAuth;
using Microsoft.Owin.Security.OpenIdConnect;
using Microsoft.Identity.Web.Diagnostics;
using Owin;

namespace Microsoft.Identity.Web
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Net.Http;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web.Diagnostics;

namespace Microsoft.Identity.Web
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Identity.Web.Diagnostics;

namespace Microsoft.Identity.Web
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,51 @@ public async Task UpdateRequestAsync_WithContent_AddsContentToRequest()
// Arrange
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://example.com");
var content = new StringContent("test content");
var options = new DownstreamApiOptions();

// Act
await _input.UpdateRequestAsync(httpRequestMessage, content, new DownstreamApiOptions(), false, null, CancellationToken.None);
await _input.UpdateRequestAsync(httpRequestMessage, content, options, false, null, CancellationToken.None);

// Assert
Assert.Equal(content, httpRequestMessage.Content);
Assert.Equal("application/json", httpRequestMessage.Headers.Accept.Single().MediaType);
Assert.Equal("text/plain", httpRequestMessage.Content?.Headers.ContentType?.MediaType);
Assert.Equal(options.AcquireTokenOptions.ExtraQueryParameters, DownstreamApi.CallerSDKDetails);
}


[Fact]
public async Task UpdateRequestAsync_AddsToExtraQP()
{
// Arrange
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://example.com");
var content = new StringContent("test content");
var options = new DownstreamApiOptions() {
AcquireTokenOptions = new AcquireTokenOptions() {
ExtraQueryParameters = new Dictionary<string, string>()
{
{ "n1", "v1" },
{ "n2", "v2" },
{ "caller-sdk-id", "bogus" } // value will be overwritten by the SDK
}
} };

// Act
await _input.UpdateRequestAsync(httpRequestMessage, content, options, false, null, CancellationToken.None);

// Assert
Assert.Equal(content, httpRequestMessage.Content);
Assert.Equal("application/json", httpRequestMessage.Headers.Accept.Single().MediaType);
Assert.Equal("text/plain", httpRequestMessage.Content?.Headers.ContentType?.MediaType);
Assert.Equal("v1", options.AcquireTokenOptions.ExtraQueryParameters["n1"]);
Assert.Equal("v2", options.AcquireTokenOptions.ExtraQueryParameters["n2"]);
Assert.Equal(
DownstreamApi.CallerSDKDetails["caller-sdk-id"],
options.AcquireTokenOptions.ExtraQueryParameters["caller-sdk-id"] );
Assert.Equal(
DownstreamApi.CallerSDKDetails["caller-sdk-ver"],
options.AcquireTokenOptions.ExtraQueryParameters["caller-sdk-ver"]);

}

[Theory]
Expand All @@ -82,6 +119,7 @@ public async Task UpdateRequestAsync_WithScopes_AddsAuthorizationHeaderToRequest
Assert.Equal("ey", httpRequestMessage.Headers.Authorization?.Parameter);
Assert.Equal("Bearer", httpRequestMessage.Headers.Authorization?.Scheme);
Assert.Equal("application/json", httpRequestMessage.Headers.Accept.Single().MediaType);
Assert.Equal(options.AcquireTokenOptions.ExtraQueryParameters, DownstreamApi.CallerSDKDetails);
}

[Fact]
Expand Down

0 comments on commit 61af76a

Please sign in to comment.