Fixed Issue #1

This commit is contained in:
Stef Heyenrath
2017-01-17 23:41:58 +01:00
parent eeaeeb2c61
commit c0872995b0
29 changed files with 493 additions and 396 deletions

View File

@@ -1,5 +1,8 @@
using System;
using Newtonsoft.Json;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
namespace WireMock.Net.ConsoleApplication
{
@@ -16,12 +19,12 @@ namespace WireMock.Net.ConsoleApplication
server
.Given(
Requests
RequestBuilder
.WithUrl("/*")
.UsingGet()
)
.RespondWith(
Responses
ResponseBuilder
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody(@"{ ""msg"": ""Hello world!""}")

View File

@@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Net;
using WireMock.RequestBuilders;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -26,7 +27,7 @@ namespace WireMock
/// The listener request.
/// </param>
/// <returns>
/// The <see cref="Request"/>.
/// The <see cref="RequestBuilder"/>.
/// </returns>
public Request Map(HttpListenerRequest listenerRequest)
{

View File

@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
@@ -21,6 +22,6 @@ namespace WireMock
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
bool IsSatisfiedBy(Request request);
bool IsSatisfiedBy([NotNull] Request request);
}
}

View File

@@ -75,7 +75,7 @@ namespace WireMock
}
Path = path;
Headers = headers.ToDictionary(kv => kv.Key.ToLower(), kv => kv.Value.ToLower());
Headers = headers; //.ToDictionary(kv => kv.Key.ToLower(), kv => kv.Value.ToLower());
Verb = verb.ToLower();
Body = body?.Trim() ?? string.Empty;
}

View File

@@ -1,4 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -22,19 +25,20 @@ namespace WireMock
public class RequestBodySpec : ISpecifyRequests
{
/// <summary>
/// The _body.
/// The bodyRegex.
/// </summary>
private readonly string _body;
private readonly Regex bodyRegex;
/// <summary>
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class.
/// </summary>
/// <param name="body">
/// The body.
/// The body Regex pattern.
/// </param>
public RequestBodySpec(string body)
public RequestBodySpec([NotNull, RegexPattern] string body)
{
_body = body.Trim();
Check.NotNull(body, nameof(body));
bodyRegex = new Regex(body);
}
/// <summary>
@@ -48,7 +52,7 @@ namespace WireMock
/// </returns>
public bool IsSatisfiedBy(Request request)
{
return WildcardPatternMatcher.MatchWildcardString(_body, request.Body.Trim());
return bodyRegex.IsMatch(request.Body);
}
}
}

View File

@@ -0,0 +1,19 @@
namespace WireMock.RequestBuilders
{
/// <summary>
/// The BodyRequestBuilder interface.
/// </summary>
public interface IBodyRequestBuilder
{
/// <summary>
/// The with body.
/// </summary>
/// <param name="body">
/// The body.
/// </param>
/// <returns>
/// The <see cref="ISpecifyRequests"/>.
/// </returns>
ISpecifyRequests WithBody(string body);
}
}

View File

@@ -0,0 +1,23 @@
namespace WireMock.RequestBuilders
{
/// <summary>
/// The HeadersRequestBuilder interface.
/// </summary>
public interface IHeadersRequestBuilder : IBodyRequestBuilder, ISpecifyRequests, IParamsRequestBuilder
{
/// <summary>
/// The with header.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="value">
/// The value.
/// </param>
/// <param name="ignoreCase">ignore Case</param>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder WithHeader(string name, string value, bool ignoreCase = true);
}
}

View File

@@ -0,0 +1,22 @@
namespace WireMock.RequestBuilders
{
/// <summary>
/// The ParametersRequestBuilder interface.
/// </summary>
public interface IParamsRequestBuilder
{
/// <summary>
/// The with parameters.
/// </summary>
/// <param name="key">
/// The key.
/// </param>
/// <param name="values">
/// The values.
/// </param>
/// <returns>
/// The <see cref="ISpecifyRequests"/>.
/// </returns>
ISpecifyRequests WithParam(string key, params string[] values);
}
}

View File

@@ -0,0 +1,59 @@
namespace WireMock.RequestBuilders
{
/// <summary>
/// The VerbRequestBuilder interface.
/// </summary>
public interface IVerbRequestBuilder : ISpecifyRequests, IHeadersRequestBuilder
{
/// <summary>
/// The using get.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingGet();
/// <summary>
/// The using post.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingPost();
/// <summary>
/// The using put.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingPut();
/// <summary>
/// The using head.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingHead();
/// <summary>
/// The using any verb.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingAnyVerb();
/// <summary>
/// The using verb.
/// </summary>
/// <param name="verb">
/// The verb.
/// </param>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingVerb(string verb);
}
}

View File

@@ -20,12 +20,12 @@ using System.Linq;
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable ArrangeThisQualifier
// ReSharper disable InconsistentNaming
namespace WireMock
namespace WireMock.RequestBuilders
{
/// <summary>
/// The requests.
/// </summary>
public class Requests : CompositeRequestSpec, IVerbRequestBuilder, IHeadersRequestBuilder, IParamsRequestBuilder
public class RequestBuilder : CompositeRequestSpec, IVerbRequestBuilder, IHeadersRequestBuilder, IParamsRequestBuilder
{
/// <summary>
/// The _request specs.
@@ -33,12 +33,12 @@ namespace WireMock
private readonly IList<ISpecifyRequests> _requestSpecs;
/// <summary>
/// Initializes a new instance of the <see cref="Requests"/> class.
/// Initializes a new instance of the <see cref="RequestBuilder"/> class.
/// </summary>
/// <param name="requestSpecs">
/// The request specs.
/// </param>
private Requests(IList<ISpecifyRequests> requestSpecs) : base(requestSpecs)
private RequestBuilder(IList<ISpecifyRequests> requestSpecs) : base(requestSpecs)
{
_requestSpecs = requestSpecs;
}
@@ -55,7 +55,7 @@ namespace WireMock
public static IVerbRequestBuilder WithUrl(string url)
{
var specs = new List<ISpecifyRequests>();
var requests = new Requests(specs);
var requests = new RequestBuilder(specs);
specs.Add(new RequestUrlSpec(url));
return requests;
}
@@ -72,7 +72,7 @@ namespace WireMock
public static IVerbRequestBuilder WithPath(string path)
{
var specs = new List<ISpecifyRequests>();
var requests = new Requests(specs);
var requests = new RequestBuilder(specs);
specs.Add(new RequestPathSpec(path));
return requests;
}
@@ -188,17 +188,18 @@ namespace WireMock
/// The with header.
/// </summary>
/// <param name="name">
/// The name.
/// The name.
/// </param>
/// <param name="value">
/// The value.
/// The value.
/// </param>
/// <param name="ignoreCase">ignore Case</param>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
public IHeadersRequestBuilder WithHeader(string name, string value)
public IHeadersRequestBuilder WithHeader(string name, string value, bool ignoreCase = true)
{
_requestSpecs.Add(new RequestHeaderSpec(name, value));
_requestSpecs.Add(new RequestHeaderSpec(name, value, ignoreCase));
return this;
}
}

View File

@@ -1,4 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -22,14 +24,14 @@ namespace WireMock
public class RequestHeaderSpec : ISpecifyRequests
{
/// <summary>
/// The _name.
/// The name.
/// </summary>
private readonly string _name;
private readonly string name;
/// <summary>
/// The _pattern.
/// The patternRegex.
/// </summary>
private readonly string _pattern;
private readonly Regex patternRegex;
/// <summary>
/// Initializes a new instance of the <see cref="RequestHeaderSpec"/> class.
@@ -40,10 +42,11 @@ namespace WireMock
/// <param name="pattern">
/// The pattern.
/// </param>
public RequestHeaderSpec(string name, string pattern)
/// <param name="ignoreCase">The ignoreCase.</param>
public RequestHeaderSpec([NotNull] string name, [NotNull, RegexPattern] string pattern, bool ignoreCase = true)
{
_name = name.ToLower();
_pattern = pattern.ToLower();
this.name = name;
patternRegex = ignoreCase ? new Regex(pattern, RegexOptions.IgnoreCase) : new Regex(pattern);
}
/// <summary>
@@ -55,10 +58,10 @@ namespace WireMock
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool IsSatisfiedBy(Request request)
public bool IsSatisfiedBy([NotNull] Request request)
{
string headerValue = request.Headers[_name];
return WildcardPatternMatcher.MatchWildcardString(_pattern, headerValue);
string headerValue = request.Headers[name];
return patternRegex.IsMatch(headerValue);
}
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using JetBrains.Annotations;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -57,7 +58,7 @@ namespace WireMock
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool IsSatisfiedBy(Request request)
public bool IsSatisfiedBy([NotNull] Request request)
{
return request.GetParameter(_key).Intersect(_values).Count() == _values.Count;
}

View File

@@ -1,4 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -24,17 +27,18 @@ namespace WireMock
/// <summary>
/// The _path.
/// </summary>
private readonly string _path;
private readonly Regex _path;
/// <summary>
/// Initializes a new instance of the <see cref="RequestPathSpec"/> class.
/// </summary>
/// <param name="path">
/// The path.
/// The path Regex pattern.
/// </param>
public RequestPathSpec(string path)
public RequestPathSpec([NotNull, RegexPattern] string path)
{
_path = path;
Check.NotNull(path, nameof(path));
_path = new Regex(path);
}
/// <summary>
@@ -46,9 +50,9 @@ namespace WireMock
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool IsSatisfiedBy(Request request)
public bool IsSatisfiedBy([NotNull] Request request)
{
return WildcardPatternMatcher.MatchWildcardString(_path, request.Path);
return _path.IsMatch(request.Path);
}
}
}

View File

@@ -1,123 +0,0 @@
using System.Diagnostics.CodeAnalysis;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
namespace WireMock
{
/// <summary>
/// The VerbRequestBuilder interface.
/// </summary>
public interface IVerbRequestBuilder : ISpecifyRequests, IHeadersRequestBuilder
{
/// <summary>
/// The using get.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingGet();
/// <summary>
/// The using post.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingPost();
/// <summary>
/// The using put.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingPut();
/// <summary>
/// The using head.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingHead();
/// <summary>
/// The using any verb.
/// </summary>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingAnyVerb();
/// <summary>
/// The using verb.
/// </summary>
/// <param name="verb">
/// The verb.
/// </param>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder UsingVerb(string verb);
}
/// <summary>
/// The HeadersRequestBuilder interface.
/// </summary>
public interface IHeadersRequestBuilder : IBodyRequestBuilder, ISpecifyRequests, IParamsRequestBuilder
{
/// <summary>
/// The with header.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="value">
/// The value.
/// </param>
/// <returns>
/// The <see cref="IHeadersRequestBuilder"/>.
/// </returns>
IHeadersRequestBuilder WithHeader(string name, string value);
}
/// <summary>
/// The BodyRequestBuilder interface.
/// </summary>
public interface IBodyRequestBuilder
{
/// <summary>
/// The with body.
/// </summary>
/// <param name="body">
/// The body.
/// </param>
/// <returns>
/// The <see cref="ISpecifyRequests"/>.
/// </returns>
ISpecifyRequests WithBody(string body);
}
/// <summary>
/// The ParametersRequestBuilder interface.
/// </summary>
public interface IParamsRequestBuilder
{
/// <summary>
/// The with parameters.
/// </summary>
/// <param name="key">
/// The key.
/// </param>
/// <param name="values">
/// The values.
/// </param>
/// <returns>
/// The <see cref="ISpecifyRequests"/>.
/// </returns>
ISpecifyRequests WithParam(string key, params string[] values);
}
}

View File

@@ -1,4 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using System.Text.RegularExpressions;
using WireMock.Validation;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -22,19 +25,20 @@ namespace WireMock
public class RequestUrlSpec : ISpecifyRequests
{
/// <summary>
/// The _url.
/// The urlRegex.
/// </summary>
private readonly string _url;
private readonly Regex urlRegex;
/// <summary>
/// Initializes a new instance of the <see cref="RequestUrlSpec"/> class.
/// </summary>
/// <param name="url">
/// The url.
/// The url Regex pattern.
/// </param>
public RequestUrlSpec(string url)
public RequestUrlSpec([NotNull, RegexPattern] string url)
{
_url = url;
Check.NotNull(url, nameof(url));
urlRegex = new Regex(url);
}
/// <summary>
@@ -48,7 +52,7 @@ namespace WireMock
/// </returns>
public bool IsSatisfiedBy(Request request)
{
return WildcardPatternMatcher.MatchWildcardString(_url, request.Url);
return urlRegex.IsMatch(request.Url);
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -46,7 +47,7 @@ namespace WireMock
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool IsSatisfiedBy(Request request)
public bool IsSatisfiedBy([NotNull] Request request)
{
return request.Verb == _verb;
}

View File

@@ -1,64 +0,0 @@
using System;
using System.Diagnostics.CodeAnalysis;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
namespace WireMock
{
/// <summary>
/// The HeadersResponseBuilder interface.
/// </summary>
public interface IHeadersResponseBuilder : IBodyResponseBuilder
{
/// <summary>
/// The with header.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="value">
/// The value.
/// </param>
/// <returns>
/// The <see cref="IHeadersResponseBuilder"/>.
/// </returns>
IHeadersResponseBuilder WithHeader(string name, string value);
}
/// <summary>
/// The BodyResponseBuilder interface.
/// </summary>
public interface IBodyResponseBuilder : IDelayResponseBuilder
{
/// <summary>
/// The with body.
/// </summary>
/// <param name="body">
/// The body.
/// </param>
/// <returns>
/// The <see cref="IDelayResponseBuilder"/>.
/// </returns>
IDelayResponseBuilder WithBody(string body);
}
/// <summary>
/// The DelayResponseBuilder interface.
/// </summary>
public interface IDelayResponseBuilder : IProvideResponses
{
/// <summary>
/// The after delay.
/// </summary>
/// <param name="delay">
/// The delay.
/// </param>
/// <returns>
/// The <see cref="IProvideResponses"/>.
/// </returns>
IProvideResponses AfterDelay(TimeSpan delay);
}
}

View File

@@ -0,0 +1,19 @@
namespace WireMock.ResponseBuilders
{
/// <summary>
/// The BodyResponseBuilder interface.
/// </summary>
public interface IBodyResponseBuilder : IDelayResponseBuilder
{
/// <summary>
/// The with body.
/// </summary>
/// <param name="body">
/// The body.
/// </param>
/// <returns>
/// The <see cref="IDelayResponseBuilder"/>.
/// </returns>
IDelayResponseBuilder WithBody(string body);
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace WireMock.ResponseBuilders
{
/// <summary>
/// The DelayResponseBuilder interface.
/// </summary>
public interface IDelayResponseBuilder : IProvideResponses
{
/// <summary>
/// The after delay.
/// </summary>
/// <param name="delay">
/// The delay.
/// </param>
/// <returns>
/// The <see cref="IProvideResponses"/>.
/// </returns>
IProvideResponses AfterDelay(TimeSpan delay);
}
}

View File

@@ -0,0 +1,22 @@
namespace WireMock.ResponseBuilders
{
/// <summary>
/// The HeadersResponseBuilder interface.
/// </summary>
public interface IHeadersResponseBuilder : IBodyResponseBuilder
{
/// <summary>
/// The with header.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="value">
/// The value.
/// </param>
/// <returns>
/// The <see cref="IHeadersResponseBuilder"/>.
/// </returns>
IHeadersResponseBuilder WithHeader(string name, string value);
}
}

View File

@@ -16,12 +16,12 @@ using System.Threading.Tasks;
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable ArrangeThisQualifier
// ReSharper disable InconsistentNaming
namespace WireMock
namespace WireMock.ResponseBuilders
{
/// <summary>
/// The responses.
/// </summary>
public class Responses : IHeadersResponseBuilder
public class ResponseBuilder : IHeadersResponseBuilder
{
/// <summary>
/// The _response.
@@ -34,12 +34,12 @@ namespace WireMock
private TimeSpan _delay = TimeSpan.Zero;
/// <summary>
/// Initializes a new instance of the <see cref="Responses"/> class.
/// Initializes a new instance of the <see cref="ResponseBuilder"/> class.
/// </summary>
/// <param name="response">
/// The response.
/// </param>
public Responses(Response response)
public ResponseBuilder(Response response)
{
_response = response;
}
@@ -74,7 +74,7 @@ namespace WireMock
public static IHeadersResponseBuilder WithStatusCode(int code)
{
var response = new Response { StatusCode = code };
return new Responses(response);
return new ResponseBuilder(response);
}
/// <summary>

View File

@@ -0,0 +1,141 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
// Copied from https://github.com/aspnet/EntityFramework/blob/dev/src/Shared/Check.cs
namespace WireMock.Validation
{
[DebuggerStepThrough]
internal static class Check
{
[ContractAnnotation("value:null => halt")]
public static T Condition<T>([NoEnumeration] T value, [NotNull] Predicate<T> condition, [InvokerParameterName] [NotNull] string parameterName)
{
NotNull(condition, nameof(condition));
NotNull(value, nameof(value));
if (!condition(value))
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentOutOfRangeException(parameterName);
}
return value;
}
[ContractAnnotation("value:null => halt")]
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName)
{
if (ReferenceEquals(value, null))
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentNullException(parameterName);
}
return value;
}
[ContractAnnotation("value:null => halt")]
public static T NotNull<T>(
[NoEnumeration] T value,
[InvokerParameterName] [NotNull] string parameterName,
[NotNull] string propertyName)
{
if (ReferenceEquals(value, null))
{
NotEmpty(parameterName, nameof(parameterName));
NotEmpty(propertyName, nameof(propertyName));
throw new ArgumentException(CoreStrings.ArgumentPropertyNull(propertyName, parameterName));
}
return value;
}
[ContractAnnotation("value:null => halt")]
public static IList<T> NotEmpty<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName)
{
NotNull(value, parameterName);
if (value.Count == 0)
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentException(CoreStrings.CollectionArgumentIsEmpty(parameterName));
}
return value;
}
[ContractAnnotation("value:null => halt")]
public static string NotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName)
{
Exception e = null;
if (ReferenceEquals(value, null))
{
e = new ArgumentNullException(parameterName);
}
else if (value.Trim().Length == 0)
{
e = new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName));
}
if (e != null)
{
NotEmpty(parameterName, nameof(parameterName));
throw e;
}
return value;
}
public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName)
{
if (!ReferenceEquals(value, null)
&& (value.Length == 0))
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName));
}
return value;
}
public static IList<T> HasNoNulls<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName)
where T : class
{
NotNull(value, parameterName);
if (value.Any(e => e == null))
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentException(parameterName);
}
return value;
}
public static Type ValidEntityType(Type value, [InvokerParameterName] [NotNull] string parameterName)
{
if (!value.GetTypeInfo().IsClass)
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentException(CoreStrings.InvalidEntityType(value, parameterName));
}
return value;
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Globalization;
using JetBrains.Annotations;
// copied from https://github.com/aspnet/EntityFramework/blob/dev/src/Microsoft.EntityFrameworkCore/Properties/CoreStrings.resx
namespace WireMock.Validation
{
internal static class CoreStrings
{
/// <summary>
/// The property '{property}' of the argument '{argument}' cannot be null.
/// </summary>
public static string ArgumentPropertyNull([CanBeNull] string property, [CanBeNull] string argument)
{
return string.Format(CultureInfo.CurrentCulture, $"The property '{property}' of the argument '{argument}' cannot be null.", property, argument);
}
/// <summary>
/// The string argument '{argumentName}' cannot be empty.
/// </summary>
public static string ArgumentIsEmpty([CanBeNull] string argumentName)
{
return string.Format(CultureInfo.CurrentCulture, $"The string argument '{argumentName}' cannot be empty.", argumentName);
}
/// <summary>
/// The entity type '{type}' provided for the argument '{argumentName}' must be a reference type.
/// </summary>
public static string InvalidEntityType([CanBeNull] Type type, [CanBeNull] string argumentName)
{
return string.Format(CultureInfo.CurrentCulture, $"The entity type '{type}' provided for the argument '{argumentName}' must be a reference type.", type, argumentName);
}
/// <summary>
/// The collection argument '{argumentName}' must contain at least one element.
/// </summary>
public static string CollectionArgumentIsEmpty([CanBeNull] string argumentName)
{
return string.Format(CultureInfo.CurrentCulture, $"The collection argument '{argumentName}' must contain at least one element.", argumentName);
}
}
}

View File

@@ -1,74 +0,0 @@
using System.Diagnostics.CodeAnalysis;
[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
{
/// <summary>
/// The wildcard pattern matcher.
/// </summary>
public static class WildcardPatternMatcher
{
/// <summary>
/// The match wildcard string.
/// </summary>
/// <param name="pattern">
/// The pattern.
/// </param>
/// <param name="input">
/// The input.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
/// <remarks>
/// Copy/paste from http://www.codeproject.com/Tips/57304/Use-wildcard-characters-and-to-compare-strings
/// </remarks>
public static bool MatchWildcardString(string pattern, string input)
{
if (string.CompareOrdinal(pattern, input) == 0)
{
return true;
}
if (string.IsNullOrEmpty(input))
{
return string.IsNullOrEmpty(pattern.Trim('*'));
}
if (pattern.Length == 0)
{
return false;
}
if (pattern[0] == '?')
{
return MatchWildcardString(pattern.Substring(1), input.Substring(1));
}
if (pattern[pattern.Length - 1] == '?')
{
return MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input.Substring(0, input.Length - 1));
}
if (pattern[0] == '*')
{
return MatchWildcardString(pattern.Substring(1), input) || MatchWildcardString(pattern, input.Substring(1));
}
if (pattern[pattern.Length - 1] == '*')
{
return MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input) || MatchWildcardString(pattern, input.Substring(0, input.Length - 1));
}
return pattern[0] == input[0] && MatchWildcardString(pattern.Substring(1), input.Substring(1));
}
}
}

View File

@@ -7,6 +7,8 @@ using System.Net.Http;
using System.Threading.Tasks;
using NFluent;
using NUnit.Framework;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -41,10 +43,10 @@ namespace WireMock.Net.Tests
_server = FluentMockServer.Start();
_server
.Given(Requests
.Given(RequestBuilder
.WithUrl("/foo")
.UsingGet())
.RespondWith(Responses
.RespondWith(ResponseBuilder
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}"));
@@ -98,7 +100,7 @@ namespace WireMock.Net.Tests
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/bar");
// then
var result = _server.SearchLogsFor(Requests.WithUrl("/b*"));
var result = _server.SearchLogsFor(RequestBuilder.WithUrl("/b.*"));
Check.That(result).HasSize(1);
var requestLogged = result.First();
Check.That(requestLogged.Url).IsEqualTo("/bar");
@@ -125,10 +127,10 @@ namespace WireMock.Net.Tests
_server = FluentMockServer.Start();
_server
.Given(Requests
.Given(RequestBuilder
.WithUrl("/foo")
.UsingGet())
.RespondWith(Responses
.RespondWith(ResponseBuilder
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}"));
@@ -147,17 +149,17 @@ namespace WireMock.Net.Tests
_server = FluentMockServer.Start();
_server
.Given(Requests
.Given(RequestBuilder
.WithUrl("/foo")
.UsingGet())
.RespondWith(Responses
.RespondWith(ResponseBuilder
.WithStatusCode(307)
.WithHeader("Location", "/bar"));
_server
.Given(Requests
.Given(RequestBuilder
.WithUrl("/bar")
.UsingGet())
.RespondWith(Responses
.RespondWith(ResponseBuilder
.WithStatusCode(200)
.WithBody("REDIRECT SUCCESSFUL"));
@@ -176,9 +178,9 @@ namespace WireMock.Net.Tests
_server = FluentMockServer.Start();
_server
.Given(Requests
.Given(RequestBuilder
.WithUrl("/*"))
.RespondWith(Responses
.RespondWith(ResponseBuilder
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}")
.AfterDelay(TimeSpan.FromMilliseconds(2000)));
@@ -200,9 +202,9 @@ namespace WireMock.Net.Tests
_server = FluentMockServer.Start();
_server.AddRequestProcessingDelay(TimeSpan.FromMilliseconds(2000));
_server
.Given(Requests
.Given(RequestBuilder
.WithUrl("/*"))
.RespondWith(Responses
.RespondWith(ResponseBuilder
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}"));

View File

@@ -94,7 +94,7 @@ namespace WireMock.Net.Tests
// then
Check.That(MapperServer.LastRequest).IsNotNull();
Check.That(MapperServer.LastRequest.Headers).Not.IsNullOrEmpty();
Check.That(MapperServer.LastRequest.Headers.Contains(new KeyValuePair<string, string>("x-alex", "1706"))).IsTrue();
Check.That(MapperServer.LastRequest.Headers.Contains(new KeyValuePair<string, string>("X-Alex", "1706"))).IsTrue();
}
[Test]
@@ -141,7 +141,7 @@ namespace WireMock.Net.Tests
public static string UrlPrefix { get; private set; }
public static new MapperServer Start()
public new static MapperServer Start()
{
var port = Ports.FindFreeTcpPort();
UrlPrefix = "http://localhost:" + port + "/";

View File

@@ -2,14 +2,15 @@
using System.Diagnostics.CodeAnalysis;
using NFluent;
using NUnit.Framework;
using WireMock.RequestBuilders;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable InconsistentNaming
namespace WireMock.Net.Tests
@@ -21,7 +22,7 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_url()
{
// given
var spec = Requests.WithUrl("/foo");
var spec = RequestBuilder.WithUrl("/foo");
// when
var request = new Request("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
@@ -34,7 +35,7 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_url_prefix()
{
// given
var spec = Requests.WithUrl("/foo*");
var spec = RequestBuilder.WithUrl("/foo*");
// when
var request = new Request("/foo/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
@@ -47,7 +48,7 @@ namespace WireMock.Net.Tests
public void Should_exclude_requests_not_matching_given_url()
{
// given
var spec = Requests.WithUrl("/foo");
var spec = RequestBuilder.WithUrl("/foo");
// when
var request = new Request("/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
@@ -60,7 +61,7 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_path()
{
// given
var spec = Requests.WithPath("/foo");
var spec = RequestBuilder.WithPath("/foo");
// when
var request = new Request("/foo", "?param=1", "blabla", "whatever", new Dictionary<string, string>());
@@ -73,7 +74,7 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_url_and_method()
{
// given
var spec = Requests.WithUrl("/foo").UsingPut();
var spec = RequestBuilder.WithUrl("/foo").UsingPut();
// when
var request = new Request("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
@@ -86,7 +87,7 @@ namespace WireMock.Net.Tests
public void Should_exclude_requests_matching_given_url_but_not_http_method()
{
// given
var spec = Requests.WithUrl("/foo").UsingPut();
var spec = RequestBuilder.WithUrl("/foo").UsingPut();
// when
var request = new Request("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
@@ -99,7 +100,7 @@ namespace WireMock.Net.Tests
public void Should_exclude_requests_matching_given_http_method_but_not_url()
{
// given
var spec = Requests.WithUrl("/bar").UsingPut();
var spec = RequestBuilder.WithUrl("/bar").UsingPut();
// when
var request = new Request("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
@@ -112,11 +113,11 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_url_and_headers()
{
// given
var spec = Requests.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
var spec = RequestBuilder.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
// when
var request = new Request("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
@@ -125,7 +126,7 @@ namespace WireMock.Net.Tests
public void Should_exclude_requests_not_matching_given_headers()
{
// given
var spec = Requests.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata");
var spec = RequestBuilder.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata");
// when
var request = new Request("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
@@ -134,14 +135,27 @@ namespace WireMock.Net.Tests
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers_ignorecase()
{
// given
var spec = RequestBuilder.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "abc", false);
// when
var request = new Request("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "ABC" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_header_prefix()
{
// given
var spec = Requests.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*");
var spec = RequestBuilder.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*");
// when
var request = new Request("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tatata" } });
var request = new Request("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "TaTaTa" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
@@ -151,7 +165,7 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_body()
{
// given
var spec = Requests.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
var spec = RequestBuilder.WithUrl("/foo").UsingAnyVerb().WithBody(".*Hello world!.*");
// when
var request = new Request("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
@@ -164,7 +178,7 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_body_as_wildcard()
{
// given
var spec = Requests.WithUrl("/foo").UsingAnyVerb().WithBody("H*o wor?d!");
var spec = RequestBuilder.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o");
// when
var request = new Request("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
@@ -177,7 +191,7 @@ namespace WireMock.Net.Tests
public void Should_exclude_requests_not_matching_given_body()
{
// given
var spec = Requests.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
var spec = RequestBuilder.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
// when
var request = new Request("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string> { { "X-toto", "tatata" } });
@@ -190,7 +204,7 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_params()
{
// given
var spec = Requests.WithPath("/foo").WithParam("bar", "1", "2");
var spec = RequestBuilder.WithPath("/foo").WithParam("bar", "1", "2");
// when
var request = new Request("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
@@ -203,7 +217,7 @@ namespace WireMock.Net.Tests
public void Should_exclude_requests_not_matching_given_params()
{
// given
var spec = Requests.WithPath("/foo").WithParam("bar", "1");
var spec = RequestBuilder.WithPath("/foo").WithParam("bar", "1");
// when
var request = new Request("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string>());

View File

@@ -1,48 +0,0 @@
using System.Diagnostics.CodeAnalysis;
using NFluent;
using NUnit.Framework;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable InconsistentNaming
namespace WireMock.Net.Tests
{
[TestFixture]
public class WildcardPatternMatcherTests
{
[Test]
public void Should_evaluate_patterns()
{
// Positive Tests
Check.That(WildcardPatternMatcher.MatchWildcardString("*", string.Empty)).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("?", " ")).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("*", "a")).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("*", "ab")).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("?", "a")).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("*?", "abc")).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("?*", "abc")).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("*abc", "abc")).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("*abc*", "abc")).IsTrue();
Check.That(WildcardPatternMatcher.MatchWildcardString("*a*bc*", "aXXXbc")).IsTrue();
// Negative Tests
Check.That(WildcardPatternMatcher.MatchWildcardString("*a", string.Empty)).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("a*", string.Empty)).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("?", string.Empty)).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("*b*", "a")).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("b*a", "ab")).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("??", "a")).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("*?", string.Empty)).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("??*", "a")).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("*abc", "abX")).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("*abc*", "Xbc")).IsFalse();
Check.That(WildcardPatternMatcher.MatchWildcardString("*a*bc*", "ac")).IsFalse();
}
}
}

View File

@@ -66,7 +66,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RequestsTests.cs" />
<Compile Include="RequestTests.cs" />
<Compile Include="WildcardPatternMatcherTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />