Skip to content

Commit

Permalink
Fixed user agent string (#860)
Browse files Browse the repository at this point in the history
* Updated to latest direct and fixed user agent string

* Update to direct to 3.3.0

* Updated changelog

* Fixed test
  • Loading branch information
j82w authored and kirankumarkolli committed Oct 7, 2019
1 parent efc5c78 commit a3db597
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ClientVersion>3.2.0</ClientVersion>
<DirectVersion>3.2.1</DirectVersion>
<DirectVersion>3.3.0</DirectVersion>
<HybridRowVersion>1.0.0-preview</HybridRowVersion>
<AboveDirBuildProps>$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))</AboveDirBuildProps>
</PropertyGroup>
Expand Down
33 changes: 5 additions & 28 deletions Microsoft.Azure.Cosmos/src/UserAgentContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,29 @@

namespace Microsoft.Azure.Cosmos
{
using System.Text;

/// <summary>
/// Contains information about the user environment and helps identify requests.
/// </summary>
internal class UserAgentContainer : Documents.UserAgentContainer
{
internal const string Delimiter = " ";
internal new static readonly string baseUserAgent;
private const int maxSuffixLength = 64;
private string suffix;
private static readonly string cosmosBaseUserAgent;

static UserAgentContainer()
{
EnvironmentInformation environmentInformation = new EnvironmentInformation();
UserAgentContainer.baseUserAgent = environmentInformation.ToString();
UserAgentContainer.cosmosBaseUserAgent = environmentInformation.ToString();
}

public UserAgentContainer()
: base()
{
this.UserAgent = UserAgentContainer.baseUserAgent;
this.UserAgentUTF8 = Encoding.UTF8.GetBytes(this.UserAgent);
}

public new string UserAgent { get; private set; }

public new byte[] UserAgentUTF8 { get; private set; }

public new string Suffix
internal override string BaseUserAgent
{
get
{
return this.suffix;
}
set
{
this.suffix = value;

// Take only the first 64 characters of the user-agent.
if (this.suffix.Length > maxSuffixLength)
{
this.suffix = this.suffix.Substring(0, 64);
}

this.UserAgent = UserAgentContainer.baseUserAgent + UserAgentContainer.Delimiter + this.suffix;
this.UserAgentUTF8 = Encoding.UTF8.GetBytes(this.UserAgent);
return UserAgentContainer.cosmosBaseUserAgent;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,26 @@ public void ValidateCustomUserAgentHeader()
const string suffix = " MyCustomUserAgent/1.0";
ConnectionPolicy policy = new ConnectionPolicy();
policy.UserAgentSuffix = suffix;
var expectedUserAgent = Cosmos.UserAgentContainer.baseUserAgent + Cosmos.UserAgentContainer.Delimiter + suffix;
var expectedUserAgent = new Cosmos.UserAgentContainer().BaseUserAgent + suffix;
Assert.AreEqual(expectedUserAgent, policy.UserAgentContainer.UserAgent);

byte[] expectedUserAgentUTF8 = Encoding.UTF8.GetBytes(expectedUserAgent);
CollectionAssert.AreEqual(expectedUserAgentUTF8, policy.UserAgentContainer.UserAgentUTF8);
}

[TestMethod]
public void ValidateCustomUserAgentContainer()
{
const string suffix = " MyCustomUserAgent/1.0";
UserAgentContainer userAgentContainer = new Cosmos.UserAgentContainer();
userAgentContainer.Suffix = suffix;
string expectedUserAgent = new Cosmos.UserAgentContainer().BaseUserAgent + suffix;
Assert.AreEqual(expectedUserAgent, userAgentContainer.UserAgent);

byte[] expectedUserAgentUTF8 = Encoding.UTF8.GetBytes(expectedUserAgent);
CollectionAssert.AreEqual(expectedUserAgentUTF8, userAgentContainer.UserAgentUTF8);
}

[TestMethod]
public void ValidateVersionHeader()
{
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#835](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/835) Fixed a bug that caused sortedRanges exceptions
- [#846](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/846) Statistics not getting populated correctly on CosmosException.
- [#857](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/857) Fixed reusability of the Bulk support across Container instances
- [#860](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/860) Fixed base user agent string

## <a name="3.2.0"/> [3.2.0](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.2.0) - 2019-09-17

Expand Down

0 comments on commit a3db597

Please sign in to comment.