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: Fixes HttpResponseMessage.RequestMessage is null in WASM #1875

Merged
merged 10 commits into from Sep 25, 2020
Expand Up @@ -228,6 +228,12 @@ ValueTask<HttpRequestMessage> CreateRequestMessage()
HttpCompletionOption.ResponseHeadersRead,
cancellationToken);

// WebAssembly HttpClient does not set the RequestMessage property on SendAsync
if (responseMessage.RequestMessage == null)
{
responseMessage.RequestMessage = requestMessage;
}

DateTime receivedTimeUtc = DateTime.UtcNow;
TimeSpan durationTimeSpan = receivedTimeUtc - sendTimeUtc;

Expand Down
Expand Up @@ -689,6 +689,44 @@ private async Task GatewayStoreModel_Exceptionless_NotUpdateSessionTokenOnKnownR
}
}

[TestMethod]
[ExpectedException(typeof(DocumentClientException))]
public async Task WhenHttpClientSendAsyncSetsNoRequestMessage()
{
Guid previousActivityId = Trace.CorrelationManager.ActivityId;
Guid testActivityId = Guid.NewGuid();
Trace.CorrelationManager.ActivityId = testActivityId;
// We don't set the RequestMessage property on purpose on the Failed response
// This will make it go through GatewayStoreClient.CreateDocumentClientExceptionAsync
Func<HttpRequestMessage, Task<HttpResponseMessage>> sendFunc = request =>
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent("test")
};

return Task.FromResult(response);
};

Mock<IDocumentClientInternal> mockDocumentClient = new Mock<IDocumentClientInternal>();
mockDocumentClient.Setup(client => client.ServiceEndpoint).Returns(new Uri("https://foo"));
GlobalEndpointManager endpointManager = new GlobalEndpointManager(mockDocumentClient.Object, new ConnectionPolicy());
SessionContainer sessionContainer = new SessionContainer(string.Empty);
DocumentClientEventSource eventSource = DocumentClientEventSource.Instance;
HttpMessageHandler messageHandler = new MockMessageHandler(sendFunc);
GatewayStoreModel storeModel = new GatewayStoreModel(
endpointManager,
sessionContainer,
ConsistencyLevel.Eventual,
eventSource,
null,
MockCosmosUtil.CreateCosmosHttpClient(() => new HttpClient(messageHandler)));
ealsur marked this conversation as resolved.
Show resolved Hide resolved

DocumentServiceRequest documentServiceRequest = new DocumentServiceRequest(OperationType.Read, ResourceType.Database, "/dbs/test", body: null, AuthorizationTokenType.PrimaryMasterKey, null);
await storeModel.ProcessMessageAsync(documentServiceRequest);
Trace.CorrelationManager.ActivityId = previousActivityId;
}

private class MockMessageHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, Task<HttpResponseMessage>> sendFunc;
Expand Down