mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Refactor fluent interfaces
This commit is contained in:
@@ -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""}"));
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace WireMock
|
||||
/// The listener request.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="Request"/>.
|
||||
/// The <see cref="AndPathRequest"/>.
|
||||
/// </returns>
|
||||
public RequestMessage Map(HttpListenerRequest listenerRequest)
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace WireMock.Matchers.Request
|
||||
/// <summary>
|
||||
/// The composite request matcher.
|
||||
/// </summary>
|
||||
public class RequestMessageCompositeMatcher : IRequestMatcher
|
||||
public abstract class RequestMessageCompositeMatcher : IRequestMatcher
|
||||
{
|
||||
private readonly IEnumerable<IRequestMatcher> _requestMatchers;
|
||||
|
||||
|
||||
6
src/WireMock/RequestBuilders/IAndPathRequestBuilder.cs
Normal file
6
src/WireMock/RequestBuilders/IAndPathRequestBuilder.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace WireMock.RequestBuilders
|
||||
{
|
||||
public interface IRequestBuilder : IUrlAndPathRequestBuilder
|
||||
{
|
||||
}
|
||||
}
|
||||
39
src/WireMock/RequestBuilders/IUrlAndPathRequestBuilder.cs
Normal file
39
src/WireMock/RequestBuilders/IUrlAndPathRequestBuilder.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace WireMock.RequestBuilders
|
||||
{
|
||||
/// <summary>
|
||||
/// IUrlAndPathRequestBuilder
|
||||
/// </summary>
|
||||
public interface IUrlAndPathRequestBuilder : IVerbRequestBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// The with url.
|
||||
/// </summary>
|
||||
/// <param name="url">The url.</param>
|
||||
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
|
||||
IUrlAndPathRequestBuilder WithUrl([NotNull] string url);
|
||||
|
||||
/// <summary>
|
||||
/// The with url.
|
||||
/// </summary>
|
||||
/// <param name="func">The url func.</param>
|
||||
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
|
||||
IUrlAndPathRequestBuilder WithUrl([NotNull] Func<string, bool> func);
|
||||
|
||||
/// <summary>
|
||||
/// The with path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
|
||||
IUrlAndPathRequestBuilder WithPath([NotNull] string path);
|
||||
|
||||
/// <summary>
|
||||
/// The with path.
|
||||
/// </summary>
|
||||
/// <param name="func">The path func.</param>
|
||||
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
|
||||
IUrlAndPathRequestBuilder WithPath([NotNull] Func<string, bool> func);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// The requests.
|
||||
/// </summary>
|
||||
public class Request : RequestMessageCompositeMatcher, IVerbRequestBuilder
|
||||
public class Request : RequestMessageCompositeMatcher, IRequestBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// The _request matchers.
|
||||
/// </summary>
|
||||
private readonly IList<IRequestMatcher> _requestMatchers;
|
||||
|
||||
/// <summary>
|
||||
/// Creates this instance.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||
public static IRequestBuilder Create()
|
||||
{
|
||||
return new Request(new List<IRequestMatcher>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Request"/> class.
|
||||
/// </summary>
|
||||
/// <param name="requestMatchers">
|
||||
/// The request matchers.
|
||||
/// </param>
|
||||
/// <param name="requestMatchers">The request matchers.</param>
|
||||
private Request(IList<IRequestMatcher> requestMatchers) : base(requestMatchers)
|
||||
{
|
||||
_requestMatchers = requestMatchers;
|
||||
@@ -50,65 +35,45 @@ namespace WireMock.RequestBuilders
|
||||
/// <summary>
|
||||
/// The with url.
|
||||
/// </summary>
|
||||
/// <param name="url">
|
||||
/// The url.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IVerbRequestBuilder"/>.
|
||||
/// </returns>
|
||||
public static IVerbRequestBuilder WithUrl(string url)
|
||||
/// <param name="url">The url.</param>
|
||||
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
|
||||
public IUrlAndPathRequestBuilder WithUrl(string url)
|
||||
{
|
||||
var specs = new List<IRequestMatcher> { new RequestMessageUrlMatcher(url) };
|
||||
|
||||
return new Request(specs);
|
||||
_requestMatchers.Add(new RequestMessageUrlMatcher(url));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with url.
|
||||
/// </summary>
|
||||
/// <param name="func">
|
||||
/// The url func.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IVerbRequestBuilder"/>.
|
||||
/// </returns>
|
||||
public static IVerbRequestBuilder WithUrl(Func<string, bool> func)
|
||||
/// <param name="func">The url func.</param>
|
||||
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
|
||||
public IUrlAndPathRequestBuilder WithUrl(Func<string, bool> func)
|
||||
{
|
||||
var specs = new List<IRequestMatcher> { new RequestMessageUrlMatcher(func) };
|
||||
|
||||
return new Request(specs);
|
||||
_requestMatchers.Add(new RequestMessageUrlMatcher(func));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with path.
|
||||
/// </summary>
|
||||
/// <param name="path">
|
||||
/// The path.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IVerbRequestBuilder"/>.
|
||||
/// </returns>
|
||||
public static IVerbRequestBuilder WithPath(string path)
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
|
||||
public IUrlAndPathRequestBuilder WithPath(string path)
|
||||
{
|
||||
var specs = new List<IRequestMatcher> { new RequestMessagePathMatcher(path) };
|
||||
|
||||
return new Request(specs);
|
||||
_requestMatchers.Add(new RequestMessagePathMatcher(path));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with path.
|
||||
/// </summary>
|
||||
/// <param name="func">
|
||||
/// The path func.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IVerbRequestBuilder"/>.
|
||||
/// </returns>
|
||||
public static IVerbRequestBuilder WithPath([NotNull] Func<string, bool> func)
|
||||
/// <param name="func">The path func.</param>
|
||||
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
|
||||
public IUrlAndPathRequestBuilder WithPath(Func<string, bool> func)
|
||||
{
|
||||
var specs = new List<IRequestMatcher> { new RequestMessagePathMatcher(func) };
|
||||
|
||||
return new Request(specs);
|
||||
_requestMatchers.Add(new RequestMessagePathMatcher(func));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -179,6 +144,12 @@ namespace WireMock.RequestBuilders
|
||||
/// </returns>
|
||||
public IHeadersRequestBuilder UsingAnyVerb()
|
||||
{
|
||||
var matchers = _requestMatchers.Where(m => m is RequestMessageVerbMatcher).ToList();
|
||||
foreach (var matcher in matchers)
|
||||
{
|
||||
_requestMatchers.Remove(matcher);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,21 +6,21 @@ namespace WireMock.ResponseBuilders
|
||||
/// <summary>
|
||||
/// The BodyResponseBuilder interface.
|
||||
/// </summary>
|
||||
public interface IBodyResponseBuilder : IDelayResponseBuilder
|
||||
public interface IBodyResponseBuilder : ITransformResponseBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// </summary>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithBody([NotNull] string body);
|
||||
/// <returns>A <see cref="ITransformResponseBuilder"/>.</returns>
|
||||
ITransformResponseBuilder WithBody([NotNull] string body);
|
||||
|
||||
/// <summary>
|
||||
/// The with body as base64.
|
||||
/// </summary>
|
||||
/// <param name="bodyAsbase64">The body asbase64.</param>
|
||||
/// <param name="encoding">The Encoding.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null);
|
||||
/// <returns>A <see cref="ITransformResponseBuilder"/>.</returns>
|
||||
ITransformResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null);
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,8 @@ namespace WireMock.ResponseBuilders
|
||||
/// <summary>
|
||||
/// The after delay.
|
||||
/// </summary>
|
||||
/// <param name="delay">
|
||||
/// The delay.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IResponseBuilder"/>.
|
||||
/// </returns>
|
||||
IResponseBuilder AfterDelay(TimeSpan delay);
|
||||
/// <param name="delay">The delay.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithDelay(TimeSpan delay);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace WireMock.ResponseBuilders
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace WireMock.ResponseBuilders
|
||||
{
|
||||
/// <summary>
|
||||
/// The HeadersResponseBuilder interface.
|
||||
@@ -8,15 +10,9 @@
|
||||
/// <summary>
|
||||
/// The with header.
|
||||
/// </summary>
|
||||
/// <param name="name">
|
||||
/// The name.
|
||||
/// </param>
|
||||
/// <param name="value">
|
||||
/// The value.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IResponseBuilder"/>.
|
||||
/// </returns>
|
||||
IResponseBuilder WithHeader(string name, string value);
|
||||
/// <param name="name">The name.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns>The <see cref="IHeadersResponseBuilder"/>.</returns>
|
||||
IHeadersResponseBuilder WithHeader([NotNull] string name, string value);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// The ResponseBuilder interface.
|
||||
/// </summary>
|
||||
public interface IResponseBuilder : ITransformResponseBuilder
|
||||
public interface IResponseBuilder : IStatusCodeResponseBuilder
|
||||
{
|
||||
}
|
||||
}
|
||||
40
src/WireMock/ResponseBuilders/IStatusCodeResponseBuilder.cs
Normal file
40
src/WireMock/ResponseBuilders/IStatusCodeResponseBuilder.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Net;
|
||||
|
||||
namespace WireMock.ResponseBuilders
|
||||
{
|
||||
/// <summary>
|
||||
/// The StatusCodeResponseBuilder interface.
|
||||
/// </summary>
|
||||
public interface IStatusCodeResponseBuilder : IHeadersResponseBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// The with status code.
|
||||
/// </summary>
|
||||
/// <param name="code">
|
||||
/// The code.
|
||||
/// </param>
|
||||
/// <returns>The <see cref="IHeadersResponseBuilder"/>.</returns>
|
||||
IHeadersResponseBuilder WithStatusCode(int code);
|
||||
|
||||
/// <summary>
|
||||
/// The with status code.
|
||||
/// </summary>
|
||||
/// <param name="code">
|
||||
/// The code.
|
||||
/// </param>
|
||||
/// <returns>The <see cref="IHeadersResponseBuilder"/>.</returns>
|
||||
IHeadersResponseBuilder WithStatusCode(HttpStatusCode code);
|
||||
|
||||
/// <summary>
|
||||
/// The with Success status code (200).
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
IHeadersResponseBuilder WithSuccess();
|
||||
|
||||
/// <summary>
|
||||
/// The with NotFound status code (404).
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
IHeadersResponseBuilder WithNotFound();
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,14 @@
|
||||
/// <summary>
|
||||
/// The BodyResponseBuilder interface.
|
||||
/// </summary>
|
||||
public interface ITransformResponseBuilder : IHeadersResponseBuilder
|
||||
public interface ITransformResponseBuilder : IDelayResponseBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// The with transformer.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="IResponseBuilder"/>.
|
||||
/// The <see cref="IDelayResponseBuilder"/>.
|
||||
/// </returns>
|
||||
IResponseBuilder WithTransformer();
|
||||
IDelayResponseBuilder WithTransformer();
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// The responses.
|
||||
/// The Response.
|
||||
/// </summary>
|
||||
public class Response : IResponseBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// The _response.
|
||||
/// </summary>
|
||||
private readonly ResponseMessage _responseMessage;
|
||||
private TimeSpan _delay = TimeSpan.Zero;
|
||||
private bool _useTransformer;
|
||||
|
||||
/// <summary>
|
||||
/// The _delay.
|
||||
/// Creates this instance.
|
||||
/// </summary>
|
||||
private TimeSpan _delay = TimeSpan.Zero;
|
||||
|
||||
private bool _useTransformer;
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static IResponseBuilder Create([CanBeNull] ResponseMessage responseMessage = null)
|
||||
{
|
||||
var message = responseMessage ?? new ResponseMessage { StatusCode = (int) HttpStatusCode.OK };
|
||||
return new Response(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Response"/> class.
|
||||
@@ -47,42 +35,120 @@ namespace WireMock.ResponseBuilders
|
||||
/// <param name="responseMessage">
|
||||
/// The response.
|
||||
/// </param>
|
||||
public Response(ResponseMessage responseMessage)
|
||||
private Response(ResponseMessage responseMessage)
|
||||
{
|
||||
_responseMessage = responseMessage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with Success status code.
|
||||
/// The with status code.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
public static IResponseBuilder WithSuccess()
|
||||
/// <param name="code">The code.</param>
|
||||
/// <returns>A <see cref="IHeadersResponseBuilder"/>.</returns>\
|
||||
[PublicAPI]
|
||||
public IHeadersResponseBuilder WithStatusCode(int code)
|
||||
{
|
||||
return WithStatusCode(200);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with NotFound status code.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
public static IResponseBuilder WithNotFound()
|
||||
{
|
||||
return WithStatusCode(404);
|
||||
_responseMessage.StatusCode = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with status code.
|
||||
/// </summary>
|
||||
/// <param name="code">
|
||||
/// The code.
|
||||
/// </param>
|
||||
/// <param name="code">The code.</param>
|
||||
/// <returns>A <see cref="IHeadersResponseBuilder"/>.</returns>
|
||||
[PublicAPI]
|
||||
public IHeadersResponseBuilder WithStatusCode(HttpStatusCode code)
|
||||
{
|
||||
return WithStatusCode((int) code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with Success status code (200).
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public IHeadersResponseBuilder WithSuccess()
|
||||
{
|
||||
return WithStatusCode((int) HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with NotFound status code (404).
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
[PublicAPI]
|
||||
public IHeadersResponseBuilder WithNotFound()
|
||||
{
|
||||
return WithStatusCode((int)HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with header.
|
||||
/// </summary>
|
||||
/// <param name="name">The name.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns>The <see cref="IHeadersResponseBuilder"/>.</returns>
|
||||
public IHeadersResponseBuilder WithHeader(string name, string value)
|
||||
{
|
||||
Check.NotNull(name, nameof(name));
|
||||
|
||||
_responseMessage.AddHeader(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// </summary>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <returns>A <see cref="ITransformResponseBuilder"/>.</returns>
|
||||
public ITransformResponseBuilder WithBody(string body)
|
||||
{
|
||||
Check.NotNull(body, nameof(body));
|
||||
|
||||
_responseMessage.Body = body;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with body as base64.
|
||||
/// </summary>
|
||||
/// <param name="bodyAsbase64">The body asbase64.</param>
|
||||
/// <param name="encoding">The Encoding.</param>
|
||||
/// <returns>A <see cref="ITransformResponseBuilder"/>.</returns>
|
||||
public ITransformResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null)
|
||||
{
|
||||
Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
|
||||
|
||||
_responseMessage.Body = (encoding ?? Encoding.UTF8).GetString(Convert.FromBase64String(bodyAsbase64));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with transformer.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="IResponseBuilder"/>.
|
||||
/// </returns>
|
||||
public static IResponseBuilder WithStatusCode(int code)
|
||||
public IDelayResponseBuilder WithTransformer()
|
||||
{
|
||||
var response = new ResponseMessage { StatusCode = code };
|
||||
return new Response(response);
|
||||
_useTransformer = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The after delay.
|
||||
/// </summary>
|
||||
/// <param name="delay">
|
||||
/// The delay.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IProvideResponses"/>.
|
||||
/// </returns>
|
||||
public IResponseBuilder WithDelay(TimeSpan delay)
|
||||
{
|
||||
_delay = delay;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -120,81 +186,5 @@ namespace WireMock.ResponseBuilders
|
||||
|
||||
return _responseMessage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with header.
|
||||
/// </summary>
|
||||
/// <param name="name">
|
||||
/// The name.
|
||||
/// </param>
|
||||
/// <param name="value">
|
||||
/// The value.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IResponseBuilder"/>.
|
||||
/// </returns>
|
||||
public IResponseBuilder WithHeader(string name, string value)
|
||||
{
|
||||
_responseMessage.AddHeader(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IResponseBuilder"/>.
|
||||
/// </returns>
|
||||
public IResponseBuilder WithBody(string body)
|
||||
{
|
||||
Check.NotNull(body, nameof(body));
|
||||
|
||||
_responseMessage.Body = body;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with body as base64.
|
||||
/// </summary>
|
||||
/// <param name="bodyAsbase64">The body asbase64.</param>
|
||||
/// <param name="encoding"></param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
public IResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null)
|
||||
{
|
||||
Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
|
||||
|
||||
_responseMessage.Body = (encoding ?? Encoding.UTF8).GetString(Convert.FromBase64String(bodyAsbase64));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with transformer.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="IResponseBuilder"/>.
|
||||
/// </returns>
|
||||
public IResponseBuilder WithTransformer()
|
||||
{
|
||||
_useTransformer = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The after delay.
|
||||
/// </summary>
|
||||
/// <param name="delay">
|
||||
/// The delay.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IProvideResponses"/>.
|
||||
/// </returns>
|
||||
public IResponseBuilder AfterDelay(TimeSpan delay)
|
||||
{
|
||||
_delay = delay;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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!";
|
||||
|
||||
Reference in New Issue
Block a user