From de914ef24d807240ebde17d87140eb975145b11e Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 30 Jan 2017 22:12:58 +0100 Subject: [PATCH] Listen on more ip-address/ports (#18) --- .../Program.cs | 12 ++--- src/WireMock.Net/Http/TinyHttpServer.cs | 15 +++---- src/WireMock.Net/Server/FluentMockServer.cs | 45 ++++++++++++++++--- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/examples/WireMock.Net.ConsoleApplication/Program.cs b/examples/WireMock.Net.ConsoleApplication/Program.cs index c3faea31..3541cb74 100644 --- a/examples/WireMock.Net.ConsoleApplication/Program.cs +++ b/examples/WireMock.Net.ConsoleApplication/Program.cs @@ -9,14 +9,14 @@ namespace WireMock.Net.ConsoleApplication { static class Program { - static void Main(string[] args) + static void Main(params string[] args) { - int port; - if (args.Length == 0 || !int.TryParse(args[0], out port)) - port = 9090; + string url1 = "http://localhost:9090/"; + string url2 = "http://localhost:9091/"; + string url3 = "https://localhost:9443/"; - var server = FluentMockServer.StartWithAdminInterface(port); - Console.WriteLine("FluentMockServer running at {0}", server.Port); + var server = FluentMockServer.StartWithAdminInterface(url1, url2, url3); + Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls)); server .Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet()) diff --git a/src/WireMock.Net/Http/TinyHttpServer.cs b/src/WireMock.Net/Http/TinyHttpServer.cs index 263147df..c2502969 100644 --- a/src/WireMock.Net/Http/TinyHttpServer.cs +++ b/src/WireMock.Net/Http/TinyHttpServer.cs @@ -27,19 +27,18 @@ namespace WireMock.Http /// /// Initializes a new instance of the class. /// - /// - /// The url prefix. - /// - /// - /// The http handler. - /// - public TinyHttpServer(string urlPrefix, Action httpHandler) + /// The urls. + /// The http handler. + public TinyHttpServer(string[] urls, Action httpHandler) { _httpHandler = httpHandler; // Create a listener. _listener = new HttpListener(); - _listener.Prefixes.Add(urlPrefix); + foreach (string urlPrefix in urls) + { + _listener.Prefixes.Add(urlPrefix); + } } /// diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index 7d2e5db4..cb73f42e 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -33,9 +33,9 @@ namespace WireMock.Server private TimeSpan _requestProcessingDelay = TimeSpan.Zero; /// - /// Gets the port. + /// Gets the urls. /// - public int Port { get; } + public string[] Urls { get; } /// /// Gets the request logs. @@ -82,6 +82,19 @@ namespace WireMock.Server return new FluentMockServer(false, port, ssl); } + /// + /// Start this FluentMockServer. + /// + /// The urls to listen on. + /// The . + [PublicAPI] + public static FluentMockServer Start(params string[] urls) + { + Check.NotEmpty(urls, nameof(urls)); + + return new FluentMockServer(false, urls); + } + /// /// Start this FluentMockServer with the admin interface. /// @@ -99,19 +112,37 @@ namespace WireMock.Server return new FluentMockServer(true, port, ssl); } - private FluentMockServer(bool startAdmin, int port, bool ssl) + /// + /// Start this FluentMockServer with the admin interface. + /// + /// The urls. + /// The . + [PublicAPI] + public static FluentMockServer StartWithAdminInterface(params string[] urls) { - string protocol = ssl ? "https" : "http"; - _httpServer = new TinyHttpServer(protocol + "://localhost:" + port + "/", HandleRequestAsync); - Port = port; + Check.NotEmpty(urls, nameof(urls)); + + 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(); - if (startAdmin) + if (startAdminInterface) { InitAdmin(); } } + /// /// Stop this server. ///