// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Test.Common; using System.Reflection; using System.Threading; using System.Threading.Tasks; using Xunit; namespace System.Net.Http.Functional.Tests { public class HttpClientTest { [Fact] public void Dispose_MultipleTimes_Success() { var client = new HttpClient(); client.Dispose(); client.Dispose(); } [Fact] public void DefaultRequestHeaders_Idempotent() { using (var client = new HttpClient()) { Assert.NotNull(client.DefaultRequestHeaders); Assert.Same(client.DefaultRequestHeaders, client.DefaultRequestHeaders); } } [Fact] public void BaseAddress_Roundtrip_Equal() { using (var client = new HttpClient()) { Assert.Null(client.BaseAddress); Uri uri = new Uri(CreateFakeUri()); client.BaseAddress = uri; Assert.Equal(uri, client.BaseAddress); client.BaseAddress = null; Assert.Null(client.BaseAddress); } } [Fact] public void BaseAddress_InvalidUri_Throws() { using (var client = new HttpClient()) { Assert.Throws("value", () => client.BaseAddress = new Uri("ftp://onlyhttpsupported")); Assert.Throws("value", () => client.BaseAddress = new Uri("/onlyabsolutesupported", UriKind.Relative)); } } [Fact] public void Timeout_Roundtrip_Equal() { using (var client = new HttpClient()) { client.Timeout = Timeout.InfiniteTimeSpan; Assert.Equal(Timeout.InfiniteTimeSpan, client.Timeout); client.Timeout = TimeSpan.FromSeconds(1); Assert.Equal(TimeSpan.FromSeconds(1), client.Timeout); } } [Fact] public void Timeout_OutOfRange_Throws() { using (var client = new HttpClient()) { Assert.Throws("value", () => client.Timeout = TimeSpan.FromSeconds(-2)); Assert.Throws("value", () => client.Timeout = TimeSpan.FromSeconds(0)); Assert.Throws("value", () => client.Timeout = TimeSpan.FromSeconds(int.MaxValue)); } } [Fact] public void MaxResponseContentBufferSize_Roundtrip_Equal() { using (var client = new HttpClient()) { client.MaxResponseContentBufferSize = 1; Assert.Equal(1, client.MaxResponseContentBufferSize); client.MaxResponseContentBufferSize = int.MaxValue; Assert.Equal(int.MaxValue, client.MaxResponseContentBufferSize); } } [Fact] public void MaxResponseContentBufferSize_OutOfRange_Throws() { using (var client = new HttpClient()) { Assert.Throws("value", () => client.MaxResponseContentBufferSize = -1); Assert.Throws("value", () => client.MaxResponseContentBufferSize = 0); Assert.Throws("value", () => client.MaxResponseContentBufferSize = 1 + (long)int.MaxValue); } } [Fact] public async Task MaxResponseContentBufferSize_TooSmallForContent_Throws() { using (var client = new HttpClient()) { client.MaxResponseContentBufferSize = 1; await Assert.ThrowsAsync(() => client.GetStringAsync(HttpTestServers.RemoteEchoServer)); } } [Fact] public async Task Properties_CantChangeAfterOperation_Throws() { using (var client = new HttpClient(new CustomResponseHandler((r,c) => Task.FromResult(new HttpResponseMessage())))) { (await client.GetAsync(CreateFakeUri())).Dispose(); Assert.Throws(() => client.BaseAddress = null); Assert.Throws(() => client.Timeout = TimeSpan.FromSeconds(1)); Assert.Throws(() => client.MaxResponseContentBufferSize = 1); } } [Theory] [InlineData(null)] [InlineData("/something.html")] public void GetAsync_NoBaseAddress_InvalidUri_ThrowsException(string uri) { using (var client = new HttpClient()) { Assert.Throws(() => { client.GetAsync(uri == null ? null : new Uri(uri, UriKind.RelativeOrAbsolute)); }); } } [Theory] [InlineData(null)] [InlineData("/")] public async Task GetAsync_BaseAddress_ValidUri_Success(string uri) { using (var client = new HttpClient(new CustomResponseHandler((r,c) => Task.FromResult(new HttpResponseMessage())))) { client.BaseAddress = new Uri(CreateFakeUri()); using (HttpResponseMessage response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); } } } [Theory] [InlineData(false)] [InlineData(true)] public async Task GetContentAsync_ErrorStatusCode_ExpectedExceptionThrown(bool withResponseContent) { using (var client = new HttpClient(new CustomResponseHandler( (r,c) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = withResponseContent ? new ByteArrayContent(new byte[1]) : null })))) { await Assert.ThrowsAsync(() => client.GetStringAsync(CreateFakeUri())); await Assert.ThrowsAsync(() => client.GetByteArrayAsync(CreateFakeUri())); await Assert.ThrowsAsync(() => client.GetStreamAsync(CreateFakeUri())); } } [Fact] public async Task GetContentAsync_NullResponse_Throws() { using (var client = new HttpClient(new CustomResponseHandler((r,c) => Task.FromResult(null)))) { await Assert.ThrowsAnyAsync(() => client.GetStringAsync(CreateFakeUri())); } } [Fact] public async Task GetContentAsync_NullResponseContent_ReturnsDefaultValue() { using (var client = new HttpClient(new CustomResponseHandler((r,c) => Task.FromResult(new HttpResponseMessage() { Content = null })))) { Assert.Same(string.Empty, await client.GetStringAsync(CreateFakeUri())); Assert.Same(Array.Empty(), await client.GetByteArrayAsync(CreateFakeUri())); Assert.Same(Stream.Null, await client.GetStreamAsync(CreateFakeUri())); } } [Fact] public async Task GetContentAsync_SerializingContentThrows_Synchronous_Throws() { var e = new FormatException(); using (var client = new HttpClient(new CustomResponseHandler( (r, c) => Task.FromResult(new HttpResponseMessage() { Content = new CustomContent(stream => { throw e; }) })))) { Assert.Same(e, await Assert.ThrowsAsync(() => client.GetStringAsync(CreateFakeUri()))); Assert.Same(e, await Assert.ThrowsAsync(() => client.GetByteArrayAsync(CreateFakeUri()))); Assert.Same(e, await Assert.ThrowsAsync(() => client.GetStreamAsync(CreateFakeUri()))); } } [Fact] public async Task GetContentAsync_SerializingContentThrows_Asynchronous_Throws() { var e = new FormatException(); using (var client = new HttpClient(new CustomResponseHandler( (r, c) => Task.FromResult(new HttpResponseMessage() { Content = new CustomContent(stream => Task.FromException(e)) })))) { Assert.Same(e, await Assert.ThrowsAsync(() => client.GetStringAsync(CreateFakeUri()))); Assert.Same(e, await Assert.ThrowsAsync(() => client.GetByteArrayAsync(CreateFakeUri()))); Assert.Same(e, await Assert.ThrowsAsync(() => client.GetStreamAsync(CreateFakeUri()))); } } [Fact] public async Task GetAsync_InvalidUrl_ExpectedExceptionThrown() { using (var client = new HttpClient()) { await Assert.ThrowsAsync(() => client.GetAsync(CreateFakeUri())); await Assert.ThrowsAsync(() => client.GetStringAsync(CreateFakeUri())); } } [Fact] public async Task GetPutPostDeleteAsync_Canceled_Throws() { using (var client = new HttpClient(new CustomResponseHandler((r, c) => WhenCanceled(c)))) { var content = new ByteArrayContent(new byte[1]); var cts = new CancellationTokenSource(); Task t1 = client.GetAsync(CreateFakeUri(), cts.Token); Task t2 = client.GetAsync(CreateFakeUri(), HttpCompletionOption.ResponseContentRead, cts.Token); Task t3 = client.PostAsync(CreateFakeUri(), content, cts.Token); Task t4 = client.PutAsync(CreateFakeUri(), content, cts.Token); Task t5 = client.DeleteAsync(CreateFakeUri(), cts.Token); cts.Cancel(); await Assert.ThrowsAnyAsync(() => t1); await Assert.ThrowsAnyAsync(() => t2); await Assert.ThrowsAnyAsync(() => t3); await Assert.ThrowsAnyAsync(() => t4); await Assert.ThrowsAnyAsync(() => t5); } } [Fact] public async Task GetPutPostDeleteAsync_Success() { Action verify = message => { using (message) Assert.Equal(HttpStatusCode.OK, message.StatusCode); }; using (var client = new HttpClient(new CustomResponseHandler((r, c) => Task.FromResult(new HttpResponseMessage())))) { verify(await client.GetAsync(CreateFakeUri())); verify(await client.GetAsync(CreateFakeUri(), CancellationToken.None)); verify(await client.GetAsync(CreateFakeUri(), HttpCompletionOption.ResponseContentRead)); verify(await client.GetAsync(CreateFakeUri(), HttpCompletionOption.ResponseContentRead, CancellationToken.None)); verify(await client.PostAsync(CreateFakeUri(), new ByteArrayContent(new byte[1]))); verify(await client.PostAsync(CreateFakeUri(), new ByteArrayContent(new byte[1]), CancellationToken.None)); verify(await client.PutAsync(CreateFakeUri(), new ByteArrayContent(new byte[1]))); verify(await client.PutAsync(CreateFakeUri(), new ByteArrayContent(new byte[1]), CancellationToken.None)); verify(await client.DeleteAsync(CreateFakeUri())); verify(await client.DeleteAsync(CreateFakeUri(), CancellationToken.None)); } } [Fact] public void GetAsync_CustomException_Synchronous_ThrowsException() { var e = new FormatException(); using (var client = new HttpClient(new CustomResponseHandler((r, c) => { throw e; }))) { FormatException thrown = Assert.Throws(() => { client.GetAsync(CreateFakeUri()); }); Assert.Same(e, thrown); } } [Fact] public async Task GetAsync_CustomException_Asynchronous_ThrowsException() { var e = new FormatException(); using (var client = new HttpClient(new CustomResponseHandler((r, c) => Task.FromException(e)))) { FormatException thrown = await Assert.ThrowsAsync(() => client.GetAsync(CreateFakeUri())); Assert.Same(e, thrown); } } [Fact] public void SendAsync_NullRequest_ThrowsException() { using (var client = new HttpClient(new CustomResponseHandler((r,c) => Task.FromResult(null)))) { Assert.Throws("request", () => { client.SendAsync(null); }); } } [Fact] public async Task SendAsync_DuplicateRequest_ThrowsException() { using (var client = new HttpClient(new CustomResponseHandler((r, c) => Task.FromResult(new HttpResponseMessage())))) using (var request = new HttpRequestMessage(HttpMethod.Get, CreateFakeUri())) { (await client.SendAsync(request)).Dispose(); Assert.Throws(() => { client.SendAsync(request); }); } } [Fact] public async Task SendAsync_RequestContentDisposed() { var content = new ByteArrayContent(new byte[1]); using (var request = new HttpRequestMessage(HttpMethod.Get, CreateFakeUri()) { Content = content }) using (var client = new HttpClient(new CustomResponseHandler((r, c) => Task.FromResult(new HttpResponseMessage())))) { await client.SendAsync(request); Assert.Throws(() => { content.ReadAsStringAsync(); }); } } [Fact] public void Dispose_UseAfterDispose_Throws() { var client = new HttpClient(); client.Dispose(); Assert.Throws(() => client.BaseAddress = null); Assert.Throws(() => client.CancelPendingRequests()); Assert.Throws(() => { client.DeleteAsync(CreateFakeUri()); }); Assert.Throws(() => { client.GetAsync(CreateFakeUri()); }); Assert.Throws(() => { client.GetByteArrayAsync(CreateFakeUri()); }); Assert.Throws(() => { client.GetStreamAsync(CreateFakeUri()); }); Assert.Throws(() => { client.GetStringAsync(CreateFakeUri()); }); Assert.Throws(() => { client.PostAsync(CreateFakeUri(), new ByteArrayContent(new byte[1])); }); Assert.Throws(() => { client.PutAsync(CreateFakeUri(), new ByteArrayContent(new byte[1])); }); Assert.Throws(() => { client.SendAsync(new HttpRequestMessage(HttpMethod.Get, CreateFakeUri())); }); Assert.Throws(() => { client.Timeout = TimeSpan.FromSeconds(1); }); } [Theory] [InlineData(false)] [InlineData(true)] public void CancelAllPending_AllPendingOperationsCanceled(bool withInfiniteTimeout) { using (var client = new HttpClient(new CustomResponseHandler((r, c) => WhenCanceled(c)))) { if (withInfiniteTimeout) { client.Timeout = Timeout.InfiniteTimeSpan; } Task[] tasks = Enumerable.Range(0, 3).Select(_ => client.GetAsync(CreateFakeUri())).ToArray(); client.CancelPendingRequests(); Assert.All(tasks, task => Assert.ThrowsAny(() => task.GetAwaiter().GetResult())); } } [Fact] public void Timeout_TooShort_AllPendingOperationsCanceled() { using (var client = new HttpClient(new CustomResponseHandler((r, c) => WhenCanceled(c)))) { client.Timeout = TimeSpan.FromMilliseconds(1); Task[] tasks = Enumerable.Range(0, 3).Select(_ => client.GetAsync(CreateFakeUri())).ToArray(); Assert.All(tasks, task => Assert.ThrowsAny(() => task.GetAwaiter().GetResult())); } } [Fact] [OuterLoop] public void Timeout_SetTo60AndGetResponseFromServerWhichTakes40_Success() { // TODO: This server path will change once the final test infrastructure is in place (Issue #1477). const string SlowServer = "http://httpbin.org/drip?numbytes=1&duration=1&delay=40&code=200"; using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(60); var response = client.GetAsync(SlowServer).GetAwaiter().GetResult(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); } } /// /// This test must be in the same test collection as any others testing HttpClient/WinHttpHandler /// DiagnosticSources, since the global logging mechanism makes them conflict inherently. /// [Fact] public void SendAsync_ExpectedDiagnosticSourceLogging() { bool requestLogged = false; Guid requestGuid = Guid.Empty; bool responseLogged = false; Guid responseGuid = Guid.Empty; var diagnosticListenerObserver = new FakeDiagnosticListenerObserver( kvp => { if (kvp.Key.Equals("System.Net.Http.Request")) { Assert.NotNull(kvp.Value); GetPropertyValueFromAnonymousTypeInstance(kvp.Value, "Request"); requestGuid = GetPropertyValueFromAnonymousTypeInstance(kvp.Value, "LoggingRequestId"); requestLogged = true; } else if (kvp.Key.Equals("System.Net.Http.Response")) { Assert.NotNull(kvp.Value); GetPropertyValueFromAnonymousTypeInstance(kvp.Value, "Response"); responseGuid = GetPropertyValueFromAnonymousTypeInstance(kvp.Value, "LoggingRequestId"); responseLogged = true; } }); using (DiagnosticListener.AllListeners.Subscribe(diagnosticListenerObserver)) { diagnosticListenerObserver.Enable(); using (var client = new HttpClient()) { var response = client.GetAsync(HttpTestServers.RemoteEchoServer).Result; } Assert.True(requestLogged, "Request was not logged."); // Poll with a timeout since logging response is not synchronized with returning a response. WaitForTrue(() => responseLogged, TimeSpan.FromSeconds(1), "Response was not logged within 1 second timeout."); Assert.Equal(requestGuid, responseGuid); diagnosticListenerObserver.Disable(); } } /// /// This test must be in the same test collection as any others testing HttpClient/WinHttpHandler /// DiagnosticSources, since the global logging mechanism makes them conflict inherently. /// [Fact] public void SendAsync_ExpectedDiagnosticSourceNoLogging() { bool requestLogged = false; bool responseLogged = false; var diagnosticListenerObserver = new FakeDiagnosticListenerObserver( kvp => { if (kvp.Key.Equals("System.Net.Http.Request")) { requestLogged = true; } else if (kvp.Key.Equals("System.Net.Http.Response")) { responseLogged = true; } }); using (DiagnosticListener.AllListeners.Subscribe(diagnosticListenerObserver)) { using (var client = new HttpClient()) { var response = client.GetAsync(HttpTestServers.RemoteEchoServer).Result; } Assert.False(requestLogged, "Request was logged while logging disabled."); // TODO: Waiting for one second is not ideal, but how else be reasonably sure that // some logging message hasn't slipped through? WaitForFalse(() => responseLogged, TimeSpan.FromSeconds(1), "Response was logged while logging disabled."); } } private void WaitForTrue(Func p, TimeSpan timeout, string message) { // Assert that spin doesn't time out. Assert.True(System.Threading.SpinWait.SpinUntil(p, timeout), message); } private void WaitForFalse(Func p, TimeSpan timeout, string message) { // Assert that spin times out. Assert.False(System.Threading.SpinWait.SpinUntil(p, timeout), message); } private T GetPropertyValueFromAnonymousTypeInstance(object obj, string propertyName) { Type t = obj.GetType(); PropertyInfo p = t.GetRuntimeProperty(propertyName); object propertyValue = p.GetValue(obj); Assert.NotNull(propertyValue); Assert.IsAssignableFrom(propertyValue); return (T)propertyValue; } private static string CreateFakeUri() => $"http://{Guid.NewGuid().ToString("N")}"; private static async Task WhenCanceled(CancellationToken cancellationToken) { await Task.Delay(-1, cancellationToken); return default(T); } private sealed class CustomResponseHandler : HttpMessageHandler { private readonly Func> _func; public CustomResponseHandler(Func> func) { _func = func; } protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { return _func(request, cancellationToken); } } private sealed class CustomContent : HttpContent { private readonly Func _func; public CustomContent(Func func) { _func = func; } protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) { return _func(stream); } protected override bool TryComputeLength(out long length) { length = 0; return false; } } } }