Skip to content

Commit

Permalink
Send user agent header as "acs-test" (#39937)
Browse files Browse the repository at this point in the history
* Always send x-ms-useragent header from tests

* Test fix

* Update assets file with test recordings stamp

* Updating test recordings

* Remove default value of user-agent header and use only value from environment variable if it's set
  • Loading branch information
nostojicMs authored Nov 23, 2023
1 parent 36ab08b commit 6b5f6d1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "net",
"TagPrefix": "net/communication/Azure.Communication.PhoneNumbers",
"Tag": "net/communication/Azure.Communication.PhoneNumbers_a2ce6fc537"
"Tag": "net/communication/Azure.Communication.PhoneNumbers_x9ndpu0nad"
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public SipRoutingClient(Uri endpoint, TokenCredential tokenCredential, SipRoutin
#region private constructors

private SipRoutingClient(ConnectionString connectionString, SipRoutingClientOptions options)
: this(connectionString.GetRequired("endpoint"), options.BuildHttpPipeline(connectionString), options)
: this(connectionString.GetRequired("endpoint"), options.BuildSipRoutingHttpPipeline(connectionString), options)
{ }

private SipRoutingClient(string endpoint, AzureKeyCredential keyCredential, SipRoutingClientOptions options)
: this(endpoint, options.BuildHttpPipeline(keyCredential), options)
: this(endpoint, options.BuildSipRoutingHttpPipeline(keyCredential), options)
{ }

private SipRoutingClient(string endpoint, TokenCredential tokenCredential, SipRoutingClientOptions options)
: this(endpoint, options.BuildHttpPipeline(tokenCredential), options)
: this(endpoint, options.BuildSipRoutingHttpPipeline(tokenCredential), options)
{ }

private SipRoutingClient(string endpoint, HttpPipeline httpPipeline, SipRoutingClientOptions options)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text;
using Azure.Communication.Pipeline;
using Azure.Core.Pipeline;
using Azure.Core;

namespace Azure.Communication.PhoneNumbers.SipRouting
{
internal static class SipRoutingClientOptionsExtensions
{
public static HttpPipeline BuildSipRoutingHttpPipeline(this ClientOptions options, ConnectionString connectionString)
{
var pipelineOptions = new HttpPipelineOptions(options)
{
RequestFailedDetailsParser = new CommunicationRequestFailedDetailsParser(),
PerRetryPolicies =
{
new HMACAuthenticationPolicy(new AzureKeyCredential(connectionString.GetRequired("accesskey"))),
new MSUserAgentPolicy()
}
};
HttpPipelineTransportOptions httpPipelineTransportOptions = new() { IsClientRedirectEnabled = true };
return HttpPipelineBuilder.Build(pipelineOptions, httpPipelineTransportOptions);
}

public static HttpPipeline BuildSipRoutingHttpPipeline(this ClientOptions options, AzureKeyCredential keyCredential)
{
var pipelineOptions = new HttpPipelineOptions(options)
{
RequestFailedDetailsParser = new CommunicationRequestFailedDetailsParser(),
PerRetryPolicies =
{
new HMACAuthenticationPolicy(keyCredential),
new MSUserAgentPolicy()
}
};
HttpPipelineTransportOptions httpPipelineTransportOptions = new() { IsClientRedirectEnabled = true };
return HttpPipelineBuilder.Build(pipelineOptions, httpPipelineTransportOptions);
}

public static HttpPipeline BuildSipRoutingHttpPipeline(this ClientOptions options, TokenCredential tokenCredential)
{
var pipelineOptions = new HttpPipelineOptions(options)
{
RequestFailedDetailsParser = new CommunicationRequestFailedDetailsParser(),
PerRetryPolicies =
{
new BearerTokenAuthenticationPolicy(tokenCredential, "https://communication.azure.com//.default"),
new MSUserAgentPolicy()
}
};
return HttpPipelineBuilder.Build(pipelineOptions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public class PhoneNumbersClientLiveTestBase : RecordedTestBase<PhoneNumbersClien
private const string UrlEncodedPhoneNumberRegEx = @"[\\%2B]{0,3}[0-9]{11,15}";
protected const string UnauthorizedNumber = "+14255550123";
protected const string UnknownPhoneNumberSearchResultId = "01234567-0123-0123-0123-0123456789AB";
private const string URIDomainNameReplacerRegEx = @"https://([^/?]+)";

public PhoneNumbersClientLiveTestBase(bool isAsync) : base(isAsync)
{
SanitizedHeaders.Add("location");
BodyRegexSanitizers.Add(new BodyRegexSanitizer(PhoneNumberRegEx, SanitizeValue));
UriRegexSanitizers.Add(new UriRegexSanitizer(UrlEncodedPhoneNumberRegEx, SanitizeValue));
UriRegexSanitizers.Add(new UriRegexSanitizer(URIDomainNameReplacerRegEx, "https://sanitized.communication.azure.com"));
SanitizedHeaders.Add("x-ms-content-sha256");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CommunicationTestEnvironment : TestEnvironment

public string LiveTestStaticConnectionString => GetRecordedVariable(
LiveTestStaticConnectionStringEnvironmentVariableName,
options => options.HasSecretConnectionStringParameter("accessKey", SanitizedValue.Base64));
options => options.IsSecret("endpoint=https://sanitized.communication.azure.com/;accesskey=Kg=="));

public Uri LiveTestStaticEndpoint => new(Core.ConnectionString.Parse(LiveTestStaticConnectionString).GetRequired("endpoint"));

Expand Down

0 comments on commit 6b5f6d1

Please sign in to comment.