From cad8286cb1b4bba93bdf37f4b511c64a1fe63c18 Mon Sep 17 00:00:00 2001 From: Deepak Shankargouda Date: Tue, 21 Aug 2018 16:28:22 -0700 Subject: [PATCH] Skipping setting user agent info if it is empty --- .../ClientRuntime.Tests/ServiceClientTests.cs | 17 +++++++++++++++++ .../Microsoft.Rest.ClientRuntime.csproj | 4 ++-- .../ClientRuntime/ServiceClient.cs | 5 +++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/SdkCommon/ClientRuntime/ClientRuntime.Tests/ServiceClientTests.cs b/src/SdkCommon/ClientRuntime/ClientRuntime.Tests/ServiceClientTests.cs index 358a0b61f0a89..97a5af01229fb 100644 --- a/src/SdkCommon/ClientRuntime/ClientRuntime.Tests/ServiceClientTests.cs +++ b/src/SdkCommon/ClientRuntime/ClientRuntime.Tests/ServiceClientTests.cs @@ -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() { diff --git a/src/SdkCommon/ClientRuntime/ClientRuntime/Microsoft.Rest.ClientRuntime.csproj b/src/SdkCommon/ClientRuntime/ClientRuntime/Microsoft.Rest.ClientRuntime.csproj index 36de3a67d49da..534e7283ef61b 100644 --- a/src/SdkCommon/ClientRuntime/ClientRuntime/Microsoft.Rest.ClientRuntime.csproj +++ b/src/SdkCommon/ClientRuntime/ClientRuntime/Microsoft.Rest.ClientRuntime.csproj @@ -4,12 +4,12 @@ Infrastructure for error handling, tracing, and HttpClient pipeline configuration. Required by client libraries generated using AutoRest. Microsoft.Rest.ClientRuntime Client Runtime Library for Microsoft AutoRest Generated Clients - 2.3.13 + 2.3.14 Microsoft.Rest.ClientRuntime Microsoft AutoRest ClientRuntime $(NugetCommonTags) $(NugetCommonProfileTags) diff --git a/src/SdkCommon/ClientRuntime/ClientRuntime/ServiceClient.cs b/src/SdkCommon/ClientRuntime/ClientRuntime/ServiceClient.cs index 08272679c8c6e..6bacc72174ffd 100644 --- a/src/SdkCommon/ClientRuntime/ClientRuntime/ServiceClient.cs +++ b/src/SdkCommon/ClientRuntime/ClientRuntime/ServiceClient.cs @@ -484,7 +484,7 @@ public bool SetUserAgent(string productName) /// Version of the product to be used in the user agent 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)); @@ -527,7 +527,8 @@ private void MergeUserAgentInfo(List 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((hv) => hv.Product.Name.Equals(piHv.Product.Name, StringComparison.OrdinalIgnoreCase))) + if(!HttpClient.DefaultRequestHeaders.UserAgent.Any((hv) => hv.Product.Name.Equals(piHv.Product.Name, StringComparison.OrdinalIgnoreCase)) + && !string.IsNullOrWhiteSpace(piHv.Product.Version)) { HttpClient.DefaultRequestHeaders.UserAgent.Add(piHv); }