From 4f7259d27aa3bfc8d3c518640ce857fd699e306f Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 7 Aug 2018 19:27:10 +0200 Subject: [PATCH 1/4] Fix JsonMatcher and JsonPathMatcher --- .../MainApp.cs | 24 +++++++++---------- src/WireMock.Net/Matchers/JSONPathMatcher.cs | 4 +++- src/WireMock.Net/Matchers/JsonMatcher.cs | 4 +++- .../Matchers/JsonMatcherTests.cs | 14 +++++++++++ .../Matchers/JsonPathMatcherTests.cs | 14 +++++++++++ 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs index a44bf4aa..a291198d 100644 --- a/examples/WireMock.Net.ConsoleApplication/MainApp.cs +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -142,11 +142,11 @@ namespace WireMock.Net.ConsoleApplication .WithBodyFromFile(@"c:\temp\x.json") ); - server - .Given(Request.Create().WithPath("/file_rel").UsingGet()) - .RespondWith(Response.Create() - .WithBodyFromFile("Program.cs", false) - ); + //server + // .Given(Request.Create().WithPath("/file_rel").UsingGet()) + // .RespondWith(Response.Create() + // .WithBodyFromFile("Program.cs", false) + // ); server .Given(Request.Create().WithHeader("ProxyThis", "true") @@ -176,13 +176,13 @@ namespace WireMock.Net.ConsoleApplication .WithStatusCode(200) .WithBody("hi")); - 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(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"))) diff --git a/src/WireMock.Net/Matchers/JSONPathMatcher.cs b/src/WireMock.Net/Matchers/JSONPathMatcher.cs index 5a4a6241..0b25790e 100644 --- a/src/WireMock.Net/Matchers/JSONPathMatcher.cs +++ b/src/WireMock.Net/Matchers/JSONPathMatcher.cs @@ -63,7 +63,9 @@ namespace WireMock.Matchers public double IsMatch(object input) { double match = MatchScores.Mismatch; - if (input != null) + + // When input is null or byte[], return Mismatch. + if (input != null && !(input is byte[])) { try { diff --git a/src/WireMock.Net/Matchers/JsonMatcher.cs b/src/WireMock.Net/Matchers/JsonMatcher.cs index d39477c3..4cfff630 100644 --- a/src/WireMock.Net/Matchers/JsonMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonMatcher.cs @@ -65,7 +65,9 @@ namespace WireMock.Matchers public double IsMatch(object input) { bool match = false; - if (input != null) + + // When input is null or byte[], return Mismatch. + if (input != null && !(input is byte[])) { try { diff --git a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs index 85bc18d6..0dca72b5 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs @@ -33,6 +33,20 @@ namespace WireMock.Net.Tests.Matchers Check.That(value).Equals("{}"); } + [Fact] + public void JsonMatcher_IsMatch_ByteArray() + { + // Assign + var bytes = new byte[0]; + var matcher = new JsonMatcher(""); + + // Act + double match = matcher.IsMatch(bytes); + + // Assert + Check.That(match).IsEqualTo(0); + } + [Fact] public void JsonMatcher_IsMatch_NullString() { diff --git a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs index 8d25cc1b..a1ffe2ce 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs @@ -33,6 +33,20 @@ namespace WireMock.Net.Tests.Matchers Check.That(patterns).ContainsExactly("X"); } + [Fact] + public void JsonPathMatcher_IsMatch_ByteArray() + { + // Assign + var bytes = new byte[0]; + var matcher = new JsonPathMatcher(""); + + // Act + double match = matcher.IsMatch(bytes); + + // Assert + Check.That(match).IsEqualTo(0); + } + [Fact] public void JsonPathMatcher_IsMatch_NullString() { From 36866d9fc30c3824c6652206f2a0ef60aa5679de Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 7 Aug 2018 20:34:35 +0200 Subject: [PATCH 2/4] #176 --- .../WireMock.Net.ConsoleApplication/MainApp.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs index a291198d..df2ede62 100644 --- a/examples/WireMock.Net.ConsoleApplication/MainApp.cs +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -53,7 +53,7 @@ namespace WireMock.Net.ConsoleApplication .WithPath("/proxy-execute-keep-alive") ) .RespondWith(Response.Create() - .WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999", BlackListedHeaders = new [] { "Keep-Alive" } }) + .WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999", BlackListedHeaders = new[] { "Keep-Alive" } }) .WithHeader("Keep-Alive-Test", "stef") ); @@ -142,11 +142,13 @@ namespace WireMock.Net.ConsoleApplication .WithBodyFromFile(@"c:\temp\x.json") ); - //server - // .Given(Request.Create().WithPath("/file_rel").UsingGet()) - // .RespondWith(Response.Create() - // .WithBodyFromFile("Program.cs", false) - // ); + server + .Given(Request.Create().WithPath("/file_rel").UsingGet()) + .WithGuid("0000aaaa-fcf4-4256-a0d3-1c76e4862947") + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/xml") + .WithBodyFromFile("WireMock.Net.xml", false) + ); server .Given(Request.Create().WithHeader("ProxyThis", "true") @@ -284,7 +286,7 @@ namespace WireMock.Net.ConsoleApplication .Given(Request.Create().WithPath("/jsonpathtestTokenJson").UsingPost()) .RespondWith(Response.Create() .WithHeader("Content-Type", "application/json") - .WithBodyAsJson(new { status = "OK", url = "{{request.url}}", transformed = "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" } ) + .WithBodyAsJson(new { status = "OK", url = "{{request.url}}", transformed = "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" }) .WithTransformer() ); From 5ee25fb1e7568819385b16cac9bc8ff8d5757f23 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Wed, 8 Aug 2018 08:20:52 +0200 Subject: [PATCH 3/4] #175 --- .../WireMock.Net.Console.NETCoreApp.csproj | 6 +- .../873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json | 19 + .../MainApp.cs | 536 +++++++++--------- .../WireMock.Net.Console.NET452.csproj | 3 + .../873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json | 19 + .../Serialization/MatcherMapper.cs | 6 +- .../Server/FluentMockServer.Admin.cs | 31 +- 7 files changed, 336 insertions(+), 284 deletions(-) create mode 100644 examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json create mode 100644 examples/WireMock.Net.ConsoleApplication/__admin/mappings/873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json 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 4b6c9735..bbe78311 100644 --- a/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj +++ b/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj @@ -6,16 +6,12 @@ ../../WireMock.Net-Logo.ico - - - - - + PreserveNewest diff --git a/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json new file mode 100644 index 00000000..dd501800 --- /dev/null +++ b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json @@ -0,0 +1,19 @@ +{ + "Guid": "873d495f-940e-4b86-a1f4-4f0fc7be8b8b", + "Priority": 4, + "Request": { + "Path": {}, + "Methods": [ + "get" + ] + }, + "Response": { + "StatusCode": 200, + "BodyDestination": "SameAsSource", + "Body": "NO PATH OR URL", + "UseTransformer": false, + "Headers": { + "Content-Type": "application/json" + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs index df2ede62..38555505 100644 --- a/examples/WireMock.Net.ConsoleApplication/MainApp.cs +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -39,300 +39,300 @@ namespace WireMock.Net.ConsoleApplication server.AllowPartialMapping(); server - .Given(Request.Create() - .UsingGet() - .WithPath("/proxy-test-keep-alive") - ) - .RespondWith(Response.Create() - .WithHeader("Keep-Alive", "timeout=1, max=1") - ); - - server - .Given(Request.Create() - .UsingGet() - .WithPath("/proxy-execute-keep-alive") - ) - .RespondWith(Response.Create() - .WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999", BlackListedHeaders = new[] { "Keep-Alive" } }) - .WithHeader("Keep-Alive-Test", "stef") - ); - - server - .Given(Request.Create() - .WithPath("/xpath").UsingPost() - .WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")) - ) - .RespondWith(Response.Create().WithBody("XPathMatcher!")); - - server - .Given(Request - .Create() - .WithPath("/jsonthings") - .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")) - .UsingPut()) - .RespondWith(Response.Create() - .WithBody(@"{ ""result"": ""JsonPathMatcher !!!""}")); - - server - .Given(Request - .Create() - .WithPath("/jsonbodytest1") - .WithBody(new JsonMatcher("{ \"x\": 42, \"s\": \"s\" }")) - .UsingPost()) - .WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2") - .RespondWith(Response.Create() - .WithBody(@"{ ""result"": ""jsonbodytest1"" }")); - - server - .Given(Request - .Create() - .WithPath("/jsonbodytest2") - .WithBody(new JsonMatcher(new { x = 42, s = "s" })) - .UsingPost()) - .WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f3") - .RespondWith(Response.Create() - .WithBody(@"{ ""result"": ""jsonbodytest2"" }")); - - server - .Given(Request - .Create() - .WithPath(new WildcardMatcher("/navision/OData/Company('My Company')/School*", true)) - .WithParam("$filter", "(substringof(Code, 'WA')") - .UsingGet()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""odata""}")); - - server - .Given(Request - .Create() - .WithPath(new WildcardMatcher("/param2", true)) - .WithParam("key", "test") - .UsingGet()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBodyAsJson(new { result = "param2" })); - - server - .Given(Request - .Create() - .WithPath(new WildcardMatcher("/param3", true)) - .WithParam("key", new WildcardMatcher("t*")) - .UsingGet()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBodyAsJson(new { result = "param3" })); - - server - .Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*")) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBodyAsJson(new { result = "data:headers posted with 201" })); - - server - .Given(Request.Create().WithPath("/file").UsingGet()) - .RespondWith(Response.Create() - .WithBodyFromFile(@"c:\temp\x.json", false) - ); - - server - .Given(Request.Create().WithPath("/filecache").UsingGet()) - .RespondWith(Response.Create() - .WithBodyFromFile(@"c:\temp\x.json") - ); - - server - .Given(Request.Create().WithPath("/file_rel").UsingGet()) - .WithGuid("0000aaaa-fcf4-4256-a0d3-1c76e4862947") - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/xml") - .WithBodyFromFile("WireMock.Net.xml", false) - ); - - server - .Given(Request.Create().WithHeader("ProxyThis", "true") - .UsingGet()) - .RespondWith(Response.Create() - .WithProxy("http://www.google.com") - ); - - server - .Given(Request.Create().WithPath("/bodyasbytes.png") - .UsingGet()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "image/png") - .WithBody(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMTczbp9jAAAAJ0lEQVQoU2NgUPuPD6Hz0RCEAtJoiAxpCCBXGgmRIo0TofORkdp/AMiMdRVnV6O0AAAAAElFTkSuQmCC")) - ); - - server - .Given(Request.Create().WithPath("/oauth2/access").UsingPost().WithBody("grant_type=password;username=u;password=p")) + .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) + .AtPriority(4) .RespondWith(Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "application/json") - .WithBodyAsJson(new { access_token = "AT", refresh_token = "RT" })); - - server - .Given(Request.Create().WithPath("/helloworld").UsingGet().WithHeader("Authorization", new RegexMatcher("^(?i)Bearer AT$"))) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithBody("hi")); + .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); //server - // .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) - // .AtPriority(4) + // .Given(Request.Create() + // .UsingGet() + // .WithPath("/proxy-test-keep-alive") + // ) + // .RespondWith(Response.Create() + // .WithHeader("Keep-Alive", "timeout=1, max=1") + // ); + + //server + // .Given(Request.Create() + // .UsingGet() + // .WithPath("/proxy-execute-keep-alive") + // ) + // .RespondWith(Response.Create() + // .WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999", BlackListedHeaders = new[] { "Keep-Alive" } }) + // .WithHeader("Keep-Alive-Test", "stef") + // ); + + //server + // .Given(Request.Create() + // .WithPath("/xpath").UsingPost() + // .WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")) + // ) + // .RespondWith(Response.Create().WithBody("XPathMatcher!")); + + //server + // .Given(Request + // .Create() + // .WithPath("/jsonthings") + // .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")) + // .UsingPut()) + // .RespondWith(Response.Create() + // .WithBody(@"{ ""result"": ""JsonPathMatcher !!!""}")); + + //server + // .Given(Request + // .Create() + // .WithPath("/jsonbodytest1") + // .WithBody(new JsonMatcher("{ \"x\": 42, \"s\": \"s\" }")) + // .UsingPost()) + // .WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2") + // .RespondWith(Response.Create() + // .WithBody(@"{ ""result"": ""jsonbodytest1"" }")); + + //server + // .Given(Request + // .Create() + // .WithPath("/jsonbodytest2") + // .WithBody(new JsonMatcher(new { x = 42, s = "s" })) + // .UsingPost()) + // .WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f3") + // .RespondWith(Response.Create() + // .WithBody(@"{ ""result"": ""jsonbodytest2"" }")); + + //server + // .Given(Request + // .Create() + // .WithPath(new WildcardMatcher("/navision/OData/Company('My Company')/School*", true)) + // .WithParam("$filter", "(substringof(Code, 'WA')") + // .UsingGet()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/json") + // .WithBody(@"{ ""result"": ""odata""}")); + + //server + // .Given(Request + // .Create() + // .WithPath(new WildcardMatcher("/param2", true)) + // .WithParam("key", "test") + // .UsingGet()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/json") + // .WithBodyAsJson(new { result = "param2" })); + + //server + // .Given(Request + // .Create() + // .WithPath(new WildcardMatcher("/param3", true)) + // .WithParam("key", new WildcardMatcher("t*")) + // .UsingGet()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/json") + // .WithBodyAsJson(new { result = "param3" })); + + //server + // .Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*")) + // .RespondWith(Response.Create() + // .WithStatusCode(201) + // .WithHeader("Content-Type", "application/json") + // .WithBodyAsJson(new { result = "data:headers posted with 201" })); + + //server + // .Given(Request.Create().WithPath("/file").UsingGet()) + // .RespondWith(Response.Create() + // .WithBodyFromFile(@"c:\temp\x.json", false) + // ); + + //server + // .Given(Request.Create().WithPath("/filecache").UsingGet()) + // .RespondWith(Response.Create() + // .WithBodyFromFile(@"c:\temp\x.json") + // ); + + //server + // .Given(Request.Create().WithPath("/file_rel").UsingGet()) + // .WithGuid("0000aaaa-fcf4-4256-a0d3-1c76e4862947") + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/xml") + // .WithBodyFromFile("WireMock.Net.xml", false) + // ); + + //server + // .Given(Request.Create().WithHeader("ProxyThis", "true") + // .UsingGet()) + // .RespondWith(Response.Create() + // .WithProxy("http://www.google.com") + //); + + //server + // .Given(Request.Create().WithPath("/bodyasbytes.png") + // .UsingGet()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "image/png") + // .WithBody(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMTczbp9jAAAAJ0lEQVQoU2NgUPuPD6Hz0RCEAtJoiAxpCCBXGgmRIo0TofORkdp/AMiMdRVnV6O0AAAAAElFTkSuQmCC")) + // ); + + //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") - // .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); + // .WithBodyAsJson(new { access_token = "AT", refresh_token = "RT" })); - server - .Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e"))) - .AtPriority(999) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBodyAsJson(new { result = "data posted with FUNC 201" })); + //server + // .Given(Request.Create().WithPath("/helloworld").UsingGet().WithHeader("Authorization", new RegexMatcher("^(?i)Bearer AT$"))) + // .RespondWith(Response.Create() + // .WithStatusCode(200) + // .WithBody("hi")); - 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("/data").UsingPost().WithBody(b => b.Contains("e"))) + // .AtPriority(999) + // .RespondWith(Response.Create() + // .WithStatusCode(201) + // .WithHeader("Content-Type", "application/json") + // .WithBodyAsJson(new { result = "data posted with FUNC 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("/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("/data").UsingDelete()) - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""data deleted with 200""}")); + //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("/needs-a-key") - .UsingGet() - .WithHeader("api-key", "*", MatchBehaviour.AcceptOnMatch) - .UsingAnyMethod()) - .RespondWith(Response.Create() - .WithStatusCode(HttpStatusCode.OK) - .WithBody(@"{ ""result"": ""api-key found""}")); + //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("/needs-a-key") - .UsingGet() - .WithHeader("api-key", "*", MatchBehaviour.RejectOnMatch) - .UsingAnyMethod()) - .RespondWith(Response.Create() - .WithStatusCode(HttpStatusCode.Unauthorized) - .WithBody(@"{ ""result"": ""api-key missing""}")); + //server + // .Given(Request.Create() + // .WithPath("/needs-a-key") + // .UsingGet() + // .WithHeader("api-key", "*", MatchBehaviour.AcceptOnMatch) + // .UsingAnyMethod()) + // .RespondWith(Response.Create() + // .WithStatusCode(HttpStatusCode.OK) + // .WithBody(@"{ ""result"": ""api-key found""}")); - server - .Given(Request.Create().WithPath("/nobody").UsingGet()) - .RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1)) - .WithStatusCode(200)); + //server + // .Given(Request.Create() + // .WithPath("/needs-a-key") + // .UsingGet() + // .WithHeader("api-key", "*", MatchBehaviour.RejectOnMatch) + // .UsingAnyMethod()) + // .RespondWith(Response.Create() + // .WithStatusCode(HttpStatusCode.Unauthorized) + // .WithBody(@"{ ""result"": ""api-key missing""}")); - server - .Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher(new[] { "cat", "dog" }))) - .RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200")); + //server + // .Given(Request.Create().WithPath("/nobody").UsingGet()) + // .RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1)) + // .WithStatusCode(200)); - // http://localhost:8080/trans?start=1000&stop=1&stop=2 - server - .Given(Request.Create().WithPath("/trans").UsingGet()) - .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05") - .RespondWith(Response.Create() - .WithStatusCode(200) - .WithHeader("Content-Type", "application/json") - .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") - .WithHeader("xyz_{{request.headers.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)) - ); + //server + // .Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher(new[] { "cat", "dog" }))) + // .RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200")); - server - .Given(Request.Create().WithPath("/jsonpathtestToken").UsingPost()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBody("{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}") - .WithTransformer() - ); + //// http://localhost:8080/trans?start=1000&stop=1&stop=2 + //server + // .Given(Request.Create().WithPath("/trans").UsingGet()) + // .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05") + // .RespondWith(Response.Create() + // .WithStatusCode(200) + // .WithHeader("Content-Type", "application/json") + // .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") + // .WithHeader("xyz_{{request.headers.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)) + // ); - server - .Given(Request.Create().WithPath("/zubinix").UsingPost()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBody("{ \"result\": \"{{JsonPath.SelectToken request.bodyAsJson \"username\"}}\" }") - .WithTransformer() - ); + //server + // .Given(Request.Create().WithPath("/jsonpathtestToken").UsingPost()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/json") + // .WithBody("{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}") + // .WithTransformer() + // ); - server - .Given(Request.Create().WithPath("/zubinix2").UsingPost()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBodyAsJson(new { path = "{{request.path}}", result = "{{JsonPath.SelectToken request.bodyAsJson \"username\"}}" }) - .WithTransformer() - ); + //server + // .Given(Request.Create().WithPath("/zubinix").UsingPost()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/json") + // .WithBody("{ \"result\": \"{{JsonPath.SelectToken request.bodyAsJson \"username\"}}\" }") + // .WithTransformer() + // ); - server - .Given(Request.Create().WithPath("/jsonpathtestTokenJson").UsingPost()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBodyAsJson(new { status = "OK", url = "{{request.url}}", transformed = "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" }) - .WithTransformer() - ); + //server + // .Given(Request.Create().WithPath("/zubinix2").UsingPost()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/json") + // .WithBodyAsJson(new { path = "{{request.path}}", result = "{{JsonPath.SelectToken request.bodyAsJson \"username\"}}" }) + // .WithTransformer() + // ); - server - .Given(Request.Create().WithPath("/jsonpathtestTokens").UsingPost()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBody("[{{#JsonPath.SelectTokens request.body \"$..Products[?(@.Price >= 50)].Name\"}} { \"idx\":{{id}}, \"value\":\"{{value}}\" }, {{/JsonPath.SelectTokens}} {} ]") - .WithTransformer() - ); + //server + // .Given(Request.Create().WithPath("/jsonpathtestTokenJson").UsingPost()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/json") + // .WithBodyAsJson(new { status = "OK", url = "{{request.url}}", transformed = "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" }) + // .WithTransformer() + // ); - server - .Given(Request.Create() - .WithPath("/state1") - .UsingGet()) - .InScenario("s1") - .WillSetStateTo("Test state 1") - .RespondWith(Response.Create() - .WithBody("No state msg 1")); + //server + // .Given(Request.Create().WithPath("/jsonpathtestTokens").UsingPost()) + // .RespondWith(Response.Create() + // .WithHeader("Content-Type", "application/json") + // .WithBody("[{{#JsonPath.SelectTokens request.body \"$..Products[?(@.Price >= 50)].Name\"}} { \"idx\":{{id}}, \"value\":\"{{value}}\" }, {{/JsonPath.SelectTokens}} {} ]") + // .WithTransformer() + // ); - server - .Given(Request.Create() - .WithPath("/foostate1") - .UsingGet()) - .InScenario("s1") - .WhenStateIs("Test state 1") - .RespondWith(Response.Create() - .WithBody("Test state msg 1")); + //server + // .Given(Request.Create() + // .WithPath("/state1") + // .UsingGet()) + // .InScenario("s1") + // .WillSetStateTo("Test state 1") + // .RespondWith(Response.Create() + // .WithBody("No state msg 1")); - server - .Given(Request.Create() - .WithPath("/state2") - .UsingGet()) - .InScenario("s2") - .WillSetStateTo("Test state 2") - .RespondWith(Response.Create() - .WithBody("No state msg 2")); + //server + // .Given(Request.Create() + // .WithPath("/foostate1") + // .UsingGet()) + // .InScenario("s1") + // .WhenStateIs("Test state 1") + // .RespondWith(Response.Create() + // .WithBody("Test state msg 1")); - server - .Given(Request.Create() - .WithPath("/foostate2") - .UsingGet()) - .InScenario("s2") - .WhenStateIs("Test state 2") - .RespondWith(Response.Create() - .WithBody("Test state msg 2")); + //server + // .Given(Request.Create() + // .WithPath("/state2") + // .UsingGet()) + // .InScenario("s2") + // .WillSetStateTo("Test state 2") + // .RespondWith(Response.Create() + // .WithBody("No state msg 2")); + + //server + // .Given(Request.Create() + // .WithPath("/foostate2") + // .UsingGet()) + // .InScenario("s2") + // .WhenStateIs("Test state 2") + // .RespondWith(Response.Create() + // .WithBody("Test state msg 2")); System.Console.WriteLine("Press any key to stop the server"); System.Console.ReadKey(); diff --git a/examples/WireMock.Net.ConsoleApplication/WireMock.Net.Console.NET452.csproj b/examples/WireMock.Net.ConsoleApplication/WireMock.Net.Console.NET452.csproj index 5f0a26a8..86f6fd3a 100644 --- a/examples/WireMock.Net.ConsoleApplication/WireMock.Net.Console.NET452.csproj +++ b/examples/WireMock.Net.ConsoleApplication/WireMock.Net.Console.NET452.csproj @@ -71,6 +71,9 @@ PreserveNewest + + PreserveNewest + diff --git a/examples/WireMock.Net.ConsoleApplication/__admin/mappings/873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json b/examples/WireMock.Net.ConsoleApplication/__admin/mappings/873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json new file mode 100644 index 00000000..dd501800 --- /dev/null +++ b/examples/WireMock.Net.ConsoleApplication/__admin/mappings/873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json @@ -0,0 +1,19 @@ +{ + "Guid": "873d495f-940e-4b86-a1f4-4f0fc7be8b8b", + "Priority": 4, + "Request": { + "Path": {}, + "Methods": [ + "get" + ] + }, + "Response": { + "StatusCode": 200, + "BodyDestination": "SameAsSource", + "Body": "NO PATH OR URL", + "UseTransformer": false, + "Headers": { + "Content-Type": "application/json" + } + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs index 2c69104b..8d513718 100644 --- a/src/WireMock.Net/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net/Serialization/MatcherMapper.cs @@ -21,7 +21,7 @@ namespace WireMock.Serialization string matcherName = parts[0]; string matcherType = parts.Length > 1 ? parts[1] : null; - string[] stringPatterns = matcher.Patterns != null ? matcher.Patterns.Cast().ToArray() : new [] { matcher.Pattern as string }; + string[] stringPatterns = matcher.Patterns != null ? matcher.Patterns.Cast().ToArray() : new[] { matcher.Pattern as string }; MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch; switch (matcherName) @@ -39,7 +39,7 @@ namespace WireMock.Serialization return new JsonPathMatcher(matchBehaviour, stringPatterns); case "XPathMatcher": - return new XPathMatcher(matchBehaviour, (string) matcher.Pattern); + return new XPathMatcher(matchBehaviour, (string)matcher.Pattern); case "WildcardMatcher": return new WildcardMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true); @@ -51,7 +51,7 @@ namespace WireMock.Serialization throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported."); } - return new SimMetricsMatcher(matchBehaviour, (string) matcher.Pattern, type); + return new SimMetricsMatcher(matchBehaviour, (string)matcher.Pattern, type); default: throw new NotSupportedException($"Matcher '{matcherName}' is not supported."); diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index c98fdd97..06655d01 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -316,9 +316,9 @@ namespace WireMock.Server Guid guid = Guid.Parse(requestMessage.Path.TrimStart(AdminMappings.ToCharArray())); var mappingModel = DeserializeObject(requestMessage); - DeserializeAndAddOrUpdateMapping(mappingModel, guid); + Guid? guidFromPut = DeserializeAndAddOrUpdateMapping(mappingModel, guid); - return ResponseMessageBuilder.Create("Mapping added or updated", 200, guid); + return ResponseMessageBuilder.Create("Mapping added or updated", 200, guidFromPut); } private ResponseMessage MappingDelete(RequestMessage requestMessage) @@ -401,13 +401,18 @@ namespace WireMock.Server return ResponseMessageBuilder.Create("Mapping added", 201, guid); } - private Guid DeserializeAndAddOrUpdateMapping(MappingModel mappingModel, Guid? guid = null, string path = null) + private Guid? DeserializeAndAddOrUpdateMapping(MappingModel mappingModel, Guid? guid = null, string path = null) { Check.NotNull(mappingModel, nameof(mappingModel)); Check.NotNull(mappingModel.Request, nameof(mappingModel.Request)); Check.NotNull(mappingModel.Response, nameof(mappingModel.Response)); - var requestBuilder = InitRequestBuilder(mappingModel.Request); + var requestBuilder = InitRequestBuilder(mappingModel.Request, true); + if (requestBuilder == null) + { + return null; + } + var responseBuilder = InitResponseBuilder(mappingModel.Response); var respondProvider = Given(requestBuilder); @@ -511,7 +516,7 @@ namespace WireMock.Server { var requestModel = DeserializeObject(requestMessage); - var request = (Request)InitRequestBuilder(requestModel); + var request = (Request)InitRequestBuilder(requestModel, false); var dict = new Dictionary(); foreach (var logEntry in LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/"))) @@ -551,7 +556,7 @@ namespace WireMock.Server } #endregion - private IRequestBuilder InitRequestBuilder(RequestModel requestModel) + private IRequestBuilder InitRequestBuilder(RequestModel requestModel, bool pathOrUrlRequired) { IRequestBuilder requestBuilder = Request.Create(); @@ -571,11 +576,13 @@ namespace WireMock.Server } } + bool pathOrUrlmatchersValid = false; if (requestModel.Path != null) { if (requestModel.Path is string path) { requestBuilder = requestBuilder.WithPath(path); + pathOrUrlmatchersValid = true; } else { @@ -583,15 +590,16 @@ namespace WireMock.Server if (pathModel?.Matchers != null) { requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(MatcherMapper.Map).Cast().ToArray()); + pathOrUrlmatchersValid = true; } } } - - if (requestModel.Url != null) + else if (requestModel.Url != null) { if (requestModel.Url is string url) { requestBuilder = requestBuilder.WithUrl(url); + pathOrUrlmatchersValid = true; } else { @@ -599,10 +607,17 @@ namespace WireMock.Server if (urlModel?.Matchers != null) { requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(MatcherMapper.Map).Cast().ToArray()); + pathOrUrlmatchersValid = true; } } } + if (pathOrUrlRequired && !pathOrUrlmatchersValid) + { + _logger.Error("Path or Url matcher is missing for this mapping, this mapping will not be added."); + return null; + } + if (requestModel.Methods != null) { requestBuilder = requestBuilder.UsingMethod(requestModel.Methods); From 9e7d3b6d2d2aaa8cf5d757b13784cb9a7e3ba835 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Wed, 8 Aug 2018 11:05:27 +0200 Subject: [PATCH 4/4] #177 --- .../791a3f31-6946-4ce7-8e6f-0237c7443275.json | 9 +- .../MainApp.cs | 506 +++++++++--------- .../Server/FluentMockServer.Admin.cs | 10 +- 3 files changed, 263 insertions(+), 262 deletions(-) diff --git a/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json index 35a1c17b..d7a6cbdd 100644 --- a/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json +++ b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json @@ -3,14 +3,7 @@ "Title": "", "Priority": 0, "Request": { - "Path": { - "Matchers": [ - { - "Name": "WildcardMatcher", - "Pattern": "/proxy-google-test-post" - } - ] - }, + "Path": "/proxy-google-test-post", "Methods": [ "post" ], diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs index 38555505..965b058b 100644 --- a/examples/WireMock.Net.ConsoleApplication/MainApp.cs +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -46,293 +46,293 @@ namespace WireMock.Net.ConsoleApplication .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); - //server - // .Given(Request.Create() - // .UsingGet() - // .WithPath("/proxy-test-keep-alive") - // ) - // .RespondWith(Response.Create() - // .WithHeader("Keep-Alive", "timeout=1, max=1") - // ); + server + .Given(Request.Create() + .UsingGet() + .WithPath("/proxy-test-keep-alive") + ) + .RespondWith(Response.Create() + .WithHeader("Keep-Alive", "timeout=1, max=1") + ); - //server - // .Given(Request.Create() - // .UsingGet() - // .WithPath("/proxy-execute-keep-alive") - // ) - // .RespondWith(Response.Create() - // .WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999", BlackListedHeaders = new[] { "Keep-Alive" } }) - // .WithHeader("Keep-Alive-Test", "stef") - // ); + server + .Given(Request.Create() + .UsingGet() + .WithPath("/proxy-execute-keep-alive") + ) + .RespondWith(Response.Create() + .WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999", BlackListedHeaders = new[] { "Keep-Alive" } }) + .WithHeader("Keep-Alive-Test", "stef") + ); - //server - // .Given(Request.Create() - // .WithPath("/xpath").UsingPost() - // .WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")) - // ) - // .RespondWith(Response.Create().WithBody("XPathMatcher!")); + server + .Given(Request.Create() + .WithPath("/xpath").UsingPost() + .WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")) + ) + .RespondWith(Response.Create().WithBody("XPathMatcher!")); - //server - // .Given(Request - // .Create() - // .WithPath("/jsonthings") - // .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")) - // .UsingPut()) - // .RespondWith(Response.Create() - // .WithBody(@"{ ""result"": ""JsonPathMatcher !!!""}")); + server + .Given(Request + .Create() + .WithPath("/jsonthings") + .WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")) + .UsingPut()) + .RespondWith(Response.Create() + .WithBody(@"{ ""result"": ""JsonPathMatcher !!!""}")); - //server - // .Given(Request - // .Create() - // .WithPath("/jsonbodytest1") - // .WithBody(new JsonMatcher("{ \"x\": 42, \"s\": \"s\" }")) - // .UsingPost()) - // .WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2") - // .RespondWith(Response.Create() - // .WithBody(@"{ ""result"": ""jsonbodytest1"" }")); + server + .Given(Request + .Create() + .WithPath("/jsonbodytest1") + .WithBody(new JsonMatcher("{ \"x\": 42, \"s\": \"s\" }")) + .UsingPost()) + .WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2") + .RespondWith(Response.Create() + .WithBody(@"{ ""result"": ""jsonbodytest1"" }")); - //server - // .Given(Request - // .Create() - // .WithPath("/jsonbodytest2") - // .WithBody(new JsonMatcher(new { x = 42, s = "s" })) - // .UsingPost()) - // .WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f3") - // .RespondWith(Response.Create() - // .WithBody(@"{ ""result"": ""jsonbodytest2"" }")); + server + .Given(Request + .Create() + .WithPath("/jsonbodytest2") + .WithBody(new JsonMatcher(new { x = 42, s = "s" })) + .UsingPost()) + .WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f3") + .RespondWith(Response.Create() + .WithBody(@"{ ""result"": ""jsonbodytest2"" }")); - //server - // .Given(Request - // .Create() - // .WithPath(new WildcardMatcher("/navision/OData/Company('My Company')/School*", true)) - // .WithParam("$filter", "(substringof(Code, 'WA')") - // .UsingGet()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/json") - // .WithBody(@"{ ""result"": ""odata""}")); + server + .Given(Request + .Create() + .WithPath(new WildcardMatcher("/navision/OData/Company('My Company')/School*", true)) + .WithParam("$filter", "(substringof(Code, 'WA')") + .UsingGet()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBody(@"{ ""result"": ""odata""}")); - //server - // .Given(Request - // .Create() - // .WithPath(new WildcardMatcher("/param2", true)) - // .WithParam("key", "test") - // .UsingGet()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/json") - // .WithBodyAsJson(new { result = "param2" })); + server + .Given(Request + .Create() + .WithPath(new WildcardMatcher("/param2", true)) + .WithParam("key", "test") + .UsingGet()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new { result = "param2" })); - //server - // .Given(Request - // .Create() - // .WithPath(new WildcardMatcher("/param3", true)) - // .WithParam("key", new WildcardMatcher("t*")) - // .UsingGet()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/json") - // .WithBodyAsJson(new { result = "param3" })); + server + .Given(Request + .Create() + .WithPath(new WildcardMatcher("/param3", true)) + .WithParam("key", new WildcardMatcher("t*")) + .UsingGet()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new { result = "param3" })); - //server - // .Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*")) - // .RespondWith(Response.Create() - // .WithStatusCode(201) - // .WithHeader("Content-Type", "application/json") - // .WithBodyAsJson(new { result = "data:headers posted with 201" })); + server + .Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*")) + .RespondWith(Response.Create() + .WithStatusCode(201) + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new { result = "data:headers posted with 201" })); - //server - // .Given(Request.Create().WithPath("/file").UsingGet()) - // .RespondWith(Response.Create() - // .WithBodyFromFile(@"c:\temp\x.json", false) - // ); + server + .Given(Request.Create().WithPath("/file").UsingGet()) + .RespondWith(Response.Create() + .WithBodyFromFile(@"c:\temp\x.json", false) + ); - //server - // .Given(Request.Create().WithPath("/filecache").UsingGet()) - // .RespondWith(Response.Create() - // .WithBodyFromFile(@"c:\temp\x.json") - // ); + server + .Given(Request.Create().WithPath("/filecache").UsingGet()) + .RespondWith(Response.Create() + .WithBodyFromFile(@"c:\temp\x.json") + ); - //server - // .Given(Request.Create().WithPath("/file_rel").UsingGet()) - // .WithGuid("0000aaaa-fcf4-4256-a0d3-1c76e4862947") - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/xml") - // .WithBodyFromFile("WireMock.Net.xml", false) - // ); + server + .Given(Request.Create().WithPath("/file_rel").UsingGet()) + .WithGuid("0000aaaa-fcf4-4256-a0d3-1c76e4862947") + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/xml") + .WithBodyFromFile("WireMock.Net.xml", false) + ); - //server - // .Given(Request.Create().WithHeader("ProxyThis", "true") - // .UsingGet()) - // .RespondWith(Response.Create() - // .WithProxy("http://www.google.com") - //); + server + .Given(Request.Create().WithHeader("ProxyThis", "true") + .UsingGet()) + .RespondWith(Response.Create() + .WithProxy("http://www.google.com") + ); - //server - // .Given(Request.Create().WithPath("/bodyasbytes.png") - // .UsingGet()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "image/png") - // .WithBody(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMTczbp9jAAAAJ0lEQVQoU2NgUPuPD6Hz0RCEAtJoiAxpCCBXGgmRIo0TofORkdp/AMiMdRVnV6O0AAAAAElFTkSuQmCC")) - // ); + server + .Given(Request.Create().WithPath("/bodyasbytes.png") + .UsingGet()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "image/png") + .WithBody(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMTczbp9jAAAAJ0lEQVQoU2NgUPuPD6Hz0RCEAtJoiAxpCCBXGgmRIo0TofORkdp/AMiMdRVnV6O0AAAAAElFTkSuQmCC")) + ); - //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("/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("/helloworld").UsingGet().WithHeader("Authorization", new RegexMatcher("^(?i)Bearer AT$"))) - // .RespondWith(Response.Create() - // .WithStatusCode(200) - // .WithBody("hi")); + server + .Given(Request.Create().WithPath("/helloworld").UsingGet().WithHeader("Authorization", new RegexMatcher("^(?i)Bearer AT$"))) + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithBody("hi")); - //server - // .Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e"))) - // .AtPriority(999) - // .RespondWith(Response.Create() - // .WithStatusCode(201) - // .WithHeader("Content-Type", "application/json") - // .WithBodyAsJson(new { result = "data posted with FUNC 201" })); + server + .Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e"))) + .AtPriority(999) + .RespondWith(Response.Create() + .WithStatusCode(201) + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new { result = "data posted with FUNC 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("/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("/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("/data").UsingDelete()) + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithHeader("Content-Type", "application/json") + .WithBody(@"{ ""result"": ""data deleted with 200""}")); - //server - // .Given(Request.Create() - // .WithPath("/needs-a-key") - // .UsingGet() - // .WithHeader("api-key", "*", MatchBehaviour.AcceptOnMatch) - // .UsingAnyMethod()) - // .RespondWith(Response.Create() - // .WithStatusCode(HttpStatusCode.OK) - // .WithBody(@"{ ""result"": ""api-key found""}")); + server + .Given(Request.Create() + .WithPath("/needs-a-key") + .UsingGet() + .WithHeader("api-key", "*", MatchBehaviour.AcceptOnMatch) + .UsingAnyMethod()) + .RespondWith(Response.Create() + .WithStatusCode(HttpStatusCode.OK) + .WithBody(@"{ ""result"": ""api-key found""}")); - //server - // .Given(Request.Create() - // .WithPath("/needs-a-key") - // .UsingGet() - // .WithHeader("api-key", "*", MatchBehaviour.RejectOnMatch) - // .UsingAnyMethod()) - // .RespondWith(Response.Create() - // .WithStatusCode(HttpStatusCode.Unauthorized) - // .WithBody(@"{ ""result"": ""api-key missing""}")); + server + .Given(Request.Create() + .WithPath("/needs-a-key") + .UsingGet() + .WithHeader("api-key", "*", MatchBehaviour.RejectOnMatch) + .UsingAnyMethod()) + .RespondWith(Response.Create() + .WithStatusCode(HttpStatusCode.Unauthorized) + .WithBody(@"{ ""result"": ""api-key missing""}")); - //server - // .Given(Request.Create().WithPath("/nobody").UsingGet()) - // .RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1)) - // .WithStatusCode(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")); + server + .Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher(new[] { "cat", "dog" }))) + .RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200")); - //// http://localhost:8080/trans?start=1000&stop=1&stop=2 - //server - // .Given(Request.Create().WithPath("/trans").UsingGet()) - // .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05") - // .RespondWith(Response.Create() - // .WithStatusCode(200) - // .WithHeader("Content-Type", "application/json") - // .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") - // .WithHeader("xyz_{{request.headers.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)) - // ); + // http://localhost:8080/trans?start=1000&stop=1&stop=2 + server + .Given(Request.Create().WithPath("/trans").UsingGet()) + .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05") + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithHeader("Content-Type", "application/json") + .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") + .WithHeader("xyz_{{request.headers.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)) + ); - //server - // .Given(Request.Create().WithPath("/jsonpathtestToken").UsingPost()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/json") - // .WithBody("{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}") - // .WithTransformer() - // ); + server + .Given(Request.Create().WithPath("/jsonpathtestToken").UsingPost()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBody("{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}") + .WithTransformer() + ); - //server - // .Given(Request.Create().WithPath("/zubinix").UsingPost()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/json") - // .WithBody("{ \"result\": \"{{JsonPath.SelectToken request.bodyAsJson \"username\"}}\" }") - // .WithTransformer() - // ); + server + .Given(Request.Create().WithPath("/zubinix").UsingPost()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBody("{ \"result\": \"{{JsonPath.SelectToken request.bodyAsJson \"username\"}}\" }") + .WithTransformer() + ); - //server - // .Given(Request.Create().WithPath("/zubinix2").UsingPost()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/json") - // .WithBodyAsJson(new { path = "{{request.path}}", result = "{{JsonPath.SelectToken request.bodyAsJson \"username\"}}" }) - // .WithTransformer() - // ); + server + .Given(Request.Create().WithPath("/zubinix2").UsingPost()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new { path = "{{request.path}}", result = "{{JsonPath.SelectToken request.bodyAsJson \"username\"}}" }) + .WithTransformer() + ); - //server - // .Given(Request.Create().WithPath("/jsonpathtestTokenJson").UsingPost()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/json") - // .WithBodyAsJson(new { status = "OK", url = "{{request.url}}", transformed = "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" }) - // .WithTransformer() - // ); + server + .Given(Request.Create().WithPath("/jsonpathtestTokenJson").UsingPost()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new { status = "OK", url = "{{request.url}}", transformed = "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" }) + .WithTransformer() + ); - //server - // .Given(Request.Create().WithPath("/jsonpathtestTokens").UsingPost()) - // .RespondWith(Response.Create() - // .WithHeader("Content-Type", "application/json") - // .WithBody("[{{#JsonPath.SelectTokens request.body \"$..Products[?(@.Price >= 50)].Name\"}} { \"idx\":{{id}}, \"value\":\"{{value}}\" }, {{/JsonPath.SelectTokens}} {} ]") - // .WithTransformer() - // ); + server + .Given(Request.Create().WithPath("/jsonpathtestTokens").UsingPost()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBody("[{{#JsonPath.SelectTokens request.body \"$..Products[?(@.Price >= 50)].Name\"}} { \"idx\":{{id}}, \"value\":\"{{value}}\" }, {{/JsonPath.SelectTokens}} {} ]") + .WithTransformer() + ); - //server - // .Given(Request.Create() - // .WithPath("/state1") - // .UsingGet()) - // .InScenario("s1") - // .WillSetStateTo("Test state 1") - // .RespondWith(Response.Create() - // .WithBody("No state msg 1")); + server + .Given(Request.Create() + .WithPath("/state1") + .UsingGet()) + .InScenario("s1") + .WillSetStateTo("Test state 1") + .RespondWith(Response.Create() + .WithBody("No state msg 1")); - //server - // .Given(Request.Create() - // .WithPath("/foostate1") - // .UsingGet()) - // .InScenario("s1") - // .WhenStateIs("Test state 1") - // .RespondWith(Response.Create() - // .WithBody("Test state msg 1")); + server + .Given(Request.Create() + .WithPath("/foostate1") + .UsingGet()) + .InScenario("s1") + .WhenStateIs("Test state 1") + .RespondWith(Response.Create() + .WithBody("Test state msg 1")); - //server - // .Given(Request.Create() - // .WithPath("/state2") - // .UsingGet()) - // .InScenario("s2") - // .WillSetStateTo("Test state 2") - // .RespondWith(Response.Create() - // .WithBody("No state msg 2")); + server + .Given(Request.Create() + .WithPath("/state2") + .UsingGet()) + .InScenario("s2") + .WillSetStateTo("Test state 2") + .RespondWith(Response.Create() + .WithBody("No state msg 2")); - //server - // .Given(Request.Create() - // .WithPath("/foostate2") - // .UsingGet()) - // .InScenario("s2") - // .WhenStateIs("Test state 2") - // .RespondWith(Response.Create() - // .WithBody("Test state msg 2")); + server + .Given(Request.Create() + .WithPath("/foostate2") + .UsingGet()) + .InScenario("s2") + .WhenStateIs("Test state 2") + .RespondWith(Response.Create() + .WithBody("Test state msg 2")); System.Console.WriteLine("Press any key to stop the server"); System.Console.ReadKey(); diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 06655d01..fd176d73 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -121,7 +121,15 @@ namespace WireMock.Server foreach (string filename in Directory.EnumerateFiles(folder).OrderBy(f => f)) { _logger.Info("Reading Static MappingFile : '{0}'", filename); - ReadStaticMappingAndAddOrUpdate(filename); + + try + { + ReadStaticMappingAndAddOrUpdate(filename); + } + catch + { + _logger.Error("Static MappingFile : '{0}' could not be read. This file will be skipped.", filename); + } } }