private async Task TestStreamInterruption_Impl() { var logFactory = new TestLoggerFactory(); using (CustomListenerHost.Start(app => { SetupDefaultAppBuilder(app); app.SetLoggerFactory(logFactory); }, new NamedPipeListener(TestContext.TestName))) { var dialer = new NamedPipeDialer(TestContext.TestName); using (var fuzzyStream = await dialer.DialAsync(new HttpRequestMessage(), CancellationToken.None)) { // just write the first line of a valid http request, and drop the connection var payload = Encoding.ASCII.GetBytes("GET /docs/index.html HTTP/1.0\n"); await fuzzyStream.WriteAsync(payload, 0, payload.Length); } var ex = await logFactory.ExceptionReceived.Task; Assert.IsInstanceOfType(ex, typeof(EndOfStreamException)); // Test the stream still works afterwards var client = new HttpClient(new DialMessageHandler(dialer)); 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 TestPost_Impl() { using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName))) { var client = new HttpClient(new DialMessageHandler(new NamedPipeDialer(TestContext.TestName))); 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 HttpClient(new DialMessageHandler(new NamedPipeDialer(TestContext.TestName))); client.Timeout = TimeSpan.FromSeconds(1); 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); } }
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 HttpClient(new DialMessageHandler(new NamedPipeDialer(TestContext.TestName), null, httpVersion)); client.Timeout = TimeSpan.FromSeconds(5); var result = await client.GetAsync("http://localhost/api/e2e-tests/hello-world"); Assert.AreEqual("Hello World", await result.Content.ReadAsAsync <string>()); } } }
public async Task TestClientTimeoutIsRespectedWhenServerTakesTooLong() { using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName))) { var client = new HttpClient(new DialMessageHandler(new NamedPipeDialer(TestContext.TestName))); client.Timeout = TimeSpan.FromMilliseconds(100); 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>()); } } }
public async Task TestGet() { using (CustomListenerHost.Start(app => { HttpConfiguration config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.SuppressDefaultHostAuthentication(); config.SuppressHostPrincipal(); app.UseWebApi(config); }, new TestListener("legacy_test_get"))) { var client = new HttpClient(new DialMessageHandler(new TestDialer("legacy_test_get"))); 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 HttpClient(new DialMessageHandler(new NamedPipeDialer(TestContext.TestName))); client2.Timeout = TimeSpan.FromSeconds(5); var result = await client2.GetAsync("http://localhost/api/e2e-tests/hello-world"); Assert.AreEqual("Hello World", await result.Content.ReadAsAsync <string>()); } } }
public async Task TestPost() { using (CustomListenerHost.Start(app => { HttpConfiguration config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.SuppressDefaultHostAuthentication(); config.SuppressHostPrincipal(); app.UseWebApi(config); }, new TestListener("legacy_test_post"))) { var client = new HttpClient(new DialMessageHandler(new TestDialer("legacy_test_post"))); 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_NonexistentEndpoint_Impl() { using (CustomListenerHost.Start(SetupDefaultAppBuilder, new NamedPipeListener(TestContext.TestName))) { var client = new HttpClient(new DialMessageHandler(new NamedPipeDialer(TestContext.TestName))); 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; } } }