-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored tests. Fixed user agent container to give a unique id for …
…each client instance.
- Loading branch information
Jake Willey
committed
Oct 11, 2019
1 parent
951f285
commit 4b16af5
Showing
4 changed files
with
122 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/UserAgentTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Cosmos.Routing; | ||
using Microsoft.Azure.Documents; | ||
using Microsoft.Azure.Documents.Client; | ||
using Microsoft.Azure.Documents.Collections; | ||
using Microsoft.Azure.Documents.Routing; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
public class UserAgentTests | ||
{ | ||
[TestMethod] | ||
public void ValidateCustomUserAgentHeader() | ||
{ | ||
const string suffix = " MyCustomUserAgent/1.0"; | ||
ConnectionPolicy policy = new ConnectionPolicy(); | ||
policy.UserAgentSuffix = suffix; | ||
Assert.IsTrue(policy.UserAgentContainer.UserAgent.EndsWith(suffix)); | ||
|
||
byte[] expectedUserAgentUTF8 = Encoding.UTF8.GetBytes(policy.UserAgentContainer.UserAgent); | ||
CollectionAssert.AreEqual(expectedUserAgentUTF8, policy.UserAgentContainer.UserAgentUTF8); | ||
} | ||
|
||
[TestMethod] | ||
public void ValidateUniqueClientIdHeader() | ||
{ | ||
using (CosmosClient client = TestCommon.CreateCosmosClient()) | ||
{ | ||
string firstClientId = this.GetClientIdFromCosmosClient(client); | ||
|
||
using (CosmosClient innerClient = TestCommon.CreateCosmosClient()) | ||
{ | ||
string secondClientId = this.GetClientIdFromCosmosClient(innerClient); | ||
Assert.AreNotEqual(firstClientId, secondClientId); | ||
} | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task ValidateUserAgentHeaderWithCustomOs() | ||
{ | ||
//This changes the runtime information to simulate a max os x response | ||
const string invalidOsField = "Darwin 18.0.0: Darwin/Kernel/Version 18.0.0: Wed Aug 22 20:13:40 PDT 2018; root:xnu-4903.201.2~1/RELEASE_X86_64"; | ||
FieldInfo fieldInfo = typeof(RuntimeInformation).GetField("s_osDescription", BindingFlags.Static | BindingFlags.NonPublic); | ||
fieldInfo.SetValue(null, invalidOsField); | ||
string updatedRuntime = RuntimeInformation.OSDescription; | ||
Assert.AreEqual(invalidOsField, updatedRuntime); | ||
|
||
const string suffix = " MyCustomUserAgent/1.0"; | ||
|
||
using (CosmosClient client = TestCommon.CreateCosmosClient(builder => builder.WithApplicationName(suffix))) | ||
{ | ||
Cosmos.UserAgentContainer userAgentContainer = client.ClientOptions.GetConnectionPolicy().UserAgentContainer; | ||
|
||
string userAgentString = userAgentContainer.UserAgent; | ||
Assert.IsTrue(userAgentString.Contains(suffix)); | ||
Assert.IsTrue(userAgentString.Contains("Darwin 18.0.0")); | ||
Cosmos.Database db = await client.CreateDatabaseIfNotExistsAsync(Guid.NewGuid().ToString()); | ||
Assert.IsNotNull(db); | ||
await db.DeleteAsync(); | ||
} | ||
} | ||
|
||
private string GetClientIdFromCosmosClient(CosmosClient client) | ||
{ | ||
Cosmos.UserAgentContainer userAgentContainer = client.ClientOptions.GetConnectionPolicy().UserAgentContainer; | ||
string userAgentString = userAgentContainer.UserAgent; | ||
string clientId = userAgentString.Split('|')[3]; | ||
Assert.AreEqual(5, clientId.Length); | ||
return clientId; | ||
} | ||
} | ||
} |