Skip to content

Commit

Permalink
Skipping setting user agent info if it is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
dsgouda committed Aug 23, 2018
1 parent 33efc6b commit cad8286
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ public void ClientAddHandlerToPipelineAddsHandler()
Assert.Equal(HttpStatusCode.InternalServerError, result2.StatusCode);
}

[Fact]
public void ClientDefaultHeaderValuesTest()
{
var fakeClient = new FakeServiceClient(new HttpClientHandler(), new BadResponseDelegatingHandler());
var arr = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent.Where(pihv => String.IsNullOrWhiteSpace(pihv.Product.Version)).ToArray();
Assert.Empty(arr);
}

[Fact]
public void ClientEmptyProductHeaderValuesTest()
{
var fakeClient = new FakeServiceClient(new HttpClientHandler(), new BadResponseDelegatingHandler());
fakeClient.SetUserAgent("MySpecialHeader", string.Empty);
var arr = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent.Where(pihv => (pihv.Product.Name == "MySpecialHeader")).ToArray();
Assert.Empty(arr);
}

[Fact]
public void ClientAddHandlersToPipelineAddSingleHandler()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<Description>Infrastructure for error handling, tracing, and HttpClient pipeline configuration. Required by client libraries generated using AutoRest.</Description>
<AssemblyName>Microsoft.Rest.ClientRuntime</AssemblyName>
<AssemblyTitle>Client Runtime Library for Microsoft AutoRest Generated Clients</AssemblyTitle>
<Version>2.3.13</Version>
<Version>2.3.14</Version>
<PackageId>Microsoft.Rest.ClientRuntime</PackageId>
<PackageTags>Microsoft AutoRest ClientRuntime $(NugetCommonTags) $(NugetCommonProfileTags)</PackageTags>
<PackageReleaseNotes>
<![CDATA[
1) Added a basic support around retrying requests on getting 429 from resource provider
1) Skipping empty product header values when sending requests
]]>
</PackageReleaseNotes>
</PropertyGroup>
Expand Down
5 changes: 3 additions & 2 deletions src/SdkCommon/ClientRuntime/ClientRuntime/ServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public bool SetUserAgent(string productName)
/// <param name="version">Version of the product to be used in the user agent</param>
public bool SetUserAgent(string productName, string version)
{
if (!_disposed && HttpClient != null)
if (!_disposed && HttpClient != null && !string.IsNullOrWhiteSpace(version))
{
string cleanedProductName = CleanUserAgentInfoEntry(productName);
AddUserAgentEntry(new ProductInfoHeaderValue(cleanedProductName, version));
Expand Down Expand Up @@ -527,7 +527,8 @@ private void MergeUserAgentInfo(List<ProductInfoHeaderValue> defaultUserAgentInf
// If you want to log ProductName in userAgent, it has to be without spaces
foreach(ProductInfoHeaderValue piHv in defaultUserAgentInfoList)
{
if(!HttpClient.DefaultRequestHeaders.UserAgent.Any<ProductInfoHeaderValue>((hv) => hv.Product.Name.Equals(piHv.Product.Name, StringComparison.OrdinalIgnoreCase)))
if(!HttpClient.DefaultRequestHeaders.UserAgent.Any<ProductInfoHeaderValue>((hv) => hv.Product.Name.Equals(piHv.Product.Name, StringComparison.OrdinalIgnoreCase))
&& !string.IsNullOrWhiteSpace(piHv.Product.Version))
{
HttpClient.DefaultRequestHeaders.UserAgent.Add(piHv);
}
Expand Down

0 comments on commit cad8286

Please sign in to comment.