Пример #1
0
        public async Task SendAsync_Expires_Success(string value, bool isValid)
        {
            await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
            {
                using (HttpClient client = CreateHttpClient())
                {
                    var message = new HttpRequestMessage(HttpMethod.Get, uri)
                    {
                        Version = UseVersion
                    };
                    HttpResponseMessage response = await client.SendAsync(TestAsync, message);
                    Assert.NotNull(response.Content.Headers.Expires);
                    // Invalid date should be converted to MinValue so everything is expired.
                    Assert.Equal(isValid ? DateTime.Parse(value) : DateTimeOffset.MinValue, response.Content.Headers.Expires);
                }
            },
                                                                   async server =>
            {
                IList <HttpHeaderData> headers = new HttpHeaderData[] { new HttpHeaderData("Expires", value) };

                HttpRequestData requestData = await server.HandleRequestAsync(HttpStatusCode.OK, headers);
            });
        }
        public async Task Dispose_HandlerWithProxy_ProxyNotDisposed()
        {
            var proxy = new TrackDisposalProxy();

            await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
            {
                using (HttpClientHandler handler = CreateHttpClientHandler())
                {
                    handler.UseProxy = true;
                    handler.Proxy    = proxy;
                    using (HttpClient client = CreateHttpClient(handler))
                    {
                        Assert.Equal("hello", await client.GetStringAsync(uri));
                    }
                }
            }, async server =>
            {
                await server.HandleRequestAsync(content: "hello");
            });

            Assert.True(proxy.ProxyUsed);
            Assert.False(proxy.Disposed);
        }
Пример #3
0
        public async Task SendAsync_InvalidHeader_Throw(string value)
        {
            await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
            {
                HttpClientHandler handler = CreateHttpClientHandler();
                using (HttpClient client = CreateHttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    Assert.True(request.Headers.TryAddWithoutValidation("bad", value));

                    await Assert.ThrowsAsync <HttpRequestException>(() => client.SendAsync(request));
                }
            },
                                                                   async server =>
            {
                try
                {
                    // Client should abort at some point so this is going to throw.
                    HttpRequestData requestData = await server.HandleRequestAsync(HttpStatusCode.OK).ConfigureAwait(false);
                }
                catch (IOException) { };
            });
        }
Пример #4
0
        public async Task IncompleteResponseStream_ResponseDropped_CancelsRequestToServer()
        {
            if (UseVersion == HttpVersion30)
            {
                // [ActiveIssue("https://github.com/dotnet/runtime/issues/58234")]
                return;
            }

            using (HttpClient client = CreateHttpClient())
            {
                bool stopGCs = false;
                await LoopbackServerFactory.CreateClientAndServerAsync(async url =>
                {
                    await GetAndDropResponse(client, url);

                    while (!Volatile.Read(ref stopGCs))
                    {
                        await Task.Delay(10);
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                },
                                                                       server => server.AcceptConnectionAsync(async connection =>
                {
                    try
                    {
                        HttpRequestData data = await connection.ReadRequestDataAsync(readBody: false);
                        await connection.SendResponseHeadersAsync(headers: new HttpHeaderData[] { new HttpHeaderData("SomeHeaderName", "AndValue") });
                        await connection.WaitForCancellationAsync();
                    }
                    finally
                    {
                        Volatile.Write(ref stopGCs, true);
                    }
                }));
            }
        }
        public async Task SendAsync_Cancel_CancellationTokenPropagates()
        {
            TaskCompletionSource <bool> clientCanceled = new TaskCompletionSource <bool>();
            await LoopbackServerFactory.CreateClientAndServerAsync(
                async uri =>
            {
                var cts = new CancellationTokenSource();
                cts.Cancel();

                using (HttpClient client = CreateHttpClient())
                {
                    OperationCanceledException ex = null;
                    try
                    {
                        await client.GetAsync(uri, cts.Token);
                    }
                    catch (OperationCanceledException e)
                    {
                        ex = e;
                    }
                    Assert.True(ex != null, "Expected OperationCancelledException, but no exception was thrown.");

                    Assert.True(cts.Token.IsCancellationRequested, "cts token IsCancellationRequested");

                    // .NET Framework has bug where it doesn't propagate token information.
                    Assert.True(ex.CancellationToken.IsCancellationRequested, "exception token IsCancellationRequested");

                    clientCanceled.SetResult(true);
                }
            },
                async server =>
            {
                Task serverTask = server.HandleRequestAsync();
                await clientCanceled.Task;
            });
        }
Пример #6
0
        public async Task SendAsync_UserAgent_CorrectlyWritten()
        {
            string userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.18 Safari/537.36";

            await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
            {
                using (HttpClient client = CreateHttpClient())
                {
                    var message = new HttpRequestMessage(HttpMethod.Get, uri)
                    {
                        Version = UseVersion
                    };
                    message.Headers.TryAddWithoutValidation("User-Agent", userAgent);
                    (await client.SendAsync(TestAsync, message).ConfigureAwait(false)).Dispose();
                }
            },
                                                                   async server =>
            {
                HttpRequestData requestData = await server.HandleRequestAsync(HttpStatusCode.OK);

                string agent = requestData.GetSingleHeaderValue("User-Agent");
                Assert.Equal(userAgent, agent);
            });
        }