Skip to content

Commit

Permalink
Switched from HttpListener to WebListener in hopes of solving for htt…
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Ruetz committed Apr 12, 2018
1 parent e8db31c commit fff7096
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
7 changes: 2 additions & 5 deletions MamaBird.Test/FakeHttpServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public void FakeHttpServer_Constructor()
{
Server = new FakeHttpServer(Prefixes.ToArray());
Assert.NotNull(Server.Config);
Server.Stop();
Server = null;
}

[Fact]
public void FakeHttpServer_CanLoadConfigFromFile()
{
Prefixes.Add($"http://localhost:{Port}/");
Server = new FakeHttpServer(Prefixes.ToArray());
Server.LoadConfig(@".\TestAssets\testCase1.json");
Assert.Single(Server.Config);
Expand All @@ -48,7 +49,6 @@ public void FakeHttpServer_CanLoadConfigFromFile()
[Fact]
public void FakeHttpServer_WhenNoConfigAdded_Returns404()
{
Prefixes.Add($"http://localhost:{Port}/");
Server = new FakeHttpServer(Prefixes.ToArray());
Server.Run();
var client = new HttpClient();
Expand All @@ -64,7 +64,6 @@ public void FakeHttpServer_WhenNoConfigAdded_Returns404()
[Fact]
public void FakeHttpServer_SingleRequest_ReturnsExpectedContent()
{
Prefixes.Add($"http://localhost:{Port}/");
Server = new FakeHttpServer(Prefixes.ToArray());
Server.Run();
Server.LoadConfig(@".\TestAssets\testCase1.json");
Expand All @@ -84,7 +83,6 @@ public void FakeHttpServer_SingleRequest_ReturnsExpectedContent()
[Fact]
public void FakeHttpServer_MultipleRequests_ReturnsExpectedContent()
{
Prefixes.Add($"http://localhost:{Port}/");
Server = new FakeHttpServer(Prefixes.ToArray());
Server.Run();
Server.LoadConfig(@".\TestAssets\testCase2.json");
Expand All @@ -108,7 +106,6 @@ public void FakeHttpServer_MultipleRequests_ReturnsExpectedContent()
[Fact]
public void FakeHttpServer_MultipleRoutes_ReturnsExpectedContent()
{
Prefixes.Add($"http://localhost:{Port}/");
Server = new FakeHttpServer(Prefixes.ToArray());
Server.Run();
Server.LoadConfig(@".\TestAssets\testCase3.json");
Expand Down
41 changes: 22 additions & 19 deletions MamaBird/FakeHttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,37 @@
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Microsoft.Net.Http.Server;

namespace MamaBird
{
public class FakeHttpServer
{
private HttpListener _listener = new HttpListener();
// private HttpListener _listener = new HttpListener();
private Func<HttpListenerRequest, string> _responderMethod;
private Dictionary<string, Queue<HttpInteraction>> _config;
private WebListenerSettings _settings;
private WebListener _webListener;
private string[] _prefixes;

public HttpListener Listener { get => _listener; set => _listener = value; }
// public HttpListener Listener { get => _listener; set => _listener = value; }
public Func<HttpListenerRequest, string> ResponderMethod { get => _responderMethod; set => _responderMethod = value; }
public Dictionary<string, Queue<HttpInteraction>> Config { get => _config; set => _config = value; }
public string[] Prefixes { get => _prefixes; set => _prefixes = value; }
public WebListenerSettings Settings { get => _settings; set => _settings = value; }
public WebListener WebListener { get => _webListener; set => _webListener = value; }

public FakeHttpServer(string[] prefixes)
{
_prefixes = prefixes;
_config = new Dictionary<string, Queue<HttpInteraction>>();
_settings = new WebListenerSettings();
foreach (var prefix in _prefixes)
{
_listener.Prefixes.Add(prefix);
_settings.UrlPrefixes.Add(prefix);
}
_listener.Start();
_webListener = new WebListener(_settings);
_webListener.Start();
}

public void LoadConfig(string filepath)
Expand All @@ -52,11 +59,11 @@ public void Run()
Console.WriteLine($"FakeHttpServer listening... ");
try
{
while (_listener.IsListening)
while (_webListener.IsListening)
{
ThreadPool.QueueUserWorkItem((c) =>
{
var ctx = c as HttpListenerContext;
var ctx = c as RequestContext;
try
{
var request = ctx.Request;
Expand All @@ -72,41 +79,37 @@ public void Run()
}
if (interaction.Headers.Count > 0)
{
foreach(var item in interaction.Headers)
foreach (var item in interaction.Headers)
{
//TODO: Support headers in response
}
}
response.StatusCode = interaction.StatusCode;
var buffer = Encoding.UTF8.GetBytes(interaction.Content);
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.ContentLength = buffer.Length;
response.ContentType = "text/plain";
response.Body.Write(buffer, 0, buffer.Length);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(ex.ToString());
}
finally
{
ctx.Response.OutputStream.Close();
ctx.Dispose();
}
}, _listener.GetContext());
}, _webListener.AcceptAsync().Result);
}
}
catch
{}
{ }
});
}

public void Stop()
{
_listener.Stop();
while(_listener.IsListening)
{
Thread.Sleep(100);
}
_listener.Close();
_webListener.Dispose();
}
}
}
1 change: 1 addition & 0 deletions MamaBird/MamaBird.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Net.Http.Server" Version="1.1.4" />
<PackageReference Include="Nerdbank.GitVersioning" Version="2.1.23" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="xunit" Version="2.3.1" />
Expand Down

0 comments on commit fff7096

Please sign in to comment.