Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed user agent string #860

Merged
merged 5 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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