Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Last active February 4, 2024 14:00

Revisions

  1. Chris S revised this gist Jan 5, 2017. No changes.
  2. Chris S revised this gist Jan 5, 2017. 1 changed file with 21 additions and 9 deletions.
    30 changes: 21 additions & 9 deletions in-memory-http-server.cs
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    public static void BasicHttpServer(string url, string outputHtml)
    public static Task BasicHttpServer(string url, string outputHtml)
    {
    var task = Task.Run(() =>
    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
    BasicHttpServer("http://localhost:8888/", "some html");
    string url = GetLocalhostAddress();

    using(BasicHttpServer(url, "some html"))
    {
    var myhttpclient = new MyHttpClient(new Uri(url));

    // Act
    string actualContent = myhttpclient.Read();

    // Server
    var myhttpclient = new MyHttpClient(new Uri("http://localhost:8888/"));
    // Assert
    Assert.That(actualContent, Does.Contain("some html"));
    }
    }

    // Act
    string actualContent = myhttpclient.Read();
    private string GetLocalhostAddress()
    {
    var listener = new TcpListener(IPAddress.Loopback, 0);
    listener.Start();
    int port = ((IPEndPoint)listener.LocalEndpoint).Port;
    listener.Stop();

    // Assert
    Assert.That(actualContent, Does.Contain("some html"));
    return $"http://localhost:{port}/";
    }
  3. Chris S revised this gist Jan 4, 2017. 1 changed file with 21 additions and 21 deletions.
    42 changes: 21 additions & 21 deletions in-memory-http-server.cs
    Original 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();
    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;
    // 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();
    });
    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");
    // Arrange
    BasicHttpServer("http://localhost:8888/", "some html");

    // Server
    var myhttpclient = new MyHttpClient();
    // Server
    var myhttpclient = new MyHttpClient(new Uri("http://localhost:8888/"));

    // Act
    string actualContent = myhttpclient.Read(new Uri("http://localhost:8888/"));
    // Act
    string actualContent = myhttpclient.Read();

    // Assert
    Assert.That(actualContent, Does.Contain("html"));
    // Assert
    Assert.That(actualContent, Does.Contain("some html"));
    }
  4. Chris S created this gist Jan 4, 2017.
    38 changes: 38 additions & 0 deletions in-memory-http-server.cs
    Original 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"));
    }