示例#1
0
        public async Task Send_MultipleRequests_Sequential_Success()
        {
            await RunMultiStreamTest(
                async (client, uri) =>
            {
                foreach ((var testIdx, var headers, var content, var trailingHeaders) in InterleavedData())
                {
                    await using ValueHttpRequest request = (await client.CreateNewRequestAsync(Version, HttpVersionPolicy.RequestVersionExact)).Value;
                    await ClientSendHelperAsync(request, uri, testIdx, headers, content, trailingHeaders);
                }
            },
                async server =>
            {
                foreach ((var testIdx, var headers, var content, var trailingHeaders) in InterleavedData())
                {
                    await using HttpTestStream serverStream = await server.AcceptStreamAsync();
                    HttpTestFullRequest request             = await serverStream.ReceiveAndSendAsync();
                    Assert.True(request.Headers.Contains(headers));
                    Assert.Equal(string.Join("", content), request.Content);

                    if (trailingHeaders is not null)
                    {
                        Assert.True(request.TrailingHeaders.Contains(trailingHeaders));
                    }
                }
            }, millisecondsTimeout : DefaultTestTimeout * 10);
        }
示例#2
0
        public async Task Pipelining_Success()
        {
            const int PipelineLength = 10;

            await RunMultiStreamTest(
                async (client, serverUri) =>
            {
                var tasks = new Task[PipelineLength];

                for (int i = 0; i < tasks.Length; ++i)
                {
                    tasks[i] = MakeRequest(i);
                }

                await tasks.WhenAllOrAnyFailed(10_000);

                async Task MakeRequest(int requestNo)
                {
                    await using ValueHttpRequest request = (await client.CreateNewRequestAsync(Version, HttpVersionPolicy.RequestVersionExact)).Value;

                    request.ConfigureRequest(contentLength: 0, hasTrailingHeaders: false);
                    request.WriteRequest(HttpMethod.Get, serverUri);
                    request.WriteHeader("X-Request-No", requestNo.ToString(CultureInfo.InvariantCulture));
                    await request.CompleteRequestAsync();

                    TestHeadersSink headers = await request.ReadAllHeadersAsync();

                    Assert.Equal(requestNo.ToString(CultureInfo.InvariantCulture), headers.GetSingleValue("X-Response-No"));
                }
            },
                async server =>
            {
                var streams = new (HttpTestStream, HttpTestFullRequest)[PipelineLength];

                for (int i = 0; i < streams.Length; ++i)
                {
                    HttpTestStream stream       = await server.AcceptStreamAsync();
                    HttpTestFullRequest request = await stream.ReceiveFullRequestAsync();
                    Assert.Equal(i.ToString(CultureInfo.InvariantCulture), request.Headers.GetSingleValue("X-Request-No"));
                    streams[i] = (stream, request);
                }

                for (int i = 0; i < streams.Length; ++i)
                {
                    (HttpTestStream stream, HttpTestFullRequest request) = streams[i];

                    var responseHeaders = new TestHeadersSink()
                    {
                        { "X-Response-No", i.ToString(CultureInfo.InvariantCulture) }
                    };

                    await stream.SendResponseAsync(headers: responseHeaders);
                    await stream.DisposeAsync();
                }
            });
示例#3
0
        internal async Task RunSingleStreamTest(Func <ValueHttpRequest, Uri, Task> clientFunc, Func <HttpTestStream, Task> serverFunc, int?millisecondsTimeout = null)
        {
            await RunMultiStreamTest(
                async (client, serverUri) =>
            {
                ValueHttpRequest?request = await client.CreateNewRequestAsync(Version, HttpVersionPolicy.RequestVersionExact).ConfigureAwait(false);
                Assert.NotNull(request);

                await using (request.ConfigureAwait(false))
                {
                    await clientFunc(request.Value, serverUri).ConfigureAwait(false);
                    await request.Value.DrainAsync().ConfigureAwait(false);
                }
            },
                async server =>
            {
                HttpTestStream request = await server.AcceptStreamAsync().ConfigureAwait(false);
                await using (request.ConfigureAwait(false))
                {
                    await serverFunc(request).ConfigureAwait(false);
                }
            }, millisecondsTimeout).ConfigureAwait(false);
        }
示例#4
0
        internal override async Task RunSingleStreamTest(Func <ValueHttpRequest, Uri, Task> clientFunc, Func <HttpTestStream, Task> serverFunc, int?millisecondsTimeout = null)
        {
            ConnectionFactory connectionFactory = CreateConnectionFactory();

            await using (connectionFactory.ConfigureAwait(false))
            {
                var server = new Http1TestServer(await connectionFactory.ListenAsync().ConfigureAwait(false));
                await using (server.ConfigureAwait(false))
                {
                    var uriBuilder = new UriBuilder
                    {
                        Scheme = Uri.UriSchemeHttp,
                        Path   = "/"
                    };

                    switch (server.EndPoint)
                    {
                    case DnsEndPoint dnsEp:
                        uriBuilder.Host = dnsEp.Host;
                        uriBuilder.Port = dnsEp.Port;
                        break;

                    case IPEndPoint ipEp:
                        uriBuilder.Host = ipEp.Address.ToString();
                        uriBuilder.Port = ipEp.Port;
                        break;

                    default:
                        uriBuilder.Host = "localhost";
                        uriBuilder.Port = 80;
                        break;
                    }

                    Uri serverUri = uriBuilder.Uri;

                    await RunClientServer(RunClientAsync, RunServerAsync, millisecondsTimeout).ConfigureAwait(false);

                    async Task RunClientAsync()
                    {
                        HttpConnection connection = new Http1Connection(await connectionFactory.ConnectAsync(server.EndPoint !).ConfigureAwait(false));

                        await using (connection.ConfigureAwait(false))
                        {
                            ValueHttpRequest?optionalRequest = await connection.CreateNewRequestAsync(HttpPrimitiveVersion.Version11, HttpVersionPolicy.RequestVersionExact).ConfigureAwait(false);

                            Assert.NotNull(optionalRequest);

                            ValueHttpRequest request = optionalRequest.Value;
                            await using (request.ConfigureAwait(false))
                            {
                                await clientFunc(request, serverUri).ConfigureAwait(false);

                                await request.DrainAsync().ConfigureAwait(false);
                            }
                        }
                    }

                    async Task RunServerAsync()
                    {
                        HttpTestConnection connection = await server.AcceptAsync().ConfigureAwait(false);

                        await using (connection.ConfigureAwait(false))
                        {
                            HttpTestStream request = await connection.AcceptStreamAsync().ConfigureAwait(false);

                            await using (request.ConfigureAwait(false))
                            {
                                await serverFunc(request).ConfigureAwait(false);
                            }
                        }
                    }
                }
            }
        }