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
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ public override Task<HttpResponseMessage> SendHttpAsync(
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Tests
{
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Azure.Documents;
using System;
using System.Net.Http;
using System.Net;
using System.Threading;

[TestClass]
public class CosmosHttpClientCoreTests
{
[TestMethod]
public async Task ResponseMessageHasRequestMessageAsync()
{
// 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);
};

DocumentClientEventSource eventSource = DocumentClientEventSource.Instance;
HttpMessageHandler messageHandler = new MockMessageHandler(sendFunc);
CosmosHttpClient cosmoshttpClient = MockCosmosUtil.CreateCosmosHttpClient(() => new HttpClient(messageHandler));

HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost"));

HttpResponseMessage responseMessage = await cosmoshttpClient.SendHttpAsync(() => new ValueTask<HttpRequestMessage>(httpRequestMessage), ResourceType.Collection, null, default);

Assert.AreEqual(httpRequestMessage, responseMessage.RequestMessage);
}

private class MockMessageHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, Task<HttpResponseMessage>> sendFunc;
public MockMessageHandler(Func<HttpRequestMessage, Task<HttpResponseMessage>> func)
{
this.sendFunc = func;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return await this.sendFunc(request);
}
}
}
}