Move some classes and restructure.

This commit is contained in:
Stef Heyenrath
2017-01-20 12:07:29 +01:00
parent 847745c256
commit e2552f03b9
28 changed files with 471 additions and 583 deletions

View File

@@ -3,6 +3,7 @@ using Newtonsoft.Json;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.ResponseBuilders; using WireMock.ResponseBuilders;
using WireMock.Server;
namespace WireMock.Net.ConsoleApplication namespace WireMock.Net.ConsoleApplication

View File

@@ -1,57 +0,0 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
[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
{
/// <summary>
/// The composite request spec.
/// </summary>
public class CompositeRequestSpec : ISpecifyRequests
{
/// <summary>
/// The _request specs.
/// </summary>
private readonly IEnumerable<ISpecifyRequests> _requestSpecs;
/// <summary>
/// Initializes a new instance of the <see cref="CompositeRequestSpec"/> class.
/// The constructor.
/// </summary>
/// <param name="requestSpecs">
/// The <see cref="IEnumerable&lt;ISpecifyRequests&gt;"/> request specs.
/// </param>
public CompositeRequestSpec(IEnumerable<ISpecifyRequests> requestSpecs)
{
_requestSpecs = requestSpecs;
}
/// <summary>
/// The is satisfied by.
/// </summary>
/// <param name="requestMessage">
/// The request.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool IsSatisfiedBy(RequestMessage requestMessage)
{
return _requestSpecs.All(spec => spec.IsSatisfiedBy(requestMessage));
}
}
}

View File

@@ -1,23 +1,8 @@
using System; using System;
using System.Diagnostics.CodeAnalysis;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
[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.Http namespace WireMock.Http
{ {
/// <summary> /// <summary>
@@ -25,21 +10,20 @@ namespace WireMock.Http
/// </summary> /// </summary>
public class TinyHttpServer public class TinyHttpServer
{ {
/// <summary>
/// The _http handler.
/// </summary>
private readonly Action<HttpListenerContext> _httpHandler; private readonly Action<HttpListenerContext> _httpHandler;
/// <summary>
/// The _listener.
/// </summary>
private readonly HttpListener _listener; private readonly HttpListener _listener;
/// <summary>
/// The cancellation token source.
/// </summary>
private CancellationTokenSource _cts; private CancellationTokenSource _cts;
/// <summary>
/// Gets a value indicating whether this server is started.
/// </summary>
/// <value>
/// <c>true</c> if this server is started; otherwise, <c>false</c>.
/// </value>
public bool IsStarted { get; private set; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TinyHttpServer"/> class. /// Initializes a new instance of the <see cref="TinyHttpServer"/> class.
/// </summary> /// </summary>
@@ -59,11 +43,13 @@ namespace WireMock.Http
} }
/// <summary> /// <summary>
/// The start. /// Start the server.
/// </summary> /// </summary>
public void Start() public void Start()
{ {
_listener.Start(); _listener.Start();
IsStarted = true;
_cts = new CancellationTokenSource(); _cts = new CancellationTokenSource();
Task.Run( Task.Run(
async () => async () =>
@@ -81,11 +67,11 @@ namespace WireMock.Http
} }
/// <summary> /// <summary>
/// The stop. /// Stop the server.
/// </summary> /// </summary>
public void Stop() public void Stop()
{ {
_cts.Cancel(); _cts.Cancel();
} }
} }
} }

View File

@@ -1,27 +0,0 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
namespace WireMock
{
/// <summary>
/// The SpecifyRequests interface.
/// </summary>
public interface ISpecifyRequests
{
/// <summary>
/// The is satisfied by.
/// </summary>
/// <param name="requestMessage">
/// The request.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
bool IsSatisfiedBy([NotNull] RequestMessage requestMessage);
}
}

View File

@@ -0,0 +1,19 @@
using JetBrains.Annotations;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The RequestMatcher interface.
/// </summary>
public interface IRequestMatcher
{
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
bool IsMatch([NotNull] RequestMessage requestMessage);
}
}

View File

@@ -1,14 +1,13 @@
using System; using System;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock namespace WireMock.Matchers.Request
{ {
/// <summary> /// <summary>
/// The request body spec. /// The request body matcher.
/// </summary> /// </summary>
public class RequestBodySpec : ISpecifyRequests public class RequestMessageBodyMatcher : IRequestMatcher
{ {
/// <summary> /// <summary>
/// The bodyRegex. /// The bodyRegex.
@@ -31,75 +30,73 @@ namespace WireMock
private readonly Func<byte[], bool> _bodyDataFunc; private readonly Func<byte[], bool> _bodyDataFunc;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="body"> /// <param name="body">
/// The body Regex pattern. /// The body Regex pattern.
/// </param> /// </param>
public RequestBodySpec([NotNull, RegexPattern] string body) public RequestMessageBodyMatcher([NotNull, RegexPattern] string body)
{ {
Check.NotNull(body, nameof(body)); Check.NotNull(body, nameof(body));
_matcher = new RegexMatcher(body); _matcher = new RegexMatcher(body);
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="body"> /// <param name="body">
/// The body Regex pattern. /// The body Regex pattern.
/// </param> /// </param>
public RequestBodySpec([NotNull] byte[] body) public RequestMessageBodyMatcher([NotNull] byte[] body)
{ {
Check.NotNull(body, nameof(body)); Check.NotNull(body, nameof(body));
_bodyData = body; _bodyData = body;
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="func"> /// <param name="func">
/// The body func. /// The body func.
/// </param> /// </param>
public RequestBodySpec([NotNull] Func<string, bool> func) public RequestMessageBodyMatcher([NotNull] Func<string, bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
_bodyFunc = func; _bodyFunc = func;
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="func"> /// <param name="func">
/// The body func. /// The body func.
/// </param> /// </param>
public RequestBodySpec([NotNull] Func<byte[], bool> func) public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
_bodyDataFunc = func; _bodyDataFunc = func;
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary> /// </summary>
/// <param name="matcher"> /// <param name="matcher">
/// The body matcher. /// The body matcher.
/// </param> /// </param>
public RequestBodySpec([NotNull] IMatcher matcher) public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
{ {
Check.NotNull(matcher, nameof(matcher)); Check.NotNull(matcher, nameof(matcher));
_matcher = matcher; _matcher = matcher;
} }
/// <summary> /// <summary>
/// The is satisfied by. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage"> /// <param name="requestMessage">The RequestMessage.</param>
/// The request.
/// </param>
/// <returns> /// <returns>
/// The <see cref="bool"/>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsSatisfiedBy(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
if (_matcher != null) if (_matcher != null)
return _matcher.IsMatch(requestMessage.BodyAsString); return _matcher.IsMatch(requestMessage.BodyAsString);

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The composite request matcher.
/// </summary>
public class RequestMessageCompositeMatcher : IRequestMatcher
{
private readonly IEnumerable<IRequestMatcher> _requestMatchers;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCompositeMatcher"/> class.
/// The constructor.
/// </summary>
/// <param name="requestMatchers">
/// The <see cref="IEnumerable&lt;IRequestMatcher&gt;"/> request matchers.
/// </param>
public RequestMessageCompositeMatcher(IEnumerable<IRequestMatcher> requestMatchers)
{
_requestMatchers = requestMatchers;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
return _requestMatchers.All(spec => spec.IsMatch(requestMessage));
}
}
}

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request header matcher.
/// </summary>
public class RequestMessageHeaderMatcher : IRequestMatcher
{
/// <summary>
/// The name.
/// </summary>
private readonly string _name;
/// <summary>
/// The patternRegex.
/// </summary>
private readonly Regex _patternRegex;
/// <summary>
/// The header function
/// </summary>
private readonly Func<IDictionary<string, string>, bool> _headerFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="pattern">
/// The pattern.
/// </param>
/// <param name="ignoreCase">The ignoreCase.</param>
public RequestMessageHeaderMatcher([NotNull] string name, [NotNull, RegexPattern] string pattern, bool ignoreCase = true)
{
_name = name;
_patternRegex = ignoreCase ? new Regex(pattern, RegexOptions.IgnoreCase) : new Regex(pattern);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
/// </summary>
/// <param name="func">
/// The func.
/// </param>
public RequestMessageHeaderMatcher([NotNull] Func<IDictionary<string, string>, bool> func)
{
Check.NotNull(func, nameof(func));
_headerFunc = func;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_patternRegex == null)
return _headerFunc(requestMessage.Headers);
string headerValue = requestMessage.Headers[_name];
return _patternRegex.IsMatch(headerValue);
}
}
}

View File

@@ -1,29 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
[module: namespace WireMock.Matchers.Request
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.")]
namespace WireMock
{ {
/// <summary> /// <summary>
/// The request parameters spec. /// The request parameters matcher.
/// </summary> /// </summary>
public class RequestParamSpec : ISpecifyRequests public class RequestMessageParamMatcher : IRequestMatcher
{ {
/// <summary> /// <summary>
/// The _key. /// The _key.
@@ -38,7 +24,7 @@ namespace WireMock
private readonly Func<IDictionary<string, List<string>>, bool> _func; private readonly Func<IDictionary<string, List<string>>, bool> _func;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestParamSpec"/> class. /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
/// </summary> /// </summary>
/// <param name="key"> /// <param name="key">
/// The key. /// The key.
@@ -46,7 +32,7 @@ namespace WireMock
/// <param name="values"> /// <param name="values">
/// The values. /// The values.
/// </param> /// </param>
public RequestParamSpec([NotNull] string key, [NotNull] IEnumerable<string> values) public RequestMessageParamMatcher([NotNull] string key, [NotNull] IEnumerable<string> values)
{ {
Check.NotNull(key, nameof(key)); Check.NotNull(key, nameof(key));
Check.NotNull(values, nameof(values)); Check.NotNull(values, nameof(values));
@@ -56,27 +42,25 @@ namespace WireMock
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestParamSpec"/> class. /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
/// </summary> /// </summary>
/// <param name="func"> /// <param name="func">
/// The func. /// The func.
/// </param> /// </param>
public RequestParamSpec([NotNull] Func<IDictionary<string, List<string>>, bool> func) public RequestMessageParamMatcher([NotNull] Func<IDictionary<string, List<string>>, bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
_func = func; _func = func;
} }
/// <summary> /// <summary>
/// The is satisfied by. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage"> /// <param name="requestMessage">The RequestMessage.</param>
/// The request.
/// </param>
/// <returns> /// <returns>
/// The <see cref="bool"/>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsSatisfiedBy(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
if (_func != null) if (_func != null)
{ {

View File

@@ -3,12 +3,12 @@ using System.Text.RegularExpressions;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock namespace WireMock.Matchers.Request
{ {
/// <summary> /// <summary>
/// The request path spec. /// The request path matcher.
/// </summary> /// </summary>
public class RequestPathSpec : ISpecifyRequests public class RequestMessagePathMatcher : IRequestMatcher
{ {
/// <summary> /// <summary>
/// The pathRegex. /// The pathRegex.
@@ -21,39 +21,37 @@ namespace WireMock
private readonly Func<string, bool> _pathFunc; private readonly Func<string, bool> _pathFunc;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestPathSpec"/> class. /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="path"> /// <param name="path">
/// The path Regex pattern. /// The path Regex pattern.
/// </param> /// </param>
public RequestPathSpec([NotNull, RegexPattern] string path) public RequestMessagePathMatcher([NotNull, RegexPattern] string path)
{ {
Check.NotNull(path, nameof(path)); Check.NotNull(path, nameof(path));
_pathRegex = new Regex(path); _pathRegex = new Regex(path);
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestPathSpec"/> class. /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="func"> /// <param name="func">
/// The url func. /// The url func.
/// </param> /// </param>
public RequestPathSpec([NotNull] Func<string, bool> func) public RequestMessagePathMatcher([NotNull] Func<string, bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
_pathFunc = func; _pathFunc = func;
} }
/// <summary> /// <summary>
/// The is satisfied by. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage"> /// <param name="requestMessage">The RequestMessage.</param>
/// The request.
/// </param>
/// <returns> /// <returns>
/// The <see cref="bool"/>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsSatisfiedBy(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
return _pathRegex?.IsMatch(requestMessage.Path) ?? _pathFunc(requestMessage.Path); return _pathRegex?.IsMatch(requestMessage.Path) ?? _pathFunc(requestMessage.Path);
} }

View File

@@ -0,0 +1,59 @@
using System;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request url matcher.
/// </summary>
public class RequestMessageUrlMatcher : IRequestMatcher
{
/// <summary>
/// The urlRegex.
/// </summary>
private readonly Regex _urlRegex;
/// <summary>
/// The url function
/// </summary>
private readonly Func<string, bool> _urlFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary>
/// <param name="url">
/// The url Regex pattern.
/// </param>
public RequestMessageUrlMatcher([NotNull, RegexPattern] string url)
{
Check.NotNull(url, nameof(url));
_urlRegex = new Regex(url);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary>
/// <param name="func">
/// The url func.
/// </param>
public RequestMessageUrlMatcher(Func<string, bool> func)
{
Check.NotNull(func, nameof(func));
_urlFunc = func;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
return _urlRegex?.IsMatch(requestMessage.Url) ?? _urlFunc(requestMessage.Url);
}
}
}

View File

@@ -1,12 +1,12 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock namespace WireMock.Matchers.Request
{ {
/// <summary> /// <summary>
/// The request verb spec. /// The request verb matcher.
/// </summary> /// </summary>
internal class RequestVerbSpec : ISpecifyRequests internal class RequestMessageVerbMatcher : IRequestMatcher
{ {
/// <summary> /// <summary>
/// The _verb. /// The _verb.
@@ -14,27 +14,25 @@ namespace WireMock
private readonly string _verb; private readonly string _verb;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestVerbSpec"/> class. /// Initializes a new instance of the <see cref="RequestMessageVerbMatcher"/> class.
/// </summary> /// </summary>
/// <param name="verb"> /// <param name="verb">
/// The verb. /// The verb.
/// </param> /// </param>
public RequestVerbSpec([NotNull] string verb) public RequestMessageVerbMatcher([NotNull] string verb)
{ {
Check.NotNull(verb, nameof(verb)); Check.NotNull(verb, nameof(verb));
_verb = verb.ToLower(); _verb = verb.ToLower();
} }
/// <summary> /// <summary>
/// The is satisfied by. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage"> /// <param name="requestMessage">The RequestMessage.</param>
/// The request.
/// </param>
/// <returns> /// <returns>
/// The <see cref="bool"/>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsSatisfiedBy(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
return requestMessage.Verb == _verb; return requestMessage.Verb == _verb;
} }

View File

@@ -1,6 +1,7 @@
using System; using System;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request;
namespace WireMock.RequestBuilders namespace WireMock.RequestBuilders
{ {
@@ -16,9 +17,9 @@ namespace WireMock.RequestBuilders
/// The matcher. /// The matcher.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
ISpecifyRequests WithBody([NotNull] IMatcher matcher); IRequestMatcher WithBody([NotNull] IMatcher matcher);
/// <summary> /// <summary>
/// The with body. /// The with body.
@@ -27,9 +28,9 @@ namespace WireMock.RequestBuilders
/// The body. /// The body.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
ISpecifyRequests WithBody(string body); IRequestMatcher WithBody(string body);
/// <summary> /// <summary>
/// The with body byte[]. /// The with body byte[].
@@ -38,9 +39,9 @@ namespace WireMock.RequestBuilders
/// The body as byte[]. /// The body as byte[].
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
ISpecifyRequests WithBody(byte[] body); IRequestMatcher WithBody(byte[] body);
/// <summary> /// <summary>
/// The with body string func. /// The with body string func.
@@ -49,9 +50,9 @@ namespace WireMock.RequestBuilders
/// The body string function. /// The body string function.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
ISpecifyRequests WithBody(Func<string, bool> body); IRequestMatcher WithBody(Func<string, bool> body);
/// <summary> /// <summary>
/// The with body byte[] func. /// The with body byte[] func.
@@ -60,8 +61,8 @@ namespace WireMock.RequestBuilders
/// The body byte[] function. /// The body byte[] function.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
ISpecifyRequests WithBody(Func<byte[], bool> body); IRequestMatcher WithBody(Func<byte[], bool> body);
} }
} }

View File

@@ -1,13 +1,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers.Request;
namespace WireMock.RequestBuilders namespace WireMock.RequestBuilders
{ {
/// <summary> /// <summary>
/// The HeadersRequestBuilder interface. /// The HeadersRequestBuilder interface.
/// </summary> /// </summary>
public interface IHeadersRequestBuilder : IBodyRequestBuilder, ISpecifyRequests, IParamsRequestBuilder public interface IHeadersRequestBuilder : IBodyRequestBuilder, IRequestMatcher, IParamsRequestBuilder
{ {
/// <summary> /// <summary>
/// The with header. /// The with header.

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers.Request;
namespace WireMock.RequestBuilders namespace WireMock.RequestBuilders
{ {
@@ -19,9 +20,9 @@ namespace WireMock.RequestBuilders
/// The values. /// The values.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
ISpecifyRequests WithParam([NotNull] string key, params string[] values); IRequestMatcher WithParam([NotNull] string key, params string[] values);
/// <summary> /// <summary>
/// The with parameters. /// The with parameters.
@@ -30,8 +31,8 @@ namespace WireMock.RequestBuilders
/// The func. /// The func.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
ISpecifyRequests WithParam([NotNull] Func<IDictionary<string, List<string>>, bool> func); IRequestMatcher WithParam([NotNull] Func<IDictionary<string, List<string>>, bool> func);
} }
} }

View File

@@ -3,7 +3,7 @@
/// <summary> /// <summary>
/// The VerbRequestBuilder interface. /// The VerbRequestBuilder interface.
/// </summary> /// </summary>
public interface IVerbRequestBuilder : ISpecifyRequests, IHeadersRequestBuilder public interface IVerbRequestBuilder : IHeadersRequestBuilder
{ {
/// <summary> /// <summary>
/// The using get. /// The using get.

View File

@@ -4,6 +4,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request;
[module: [module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules", SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -28,22 +29,22 @@ namespace WireMock.RequestBuilders
/// <summary> /// <summary>
/// The requests. /// The requests.
/// </summary> /// </summary>
public class Request : CompositeRequestSpec, IVerbRequestBuilder public class Request : RequestMessageCompositeMatcher, IVerbRequestBuilder
{ {
/// <summary> /// <summary>
/// The _request specs. /// The _request matchers.
/// </summary> /// </summary>
private readonly IList<ISpecifyRequests> _requestSpecs; private readonly IList<IRequestMatcher> _requestMatchers;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Request"/> class. /// Initializes a new instance of the <see cref="Request"/> class.
/// </summary> /// </summary>
/// <param name="requestSpecs"> /// <param name="requestMatchers">
/// The request specs. /// The request matchers.
/// </param> /// </param>
private Request(IList<ISpecifyRequests> requestSpecs) : base(requestSpecs) private Request(IList<IRequestMatcher> requestMatchers) : base(requestMatchers)
{ {
_requestSpecs = requestSpecs; _requestMatchers = requestMatchers;
} }
/// <summary> /// <summary>
@@ -57,7 +58,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public static IVerbRequestBuilder WithUrl(string url) public static IVerbRequestBuilder WithUrl(string url)
{ {
var specs = new List<ISpecifyRequests> { new RequestUrlSpec(url) }; var specs = new List<IRequestMatcher> { new RequestMessageUrlMatcher(url) };
return new Request(specs); return new Request(specs);
} }
@@ -73,7 +74,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public static IVerbRequestBuilder WithUrl(Func<string, bool> func) public static IVerbRequestBuilder WithUrl(Func<string, bool> func)
{ {
var specs = new List<ISpecifyRequests> { new RequestUrlSpec(func) }; var specs = new List<IRequestMatcher> { new RequestMessageUrlMatcher(func) };
return new Request(specs); return new Request(specs);
} }
@@ -89,7 +90,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public static IVerbRequestBuilder WithPath(string path) public static IVerbRequestBuilder WithPath(string path)
{ {
var specs = new List<ISpecifyRequests> { new RequestPathSpec(path) }; var specs = new List<IRequestMatcher> { new RequestMessagePathMatcher(path) };
return new Request(specs); return new Request(specs);
} }
@@ -105,7 +106,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public static IVerbRequestBuilder WithPath([NotNull] Func<string, bool> func) public static IVerbRequestBuilder WithPath([NotNull] Func<string, bool> func)
{ {
var specs = new List<ISpecifyRequests> { new RequestPathSpec(func) }; var specs = new List<IRequestMatcher> { new RequestMessagePathMatcher(func) };
return new Request(specs); return new Request(specs);
} }
@@ -118,7 +119,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersRequestBuilder UsingGet() public IHeadersRequestBuilder UsingGet()
{ {
_requestSpecs.Add(new RequestVerbSpec("get")); _requestMatchers.Add(new RequestMessageVerbMatcher("get"));
return this; return this;
} }
@@ -130,7 +131,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersRequestBuilder UsingPost() public IHeadersRequestBuilder UsingPost()
{ {
_requestSpecs.Add(new RequestVerbSpec("post")); _requestMatchers.Add(new RequestMessageVerbMatcher("post"));
return this; return this;
} }
@@ -142,7 +143,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersRequestBuilder UsingPut() public IHeadersRequestBuilder UsingPut()
{ {
_requestSpecs.Add(new RequestVerbSpec("put")); _requestMatchers.Add(new RequestMessageVerbMatcher("put"));
return this; return this;
} }
@@ -154,7 +155,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersRequestBuilder UsingDelete() public IHeadersRequestBuilder UsingDelete()
{ {
_requestSpecs.Add(new RequestVerbSpec("delete")); _requestMatchers.Add(new RequestMessageVerbMatcher("delete"));
return this; return this;
} }
@@ -166,7 +167,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersRequestBuilder UsingHead() public IHeadersRequestBuilder UsingHead()
{ {
_requestSpecs.Add(new RequestVerbSpec("head")); _requestMatchers.Add(new RequestMessageVerbMatcher("head"));
return this; return this;
} }
@@ -192,7 +193,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersRequestBuilder UsingVerb(string verb) public IHeadersRequestBuilder UsingVerb(string verb)
{ {
_requestSpecs.Add(new RequestVerbSpec(verb)); _requestMatchers.Add(new RequestMessageVerbMatcher(verb));
return this; return this;
} }
@@ -203,11 +204,11 @@ namespace WireMock.RequestBuilders
/// The body. /// The body.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
public ISpecifyRequests WithBody(string body) public IRequestMatcher WithBody(string body)
{ {
_requestSpecs.Add(new RequestBodySpec(body)); _requestMatchers.Add(new RequestMessageBodyMatcher(body));
return this; return this;
} }
@@ -218,11 +219,11 @@ namespace WireMock.RequestBuilders
/// The body as byte[]. /// The body as byte[].
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
public ISpecifyRequests WithBody(byte[] body) public IRequestMatcher WithBody(byte[] body)
{ {
_requestSpecs.Add(new RequestBodySpec(body)); _requestMatchers.Add(new RequestMessageBodyMatcher(body));
return this; return this;
} }
@@ -233,11 +234,11 @@ namespace WireMock.RequestBuilders
/// The body function. /// The body function.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
public ISpecifyRequests WithBody(Func<string, bool> func) public IRequestMatcher WithBody(Func<string, bool> func)
{ {
_requestSpecs.Add(new RequestBodySpec(func)); _requestMatchers.Add(new RequestMessageBodyMatcher(func));
return this; return this;
} }
@@ -248,11 +249,11 @@ namespace WireMock.RequestBuilders
/// The body function. /// The body function.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
public ISpecifyRequests WithBody(Func<byte[], bool> func) public IRequestMatcher WithBody(Func<byte[], bool> func)
{ {
_requestSpecs.Add(new RequestBodySpec(func)); _requestMatchers.Add(new RequestMessageBodyMatcher(func));
return this; return this;
} }
@@ -261,11 +262,11 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
/// <param name="matcher">The matcher.</param> /// <param name="matcher">The matcher.</param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests" />. /// The <see cref="IRequestMatcher" />.
/// </returns> /// </returns>
public ISpecifyRequests WithBody(IMatcher matcher) public IRequestMatcher WithBody(IMatcher matcher)
{ {
_requestSpecs.Add(new RequestBodySpec(matcher)); _requestMatchers.Add(new RequestMessageBodyMatcher(matcher));
return this; return this;
} }
@@ -279,11 +280,11 @@ namespace WireMock.RequestBuilders
/// The values. /// The values.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
public ISpecifyRequests WithParam(string key, params string[] values) public IRequestMatcher WithParam(string key, params string[] values)
{ {
_requestSpecs.Add(new RequestParamSpec(key, values.ToList())); _requestMatchers.Add(new RequestMessageParamMatcher(key, values.ToList()));
return this; return this;
} }
@@ -294,11 +295,11 @@ namespace WireMock.RequestBuilders
/// The func. /// The func.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="ISpecifyRequests"/>. /// The <see cref="IRequestMatcher"/>.
/// </returns> /// </returns>
public ISpecifyRequests WithParam(Func<IDictionary<string, List<string>>, bool> func) public IRequestMatcher WithParam(Func<IDictionary<string, List<string>>, bool> func)
{ {
_requestSpecs.Add(new RequestParamSpec(func)); _requestMatchers.Add(new RequestMessageParamMatcher(func));
return this; return this;
} }
@@ -317,7 +318,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersRequestBuilder WithHeader(string name, string value, bool ignoreCase = true) public IHeadersRequestBuilder WithHeader(string name, string value, bool ignoreCase = true)
{ {
_requestSpecs.Add(new RequestHeaderSpec(name, value, ignoreCase)); _requestMatchers.Add(new RequestMessageHeaderMatcher(name, value, ignoreCase));
return this; return this;
} }
@@ -332,7 +333,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersRequestBuilder WithHeader(Func<IDictionary<string, string>, bool> func) public IHeadersRequestBuilder WithHeader(Func<IDictionary<string, string>, bool> func)
{ {
_requestSpecs.Add(new RequestHeaderSpec(func)); _requestMatchers.Add(new RequestMessageHeaderMatcher(func));
return this; return this;
} }
} }

View File

@@ -1,90 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
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
{
/// <summary>
/// The request header spec.
/// </summary>
public class RequestHeaderSpec : ISpecifyRequests
{
/// <summary>
/// The name.
/// </summary>
private readonly string name;
/// <summary>
/// The patternRegex.
/// </summary>
private readonly Regex patternRegex;
/// <summary>
/// The header function
/// </summary>
private readonly Func<IDictionary<string, string>, bool> headerFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestHeaderSpec"/> class.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="pattern">
/// The pattern.
/// </param>
/// <param name="ignoreCase">The ignoreCase.</param>
public RequestHeaderSpec([NotNull] string name, [NotNull, RegexPattern] string pattern, bool ignoreCase = true)
{
this.name = name;
patternRegex = ignoreCase ? new Regex(pattern, RegexOptions.IgnoreCase) : new Regex(pattern);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestHeaderSpec"/> class.
/// </summary>
/// <param name="func">
/// The func.
/// </param>
public RequestHeaderSpec([NotNull] Func<IDictionary<string, string>, bool> func)
{
Check.NotNull(func, nameof(func));
headerFunc = func;
}
/// <summary>
/// The is satisfied by.
/// </summary>
/// <param name="requestMessage">
/// The request.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool IsSatisfiedBy(RequestMessage requestMessage)
{
if (patternRegex == null)
return headerFunc(requestMessage.Headers);
string headerValue = requestMessage.Headers[name];
return patternRegex.IsMatch(headerValue);
}
}
}

View File

@@ -1,27 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Extensions; using WireMock.Extensions;
[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.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1650:ElementDocumentationMustBeSpelledCorrectly",
Justification = "Reviewed. Suppression is OK here.")]
namespace WireMock namespace WireMock
{ {
/// <summary> /// <summary>

View File

@@ -1,76 +0,0 @@
using System;
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using System.Text.RegularExpressions;
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
{
/// <summary>
/// The request url spec.
/// </summary>
public class RequestUrlSpec : ISpecifyRequests
{
/// <summary>
/// The urlRegex.
/// </summary>
private readonly Regex urlRegex;
/// <summary>
/// The url function
/// </summary>
private readonly Func<string, bool> urlFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestUrlSpec"/> class.
/// </summary>
/// <param name="url">
/// The url Regex pattern.
/// </param>
public RequestUrlSpec([NotNull, RegexPattern] string url)
{
Check.NotNull(url, nameof(url));
urlRegex = new Regex(url);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestUrlSpec"/> class.
/// </summary>
/// <param name="func">
/// The url func.
/// </param>
public RequestUrlSpec(Func<string, bool> func)
{
Check.NotNull(func, nameof(func));
urlFunc = func;
}
/// <summary>
/// The is satisfied by.
/// </summary>
/// <param name="requestMessage">
/// The request.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool IsSatisfiedBy(RequestMessage requestMessage)
{
return urlRegex?.IsMatch(requestMessage.Url) ?? urlFunc(requestMessage.Url);
}
}
}

View File

@@ -1,21 +1,5 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
[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 namespace WireMock
{ {
/// <summary> /// <summary>

View File

@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks; using System.Threading.Tasks;
using WireMock.Matchers.Request;
[module: [module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules", SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -23,9 +24,9 @@ namespace WireMock
public class Route public class Route
{ {
/// <summary> /// <summary>
/// The _request spec. /// The _request matcher.
/// </summary> /// </summary>
private readonly ISpecifyRequests _requestSpec; private readonly IRequestMatcher _requestSpec;
/// <summary> /// <summary>
/// The _provider. /// The _provider.
@@ -36,12 +37,12 @@ namespace WireMock
/// Initializes a new instance of the <see cref="Route"/> class. /// Initializes a new instance of the <see cref="Route"/> class.
/// </summary> /// </summary>
/// <param name="requestSpec"> /// <param name="requestSpec">
/// The request spec. /// The request matcher.
/// </param> /// </param>
/// <param name="provider"> /// <param name="provider">
/// The provider. /// The provider.
/// </param> /// </param>
public Route(ISpecifyRequests requestSpec, IProvideResponses provider) public Route(IRequestMatcher requestSpec, IProvideResponses provider)
{ {
_requestSpec = requestSpec; _requestSpec = requestSpec;
_provider = provider; _provider = provider;
@@ -72,7 +73,7 @@ namespace WireMock
/// </returns> /// </returns>
public bool IsRequestHandled(RequestMessage requestMessage) public bool IsRequestHandled(RequestMessage requestMessage)
{ {
return _requestSpec.IsSatisfiedBy(requestMessage); return _requestSpec.IsMatch(requestMessage);
} }
} }
} }

View File

@@ -1,11 +1,4 @@
using System.Diagnostics.CodeAnalysis; namespace WireMock
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
namespace WireMock
{ {
/// <summary> /// <summary>
/// The registration callback. /// The registration callback.
@@ -14,4 +7,4 @@ namespace WireMock
/// The route. /// The route.
/// </param> /// </param>
public delegate void RegistrationCallback(Route route); public delegate void RegistrationCallback(Route route);
} }

View File

@@ -1,29 +1,17 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Http; using WireMock.Http;
using WireMock.Matchers.Request;
using WireMock.Validation;
[module: namespace WireMock.Server
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
{ {
/// <summary> /// <summary>
/// The fluent mock server. /// The fluent mock server.
@@ -65,37 +53,6 @@ namespace WireMock
/// </summary> /// </summary>
private TimeSpan _requestProcessingDelay = TimeSpan.Zero; private TimeSpan _requestProcessingDelay = TimeSpan.Zero;
/// <summary>
/// Initializes a new instance of the <see cref="FluentMockServer"/> class.
/// </summary>
/// <param name="port">
/// The port.
/// </param>
/// <param name="ssl">
/// The SSL support.
/// </param>
private FluentMockServer(int port, bool ssl)
{
string protocol = ssl ? "https" : "http";
_httpServer = new TinyHttpServer(protocol + "://localhost:" + port + "/", HandleRequest);
Port = port;
_httpServer.Start();
}
/// <summary>
/// The RespondWithAProvider interface.
/// </summary>
public interface IRespondWithAProvider
{
/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">
/// The provider.
/// </param>
void RespondWith(IProvideResponses provider);
}
/// <summary> /// <summary>
/// Gets the port. /// Gets the port.
/// </summary> /// </summary>
@@ -116,7 +73,7 @@ namespace WireMock
} }
/// <summary> /// <summary>
/// The start. /// Start this FluentMockServer.
/// </summary> /// </summary>
/// <param name="port"> /// <param name="port">
/// The port. /// The port.
@@ -127,8 +84,34 @@ namespace WireMock
/// <returns> /// <returns>
/// The <see cref="FluentMockServer"/>. /// The <see cref="FluentMockServer"/>.
/// </returns> /// </returns>
[PublicAPI]
public static FluentMockServer Start(int port = 0, bool ssl = false) public static FluentMockServer Start(int port = 0, bool ssl = false)
{ {
Check.Condition(port, p => p >= 0, nameof(port));
if (port == 0)
port = Ports.FindFreeTcpPort();
return new FluentMockServer(port, ssl);
}
/// <summary>
/// Create this FluentMockServer.
/// </summary>
/// <param name="port">
/// The port.
/// </param>
/// <param name="ssl">
/// The SSL support.
/// </param>
/// <returns>
/// The <see cref="FluentMockServer"/>.
/// </returns>
[PublicAPI]
public static FluentMockServer Create(int port = 0, bool ssl = false)
{
Check.Condition(port, p => p > 0, nameof(port));
if (port == 0) if (port == 0)
{ {
port = Ports.FindFreeTcpPort(); port = Ports.FindFreeTcpPort();
@@ -137,6 +120,31 @@ namespace WireMock
return new FluentMockServer(port, ssl); return new FluentMockServer(port, ssl);
} }
/// <summary>
/// Initializes a new instance of the <see cref="FluentMockServer"/> class, and starts the server.
/// </summary>
/// <param name="port">
/// The port.
/// </param>
/// <param name="ssl">
/// The SSL support.
/// </param>
private FluentMockServer(int port, bool ssl)
{
string protocol = ssl ? "https" : "http";
_httpServer = new TinyHttpServer(protocol + "://localhost:" + port + "/", HandleRequest);
Port = port;
_httpServer.Start();
}
/// <summary>
/// Stop this server.
/// </summary>
public void Stop()
{
_httpServer.Stop();
}
/// <summary> /// <summary>
/// The reset. /// The reset.
/// </summary> /// </summary>
@@ -157,16 +165,16 @@ namespace WireMock
/// The search logs for. /// The search logs for.
/// </summary> /// </summary>
/// <param name="spec"> /// <param name="spec">
/// The spec. /// The matcher.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="IEnumerable"/>. /// The <see cref="IEnumerable"/>.
/// </returns> /// </returns>
public IEnumerable<RequestMessage> SearchLogsFor(ISpecifyRequests spec) public IEnumerable<RequestMessage> SearchLogsFor(IRequestMatcher spec)
{ {
lock (((ICollection)_requestLogs).SyncRoot) lock (((ICollection)_requestLogs).SyncRoot)
{ {
return _requestLogs.Where(spec.IsSatisfiedBy); return _requestLogs.Where(spec.IsMatch);
} }
} }
@@ -184,24 +192,16 @@ namespace WireMock
} }
} }
/// <summary>
/// The stop.
/// </summary>
public void Stop()
{
_httpServer.Stop();
}
/// <summary> /// <summary>
/// The given. /// The given.
/// </summary> /// </summary>
/// <param name="requestSpec"> /// <param name="requestSpec">
/// The request spec. /// The request matcher.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="IRespondWithAProvider"/>. /// The <see cref="IRespondWithAProvider"/>.
/// </returns> /// </returns>
public IRespondWithAProvider Given(ISpecifyRequests requestSpec) public IRespondWithAProvider Given(IRequestMatcher requestSpec)
{ {
return new RespondWithAProvider(RegisterRoute, requestSpec); return new RespondWithAProvider(RegisterRoute, requestSpec);
} }
@@ -249,6 +249,7 @@ namespace WireMock
var request = _requestMapper.Map(ctx.Request); var request = _requestMapper.Map(ctx.Request);
LogRequest(request); LogRequest(request);
try try
{ {
var targetRoute = _routes.FirstOrDefault(route => route.IsRequestHandled(request)); var targetRoute = _routes.FirstOrDefault(route => route.IsRequestHandled(request));
@@ -277,47 +278,5 @@ namespace WireMock
ctx.Response.Close(); ctx.Response.Close();
} }
} }
/// <summary>
/// The respond with a provider.
/// </summary>
private class RespondWithAProvider : IRespondWithAProvider
{
/// <summary>
/// The _registration callback.
/// </summary>
private readonly RegistrationCallback _registrationCallback;
/// <summary>
/// The _request spec.
/// </summary>
private readonly ISpecifyRequests _requestSpec;
/// <summary>
/// Initializes a new instance of the <see cref="RespondWithAProvider"/> class.
/// </summary>
/// <param name="registrationCallback">
/// The registration callback.
/// </param>
/// <param name="requestSpec">
/// The request spec.
/// </param>
public RespondWithAProvider(RegistrationCallback registrationCallback, ISpecifyRequests requestSpec)
{
_registrationCallback = registrationCallback;
_requestSpec = requestSpec;
}
/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">
/// The provider.
/// </param>
public void RespondWith(IProvideResponses provider)
{
_registrationCallback(new Route(_requestSpec, provider));
}
}
} }
} }

View File

@@ -0,0 +1,16 @@
namespace WireMock.Server
{
/// <summary>
/// IRespondWithAProvider
/// </summary>
public interface IRespondWithAProvider
{
/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">
/// The provider.
/// </param>
void RespondWith(IProvideResponses provider);
}
}

View File

@@ -0,0 +1,46 @@
using WireMock.Matchers.Request;
namespace WireMock.Server
{
/// <summary>
/// The respond with a provider.
/// </summary>
internal class RespondWithAProvider : IRespondWithAProvider
{
/// <summary>
/// The _registration callback.
/// </summary>
private readonly RegistrationCallback _registrationCallback;
/// <summary>
/// The _request matcher.
/// </summary>
private readonly IRequestMatcher _requestSpec;
/// <summary>
/// Initializes a new instance of the <see cref="RespondWithAProvider"/> class.
/// </summary>
/// <param name="registrationCallback">
/// The registration callback.
/// </param>
/// <param name="requestSpec">
/// The request matcher.
/// </param>
public RespondWithAProvider(RegistrationCallback registrationCallback, IRequestMatcher requestSpec)
{
_registrationCallback = registrationCallback;
_requestSpec = requestSpec;
}
/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">
/// The provider.
/// </param>
public void RespondWith(IProvideResponses provider)
{
_registrationCallback(new Route(_requestSpec, provider));
}
}
}

View File

@@ -9,6 +9,7 @@ using NFluent;
using NUnit.Framework; using NUnit.Framework;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.ResponseBuilders; using WireMock.ResponseBuilders;
using WireMock.Server;
[module: [module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules", SuppressMessage("StyleCop.CSharp.ReadabilityRules",

View File

@@ -23,7 +23,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -38,7 +38,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -53,7 +53,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
[Test] [Test]
@@ -68,7 +68,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -83,7 +83,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -98,7 +98,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -113,7 +113,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -128,7 +128,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -143,7 +143,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -158,7 +158,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
[Test] [Test]
@@ -173,7 +173,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
[Test] [Test]
@@ -188,7 +188,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } });
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -203,7 +203,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } });
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
[Test] [Test]
@@ -218,7 +218,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "ABC" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "ABC" } });
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
[Test] [Test]
@@ -233,7 +233,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "TaTaTa" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "TaTaTa" } });
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -248,7 +248,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } });
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -263,7 +263,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } });
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -278,7 +278,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -298,7 +298,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -318,7 +318,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
[Test] [Test]
@@ -333,7 +333,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -348,7 +348,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
[Test] [Test]
@@ -363,7 +363,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } });
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
[Test] [Test]
@@ -378,14 +378,14 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
public void Should_specify_requests_matching_given_params_func() public void Should_specify_requests_matching_given_params_func()
{ {
// given // given
var spec = Request.WithPath("/foo").WithParam(p => p.ContainsKey("bar") && (p["bar"].Contains("1") || p["bar"].Contains("2"))); var spec = Request.WithPath("/foo").UsingAnyVerb().WithParam(p => p.ContainsKey("bar"));
// when // when
string bodyAsString = "Hello world!"; string bodyAsString = "Hello world!";
@@ -393,7 +393,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test] [Test]
@@ -408,7 +408,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsSatisfiedBy(request)).IsFalse(); Check.That(spec.IsMatch(request)).IsFalse();
} }
} }
} }