Last active
February 4, 2024 14:00
Revisions
-
Chris S revised this gist
Jan 5, 2017 . No changes.There are no files selected for viewing
-
Chris S revised this gist
Jan 5, 2017 . 1 changed file with 21 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ public static Task BasicHttpServer(string url, string outputHtml) { return Task.Run(() => { HttpListener listener = new HttpListener(); listener.Prefixes.Add(url); @@ -25,14 +25,26 @@ public static void BasicHttpServer(string url, string outputHtml) public void should_return_html() { // Arrange string url = GetLocalhostAddress(); using(BasicHttpServer(url, "some html")) { var myhttpclient = new MyHttpClient(new Uri(url)); // Act string actualContent = myhttpclient.Read(); // Assert Assert.That(actualContent, Does.Contain("some html")); } } private string GetLocalhostAddress() { var listener = new TcpListener(IPAddress.Loopback, 0); listener.Start(); int port = ((IPEndPoint)listener.LocalEndpoint).Port; listener.Stop(); return $"http://localhost:{port}/"; } -
Chris S revised this gist
Jan 4, 2017 . 1 changed file with 21 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,20 @@ public static void BasicHttpServer(string url, string outputHtml) { var task = Task.Run(() => { HttpListener listener = new HttpListener(); listener.Prefixes.Add(url); listener.Start(); // GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerResponse response = context.Response; Stream stream = response.OutputStream; var writer = new StreamWriter(stream); writer.Write(outputHtml); writer.Close(); }); } // @@ -24,15 +24,15 @@ public static void BasicHttpServer(string url, string outputHtml) [Test] public void should_return_html() { // Arrange BasicHttpServer("http://localhost:8888/", "some html"); // Server var myhttpclient = new MyHttpClient(new Uri("http://localhost:8888/")); // Act string actualContent = myhttpclient.Read(); // Assert Assert.That(actualContent, Does.Contain("some html")); } -
Chris S created this gist
Jan 4, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ public static void BasicHttpServer(string url, string outputHtml) { var task = Task.Run(() => { HttpListener listener = new HttpListener(); listener.Prefixes.Add(url); listener.Start(); // GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerResponse response = context.Response; Stream stream = response.OutputStream; var writer = new StreamWriter(stream); writer.Write(outputHtml); writer.Close(); }); } // // Example // [Test] public void should_return_html() { // Arrange BasicHttpServer("http://localhost:8888/", "some html"); // Server var myhttpclient = new MyHttpClient(); // Act string actualContent = myhttpclient.Read(new Uri("http://localhost:8888/")); // Assert Assert.That(actualContent, Does.Contain("html")); }