diff --git a/examples/WireMock.Net.Console.NETCoreApp/Program.cs b/examples/WireMock.Net.Console.NETCoreApp/Program.cs index 6afcf5d0..90297c8c 100644 --- a/examples/WireMock.Net.Console.NETCoreApp/Program.cs +++ b/examples/WireMock.Net.Console.NETCoreApp/Program.cs @@ -1,11 +1,4 @@ -using System; -using System.Linq; -using Newtonsoft.Json; -using WireMock.Matchers; -using WireMock.RequestBuilders; -using WireMock.ResponseBuilders; -using WireMock.Server; -using WireMock.Settings; +using WireMock.Net.ConsoleApplication; namespace WireMock.Net.Console.NETCoreApp { @@ -13,107 +6,7 @@ namespace WireMock.Net.Console.NETCoreApp { static void Main(params string[] args) { - string url1 = "http://localhost:9090/"; - string url2 = "http://localhost:9091/"; - string url3 = "https://localhost:9443/"; - - var server = FluentMockServer.Start(new FluentMockServerSettings - { - Urls = new[] { url1, url2, url3 }, - StartAdminInterface = true, - ReadStaticMappings = true - }); - System.Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls)); - - server.SetBasicAuthentication("a", "b"); - - server.AllowPartialMapping(); - - server - .Given(Request.Create().WithPath("/bbc").UsingGet()) - .RespondWith(Response.Create().WithProxy("http://www.bbc.com")); - - server - .Given(Request.Create().WithPath("/google").UsingGet()) - .RespondWith(Response.Create().WithProxy("http://www.google.com")); - - server - .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) - .AtPriority(4) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); - - server - .Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e"))) - .AtPriority(999) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""data posted with FUNC 201""}")); - - server - .Given(Request.Create().WithPath("/data", "/ax").UsingPost().WithHeader("Content-Type", "application/json*")) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""data posted with 201""}")); - - server - .Given(Request.Create().WithPath("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""json posted with 201""}")); - - server - .Given(Request.Create().WithPath("/json2").UsingPost().WithBody("x")) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""json posted with x - 201""}")); - - server - .Given(Request.Create().WithPath("/data").UsingDelete()) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""data deleted with 200""}")); - - server - .Given(Request.Create().WithPath("/nobody").UsingGet()) - .RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1)) - .WithStatusCode(200)); - - server - .Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher(new[] { "cat", "dog" }))) - .RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200")); - - // http://localhost:8080/any/any?start=1000&stop=1&stop=2 - server - .Given(Request.Create().WithPath("/*").UsingGet()) - .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05") - .AtPriority(server.Mappings.Count() + 1) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") - .WithBody(@"{""msg"": ""Hello world CATCH-ALL on /*, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }") - .WithTransformer() - .WithDelay(TimeSpan.FromMilliseconds(100)) - ); - - System.Console.WriteLine("Press any key to stop the server"); - System.Console.ReadKey(); - server.Stop(); - - System.Console.WriteLine("Displaying all requests"); - var allRequests = server.LogEntries; - System.Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented)); - - System.Console.WriteLine("Press any key to quit"); - System.Console.ReadKey(); + MainApp.Run(); } } } \ 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 8c2f8caa..7183b0f0 100644 --- a/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj +++ b/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj @@ -10,6 +10,10 @@ + + + + PreserveNewest diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs new file mode 100644 index 00000000..3595a185 --- /dev/null +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -0,0 +1,118 @@ +using System; +using System.Linq; +using Newtonsoft.Json; +using WireMock.Matchers; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; +using WireMock.Server; +using WireMock.Settings; + +namespace WireMock.Net.ConsoleApplication +{ + public static class MainApp + { + public static void Run() + { + string url1 = "http://localhost:9090/"; + string url2 = "http://localhost:9091/"; + string url3 = "https://localhost:9443/"; + + var server = FluentMockServer.Start(new FluentMockServerSettings + { + Urls = new[] { url1, url2, url3 }, + StartAdminInterface = true, + ReadStaticMappings = true + }); + System.Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls)); + + server.SetBasicAuthentication("a", "b"); + + server.AllowPartialMapping(); + + server + .Given(Request.Create().WithPath("/oauth2/access").UsingPost().WithBody("grant_type=password;username=u;password=p")) + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new { access_token = "AT", refresh_token = "RT" })); + + server + .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) + .AtPriority(4) + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithHeader("Content-Type", "application/json") + .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); + + server + .Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e"))) + .AtPriority(999) + .RespondWith(Response.Create() + .WithStatusCode(201) + .WithHeader("Content-Type", "application/json") + .WithBody(@"{ ""result"": ""data posted with FUNC 201""}")); + + server + .Given(Request.Create().WithPath("/data", "/ax").UsingPost().WithHeader("Content-Type", "application/json*")) + .RespondWith(Response.Create() + .WithStatusCode(201) + .WithHeader("Content-Type", "application/json") + .WithBody(@"{ ""result"": ""data posted with 201""}")); + + server + .Given(Request.Create().WithPath("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))) + .RespondWith(Response.Create() + .WithStatusCode(201) + .WithHeader("Content-Type", "application/json") + .WithBody(@"{ ""result"": ""json posted with 201""}")); + + server + .Given(Request.Create().WithPath("/json2").UsingPost().WithBody("x")) + .RespondWith(Response.Create() + .WithStatusCode(201) + .WithHeader("Content-Type", "application/json") + .WithBody(@"{ ""result"": ""json posted with x - 201""}")); + + server + .Given(Request.Create().WithPath("/data").UsingDelete()) + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithHeader("Content-Type", "application/json") + .WithBody(@"{ ""result"": ""data deleted with 200""}")); + + server + .Given(Request.Create().WithPath("/nobody").UsingGet()) + .RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1)) + .WithStatusCode(200)); + + server + .Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher(new[] { "cat", "dog" }))) + .RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200")); + + // http://localhost:8080/any/any?start=1000&stop=1&stop=2 + server + .Given(Request.Create().WithPath("/*").UsingGet()) + .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05") + .AtPriority(server.Mappings.Count() + 1) + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithHeader("Content-Type", "application/json") + .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") + .WithBody(@"{""msg"": ""Hello world CATCH-ALL on /*, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }") + .WithTransformer() + .WithDelay(TimeSpan.FromMilliseconds(100)) + ); + + System.Console.WriteLine("Press any key to stop the server"); + System.Console.ReadKey(); + server.Stop(); + + System.Console.WriteLine("Displaying all requests"); + var allRequests = server.LogEntries; + System.Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented)); + + System.Console.WriteLine("Press any key to quit"); + System.Console.ReadKey(); + } + } +} diff --git a/examples/WireMock.Net.ConsoleApplication/Program.cs b/examples/WireMock.Net.ConsoleApplication/Program.cs index 93163fb7..5765ad6c 100644 --- a/examples/WireMock.Net.ConsoleApplication/Program.cs +++ b/examples/WireMock.Net.ConsoleApplication/Program.cs @@ -1,111 +1,10 @@ -using System; -using System.Linq; -using Newtonsoft.Json; -using WireMock.Matchers; -using WireMock.RequestBuilders; -using WireMock.ResponseBuilders; -using WireMock.Server; -using WireMock.Settings; - -namespace WireMock.Net.ConsoleApplication +namespace WireMock.Net.ConsoleApplication { static class Program { static void Main(params string[] args) { - string url1 = "http://localhost:9090/"; - string url2 = "http://localhost:9091/"; - string url3 = "https://localhost:9443/"; - - var server = FluentMockServer.Start(new FluentMockServerSettings - { - Urls = new [] { url1, url2, url3 }, - StartAdminInterface = true, - ReadStaticMappings = true - }); - Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls)); - - server.SetBasicAuthentication("a", "b"); - - server.AllowPartialMapping(); - - server - .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) - .AtPriority(4) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); - - server - .Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e"))) - .AtPriority(999) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""data posted with FUNC 201""}")); - - server - .Given(Request.Create().WithPath("/data", "/ax").UsingPost().WithHeader("Content-Type", "application/json*")) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""data posted with 201""}")); - - server - .Given(Request.Create().WithPath("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""json posted with 201""}")); - - server - .Given(Request.Create().WithPath("/json2").UsingPost().WithBody("x")) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""json posted with x - 201""}")); - - server - .Given(Request.Create().WithPath("/data").UsingDelete()) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""data deleted with 200""}")); - - server - .Given(Request.Create().WithPath("/nobody").UsingGet()) - .RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1)) - .WithStatusCode(200)); - - server - .Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher(new [] { "cat", "dog" }))) - .RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200")); - - // http://localhost:8080/any/any?start=1000&stop=1&stop=2 - server - .Given(Request.Create().WithPath("/*").UsingGet()) - .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05") - .AtPriority(server.Mappings.Count() + 1) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") - .WithBody(@"{""msg"": ""Hello world CATCH-ALL on /*, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }") - .WithTransformer() - .WithDelay(TimeSpan.FromMilliseconds(100)) - ); - - Console.WriteLine("Press any key to stop the server"); - Console.ReadKey(); - server.Stop(); - - Console.WriteLine("Displaying all requests"); - var allRequests = server.LogEntries; - Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented)); - - Console.WriteLine("Press any key to quit"); - Console.ReadKey(); + MainApp.Run(); } } } \ No newline at end of file diff --git a/examples/WireMock.Net.ConsoleApplication/WireMock.Net.ConsoleApplication.csproj b/examples/WireMock.Net.ConsoleApplication/WireMock.Net.ConsoleApplication.csproj index d7fc210c..09ad09ce 100644 --- a/examples/WireMock.Net.ConsoleApplication/WireMock.Net.ConsoleApplication.csproj +++ b/examples/WireMock.Net.ConsoleApplication/WireMock.Net.ConsoleApplication.csproj @@ -52,6 +52,7 @@ +