Handlebars #4

This commit is contained in:
Stef Heyenrath
2017-01-18 22:47:40 +01:00
parent 1d2c7fcf79
commit 8dfcf7288f
18 changed files with 501 additions and 362 deletions

View File

@@ -24,12 +24,16 @@ namespace WireMock.Net.ConsoleApplication
.WithHeader("Content-Type", "application/json")
.WithBody(@"{ ""result"": ""/x with FUNC 200""}"));
// http://localhost:8080/gffgfgf/sddsds?start=1000&stop=1&stop=2
server
.Given(Request.WithUrl("/*").UsingGet())
.RespondWith(Response
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody(@"{ ""msg"": ""Hello world!""}")
.WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}")
.WithBody(@"{""msg"": ""Hello world! : {{request.url}} : {{request.path}} : {{request.query.start}} : {{request.query.stop.[0]}}""")
.AfterDelay(TimeSpan.FromMilliseconds(100))
.WithTransformer()
);
server

View File

@@ -0,0 +1,57 @@
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
namespace WireMock.Extensions
{
/// <summary>
/// Dictionary Extensions
/// </summary>
public static class DictionaryExtensions
{
public static dynamic ToExpandoObject<T>(this IDictionary<string, T> dictionary)
{
dynamic expando = new ExpandoObject();
var expandoDic = (IDictionary<string, object>)expando;
// go through the items in the dictionary and copy over the key value pairs)
foreach (var kvp in dictionary)
{
// if the value can also be turned into an ExpandoObject, then do it!
var value = kvp.Value as IDictionary<string, object>;
if (value != null)
{
var expandoValue = value.ToExpandoObject();
expandoDic.Add(kvp.Key, expandoValue);
}
else if (kvp.Value is ICollection)
{
// iterate through the collection and convert any strin-object dictionaries
// along the way into expando objects
var itemList = new List<object>();
foreach (var item in (ICollection)kvp.Value)
{
var objects = item as IDictionary<string, object>;
if (objects != null)
{
var expandoItem = objects.ToExpandoObject();
itemList.Add(expandoItem);
}
else
{
itemList.Add(item);
}
}
expandoDic.Add(kvp.Key, itemList);
}
else
{
expandoDic.Add(kvp.Key, kvp.Value);
}
}
return expando;
}
}
}

View File

@@ -31,10 +31,10 @@ namespace WireMock
/// </returns>
public RequestMessage Map(HttpListenerRequest listenerRequest)
{
var path = listenerRequest.Url.AbsolutePath;
var query = listenerRequest.Url.Query;
var verb = listenerRequest.HttpMethod;
var body = GetRequestBody(listenerRequest);
string path = listenerRequest.Url.AbsolutePath;
string query = listenerRequest.Url.Query;
string verb = listenerRequest.HttpMethod;
string body = GetRequestBody(listenerRequest);
var listenerHeaders = listenerRequest.Headers;
var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]);

View File

@@ -27,7 +27,7 @@ namespace WireMock.RequestBuilders
/// <summary>
/// The requests.
/// </summary>
public class Request : CompositeRequestSpec, IVerbRequestBuilder, IHeadersRequestBuilder, IParamsRequestBuilder
public class Request : CompositeRequestSpec, IVerbRequestBuilder
{
/// <summary>
/// The _request specs.

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using WireMock.Extensions;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -26,11 +28,6 @@ namespace WireMock
/// </summary>
public class RequestMessage
{
/// <summary>
/// The _params.
/// </summary>
private readonly IDictionary<string, List<string>> _params = new Dictionary<string, List<string>>();
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
/// </summary>
@@ -58,7 +55,7 @@ namespace WireMock
query = query.Substring(1);
}
_params = query.Split('&').Aggregate(
Parameters = query.Split('&').Aggregate(
new Dictionary<string, List<string>>(),
(dict, term) =>
{
@@ -72,7 +69,19 @@ namespace WireMock
return dict;
});
Parameters = _params;
var tmpDictionary = new Dictionary<string, object>();
foreach (var parameter in Parameters.Where(p => p.Value.Any()))
{
if (parameter.Value.Count == 1)
{
tmpDictionary.Add(parameter.Key, parameter.Value.First());
}
else
{
tmpDictionary.Add(parameter.Key, parameter.Value);
}
}
Query = tmpDictionary.ToExpandoObject();
}
Path = path;
@@ -88,12 +97,12 @@ namespace WireMock
{
get
{
if (!_params.Any())
if (!Parameters.Any())
{
return Path;
}
return Path + "?" + string.Join("&", _params.SelectMany(kv => kv.Value.Select(value => kv.Key + "=" + value)));
return Path + "?" + string.Join("&", Parameters.SelectMany(kv => kv.Value.Select(value => kv.Key + "=" + value)));
}
}
@@ -113,9 +122,14 @@ namespace WireMock
public IDictionary<string, string> Headers { get; }
/// <summary>
/// Gets the parameters.
/// Gets the query parameters.
/// </summary>
public IDictionary<string, List<string>> Parameters { get; }
public IDictionary<string, List<string>> Parameters { get; } = new Dictionary<string, List<string>>();
/// <summary>
/// Gets the query as object.
/// </summary>
public dynamic Query { get; }
/// <summary>
/// Gets the body.
@@ -133,7 +147,7 @@ namespace WireMock
/// </returns>
public List<string> GetParameter(string key)
{
return _params.ContainsKey(key) ? _params[key] : new List<string>();
return Parameters.ContainsKey(key) ? Parameters[key] : new List<string>();
}
}
}

View File

@@ -12,8 +12,8 @@
/// The body.
/// </param>
/// <returns>
/// The <see cref="IDelayResponseBuilder"/>.
/// The <see cref="IResponseBuilder"/>.
/// </returns>
IDelayResponseBuilder WithBody(string body);
IResponseBuilder WithBody(string body);
}
}

View File

@@ -14,8 +14,8 @@ namespace WireMock.ResponseBuilders
/// The delay.
/// </param>
/// <returns>
/// The <see cref="IProvideResponses"/>.
/// The <see cref="IResponseBuilder"/>.
/// </returns>
IProvideResponses AfterDelay(TimeSpan delay);
IResponseBuilder AfterDelay(TimeSpan delay);
}
}

View File

@@ -15,8 +15,8 @@
/// The value.
/// </param>
/// <returns>
/// The <see cref="IHeadersResponseBuilder"/>.
/// The <see cref="IResponseBuilder"/>.
/// </returns>
IHeadersResponseBuilder WithHeader(string name, string value);
IResponseBuilder WithHeader(string name, string value);
}
}

View File

@@ -0,0 +1,9 @@
namespace WireMock.ResponseBuilders
{
/// <summary>
/// The ResponseBuilder interface.
/// </summary>
public interface IResponseBuilder : ITransformResponseBuilder
{
}
}

View File

@@ -0,0 +1,16 @@
namespace WireMock.ResponseBuilders
{
/// <summary>
/// The BodyResponseBuilder interface.
/// </summary>
public interface ITransformResponseBuilder : IHeadersResponseBuilder
{
/// <summary>
/// The with transformer.
/// </summary>
/// <returns>
/// The <see cref="IResponseBuilder"/>.
/// </returns>
IResponseBuilder WithTransformer();
}
}

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using HandlebarsDotNet;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -21,7 +23,7 @@ namespace WireMock.ResponseBuilders
/// <summary>
/// The responses.
/// </summary>
public class Response : IHeadersResponseBuilder
public class Response : IResponseBuilder
{
/// <summary>
/// The _response.
@@ -33,6 +35,8 @@ namespace WireMock.ResponseBuilders
/// </summary>
private TimeSpan _delay = TimeSpan.Zero;
private bool _useTransformer;
/// <summary>
/// Initializes a new instance of the <see cref="Response"/> class.
/// </summary>
@@ -47,8 +51,8 @@ namespace WireMock.ResponseBuilders
/// <summary>
/// The with Success status code.
/// </summary>
/// <returns>The <see cref="IHeadersResponseBuilder"/>.</returns>
public static IHeadersResponseBuilder WithSuccess()
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
public static IResponseBuilder WithSuccess()
{
return WithStatusCode(200);
}
@@ -56,8 +60,8 @@ namespace WireMock.ResponseBuilders
/// <summary>
/// The with NotFound status code.
/// </summary>
/// <returns>The <see cref="IHeadersResponseBuilder"/>.</returns>
public static IHeadersResponseBuilder WithNotFound()
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
public static IResponseBuilder WithNotFound()
{
return WithStatusCode(404);
}
@@ -69,9 +73,9 @@ namespace WireMock.ResponseBuilders
/// The code.
/// </param>
/// <returns>
/// The <see cref="IHeadersResponseBuilder"/>.
/// The <see cref="IResponseBuilder"/>.
/// </returns>
public static IHeadersResponseBuilder WithStatusCode(int code)
public static IResponseBuilder WithStatusCode(int code)
{
var response = new ResponseMessage { StatusCode = code };
return new Response(response);
@@ -88,7 +92,28 @@ namespace WireMock.ResponseBuilders
/// </returns>
public async Task<ResponseMessage> ProvideResponse(RequestMessage requestMessage)
{
if (_useTransformer)
{
var template = new { request = requestMessage };
// Body
var templateBody = Handlebars.Compile(_responseMessage.Body);
_responseMessage.Body = templateBody(template);
// Headers
var newHeaders = new Dictionary<string, string>();
foreach (var header in _responseMessage.Headers)
{
var templateHeaderKey = Handlebars.Compile(header.Key);
var templateHeaderValue = Handlebars.Compile(header.Value);
newHeaders.Add(templateHeaderKey(template), templateHeaderValue(template));
}
_responseMessage.Headers = newHeaders;
}
await Task.Delay(_delay);
return _responseMessage;
}
@@ -102,9 +127,9 @@ namespace WireMock.ResponseBuilders
/// The value.
/// </param>
/// <returns>
/// The <see cref="IHeadersResponseBuilder"/>.
/// The <see cref="IResponseBuilder"/>.
/// </returns>
public IHeadersResponseBuilder WithHeader(string name, string value)
public IResponseBuilder WithHeader(string name, string value)
{
_responseMessage.AddHeader(name, value);
return this;
@@ -117,14 +142,26 @@ namespace WireMock.ResponseBuilders
/// The body.
/// </param>
/// <returns>
/// The <see cref="IDelayResponseBuilder"/>.
/// The <see cref="IResponseBuilder"/>.
/// </returns>
public IDelayResponseBuilder WithBody(string body)
public IResponseBuilder WithBody(string body)
{
_responseMessage.Body = body;
return this;
}
/// <summary>
/// The with transformer.
/// </summary>
/// <returns>
/// The <see cref="IResponseBuilder"/>.
/// </returns>
public IResponseBuilder WithTransformer()
{
_useTransformer = true;
return this;
}
/// <summary>
/// The after delay.
/// </summary>
@@ -134,7 +171,7 @@ namespace WireMock.ResponseBuilders
/// <returns>
/// The <see cref="IProvideResponses"/>.
/// </returns>
public IProvideResponses AfterDelay(TimeSpan delay)
public IResponseBuilder AfterDelay(TimeSpan delay)
{
_delay = delay;
return this;

View File

@@ -23,15 +23,10 @@ namespace WireMock
/// </summary>
public class ResponseMessage
{
/// <summary>
/// The _headers.
/// </summary>
private readonly IDictionary<string, string> _headers = new ConcurrentDictionary<string, string>();
/// <summary>
/// Gets the headers.
/// </summary>
public IDictionary<string, string> Headers => _headers;
public IDictionary<string, string> Headers { get; set; } = new ConcurrentDictionary<string, string>();
/// <summary>
/// Gets or sets the status code.
@@ -54,7 +49,7 @@ namespace WireMock
/// </param>
public void AddHeader(string name, string value)
{
_headers.Add(name, value);
Headers.Add(name, value);
}
}
}

View File

@@ -30,6 +30,9 @@
"frameworks": {
"net45": {
"dependencies": {
"Handlebars.Net": "1.8.0"
},
"frameworkAssemblies": {
}
}

View File

@@ -8,20 +8,20 @@ using NUnit.Framework;
using WireMock.Http;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
"SA1101:PrefixLocalCallsWithThis",
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",
SuppressMessage("StyleCop.CSharp.NamingRules",
"SA1309:FieldNamesMustNotBeginWithUnderscore",
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
[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 ArrangeThisQualifier
// ReSharper disable InconsistentNaming
@@ -68,8 +68,11 @@ namespace WireMock.Net.Tests
public void Should_map_body_from_original_response()
{
// given
var response = new ResponseMessage();
response.Body = "Hello !!!";
var response = new ResponseMessage
{
Body = "Hello !!!"
};
var httpListenerResponse = CreateHttpListenerResponse();
// when
@@ -78,6 +81,7 @@ namespace WireMock.Net.Tests
// then
var responseMessage = ToResponseMessage(httpListenerResponse);
Check.That(responseMessage).IsNotNull();
var contentTask = responseMessage.Content.ReadAsStringAsync();
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
}
@@ -104,7 +108,7 @@ namespace WireMock.Net.Tests
var responseReady = new AutoResetEvent(false);
HttpListenerResponse response = null;
_server = new TinyHttpServer(
urlPrefix,
urlPrefix,
context =>
{
response = context.Response;

View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
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 RequestMessageTests
{
[Test]
public void Should_handle_empty_query()
{
// given
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(request.GetParameter("foo")).IsEmpty();
}
[Test]
public void Should_parse_query_params()
{
// given
var request = new RequestMessage("/foo", "foo=bar&multi=1&multi=2", "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(request.GetParameter("foo")).Contains("bar");
Check.That(request.GetParameter("multi")).Contains("1");
Check.That(request.GetParameter("multi")).Contains("2");
}
}
}

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
@@ -18,25 +19,277 @@ namespace WireMock.Net.Tests
public class RequestTests
{
[Test]
public void Should_handle_empty_query()
public void Should_specify_requests_matching_given_url()
{
// given
var spec = Request.WithUrl("/foo");
// when
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(request.GetParameter("foo")).IsEmpty();
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_parse_query_params()
public void Should_specify_requests_matching_given_url_prefix()
{
// given
var request = new RequestMessage("/foo", "foo=bar&multi=1&multi=2", "blabla", "whatever", new Dictionary<string, string>());
var spec = Request.WithUrl("/foo*");
// when
var request = new RequestMessage("/foo/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(request.GetParameter("foo")).Contains("bar");
Check.That(request.GetParameter("multi")).Contains("1");
Check.That(request.GetParameter("multi")).Contains("2");
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_url()
{
// given
var spec = Request.WithUrl("/foo");
// when
var request = new RequestMessage("/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_path()
{
// given
var spec = Request.WithPath("/foo");
// when
var request = new RequestMessage("/foo", "?param=1", "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_put()
{
// given
var spec = Request.WithUrl("/foo").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_post()
{
// given
var spec = Request.WithUrl("/foo").UsingPost();
// when
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_get()
{
// given
var spec = Request.WithUrl("/foo").UsingGet();
// when
var request = new RequestMessage("/foo", string.Empty, "GET", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_delete()
{
// given
var spec = Request.WithUrl("/foo").UsingDelete();
// when
var request = new RequestMessage("/foo", string.Empty, "DELETE", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_head()
{
// given
var spec = Request.WithUrl("/foo").UsingHead();
// when
var request = new RequestMessage("/foo", string.Empty, "HEAD", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_matching_given_url_but_not_http_method()
{
// given
var spec = Request.WithUrl("/foo").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_matching_given_http_method_but_not_url()
{
// given
var spec = Request.WithUrl("/bar").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_url_and_headers()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers_ignorecase()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "abc", false);
// when
var request = new RequestMessage("/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 = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "TaTaTa" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(".*Hello world!.*");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body_as_wildcard()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_body()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_params()
{
// given
var spec = Request.WithPath("/foo").WithParam("bar", "1", "2");
// when
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_params_func()
{
// given
var spec = Request.WithPath("/foo").WithParam(p => p.ContainsKey("bar") && (p["bar"].Contains("1") || p["bar"].Contains("2")));
// when
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_params()
{
// given
var spec = Request.WithPath("/foo").WithParam("bar", "1");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
}
}

View File

@@ -1,295 +0,0 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using NFluent;
using NUnit.Framework;
using WireMock.RequestBuilders;
[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 RequestsTests
{
[Test]
public void Should_specify_requests_matching_given_url()
{
// given
var spec = Request.WithUrl("/foo");
// when
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_prefix()
{
// given
var spec = Request.WithUrl("/foo*");
// when
var request = new RequestMessage("/foo/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_url()
{
// given
var spec = Request.WithUrl("/foo");
// when
var request = new RequestMessage("/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_path()
{
// given
var spec = Request.WithPath("/foo");
// when
var request = new RequestMessage("/foo", "?param=1", "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_put()
{
// given
var spec = Request.WithUrl("/foo").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_post()
{
// given
var spec = Request.WithUrl("/foo").UsingPost();
// when
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_get()
{
// given
var spec = Request.WithUrl("/foo").UsingGet();
// when
var request = new RequestMessage("/foo", string.Empty, "GET", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_delete()
{
// given
var spec = Request.WithUrl("/foo").UsingDelete();
// when
var request = new RequestMessage("/foo", string.Empty, "DELETE", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_head()
{
// given
var spec = Request.WithUrl("/foo").UsingHead();
// when
var request = new RequestMessage("/foo", string.Empty, "HEAD", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_matching_given_url_but_not_http_method()
{
// given
var spec = Request.WithUrl("/foo").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_matching_given_http_method_but_not_url()
{
// given
var spec = Request.WithUrl("/bar").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_url_and_headers()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers_ignorecase()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "abc", false);
// when
var request = new RequestMessage("/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 = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "TaTaTa" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(".*Hello world!.*");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body_as_wildcard()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_body()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_params()
{
// given
var spec = Request.WithPath("/foo").WithParam("bar", "1", "2");
// when
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_params_func()
{
// given
var spec = Request.WithPath("/foo").WithParam(p => p.ContainsKey("bar") && (p["bar"].Contains("1") || p["bar"].Contains("2")));
// when
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_params()
{
// given
var spec = Request.WithPath("/foo").WithParam("bar", "1");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
}
}

View File

@@ -64,8 +64,8 @@
<Compile Include="HttpListenerResponseMapperTests.cs" />
<Compile Include="Http\TinyHttpServerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RequestsTests.cs" />
<Compile Include="RequestTests.cs" />
<Compile Include="RequestMessageTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />