From 1b2d20fd69414723f32912b6ea00e94bd0b86859 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 20 Jan 2017 15:01:06 +0100 Subject: [PATCH] Refactor fluent interfaces --- .../Program.cs | 26 +- src/WireMock/HttpListenerRequestMapper.cs | 2 +- .../Request/RequestMessageCompositeMatcher.cs | 2 +- .../RequestBuilders/IAndPathRequestBuilder.cs | 6 + .../IUrlAndPathRequestBuilder.cs | 39 +++ src/WireMock/RequestBuilders/Request.cs | 103 +++----- .../ResponseBuilders/IBodyResponseBuilder.cs | 10 +- .../ResponseBuilders/IDelayResponseBuilder.cs | 10 +- .../IHeadersResponseBuilder.cs | 18 +- .../ResponseBuilders/IResponseBuilder.cs | 2 +- .../IStatusCodeResponseBuilder.cs | 40 +++ .../ITransformResponseBuilder.cs | 6 +- src/WireMock/ResponseBuilders/Response.cs | 232 +++++++++--------- .../FluentMockServerTests.cs | 33 ++- test/WireMock.Net.Tests/RequestTests.cs | 52 ++-- 15 files changed, 308 insertions(+), 273 deletions(-) create mode 100644 src/WireMock/RequestBuilders/IAndPathRequestBuilder.cs create mode 100644 src/WireMock/RequestBuilders/IUrlAndPathRequestBuilder.cs create mode 100644 src/WireMock/ResponseBuilders/IStatusCodeResponseBuilder.cs diff --git a/examples/WireMock.Net.ConsoleApplication/Program.cs b/examples/WireMock.Net.ConsoleApplication/Program.cs index e60cb32a..9ef66261 100644 --- a/examples/WireMock.Net.ConsoleApplication/Program.cs +++ b/examples/WireMock.Net.ConsoleApplication/Program.cs @@ -20,48 +20,48 @@ namespace WireMock.Net.ConsoleApplication Console.WriteLine("FluentMockServer running at {0}", server.Port); server - .Given(Request.WithUrl(u => u.Contains("x")).UsingGet()) - .RespondWith(Response + .Given(Request.Create().WithUrl(u => u.Contains("x")).UsingGet()) + .RespondWith(Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""/x with FUNC 200""}")); // http://localhost:8080/gffgfgf/sddsds?start=1000&stop=1&stop=2 server - .Given(Request.WithUrl("/*").UsingGet()) - .RespondWith(Response + .Given(Request.Create().WithUrl("/*").UsingGet()) + .RespondWith(Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "application/json") .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") .WithBody(@"{""msg"": ""Hello world! : {{request.url}} : {{request.path}} : {{request.query.start}} : {{request.query.stop.[0]}}""") - .AfterDelay(TimeSpan.FromMilliseconds(100)) .WithTransformer() + .WithDelay(TimeSpan.FromMilliseconds(100)) ); server - .Given(Request.WithUrl("/data").UsingPost().WithBody(b => b.Contains("e"))) - .RespondWith(Response + .Given(Request.Create().WithUrl("/data").UsingPost().WithBody(b => b.Contains("e"))) + .RespondWith(Response.Create() .WithStatusCode(201) .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""data posted with FUNC 201""}")); server - .Given(Request.WithUrl("/data").UsingPost()) - .RespondWith(Response + .Given(Request.Create().WithUrl("/data").UsingPost()) + .RespondWith(Response.Create() .WithStatusCode(201) .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""data posted with 201""}")); server - .Given(Request.WithUrl("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))) - .RespondWith(Response + .Given(Request.Create().WithUrl("/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.WithUrl("/data").UsingDelete()) - .RespondWith(Response + .Given(Request.Create().WithUrl("/data").UsingDelete()) + .RespondWith(Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""data deleted with 200""}")); diff --git a/src/WireMock/HttpListenerRequestMapper.cs b/src/WireMock/HttpListenerRequestMapper.cs index ea820679..3f5d35cd 100644 --- a/src/WireMock/HttpListenerRequestMapper.cs +++ b/src/WireMock/HttpListenerRequestMapper.cs @@ -28,7 +28,7 @@ namespace WireMock /// The listener request. /// /// - /// The . + /// The . /// public RequestMessage Map(HttpListenerRequest listenerRequest) { diff --git a/src/WireMock/Matchers/Request/RequestMessageCompositeMatcher.cs b/src/WireMock/Matchers/Request/RequestMessageCompositeMatcher.cs index a7d574f5..54682b5f 100644 --- a/src/WireMock/Matchers/Request/RequestMessageCompositeMatcher.cs +++ b/src/WireMock/Matchers/Request/RequestMessageCompositeMatcher.cs @@ -6,7 +6,7 @@ namespace WireMock.Matchers.Request /// /// The composite request matcher. /// - public class RequestMessageCompositeMatcher : IRequestMatcher + public abstract class RequestMessageCompositeMatcher : IRequestMatcher { private readonly IEnumerable _requestMatchers; diff --git a/src/WireMock/RequestBuilders/IAndPathRequestBuilder.cs b/src/WireMock/RequestBuilders/IAndPathRequestBuilder.cs new file mode 100644 index 00000000..55d0f5a8 --- /dev/null +++ b/src/WireMock/RequestBuilders/IAndPathRequestBuilder.cs @@ -0,0 +1,6 @@ +namespace WireMock.RequestBuilders +{ + public interface IRequestBuilder : IUrlAndPathRequestBuilder + { + } +} diff --git a/src/WireMock/RequestBuilders/IUrlAndPathRequestBuilder.cs b/src/WireMock/RequestBuilders/IUrlAndPathRequestBuilder.cs new file mode 100644 index 00000000..532136f8 --- /dev/null +++ b/src/WireMock/RequestBuilders/IUrlAndPathRequestBuilder.cs @@ -0,0 +1,39 @@ +using System; +using JetBrains.Annotations; + +namespace WireMock.RequestBuilders +{ + /// + /// IUrlAndPathRequestBuilder + /// + public interface IUrlAndPathRequestBuilder : IVerbRequestBuilder + { + /// + /// The with url. + /// + /// The url. + /// The . + IUrlAndPathRequestBuilder WithUrl([NotNull] string url); + + /// + /// The with url. + /// + /// The url func. + /// The . + IUrlAndPathRequestBuilder WithUrl([NotNull] Func func); + + /// + /// The with path. + /// + /// The path. + /// The . + IUrlAndPathRequestBuilder WithPath([NotNull] string path); + + /// + /// The with path. + /// + /// The path func. + /// The . + IUrlAndPathRequestBuilder WithPath([NotNull] Func func); + } +} \ No newline at end of file diff --git a/src/WireMock/RequestBuilders/Request.cs b/src/WireMock/RequestBuilders/Request.cs index f541d046..3def9ecf 100644 --- a/src/WireMock/RequestBuilders/Request.cs +++ b/src/WireMock/RequestBuilders/Request.cs @@ -1,47 +1,32 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Linq; using JetBrains.Annotations; using WireMock.Matchers; using WireMock.Matchers.Request; -[module: - SuppressMessage("StyleCop.CSharp.ReadabilityRules", - "SA1101:PrefixLocalCallsWithThis", - Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")] -[module: - SuppressMessage("StyleCop.CSharp.ReadabilityRules", - "SA1126:PrefixCallsCorrectly", - Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")] -[module: - SuppressMessage("StyleCop.CSharp.NamingRules", - "SA1309:FieldNamesMustNotBeginWithUnderscore", - Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")] -[module: - SuppressMessage("StyleCop.CSharp.DocumentationRules", - "SA1633:FileMustHaveHeader", - Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")] -// ReSharper disable ArrangeThisQualifier -// ReSharper disable InconsistentNaming namespace WireMock.RequestBuilders { /// /// The requests. /// - public class Request : RequestMessageCompositeMatcher, IVerbRequestBuilder + public class Request : RequestMessageCompositeMatcher, IRequestBuilder { - /// - /// The _request matchers. - /// private readonly IList _requestMatchers; + /// + /// Creates this instance. + /// + /// The . + public static IRequestBuilder Create() + { + return new Request(new List()); + } + /// /// Initializes a new instance of the class. /// - /// - /// The request matchers. - /// + /// The request matchers. private Request(IList requestMatchers) : base(requestMatchers) { _requestMatchers = requestMatchers; @@ -50,65 +35,45 @@ namespace WireMock.RequestBuilders /// /// The with url. /// - /// - /// The url. - /// - /// - /// The . - /// - public static IVerbRequestBuilder WithUrl(string url) + /// The url. + /// The . + public IUrlAndPathRequestBuilder WithUrl(string url) { - var specs = new List { new RequestMessageUrlMatcher(url) }; - - return new Request(specs); + _requestMatchers.Add(new RequestMessageUrlMatcher(url)); + return this; } /// /// The with url. /// - /// - /// The url func. - /// - /// - /// The . - /// - public static IVerbRequestBuilder WithUrl(Func func) + /// The url func. + /// The . + public IUrlAndPathRequestBuilder WithUrl(Func func) { - var specs = new List { new RequestMessageUrlMatcher(func) }; - - return new Request(specs); + _requestMatchers.Add(new RequestMessageUrlMatcher(func)); + return this; } /// /// The with path. /// - /// - /// The path. - /// - /// - /// The . - /// - public static IVerbRequestBuilder WithPath(string path) + /// The path. + /// The . + public IUrlAndPathRequestBuilder WithPath(string path) { - var specs = new List { new RequestMessagePathMatcher(path) }; - - return new Request(specs); + _requestMatchers.Add(new RequestMessagePathMatcher(path)); + return this; } /// /// The with path. /// - /// - /// The path func. - /// - /// - /// The . - /// - public static IVerbRequestBuilder WithPath([NotNull] Func func) + /// The path func. + /// The . + public IUrlAndPathRequestBuilder WithPath(Func func) { - var specs = new List { new RequestMessagePathMatcher(func) }; - - return new Request(specs); + _requestMatchers.Add(new RequestMessagePathMatcher(func)); + return this; } /// @@ -179,6 +144,12 @@ namespace WireMock.RequestBuilders /// public IHeadersRequestBuilder UsingAnyVerb() { + var matchers = _requestMatchers.Where(m => m is RequestMessageVerbMatcher).ToList(); + foreach (var matcher in matchers) + { + _requestMatchers.Remove(matcher); + } + return this; } diff --git a/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs index 11d7bd43..e40ba557 100644 --- a/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs @@ -6,21 +6,21 @@ namespace WireMock.ResponseBuilders /// /// The BodyResponseBuilder interface. /// - public interface IBodyResponseBuilder : IDelayResponseBuilder + public interface IBodyResponseBuilder : ITransformResponseBuilder { /// /// The with body. /// /// The body. - /// A . - IResponseBuilder WithBody([NotNull] string body); + /// A . + ITransformResponseBuilder WithBody([NotNull] string body); /// /// The with body as base64. /// /// The body asbase64. /// The Encoding. - /// A . - IResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null); + /// A . + ITransformResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null); } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/IDelayResponseBuilder.cs b/src/WireMock/ResponseBuilders/IDelayResponseBuilder.cs index 37e5115e..11b48917 100644 --- a/src/WireMock/ResponseBuilders/IDelayResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/IDelayResponseBuilder.cs @@ -10,12 +10,8 @@ namespace WireMock.ResponseBuilders /// /// The after delay. /// - /// - /// The delay. - /// - /// - /// The . - /// - IResponseBuilder AfterDelay(TimeSpan delay); + /// The delay. + /// The . + IResponseBuilder WithDelay(TimeSpan delay); } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/IHeadersResponseBuilder.cs b/src/WireMock/ResponseBuilders/IHeadersResponseBuilder.cs index cf87e2c5..3d62b949 100644 --- a/src/WireMock/ResponseBuilders/IHeadersResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/IHeadersResponseBuilder.cs @@ -1,4 +1,6 @@ -namespace WireMock.ResponseBuilders +using JetBrains.Annotations; + +namespace WireMock.ResponseBuilders { /// /// The HeadersResponseBuilder interface. @@ -8,15 +10,9 @@ /// /// The with header. /// - /// - /// The name. - /// - /// - /// The value. - /// - /// - /// The . - /// - IResponseBuilder WithHeader(string name, string value); + /// The name. + /// The value. + /// The . + IHeadersResponseBuilder WithHeader([NotNull] string name, string value); } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/IResponseBuilder.cs b/src/WireMock/ResponseBuilders/IResponseBuilder.cs index 6a115a26..276cdd63 100644 --- a/src/WireMock/ResponseBuilders/IResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/IResponseBuilder.cs @@ -3,7 +3,7 @@ /// /// The ResponseBuilder interface. /// - public interface IResponseBuilder : ITransformResponseBuilder + public interface IResponseBuilder : IStatusCodeResponseBuilder { } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/IStatusCodeResponseBuilder.cs b/src/WireMock/ResponseBuilders/IStatusCodeResponseBuilder.cs new file mode 100644 index 00000000..64ae0461 --- /dev/null +++ b/src/WireMock/ResponseBuilders/IStatusCodeResponseBuilder.cs @@ -0,0 +1,40 @@ +using System.Net; + +namespace WireMock.ResponseBuilders +{ + /// + /// The StatusCodeResponseBuilder interface. + /// + public interface IStatusCodeResponseBuilder : IHeadersResponseBuilder + { + /// + /// The with status code. + /// + /// + /// The code. + /// + /// The . + IHeadersResponseBuilder WithStatusCode(int code); + + /// + /// The with status code. + /// + /// + /// The code. + /// + /// The . + IHeadersResponseBuilder WithStatusCode(HttpStatusCode code); + + /// + /// The with Success status code (200). + /// + /// The . + IHeadersResponseBuilder WithSuccess(); + + /// + /// The with NotFound status code (404). + /// + /// The . + IHeadersResponseBuilder WithNotFound(); + } +} \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs b/src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs index 8c0b3d5b..ff4725a2 100644 --- a/src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs @@ -3,14 +3,14 @@ /// /// The BodyResponseBuilder interface. /// - public interface ITransformResponseBuilder : IHeadersResponseBuilder + public interface ITransformResponseBuilder : IDelayResponseBuilder { /// /// The with transformer. /// /// - /// The . + /// The . /// - IResponseBuilder WithTransformer(); + IDelayResponseBuilder WithTransformer(); } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/Response.cs b/src/WireMock/ResponseBuilders/Response.cs index 6aafd92a..ff7095c0 100644 --- a/src/WireMock/ResponseBuilders/Response.cs +++ b/src/WireMock/ResponseBuilders/Response.cs @@ -1,45 +1,33 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; +using System.Net; using System.Text; using System.Threading.Tasks; -using System.Xml; using HandlebarsDotNet; using JetBrains.Annotations; using WireMock.Validation; -[module: - SuppressMessage("StyleCop.CSharp.ReadabilityRules", - "SA1101:PrefixLocalCallsWithThis", - Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")] -[module: - SuppressMessage("StyleCop.CSharp.NamingRules", - "SA1309:FieldNamesMustNotBeginWithUnderscore", - Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")] -[module: - SuppressMessage("StyleCop.CSharp.DocumentationRules", - "SA1633:FileMustHaveHeader", - Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")] -// ReSharper disable ArrangeThisQualifier -// ReSharper disable InconsistentNaming namespace WireMock.ResponseBuilders { /// - /// The responses. + /// The Response. /// public class Response : IResponseBuilder { - /// - /// The _response. - /// private readonly ResponseMessage _responseMessage; + private TimeSpan _delay = TimeSpan.Zero; + private bool _useTransformer; /// - /// The _delay. + /// Creates this instance. /// - private TimeSpan _delay = TimeSpan.Zero; - - private bool _useTransformer; + /// A . + [PublicAPI] + public static IResponseBuilder Create([CanBeNull] ResponseMessage responseMessage = null) + { + var message = responseMessage ?? new ResponseMessage { StatusCode = (int) HttpStatusCode.OK }; + return new Response(message); + } /// /// Initializes a new instance of the class. @@ -47,42 +35,120 @@ namespace WireMock.ResponseBuilders /// /// The response. /// - public Response(ResponseMessage responseMessage) + private Response(ResponseMessage responseMessage) { _responseMessage = responseMessage; } /// - /// The with Success status code. + /// The with status code. /// - /// The . - public static IResponseBuilder WithSuccess() + /// The code. + /// A .\ + [PublicAPI] + public IHeadersResponseBuilder WithStatusCode(int code) { - return WithStatusCode(200); - } - - /// - /// The with NotFound status code. - /// - /// The . - public static IResponseBuilder WithNotFound() - { - return WithStatusCode(404); + _responseMessage.StatusCode = code; + return this; } /// /// The with status code. /// - /// - /// The code. - /// + /// The code. + /// A . + [PublicAPI] + public IHeadersResponseBuilder WithStatusCode(HttpStatusCode code) + { + return WithStatusCode((int) code); + } + + /// + /// The with Success status code (200). + /// + /// + [PublicAPI] + public IHeadersResponseBuilder WithSuccess() + { + return WithStatusCode((int) HttpStatusCode.OK); + } + + /// + /// The with NotFound status code (404). + /// + /// The . + [PublicAPI] + public IHeadersResponseBuilder WithNotFound() + { + return WithStatusCode((int)HttpStatusCode.NotFound); + } + + /// + /// The with header. + /// + /// The name. + /// The value. + /// The . + public IHeadersResponseBuilder WithHeader(string name, string value) + { + Check.NotNull(name, nameof(name)); + + _responseMessage.AddHeader(name, value); + return this; + } + + /// + /// The with body. + /// + /// The body. + /// A . + public ITransformResponseBuilder WithBody(string body) + { + Check.NotNull(body, nameof(body)); + + _responseMessage.Body = body; + return this; + } + + /// + /// The with body as base64. + /// + /// The body asbase64. + /// The Encoding. + /// A . + public ITransformResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null) + { + Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); + + _responseMessage.Body = (encoding ?? Encoding.UTF8).GetString(Convert.FromBase64String(bodyAsbase64)); + return this; + } + + /// + /// The with transformer. + /// /// /// The . /// - public static IResponseBuilder WithStatusCode(int code) + public IDelayResponseBuilder WithTransformer() { - var response = new ResponseMessage { StatusCode = code }; - return new Response(response); + _useTransformer = true; + return this; + } + + /// + /// The after delay. + /// + /// + /// The delay. + /// + /// + /// The . + /// + public IResponseBuilder WithDelay(TimeSpan delay) + { + _delay = delay; + return this; } /// @@ -120,81 +186,5 @@ namespace WireMock.ResponseBuilders return _responseMessage; } - - /// - /// The with header. - /// - /// - /// The name. - /// - /// - /// The value. - /// - /// - /// The . - /// - public IResponseBuilder WithHeader(string name, string value) - { - _responseMessage.AddHeader(name, value); - return this; - } - - /// - /// The with body. - /// - /// - /// The body. - /// - /// - /// The . - /// - public IResponseBuilder WithBody(string body) - { - Check.NotNull(body, nameof(body)); - - _responseMessage.Body = body; - return this; - } - - /// - /// The with body as base64. - /// - /// The body asbase64. - /// - /// A . - public IResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null) - { - Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); - - _responseMessage.Body = (encoding ?? Encoding.UTF8).GetString(Convert.FromBase64String(bodyAsbase64)); - return this; - } - - /// - /// The with transformer. - /// - /// - /// The . - /// - public IResponseBuilder WithTransformer() - { - _useTransformer = true; - return this; - } - - /// - /// The after delay. - /// - /// - /// The delay. - /// - /// - /// The . - /// - public IResponseBuilder AfterDelay(TimeSpan delay) - { - _delay = delay; - return this; - } } -} +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.cs b/test/WireMock.Net.Tests/FluentMockServerTests.cs index 54de33df..64d250e1 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs @@ -44,10 +44,10 @@ namespace WireMock.Net.Tests _server = FluentMockServer.Start(); _server - .Given(Request + .Given(Request.Create() .WithUrl("/foo") .UsingGet()) - .RespondWith(Response + .RespondWith(Response.Create() .WithStatusCode(200) .WithBody(@"{ msg: ""Hello world!""}")); @@ -64,7 +64,7 @@ namespace WireMock.Net.Tests // given _server = FluentMockServer.Start(); - _server.Given(Request.WithUrl("/foo").UsingGet()).RespondWith(Response.WithSuccess().WithBodyAsBase64("SGVsbG8gV29ybGQ/")); + _server.Given(Request.Create().WithUrl("/foo").UsingGet()).RespondWith(Response.Create().WithBodyAsBase64("SGVsbG8gV29ybGQ/")); // when var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo"); @@ -114,7 +114,7 @@ namespace WireMock.Net.Tests await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/bar"); // then - var result = _server.SearchLogsFor(Request.WithUrl("/b.*")).ToList(); + var result = _server.SearchLogsFor(Request.Create().WithUrl("/b.*")).ToList(); Check.That(result).HasSize(1); var requestLogged = result.First(); @@ -143,11 +143,10 @@ namespace WireMock.Net.Tests _server = FluentMockServer.Start(); _server - .Given(Request + .Given(Request.Create() .WithUrl("/foo") .UsingGet()) - .RespondWith(Response - .WithStatusCode(200) + .RespondWith(Response.Create() .WithBody(@"{ msg: ""Hello world!""}")); // when @@ -165,17 +164,17 @@ namespace WireMock.Net.Tests _server = FluentMockServer.Start(); _server - .Given(Request + .Given(Request.Create() .WithUrl("/foo") .UsingGet()) - .RespondWith(Response + .RespondWith(Response.Create() .WithStatusCode(307) .WithHeader("Location", "/bar")); _server - .Given(Request + .Given(Request.Create() .WithUrl("/bar") .UsingGet()) - .RespondWith(Response + .RespondWith(Response.Create() .WithStatusCode(200) .WithBody("REDIRECT SUCCESSFUL")); @@ -193,12 +192,11 @@ namespace WireMock.Net.Tests _server = FluentMockServer.Start(); _server - .Given(Request + .Given(Request.Create() .WithUrl("/*")) - .RespondWith(Response - .WithStatusCode(200) + .RespondWith(Response.Create() .WithBody(@"{ msg: ""Hello world!""}") - .AfterDelay(TimeSpan.FromMilliseconds(2000))); + .WithDelay(TimeSpan.FromMilliseconds(2000))); // when var watch = new Stopwatch(); @@ -217,10 +215,9 @@ namespace WireMock.Net.Tests _server = FluentMockServer.Start(); _server.AddRequestProcessingDelay(TimeSpan.FromMilliseconds(2000)); _server - .Given(Request + .Given(Request.Create() .WithUrl("/*")) - .RespondWith(Response - .WithStatusCode(200) + .RespondWith(Response.Create() .WithBody(@"{ msg: ""Hello world!""}")); // when diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index 2fee6684..084fa1bf 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -15,7 +15,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_url() { // given - var spec = Request.WithUrl("/foo"); + var spec = Request.Create().WithUrl("/foo"); // when string bodyAsString = "whatever"; @@ -30,7 +30,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_url_prefix() { // given - var spec = Request.WithUrl("/foo*"); + var spec = Request.Create().WithUrl("/foo*"); // when string bodyAsString = "whatever"; @@ -45,7 +45,7 @@ namespace WireMock.Net.Tests public void Should_exclude_requests_not_matching_given_url() { // given - var spec = Request.WithUrl("/foo"); + var spec = Request.Create().WithUrl("/foo"); // when string bodyAsString = "whatever"; @@ -60,7 +60,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_path() { // given - var spec = Request.WithPath("/foo"); + var spec = Request.Create().WithPath("/foo"); // when string bodyAsString = "whatever"; @@ -75,7 +75,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_url_and_method_put() { // given - var spec = Request.WithUrl("/foo").UsingPut(); + var spec = Request.Create().WithUrl("/foo").UsingPut(); // when string bodyAsString = "whatever"; @@ -90,7 +90,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_url_and_method_post() { // given - var spec = Request.WithUrl("/foo").UsingPost(); + var spec = Request.Create().WithUrl("/foo").UsingPost(); // when string bodyAsString = "whatever"; @@ -105,7 +105,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_url_and_method_get() { // given - var spec = Request.WithUrl("/foo").UsingGet(); + var spec = Request.Create().WithUrl("/foo").UsingGet(); // when string bodyAsString = "whatever"; @@ -120,7 +120,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_url_and_method_delete() { // given - var spec = Request.WithUrl("/foo").UsingDelete(); + var spec = Request.Create().WithUrl("/foo").UsingDelete(); // when string bodyAsString = "whatever"; @@ -135,7 +135,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_url_and_method_head() { // given - var spec = Request.WithUrl("/foo").UsingHead(); + var spec = Request.Create().WithUrl("/foo").UsingHead(); // when string bodyAsString = "whatever"; @@ -150,7 +150,7 @@ namespace WireMock.Net.Tests public void Should_exclude_requests_matching_given_url_but_not_http_method() { // given - var spec = Request.WithUrl("/foo").UsingPut(); + var spec = Request.Create().WithUrl("/foo").UsingPut(); // when string bodyAsString = "whatever"; @@ -165,7 +165,7 @@ namespace WireMock.Net.Tests public void Should_exclude_requests_matching_given_http_method_but_not_url() { // given - var spec = Request.WithUrl("/bar").UsingPut(); + var spec = Request.Create().WithUrl("/bar").UsingPut(); // when string bodyAsString = "whatever"; @@ -180,7 +180,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_url_and_headers() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata"); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata"); // when string bodyAsString = "whatever"; @@ -195,7 +195,7 @@ namespace WireMock.Net.Tests public void Should_exclude_requests_not_matching_given_headers() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata"); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata"); // when string bodyAsString = "whatever"; @@ -210,7 +210,7 @@ namespace WireMock.Net.Tests public void Should_exclude_requests_not_matching_given_headers_ignorecase() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "abc", false); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "abc", false); // when string bodyAsString = "whatever"; @@ -225,7 +225,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_header_prefix() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*"); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*"); // when string bodyAsString = "whatever"; @@ -240,7 +240,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_body() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("Hello world!"); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody("Hello world!"); // when string bodyAsString = "Hello world!"; @@ -255,7 +255,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_body_as_regex() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o"); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody("H.*o"); // when string bodyAsString = "Hello world!"; @@ -270,7 +270,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_body_as_regexmatcher() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(new RegexMatcher("H.*o")); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody(new RegexMatcher("H.*o")); // when string bodyAsString = "Hello world!"; @@ -285,7 +285,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_body_as_xpathmatcher_true() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")); // when string xmlBodyAsString = @" @@ -305,7 +305,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_body_as_xpathmatcher_false() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]")); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]")); // when string xmlBodyAsString = @" @@ -325,7 +325,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_body_as_jsonpathmatcher_true() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); // when string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; @@ -340,7 +340,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_body_as_jsonpathmatcher_false() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); // when string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"; @@ -355,7 +355,7 @@ namespace WireMock.Net.Tests public void Should_exclude_requests_not_matching_given_body() { // given - var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! "); + var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! "); // when string bodyAsString = "xxx"; @@ -370,7 +370,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_params() { // given - var spec = Request.WithPath("/foo").WithParam("bar", "1", "2"); + var spec = Request.Create().WithPath("/foo").WithParam("bar", "1", "2"); // when string bodyAsString = "Hello world!"; @@ -385,7 +385,7 @@ namespace WireMock.Net.Tests public void Should_specify_requests_matching_given_params_func() { // given - var spec = Request.WithPath("/foo").UsingAnyVerb().WithParam(p => p.ContainsKey("bar")); + var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithParam(p => p.ContainsKey("bar")); // when string bodyAsString = "Hello world!"; @@ -400,7 +400,7 @@ namespace WireMock.Net.Tests public void Should_exclude_requests_not_matching_given_params() { // given - var spec = Request.WithPath("/foo").WithParam("bar", "1"); + var spec = Request.Create().WithPath("/foo").WithParam("bar", "1"); // when string bodyAsString = "Hello world!";