Rename to WireMock.Net

This commit is contained in:
Stef Heyenrath
2017-01-24 22:28:08 +01:00
parent 4809fed513
commit 3cb1a6d2e1
65 changed files with 5 additions and 5 deletions

View File

@@ -0,0 +1,34 @@
using System.Net;
using System.Net.Sockets;
namespace WireMock.Http
{
/// <summary>
/// The ports.
/// </summary>
public static class Ports
{
/// <summary>
/// The find free TCP port.
/// </summary>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
/// <remarks>see http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net.</remarks>
public static int FindFreeTcpPort()
{
TcpListener tcpListener = null;
try
{
tcpListener = new TcpListener(IPAddress.Loopback, 0);
tcpListener.Start();
return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
}
finally
{
tcpListener?.Stop();
}
}
}
}

View File

@@ -0,0 +1,77 @@
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace WireMock.Http
{
/// <summary>
/// The tiny http server.
/// </summary>
public class TinyHttpServer
{
private readonly Action<HttpListenerContext> _httpHandler;
private readonly HttpListener _listener;
private CancellationTokenSource _cts;
/// <summary>
/// Gets a value indicating whether this server is started.
/// </summary>
/// <value>
/// <c>true</c> if this server is started; otherwise, <c>false</c>.
/// </value>
public bool IsStarted { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="TinyHttpServer"/> class.
/// </summary>
/// <param name="urlPrefix">
/// The url prefix.
/// </param>
/// <param name="httpHandler">
/// The http handler.
/// </param>
public TinyHttpServer(string urlPrefix, Action<HttpListenerContext> httpHandler)
{
_httpHandler = httpHandler;
// Create a listener.
_listener = new HttpListener();
_listener.Prefixes.Add(urlPrefix);
}
/// <summary>
/// Start the server.
/// </summary>
public void Start()
{
_listener.Start();
IsStarted = true;
_cts = new CancellationTokenSource();
Task.Run(
async () =>
{
using (_listener)
{
while (!_cts.Token.IsCancellationRequested)
{
HttpListenerContext context = await _listener.GetContextAsync();
_httpHandler(context);
}
}
},
_cts.Token);
}
/// <summary>
/// Stop the server.
/// </summary>
public void Stop()
{
_cts.Cancel();
}
}
}