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

HttpClient: Adds Properties to the Http messages if available #3803

Merged
merged 2 commits into from
Apr 6, 2023
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
8 changes: 8 additions & 0 deletions Microsoft.Azure.Cosmos/src/GatewayStoreClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ private async ValueTask<HttpRequestMessage> PrepareRequestMessageAsync(
}
}

if (request.Properties != null)
{
foreach (KeyValuePair<string, object> property in request.Properties)
{
requestMessage.Properties.Add(property);
}
}

// add activityId
Guid activityId = System.Diagnostics.Trace.CorrelationManager.ActivityId;
Debug.Assert(activityId != Guid.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,67 @@ public async Task TestRetries()

}

/// <summary>
/// Verifies that if the DCE has Properties set, the HttpRequestMessage has them too. Used on ThinClient.
/// </summary>
[TestMethod]
public async Task PassesPropertiesFromDocumentServiceRequest()
{
IDictionary<string, object> properties = new Dictionary<string, object>()
{
{"property1", Guid.NewGuid() },
{"property2", Guid.NewGuid().ToString() }
};

Func<HttpRequestMessage, Task<HttpResponseMessage>> sendFunc = request =>
{
Assert.AreEqual(properties.Count, request.Properties.Count);
foreach (KeyValuePair<string, object> item in properties)
{
Assert.AreEqual(item.Value, request.Properties[item.Key]);
}

return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) );
};

Mock<IDocumentClientInternal> mockDocumentClient = new Mock<IDocumentClientInternal>();
mockDocumentClient.Setup(client => client.ServiceEndpoint).Returns(new Uri("https://foo"));

using GlobalEndpointManager endpointManager = new GlobalEndpointManager(mockDocumentClient.Object, new ConnectionPolicy());
ISessionContainer sessionContainer = new SessionContainer(string.Empty);
DocumentClientEventSource eventSource = DocumentClientEventSource.Instance;
HttpMessageHandler messageHandler = new MockMessageHandler(sendFunc);
using GatewayStoreModel storeModel = new GatewayStoreModel(
endpointManager,
sessionContainer,
ConsistencyLevel.Eventual,
eventSource,
null,
MockCosmosUtil.CreateCosmosHttpClient(() => new HttpClient(messageHandler)));

using (new ActivityScope(Guid.NewGuid()))
{
using (DocumentServiceRequest request =
DocumentServiceRequest.Create(
Documents.OperationType.Query,
Documents.ResourceType.Document,
new Uri("https://foo.com/dbs/db1/colls/coll1", UriKind.Absolute),
new MemoryStream(Encoding.UTF8.GetBytes("content1")),
AuthorizationTokenType.PrimaryMasterKey,
null))
{
// Add properties to the DCE
request.Properties = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> property in properties)
{
request.Properties.Add(property.Key, property.Value);
}

await storeModel.ProcessMessageAsync(request);
}
}
}

[TestMethod]
public async Task TestApplySessionForMasterOperation()
{
Expand Down