From 961e8b555f3653ddd5e3e3da6f23357f063e2b12 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sat, 13 May 2017 10:10:17 +0200 Subject: [PATCH] Update standalone commandline parsing (#27) --- .axoCover/settings.json | 15 ++++++++ .../WireMock.Net.Console.NETCoreApp.csproj | 2 +- .../Program.cs | 10 ++++-- ...eMock.Net.Console.Record.NETCoreApp.csproj | 2 +- .../Program.cs | 30 ++++++++++++---- .../WireMock.Net.StandAlone.NETCoreApp.csproj | 2 +- src/WireMock.Net.StandAlone/Program.cs | 34 +++++++++++++++---- .../WireMock.Net.StandAlone.csproj | 5 ++- src/WireMock.Net.StandAlone/app.config | 3 -- src/WireMock.Net.StandAlone/packages.config | 2 +- .../Server/FluentMockServer.Admin.cs | 11 +++--- 11 files changed, 85 insertions(+), 31 deletions(-) create mode 100644 .axoCover/settings.json delete mode 100644 src/WireMock.Net.StandAlone/app.config diff --git a/.axoCover/settings.json b/.axoCover/settings.json new file mode 100644 index 00000000..c6f5e07d --- /dev/null +++ b/.axoCover/settings.json @@ -0,0 +1,15 @@ +{ + "TestRunner": "", + "TestPlatform": "x86", + "TestApartmentState": "STA", + "TestSettings": "", + "ExcludeAttributes": "System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute", + "ExcludeFiles": "", + "ExcludeDirectories": "", + "Filters": "+[*]*", + "IsIncludingSolutionAssemblies": true, + "IsExcludingTestAssemblies": false, + "IsCoveringByTest": true, + "IsMergingByHash": true, + "IsSkippingAutoProps": true +} \ No newline at end of file diff --git a/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj b/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj index 936d5ebf..8c2f8caa 100644 --- a/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj +++ b/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj @@ -20,7 +20,7 @@ - + diff --git a/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs b/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs index 0b042024..449ba75a 100644 --- a/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs +++ b/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs @@ -8,13 +8,17 @@ namespace WireMock.Net.Console.NETCoreApp { static void Main(params string[] args) { - string url1 = "http://localhost:9095/"; + string url = "http://localhost:9095/"; var server = FluentMockServer.Start(new FluentMockServerSettings { - Urls = new[] { url1 }, + Urls = new[] { url }, StartAdminInterface = true, - ProxyAndRecordSettings = new ProxyAndRecordSettings { Url = "http://www.bbc.com" } + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = "http://www.bbc.com", + SaveMapping = true + } }); System.Console.WriteLine("Press any key to stop the server"); diff --git a/examples/WireMock.Net.Console.Record.NETCoreApp/WireMock.Net.Console.Record.NETCoreApp.csproj b/examples/WireMock.Net.Console.Record.NETCoreApp/WireMock.Net.Console.Record.NETCoreApp.csproj index bc52b49e..ce5ea4e7 100644 --- a/examples/WireMock.Net.Console.Record.NETCoreApp/WireMock.Net.Console.Record.NETCoreApp.csproj +++ b/examples/WireMock.Net.Console.Record.NETCoreApp/WireMock.Net.Console.Record.NETCoreApp.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/WireMock.Net.StandAlone.NETCoreApp/Program.cs b/src/WireMock.Net.StandAlone.NETCoreApp/Program.cs index ad835568..a7323167 100644 --- a/src/WireMock.Net.StandAlone.NETCoreApp/Program.cs +++ b/src/WireMock.Net.StandAlone.NETCoreApp/Program.cs @@ -12,17 +12,23 @@ namespace WireMock.Net.StandAlone.NETCoreApp { private class Options { - [ValueArgument(typeof(string), 'u', "Urls", Description = "URL(s) to listen on.", Optional = true, AllowMultiple = true)] + [ValueArgument(typeof(string), "Urls", Description = "URL(s) to listen on.", Optional = true, AllowMultiple = true)] public List Urls { get; set; } - [SwitchArgument('p', "AllowPartialMapping", true, Description = "Allow Partial Mapping (default set to true).", Optional = true)] + [SwitchArgument("AllowPartialMapping", true, Description = "Allow Partial Mapping (default set to true).", Optional = true)] public bool AllowPartialMapping { get; set; } - [SwitchArgument('s', "StartAdminInterface", true, Description = "Start the AdminInterface (default set to true).", Optional = true)] + [SwitchArgument("StartAdminInterface", true, Description = "Start the AdminInterface (default set to true).", Optional = true)] public bool StartAdminInterface { get; set; } - [SwitchArgument('r', "ReadStaticMappings", true, Description = "Read StaticMappings from ./__admin/mappings (default set to true).", Optional = true)] + [SwitchArgument("ReadStaticMappings", true, Description = "Read StaticMappings from ./__admin/mappings (default set to true).", Optional = true)] public bool ReadStaticMappings { get; set; } + + [ValueArgument(typeof(string), "ProxyURL", Description = "The ProxyURL to use.", Optional = true)] + public string ProxyURL { get; set; } + + [SwitchArgument("SaveProxyMapping", false, Description = "Save the proxied request and response mapping files in ./__admin/mappings. (default set to false).", Optional = true)] + public bool SaveMapping { get; set; } } static void Main(string[] args) @@ -40,13 +46,23 @@ namespace WireMock.Net.StandAlone.NETCoreApp options.Urls.Add("http://localhost:9090/"); } - var server = FluentMockServer.Start(new FluentMockServerSettings + var settings = new FluentMockServerSettings { Urls = options.Urls.ToArray(), StartAdminInterface = options.StartAdminInterface, - ReadStaticMappings = options.ReadStaticMappings - }); + ReadStaticMappings = options.ReadStaticMappings, + }; + if (!string.IsNullOrEmpty(options.ProxyURL)) + { + settings.ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = options.ProxyURL, + SaveMapping = options.SaveMapping + }; + } + + var server = FluentMockServer.Start(settings); if (options.AllowPartialMapping) { server.AllowPartialMapping(); diff --git a/src/WireMock.Net.StandAlone.NETCoreApp/WireMock.Net.StandAlone.NETCoreApp.csproj b/src/WireMock.Net.StandAlone.NETCoreApp/WireMock.Net.StandAlone.NETCoreApp.csproj index 2b21eb35..ddeb3896 100644 --- a/src/WireMock.Net.StandAlone.NETCoreApp/WireMock.Net.StandAlone.NETCoreApp.csproj +++ b/src/WireMock.Net.StandAlone.NETCoreApp/WireMock.Net.StandAlone.NETCoreApp.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/WireMock.Net.StandAlone/Program.cs b/src/WireMock.Net.StandAlone/Program.cs index a5bb3d0d..381896ed 100644 --- a/src/WireMock.Net.StandAlone/Program.cs +++ b/src/WireMock.Net.StandAlone/Program.cs @@ -12,17 +12,23 @@ namespace WireMock.Net.StandAlone { private class Options { - [ValueArgument(typeof(string), 'u', "Urls", Description = "URL(s) to listen on.", Optional = true, AllowMultiple = true)] + [ValueArgument(typeof(string), "Urls", Description = "URL(s) to listen on.", Optional = true, AllowMultiple = true)] public List Urls { get; set; } - [SwitchArgument('p', "AllowPartialMapping", true, Description = "Allow Partial Mapping (default set to true).", Optional = true)] + [SwitchArgument("AllowPartialMapping", true, Description = "Allow Partial Mapping (default set to true).", Optional = true)] public bool AllowPartialMapping { get; set; } - [SwitchArgument('s', "StartAdminInterface", true, Description = "Start the AdminInterface (default set to true).", Optional = true)] + [SwitchArgument("StartAdminInterface", true, Description = "Start the AdminInterface (default set to true).", Optional = true)] public bool StartAdminInterface { get; set; } - [SwitchArgument('r', "ReadStaticMappings", true, Description = "Read StaticMappings from ./__admin/mappings (default set to true).", Optional = true)] + [SwitchArgument("ReadStaticMappings", true, Description = "Read StaticMappings from ./__admin/mappings (default set to true).", Optional = true)] public bool ReadStaticMappings { get; set; } + + [ValueArgument(typeof(string), "ProxyURL", Description = "The ProxyURL to use.", Optional = true)] + public string ProxyURL { get; set; } + + [SwitchArgument("SaveProxyMapping", false, Description = "Save the proxied request and response mapping files in ./__admin/mappings. (default set to false).", Optional = true)] + public bool SaveMapping { get; set; } } static void Main(params string[] args) @@ -36,17 +42,31 @@ namespace WireMock.Net.StandAlone parser.ParseCommandLine(args); if (!options.Urls.Any()) + { options.Urls.Add("http://localhost:9090/"); + } - var server = FluentMockServer.Start(new FluentMockServerSettings + var settings = new FluentMockServerSettings { Urls = options.Urls.ToArray(), StartAdminInterface = options.StartAdminInterface, - ReadStaticMappings = options.ReadStaticMappings - }); + ReadStaticMappings = options.ReadStaticMappings, + }; + if (!string.IsNullOrEmpty(options.ProxyURL)) + { + settings.ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = options.ProxyURL, + SaveMapping = options.SaveMapping + }; + } + + var server = FluentMockServer.Start(settings); if (options.AllowPartialMapping) + { server.AllowPartialMapping(); + } Console.WriteLine("WireMock.Net server listening at {0}", string.Join(" and ", server.Urls)); } diff --git a/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj b/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj index c691b3ae..e607885d 100644 --- a/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj +++ b/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj @@ -36,8 +36,8 @@ ..\..\WireMock.Net-Logo.ico - - ..\..\packages\CommandLineArgumentsParser.3.0.9\lib\net45\CommandLineArgumentsParser.dll + + ..\..\packages\CommandLineArgumentsParser.3.0.10\lib\net45\CommandLineArgumentsParser.dll ..\..\packages\Microsoft.Owin.Host.HttpListener.3.1.0\lib\net45\Microsoft.Owin.Host.HttpListener.dll @@ -53,7 +53,6 @@ - diff --git a/src/WireMock.Net.StandAlone/app.config b/src/WireMock.Net.StandAlone/app.config deleted file mode 100644 index 99ddf3e0..00000000 --- a/src/WireMock.Net.StandAlone/app.config +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/src/WireMock.Net.StandAlone/packages.config b/src/WireMock.Net.StandAlone/packages.config index b14ce5ca..b9c328e4 100644 --- a/src/WireMock.Net.StandAlone/packages.config +++ b/src/WireMock.Net.StandAlone/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 9a2a6255..c75eb137 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -120,18 +120,21 @@ namespace WireMock.Server Given(Request.Create().WithPath(AdminRequests + "/find").UsingPost()).RespondWith(new DynamicResponseProvider(RequestsFind)); } + #region Proxy and Record private void InitProxyAndRecord(ProxyAndRecordSettings settings) { Given(Request.Create().WithPath("/*").UsingAnyVerb()).RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings)); } - - #region Proxy and Record + private async Task ProxyAndRecordAsync(RequestMessage requestMessage, ProxyAndRecordSettings settings) { var responseMessage = await HttpClientHelper.SendAsync(requestMessage, settings.Url); - var mapping = ToMapping(requestMessage, responseMessage); - SaveMappingToFile(mapping); + if (settings.SaveMapping) + { + var mapping = ToMapping(requestMessage, responseMessage); + SaveMappingToFile(mapping); + } return responseMessage; }