Listen on more ip-address/ports (#18)

This commit is contained in:
Stef Heyenrath
2017-01-30 22:12:58 +01:00
parent 517304b999
commit de914ef24d
3 changed files with 51 additions and 21 deletions

View File

@@ -9,14 +9,14 @@ namespace WireMock.Net.ConsoleApplication
{ {
static class Program static class Program
{ {
static void Main(string[] args) static void Main(params string[] args)
{ {
int port; string url1 = "http://localhost:9090/";
if (args.Length == 0 || !int.TryParse(args[0], out port)) string url2 = "http://localhost:9091/";
port = 9090; string url3 = "https://localhost:9443/";
var server = FluentMockServer.StartWithAdminInterface(port); var server = FluentMockServer.StartWithAdminInterface(url1, url2, url3);
Console.WriteLine("FluentMockServer running at {0}", server.Port); Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls));
server server
.Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet()) .Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet())

View File

@@ -27,19 +27,18 @@ namespace WireMock.Http
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TinyHttpServer"/> class. /// Initializes a new instance of the <see cref="TinyHttpServer"/> class.
/// </summary> /// </summary>
/// <param name="urlPrefix"> /// <param name="urls">The urls.</param>
/// The url prefix. /// <param name="httpHandler">The http handler.</param>
/// </param> public TinyHttpServer(string[] urls, Action<HttpListenerContext> httpHandler)
/// <param name="httpHandler">
/// The http handler.
/// </param>
public TinyHttpServer(string urlPrefix, Action<HttpListenerContext> httpHandler)
{ {
_httpHandler = httpHandler; _httpHandler = httpHandler;
// Create a listener. // Create a listener.
_listener = new HttpListener(); _listener = new HttpListener();
_listener.Prefixes.Add(urlPrefix); foreach (string urlPrefix in urls)
{
_listener.Prefixes.Add(urlPrefix);
}
} }
/// <summary> /// <summary>

View File

@@ -33,9 +33,9 @@ namespace WireMock.Server
private TimeSpan _requestProcessingDelay = TimeSpan.Zero; private TimeSpan _requestProcessingDelay = TimeSpan.Zero;
/// <summary> /// <summary>
/// Gets the port. /// Gets the urls.
/// </summary> /// </summary>
public int Port { get; } public string[] Urls { get; }
/// <summary> /// <summary>
/// Gets the request logs. /// Gets the request logs.
@@ -82,6 +82,19 @@ namespace WireMock.Server
return new FluentMockServer(false, port, ssl); return new FluentMockServer(false, port, ssl);
} }
/// <summary>
/// Start this FluentMockServer.
/// </summary>
/// <param name="urls">The urls to listen on.</param>
/// <returns>The <see cref="FluentMockServer"/>.</returns>
[PublicAPI]
public static FluentMockServer Start(params string[] urls)
{
Check.NotEmpty(urls, nameof(urls));
return new FluentMockServer(false, urls);
}
/// <summary> /// <summary>
/// Start this FluentMockServer with the admin interface. /// Start this FluentMockServer with the admin interface.
/// </summary> /// </summary>
@@ -99,19 +112,37 @@ namespace WireMock.Server
return new FluentMockServer(true, port, ssl); return new FluentMockServer(true, port, ssl);
} }
private FluentMockServer(bool startAdmin, int port, bool ssl) /// <summary>
/// Start this FluentMockServer with the admin interface.
/// </summary>
/// <param name="urls">The urls.</param>
/// <returns>The <see cref="FluentMockServer"/>.</returns>
[PublicAPI]
public static FluentMockServer StartWithAdminInterface(params string[] urls)
{ {
string protocol = ssl ? "https" : "http"; Check.NotEmpty(urls, nameof(urls));
_httpServer = new TinyHttpServer(protocol + "://localhost:" + port + "/", HandleRequestAsync);
Port = port; return new FluentMockServer(true, urls);
}
private FluentMockServer(bool startAdminInterface, int port, bool ssl) : this(startAdminInterface, (ssl ? "https" : "http") + "://localhost:" + port + "/")
{
}
private FluentMockServer(bool startAdminInterface, params string[] urls)
{
Urls = urls;
_httpServer = new TinyHttpServer(urls, HandleRequestAsync);
_httpServer.Start(); _httpServer.Start();
if (startAdmin) if (startAdminInterface)
{ {
InitAdmin(); InitAdmin();
} }
} }
/// <summary> /// <summary>
/// Stop this server. /// Stop this server.
/// </summary> /// </summary>