Beispiel #1
0
        public async Task PostRewindableContentUsingAuth_NoPreAuthenticate_Success(Uri serverUri)
        {
            HttpContent content    = CustomContent.Create(ExpectedContent, true);
            var         credential = new NetworkCredential(UserName, Password);

            await PostUsingAuthHelper(serverUri, ExpectedContent, content, credential, false);
        }
Beispiel #2
0
        public async Task MaxResponseContentBufferSize_ThrowsIfTooSmallForContent(int maxSize, int contentLength, bool exceptionExpected)
        {
            var content = new CustomContent(async s =>
            {
                await s.WriteAsync(TestHelper.GenerateRandomContent(contentLength));
            });

            var handler = new CustomResponseHandler((r, c) => Task.FromResult(new HttpResponseMessage()
            {
                Content = content
            }));

            using (var client = new HttpClient(handler))
            {
                client.MaxResponseContentBufferSize = maxSize;

                if (exceptionExpected)
                {
                    await Assert.ThrowsAsync <HttpRequestException>(() => client.GetAsync(CreateFakeUri()));
                }
                else
                {
                    await client.GetAsync(CreateFakeUri());
                }
            }
        }
Beispiel #3
0
 public async Task PostNonRewindableContentUsingAuth_NoPreAuthenticate_ThrowsInvalidOperationException(Uri serverUri)
 {
     HttpContent content    = CustomContent.Create(ExpectedContent, false);
     var         credential = new NetworkCredential(UserName, Password);
     await Assert.ThrowsAsync <InvalidOperationException>(() =>
                                                          PostUsingAuthHelper(serverUri, ExpectedContent, content, credential, preAuthenticate : false));
 }
Beispiel #4
0
        public async Task PostAsyncExpect100Continue_RetryOnConnectionClosed_Success()
        {
            if (!IsRetrySupported)
            {
                return;
            }

            await LoopbackServer.CreateClientAndServerAsync(async url =>
            {
                using (HttpClient client = CreateHttpClient())
                {
                    // Send initial request and receive response so connection is established
                    HttpResponseMessage response1 = await client.GetAsync(url);
                    Assert.Equal(HttpStatusCode.OK, response1.StatusCode);
                    Assert.Equal(s_simpleContent, await response1.Content.ReadAsStringAsync());

                    // Send second request.  Should reuse same connection.
                    // The server will close the connection, but HttpClient should retry the request.
                    HttpRequestMessage request     = new HttpRequestMessage(HttpMethod.Post, url);
                    request.Headers.ExpectContinue = true;
                    var content     = new CustomContent();
                    request.Content = content;

                    HttpResponseMessage response2 = await client.SendAsync(request);
                    Assert.Equal(HttpStatusCode.OK, response1.StatusCode);
                    Assert.Equal(s_simpleContent, await response1.Content.ReadAsStringAsync());

                    Assert.Equal(1, content.SerializeCount);
                }
            },
                                                            async server =>
            {
                // Accept first connection
                await server.AcceptConnectionAsync(async connection =>
                {
                    // Initial response
                    await connection.ReadRequestHeaderAndSendResponseAsync(content: s_simpleContent);

                    // Second response: Read request headers, then close connection
                    List <string> lines = await connection.ReadRequestHeaderAsync();
                    Assert.Contains("Expect: 100-continue", lines);
                });

                // Client should reconnect.  Accept that connection and send response.
                await server.AcceptConnectionAsync(async connection =>
                {
                    List <string> lines = await connection.ReadRequestHeaderAsync();
                    Assert.Contains("Expect: 100-continue", lines);

                    await connection.Writer.WriteAsync("HTTP/1.1 100 Continue\r\n\r\n");

                    string contentLine = await connection.Reader.ReadLineAsync();
                    Assert.Equal(s_simpleContent, contentLine + "\r\n");

                    await connection.SendResponseAsync(content: s_simpleContent);
                });
            });
        }
Beispiel #5
0
        public async Task PostNonRewindableContentUsingAuth_PreAuthenticate_Success(Uri serverUri)
        {
            if (IsWinHttpHandler)
            {
                // Issue #9228
                return;
            }

            HttpContent content    = CustomContent.Create(ExpectedContent, false);
            var         credential = new NetworkCredential(UserName, Password);

            await PostUsingAuthHelper(serverUri, ExpectedContent, content, credential, preAuthenticate : true);
        }