public async Task ReadAndWriteSlowConnection()
        {
            using (var connection = _fixture.CreateTestConnection())
            {
                var testString = "hello world";
                var request    = $"POST /ReadAndWriteSlowConnection HTTP/1.0\r\n" +
                                 $"Content-Length: {testString.Length}\r\n" +
                                 "Host: " + "localhost\r\n" +
                                 "\r\n" + testString;

                foreach (var c in request)
                {
                    await connection.Send(c.ToString());

                    await Task.Delay(10);
                }

                await connection.Receive(
                    "HTTP/1.1 200 OK",
                    "");

                await connection.ReceiveHeaders();

                for (int i = 0; i < 100; i++)
                {
                    foreach (var c in testString)
                    {
                        await connection.Receive(c.ToString());
                    }
                    await Task.Delay(10);
                }
                await connection.WaitForConnectionClose();
            }
        }
Exemplo n.º 2
0
        public async Task ServerWorksAfterClientDisconnect()
        {
            using (var connection = _fixture.CreateTestConnection())
            {
                var message = "Hello";
                await connection.Send(
                    "POST /ReadAndWriteSynchronously HTTP/1.1",
                    $"Content-Length: {100000}",
                    "Host: localhost",
                    "Connection: close",
                    "",
                    "");

                await connection.Send(message);

                await connection.Receive(
                    "HTTP/1.1 200 OK",
                    "");
            }

            var response = await _fixture.Client.GetAsync("HelloWorld");

            var responseText = await response.Content.ReadAsStringAsync();

            Assert.Equal("Hello World", responseText);
        }
Exemplo n.º 3
0
        private async Task <(int Status, string Body)> SendSocketRequestAsync(string path)
        {
            using (var connection = _fixture.CreateTestConnection())
            {
                await connection.Send(
                    "GET " + path + " HTTP/1.1",
                    "Host: " + _fixture.Client.BaseAddress.Authority,
                    "",
                    "");

                var headers = await connection.ReceiveHeaders();

                var status = int.Parse(headers[0].Substring(9, 3));
                if (headers.Contains("Transfer-Encoding: chunked"))
                {
                    var bytes0 = await connection.ReceiveChunk();

                    Assert.False(bytes0.IsEmpty);
                    return(status, Encoding.UTF8.GetString(bytes0.Span));
                }
                var length = int.Parse(headers.Single(h => h.StartsWith("Content-Length: ")).Substring("Content-Length: ".Length));
                var bytes1 = await connection.Receive(length);

                return(status, Encoding.ASCII.GetString(bytes1.Span));
            }
        }