mirror of
https://github.com/AlexMacocian/SystemExtensions.git
synced 2026-07-15 14:19:29 +00:00
Mockable http client implementation.
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System.Http
|
||||
{
|
||||
public sealed class HttpClient<Tscope> : IHttpClient<Tscope>, IDisposable
|
||||
{
|
||||
private readonly HttpClient httpClient;
|
||||
private readonly Type scope;
|
||||
private EventHandler<HttpClientEventMessage> eventEmitted;
|
||||
|
||||
public event EventHandler<HttpClientEventMessage> EventEmitted
|
||||
{
|
||||
add
|
||||
{
|
||||
this.eventEmitted += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.eventEmitted -= value;
|
||||
}
|
||||
}
|
||||
public Uri BaseAddress { get => this.httpClient.BaseAddress; set => this.httpClient.BaseAddress = value; }
|
||||
public HttpRequestHeaders DefaultRequestHeaders => this.httpClient.DefaultRequestHeaders;
|
||||
public long MaxResponseContentBufferSize { get => this.httpClient.MaxResponseContentBufferSize; set => this.httpClient.MaxResponseContentBufferSize = value; }
|
||||
public TimeSpan Timeout { get => this.httpClient.Timeout; set => this.httpClient.Timeout = value; }
|
||||
|
||||
public HttpClient()
|
||||
{
|
||||
this.httpClient = new HttpClient();
|
||||
this.scope = typeof(Tscope);
|
||||
}
|
||||
|
||||
public HttpClient(
|
||||
HttpMessageHandler handler)
|
||||
{
|
||||
this.httpClient = new HttpClient(handler);
|
||||
this.scope = typeof(Tscope);
|
||||
}
|
||||
|
||||
public HttpClient(
|
||||
HttpMessageHandler handler,
|
||||
bool disposeHandler)
|
||||
{
|
||||
this.httpClient = new HttpClient(handler, disposeHandler);
|
||||
this.scope = typeof(Tscope);
|
||||
}
|
||||
|
||||
public void CancelPendingRequests()
|
||||
{
|
||||
this.LogInformation(string.Empty, "Canceling request");
|
||||
this.httpClient.CancelPendingRequests();
|
||||
}
|
||||
public Task<HttpResponseMessage> DeleteAsync(string requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(DeleteAsync));
|
||||
return this.httpClient.DeleteAsync(requestUri);
|
||||
}
|
||||
public Task<HttpResponseMessage> DeleteAsync(string requestUri, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(DeleteAsync));
|
||||
return this.httpClient.DeleteAsync(requestUri, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> DeleteAsync(Uri requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(DeleteAsync));
|
||||
return this.httpClient.DeleteAsync(requestUri);
|
||||
}
|
||||
public Task<HttpResponseMessage> DeleteAsync(Uri requestUri, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(DeleteAsync));
|
||||
return this.httpClient.DeleteAsync(requestUri, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> GetAsync(string requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(GetAsync));
|
||||
return this.httpClient.GetAsync(requestUri);
|
||||
}
|
||||
public Task<HttpResponseMessage> GetAsync(string requestUri, HttpCompletionOption completionOption)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(GetAsync));
|
||||
return this.httpClient.GetAsync(requestUri, completionOption);
|
||||
}
|
||||
public Task<HttpResponseMessage> GetAsync(string requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(GetAsync));
|
||||
return this.httpClient.GetAsync(requestUri, completionOption, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> GetAsync(string requestUri, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(GetAsync));
|
||||
return this.httpClient.GetAsync(requestUri, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> GetAsync(Uri requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(GetAsync));
|
||||
return this.httpClient.GetAsync(requestUri);
|
||||
}
|
||||
public Task<HttpResponseMessage> GetAsync(Uri requestUri, HttpCompletionOption completionOption)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(GetAsync));
|
||||
return this.httpClient.GetAsync(requestUri, completionOption);
|
||||
}
|
||||
public Task<HttpResponseMessage> GetAsync(Uri requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(GetAsync));
|
||||
return this.httpClient.GetAsync(requestUri, completionOption, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> GetAsync(Uri requestUri, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(GetAsync));
|
||||
return this.httpClient.GetAsync(requestUri, cancellationToken);
|
||||
}
|
||||
public Task<byte[]> GetByteArrayAsync(string requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(GetByteArrayAsync));
|
||||
return this.httpClient.GetByteArrayAsync(requestUri);
|
||||
}
|
||||
public Task<byte[]> GetByteArrayAsync(Uri requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(GetByteArrayAsync));
|
||||
return this.httpClient.GetByteArrayAsync(requestUri);
|
||||
}
|
||||
public Task<Stream> GetStreamAsync(string requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(GetStreamAsync));
|
||||
return this.httpClient.GetStreamAsync(requestUri);
|
||||
}
|
||||
public Task<Stream> GetStreamAsync(Uri requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(GetStreamAsync));
|
||||
return this.httpClient.GetStreamAsync(requestUri);
|
||||
}
|
||||
public Task<string> GetStringAsync(string requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(GetStringAsync));
|
||||
return this.httpClient.GetStringAsync(requestUri);
|
||||
}
|
||||
public Task<string> GetStringAsync(Uri requestUri)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(GetStringAsync));
|
||||
return this.httpClient.GetStringAsync(requestUri);
|
||||
}
|
||||
public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(PostAsync));
|
||||
return this.httpClient.PostAsync(requestUri, content);
|
||||
}
|
||||
public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(PostAsync));
|
||||
return this.httpClient.PostAsync(requestUri, content, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(PostAsync));
|
||||
return this.httpClient.PostAsync(requestUri, content);
|
||||
}
|
||||
public Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(PostAsync));
|
||||
return this.httpClient.PostAsync(requestUri, content, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(PutAsync));
|
||||
return this.httpClient.PutAsync(requestUri, content);
|
||||
}
|
||||
public Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri, nameof(PutAsync));
|
||||
return this.httpClient.PutAsync(requestUri, content, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> PutAsync(Uri requestUri, HttpContent content)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(PutAsync));
|
||||
return this.httpClient.PutAsync(requestUri, content);
|
||||
}
|
||||
public Task<HttpResponseMessage> PutAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(requestUri.ToString(), nameof(PutAsync));
|
||||
return this.httpClient.PutAsync(requestUri, content, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
|
||||
{
|
||||
this.LogInformation(request.RequestUri.ToString(), nameof(SendAsync));
|
||||
return this.httpClient.SendAsync(request);
|
||||
}
|
||||
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption)
|
||||
{
|
||||
this.LogInformation(request.RequestUri.ToString(), nameof(SendAsync));
|
||||
return this.httpClient.SendAsync(request, completionOption);
|
||||
}
|
||||
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(request.RequestUri.ToString(), nameof(SendAsync));
|
||||
return this.httpClient.SendAsync(request, completionOption, cancellationToken);
|
||||
}
|
||||
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
this.LogInformation(request.RequestUri.ToString(), nameof(SendAsync));
|
||||
return this.httpClient.SendAsync(request, cancellationToken);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
this.httpClient.Dispose();
|
||||
}
|
||||
|
||||
private void LogInformation(string url, string method)
|
||||
{
|
||||
this.eventEmitted?.Invoke(this, new HttpClientEventMessage(this.scope, method, url));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace System.Http
|
||||
{
|
||||
public sealed class HttpClientEventMessage
|
||||
{
|
||||
public Type Scope { get; }
|
||||
public string Method { get; }
|
||||
public string Url { get; }
|
||||
|
||||
internal HttpClientEventMessage(Type scope, string method, string url)
|
||||
{
|
||||
this.Scope = scope;
|
||||
this.Method = method;
|
||||
this.Url = url;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System.Http
|
||||
{
|
||||
public interface IHttpClient<TScope>
|
||||
{
|
||||
event EventHandler<HttpClientEventMessage> EventEmitted;
|
||||
Uri BaseAddress { get; set; }
|
||||
HttpRequestHeaders DefaultRequestHeaders { get; }
|
||||
long MaxResponseContentBufferSize { get; set; }
|
||||
TimeSpan Timeout { get; set; }
|
||||
|
||||
void CancelPendingRequests();
|
||||
Task<HttpResponseMessage> DeleteAsync(string requestUri);
|
||||
Task<HttpResponseMessage> DeleteAsync(string requestUri, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> DeleteAsync(Uri requestUri);
|
||||
Task<HttpResponseMessage> DeleteAsync(Uri requestUri, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> GetAsync(string requestUri);
|
||||
Task<HttpResponseMessage> GetAsync(string requestUri, HttpCompletionOption completionOption);
|
||||
Task<HttpResponseMessage> GetAsync(string requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> GetAsync(string requestUri, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> GetAsync(Uri requestUri);
|
||||
Task<HttpResponseMessage> GetAsync(Uri requestUri, HttpCompletionOption completionOption);
|
||||
Task<HttpResponseMessage> GetAsync(Uri requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> GetAsync(Uri requestUri, CancellationToken cancellationToken);
|
||||
Task<byte[]> GetByteArrayAsync(string requestUri);
|
||||
Task<byte[]> GetByteArrayAsync(Uri requestUri);
|
||||
Task<Stream> GetStreamAsync(string requestUri);
|
||||
Task<Stream> GetStreamAsync(Uri requestUri);
|
||||
Task<string> GetStringAsync(string requestUri);
|
||||
Task<string> GetStringAsync(Uri requestUri);
|
||||
Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content);
|
||||
Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content);
|
||||
Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content);
|
||||
Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> PutAsync(Uri requestUri, HttpContent content);
|
||||
Task<HttpResponseMessage> PutAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request);
|
||||
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption);
|
||||
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken);
|
||||
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<RootNamespace>System</RootNamespace>
|
||||
<Version>1.1.4</Version>
|
||||
<Version>1.2.0</Version>
|
||||
<Authors>Alexandru Macocian</Authors>
|
||||
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -0,0 +1,321 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Http;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SystemExtensionsTests.Utils;
|
||||
|
||||
namespace SystemExtensionsTests.Http
|
||||
{
|
||||
[TestClass]
|
||||
public class HttpClientTests
|
||||
{
|
||||
private const string resourceUrl = "resource";
|
||||
|
||||
private readonly Uri TestAddressUrl = new Uri("https://helloworld.xyz");
|
||||
private readonly MockHttpMessageHandler handler = new MockHttpMessageHandler();
|
||||
private IHttpClient<object> httpClient;
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
this.httpClient = new HttpClient<object>(handler, false)
|
||||
{
|
||||
BaseAddress = TestAddressUrl
|
||||
};
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task HttpClientEmitsCorrectMessages()
|
||||
{
|
||||
int messagesEmitted = 0;
|
||||
this.httpClient.EventEmitted += (sender, message) =>
|
||||
{
|
||||
messagesEmitted++;
|
||||
message.Scope.Should().Be(typeof(object));
|
||||
message.Method.Should().Be("GetAsync");
|
||||
message.Url.Should().Be(TestAddressUrl.ToString());
|
||||
};
|
||||
this.SetupResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
|
||||
await this.httpClient.GetAsync(TestAddressUrl);
|
||||
|
||||
messagesEmitted.Should().Be(1);
|
||||
}
|
||||
[TestMethod]
|
||||
public void SetBaseAddress()
|
||||
{
|
||||
this.httpClient.BaseAddress = TestAddressUrl;
|
||||
this.httpClient.BaseAddress.Should().Be(TestAddressUrl);
|
||||
}
|
||||
[TestMethod]
|
||||
public void SetDefaultRequestHeaders()
|
||||
{
|
||||
this.httpClient.DefaultRequestHeaders.TryAddWithoutValidation("someheader", "thisvalue");
|
||||
this.httpClient.DefaultRequestHeaders.GetValues("someheader").Should().BeEquivalentTo("thisvalue");
|
||||
}
|
||||
[TestMethod]
|
||||
public void SetMaxResponseContentBufferSize()
|
||||
{
|
||||
this.httpClient.MaxResponseContentBufferSize = 2000;
|
||||
this.httpClient.MaxResponseContentBufferSize.Should().Be(2000);
|
||||
}
|
||||
[TestMethod]
|
||||
public void SetTimeout()
|
||||
{
|
||||
this.httpClient.Timeout = TimeSpan.FromSeconds(1);
|
||||
this.httpClient.Timeout.Should().Be(TimeSpan.FromSeconds(1));
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task DeleteAsyncStringReturns200()
|
||||
{
|
||||
this.SetupResponse(new Uri(TestAddressUrl, resourceUrl), HttpStatusCode.OK);
|
||||
|
||||
var response = await this.httpClient.DeleteAsync(resourceUrl);
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task DeleteAsyncUriReturns200()
|
||||
{
|
||||
this.SetupResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
|
||||
var response = await this.httpClient.DeleteAsync(TestAddressUrl);
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
}
|
||||
[TestMethod]
|
||||
[Ignore("NetStandard2.0 implementation doesn't cancel operation when token is cancelled")]
|
||||
public async Task DeleteAsyncCanceledThrowsTaskCanceledException()
|
||||
{
|
||||
this.SetupLongResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
var responseTask = this.httpClient.DeleteAsync(TestAddressUrl, cts.Token);
|
||||
cts.Cancel();
|
||||
|
||||
var awaitAction = new Func<Task<HttpResponseMessage>>(async () =>
|
||||
{
|
||||
return await responseTask;
|
||||
});
|
||||
|
||||
await awaitAction.Should().ThrowAsync<TaskCanceledException>();
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task GetAsyncStringReturns200()
|
||||
{
|
||||
this.SetupResponse(new Uri(TestAddressUrl, resourceUrl), HttpStatusCode.OK);
|
||||
|
||||
var response = await this.httpClient.GetAsync(resourceUrl);
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task GetAsyncUriReturns200()
|
||||
{
|
||||
this.SetupResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
|
||||
var response = await this.httpClient.GetAsync(TestAddressUrl);
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
}
|
||||
[TestMethod]
|
||||
[Ignore("NetStandard2.0 implementation doesn't cancel operation when token is cancelled")]
|
||||
public async Task GetAsyncCanceledThrowsTaskCanceledException()
|
||||
{
|
||||
this.SetupLongResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
var responseTask = this.httpClient.GetAsync(TestAddressUrl, cts.Token);
|
||||
cts.Cancel();
|
||||
|
||||
var awaitAction = new Func<Task<HttpResponseMessage>>(async () =>
|
||||
{
|
||||
return await responseTask;
|
||||
});
|
||||
|
||||
await awaitAction.Should().ThrowAsync<TaskCanceledException>();
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task PostAsyncStringReturns200()
|
||||
{
|
||||
this.SetupResponse(new Uri(TestAddressUrl, resourceUrl), HttpStatusCode.OK);
|
||||
|
||||
var response = await this.httpClient.PostAsync(resourceUrl, null);
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task PostAsyncUriReturns200()
|
||||
{
|
||||
this.SetupResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
|
||||
var response = await this.httpClient.PostAsync(TestAddressUrl, null);
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
}
|
||||
[TestMethod]
|
||||
[Ignore("NetStandard2.0 implementation doesn't cancel operation when token is cancelled")]
|
||||
public async Task PostAsyncCanceledThrowsTaskCanceledException()
|
||||
{
|
||||
this.SetupLongResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
var responseTask = this.httpClient.PostAsync(TestAddressUrl, null, cts.Token);
|
||||
cts.Cancel();
|
||||
|
||||
var awaitAction = new Func<Task<HttpResponseMessage>>(async () =>
|
||||
{
|
||||
return await responseTask;
|
||||
});
|
||||
|
||||
await awaitAction.Should().ThrowAsync<TaskCanceledException>();
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task SendAsyncUriReturns200()
|
||||
{
|
||||
this.SetupResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
|
||||
var response = await this.httpClient.SendAsync(new HttpRequestMessage { RequestUri = TestAddressUrl });
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
}
|
||||
[TestMethod]
|
||||
[Ignore("NetStandard2.0 implementation doesn't cancel operation when token is cancelled")]
|
||||
public async Task SendAsyncCanceledThrowsTaskCanceledException()
|
||||
{
|
||||
this.SetupLongResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
var responseTask = this.httpClient.SendAsync(new HttpRequestMessage { RequestUri = TestAddressUrl }, cts.Token);
|
||||
cts.Cancel();
|
||||
|
||||
var awaitAction = new Func<Task<HttpResponseMessage>>(async () =>
|
||||
{
|
||||
return await responseTask;
|
||||
});
|
||||
|
||||
await awaitAction.Should().ThrowAsync<TaskCanceledException>();
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task GetStreamAsyncStringReturns200()
|
||||
{
|
||||
var ms = new MemoryStream(new byte[] { 13, 10, 11, 12 });
|
||||
var sc = new StreamContent(ms);
|
||||
this.SetupResponse(new Uri(TestAddressUrl, resourceUrl), HttpStatusCode.OK, sc);
|
||||
|
||||
var response = await this.httpClient.GetStreamAsync(resourceUrl);
|
||||
|
||||
var responseBuffer = new byte[4];
|
||||
await response.ReadAsync(responseBuffer, 0, 4);
|
||||
responseBuffer[0].Should().Be(13);
|
||||
responseBuffer[1].Should().Be(10);
|
||||
responseBuffer[2].Should().Be(11);
|
||||
responseBuffer[3].Should().Be(12);
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task GetStreamAsyncUriReturns200()
|
||||
{
|
||||
var ms = new MemoryStream(new byte[] { 13, 10, 11, 12 });
|
||||
var sc = new StreamContent(ms);
|
||||
this.SetupResponse(TestAddressUrl, HttpStatusCode.OK, sc);
|
||||
|
||||
var response = await this.httpClient.GetStreamAsync(TestAddressUrl);
|
||||
|
||||
var responseBuffer = new byte[4];
|
||||
await response.ReadAsync(responseBuffer, 0, 4);
|
||||
responseBuffer[0].Should().Be(13);
|
||||
responseBuffer[1].Should().Be(10);
|
||||
responseBuffer[2].Should().Be(11);
|
||||
responseBuffer[3].Should().Be(12);
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task GetStringAsyncStringReturns200()
|
||||
{
|
||||
var sc = new StringContent("hello");
|
||||
this.SetupResponse(new Uri(TestAddressUrl, resourceUrl), HttpStatusCode.OK, sc);
|
||||
|
||||
var response = await this.httpClient.GetStringAsync(resourceUrl);
|
||||
|
||||
response.Should().Be("hello");
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task GetStringAsyncUriReturns200()
|
||||
{
|
||||
var sc = new StringContent("hello");
|
||||
this.SetupResponse(TestAddressUrl, HttpStatusCode.OK, sc);
|
||||
|
||||
var response = await this.httpClient.GetStringAsync(TestAddressUrl);
|
||||
|
||||
response.Should().Be("hello");
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task GetByteArrayAsyncStringReturns200()
|
||||
{
|
||||
var sc = new ByteArrayContent(new byte[4] { 13, 10, 11, 12 });
|
||||
this.SetupResponse(new Uri(TestAddressUrl, resourceUrl), HttpStatusCode.OK, sc);
|
||||
|
||||
var response = await this.httpClient.GetByteArrayAsync(resourceUrl);
|
||||
|
||||
response[0].Should().Be(13);
|
||||
response[1].Should().Be(10);
|
||||
response[2].Should().Be(11);
|
||||
response[3].Should().Be(12);
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task GetByteArrayAsyncUriReturns200()
|
||||
{
|
||||
var sc = new ByteArrayContent(new byte[4] { 13, 10, 11, 12 });
|
||||
this.SetupResponse(TestAddressUrl, HttpStatusCode.OK, sc);
|
||||
|
||||
var response = await this.httpClient.GetByteArrayAsync(TestAddressUrl);
|
||||
|
||||
response[0].Should().Be(13);
|
||||
response[1].Should().Be(10);
|
||||
response[2].Should().Be(11);
|
||||
response[3].Should().Be(12);
|
||||
}
|
||||
[TestMethod]
|
||||
[Ignore("NetStandard2.0 implementation doesn't cancel operation when token is cancelled")]
|
||||
public async Task CancelPendingRequestThrowsTaskCanceledException()
|
||||
{
|
||||
this.SetupLongResponse(TestAddressUrl, HttpStatusCode.OK);
|
||||
|
||||
var responseTask = this.httpClient.GetAsync(TestAddressUrl);
|
||||
this.httpClient.CancelPendingRequests();
|
||||
|
||||
var awaitAction = new Func<Task<HttpResponseMessage>>(async () =>
|
||||
{
|
||||
return await responseTask;
|
||||
});
|
||||
|
||||
await awaitAction.Should().ThrowAsync<TaskCanceledException>();
|
||||
}
|
||||
|
||||
private void SetupResponse(Uri expectedUri, HttpStatusCode statusCode, HttpContent httpContent = null)
|
||||
{
|
||||
this.handler.ResponseResolver = (request) =>
|
||||
{
|
||||
request.RequestUri.Should().Be(expectedUri);
|
||||
return new HttpResponseMessage { StatusCode = statusCode, Content = httpContent };
|
||||
};
|
||||
}
|
||||
private void SetupLongResponse(Uri expectedUri, HttpStatusCode statusCode, HttpContent httpContent = null)
|
||||
{
|
||||
this.handler.ResponseResolverAsync = async (request) =>
|
||||
{
|
||||
request.RequestUri.Should().Be(expectedUri);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
await Task.Delay(100);
|
||||
}
|
||||
|
||||
return new HttpResponseMessage { StatusCode = statusCode, Content = httpContent };
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>SystemExtensionsTests</RootNamespace>
|
||||
<AssemblyName>SystemExtensionsTests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||
@@ -51,6 +51,7 @@
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0-preview.2.21154.6\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -77,10 +78,12 @@
|
||||
<Compile Include="Collections\TreapTests.cs" />
|
||||
<Compile Include="Extensions\OptionalTests.cs" />
|
||||
<Compile Include="Extensions\ResultTests.cs" />
|
||||
<Compile Include="Http\HttpClientTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Structures\Int32BitStructTests.cs" />
|
||||
<Compile Include="Structures\Int64BitStructTests.cs" />
|
||||
<Compile Include="Threading\PriorityThreadPoolTests.cs" />
|
||||
<Compile Include="Utils\MockHttpMessageHandler.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SystemExtensionsTests.Utils
|
||||
{
|
||||
public sealed class MockHttpMessageHandler : HttpMessageHandler
|
||||
{
|
||||
public Func<HttpRequestMessage, HttpResponseMessage> ResponseResolver { get; set; }
|
||||
public Func<HttpRequestMessage, Task<HttpResponseMessage>> ResponseResolverAsync { get; set; }
|
||||
|
||||
public MockHttpMessageHandler()
|
||||
{
|
||||
}
|
||||
|
||||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (this.ResponseResolver is object)
|
||||
{
|
||||
return Task.FromResult(this.ResponseResolver(request));
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.ResponseResolverAsync(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
|
||||
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1"/>
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
|
||||
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
|
||||
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
</configuration>
|
||||
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>
|
||||
|
||||
Reference in New Issue
Block a user