public async Task TestPost_WhenNoServerListening_ThrowsTimeoutException()
 {
     var client = new NamedPipeHttpClientBuilder(TestContext.TestName).Build();
     await Assert.ThrowsExceptionAsync <TimeoutException>(async() => await client.PostAsJsonAsync("http://localhost/api/e2e-tests/hello", new PersonMessage {
         Name = "Test"
     }));
 }
        private async Task TestPost_Impl()
        {
            using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName)))
            {
                var client = new NamedPipeHttpClientBuilder(TestContext.TestName).Build();
                var result = await client.PostAsJsonAsync("http://localhost/api/e2e-tests/hello", new PersonMessage { Name = "Test" });

                var wlcMsg = await result.Content.ReadAsAsync <WelcomeMessage>();

                Assert.AreEqual("Hello Test", wlcMsg.Text);
            }
        }
        private async Task TestBadRequest_BadMediaType_Impl()
        {
            using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName)))
            {
                var client = new NamedPipeHttpClientBuilder(TestContext.TestName)
                             .WithPerRequestTimeout(TimeSpan.FromSeconds(1))
                             .Build();
                var badContent = new StringContent("{ ", Encoding.UTF8, "application/broken");
                var result     = await client.PostAsJsonAsync("http://localhost/api/e2e-tests/hello", badContent);

                var wlcMsg = await result.Content.ReadAsAsync <WelcomeMessage>();

                Assert.AreEqual("Hello ", wlcMsg.Text);
            }
        }
        public async Task TestClientTimeoutIsRespectedWhenServerTakesTooLong()
        {
            using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName)))
            {
                var client = new NamedPipeHttpClientBuilder(TestContext.TestName)
                             .WithPerRequestTimeout(TimeSpan.FromMilliseconds(100))
                             .Build();
                var sw = Stopwatch.StartNew();
                await Assert.ThrowsExceptionAsync <TaskCanceledException>(async() =>
                {
                    await client.GetAsync("http://localhost/api/e2e-tests/timeout");
                });

                sw.Stop();
                Assert.IsTrue(sw.ElapsedMilliseconds < 1000, $"Client Timeout wasn't respected - GetAsync took too long ({sw.ElapsedMilliseconds} ms)");
            }
        }
        private async Task TestGet_Impl(int numberOfRequests = 1, Version httpVersion = null)
        {
            using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName)))
            {
                for (int i = 0; i < numberOfRequests; i++)
                {
                    var client = new NamedPipeHttpClientBuilder(TestContext.TestName)
                                 .WithPerRequestTimeout(TimeSpan.FromSeconds(5))
                                 .WithHttpVersion(httpVersion)
                                 .WithDelegatingHandler(new TestLoggingHandler())
                                 .Build();
                    var result = await client.GetAsync("http://localhost/api/e2e-tests/hello-world");

                    Assert.AreEqual("Hello World", await result.Content.ReadAsAsync <string>());
                }
            }
        }
        private async Task TestClientStartsFirst_ServerDropsClientButComesUpAfterwards_Impl(int numberOfRequests)
        {
            var client = new HttpClient(new DialMessageHandler(new NamedPipeDialer(TestContext.TestName, (int)TimeSpan.FromSeconds(30).TotalMilliseconds)));

            client.Timeout = TimeSpan.FromSeconds(30);
            var clientTask = client.GetAsync("http://localhost/api/e2e-tests/hello-world");

            using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName)))
            {
                for (int i = 0; i < numberOfRequests; i++)
                {
                    var client2 = new NamedPipeHttpClientBuilder(TestContext.TestName)
                                  .WithPerRequestTimeout(TimeSpan.FromSeconds(5))
                                  .Build();
                    var result = await client2.GetAsync("http://localhost/api/e2e-tests/hello-world");

                    Assert.AreEqual("Hello World", await result.Content.ReadAsAsync <string>());
                }
            }
        }
        private async Task TestBadRequest_NonexistentEndpoint_Impl()
        {
            using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName)))
            {
                var client = new NamedPipeHttpClientBuilder(TestContext.TestName).Build();
                try
                {
                    var badContent = new StringContent("{ }");
                    var result     = await client.PostAsJsonAsync("http://localhost/api/this-doesnt-exist", badContent);

                    Debug.WriteLine("Client: Posted Json ");

                    Assert.AreEqual(result.StatusCode, System.Net.HttpStatusCode.NotFound);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("EXCEPTION " + e);
                    throw;
                }
            }
        }