mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-26 19:11:04 +01:00
Rename to WireMock.Net
This commit is contained in:
34
src/WireMock.Net/Http/Ports.cs
Normal file
34
src/WireMock.Net/Http/Ports.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
77
src/WireMock.Net/Http/TinyHttpServer.cs
Normal file
77
src/WireMock.Net/Http/TinyHttpServer.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user