mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 01:08:49 +02:00
Handlebars #4
This commit is contained in:
@@ -24,12 +24,16 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
.WithHeader("Content-Type", "application/json")
|
.WithHeader("Content-Type", "application/json")
|
||||||
.WithBody(@"{ ""result"": ""/x with FUNC 200""}"));
|
.WithBody(@"{ ""result"": ""/x with FUNC 200""}"));
|
||||||
|
|
||||||
|
// http://localhost:8080/gffgfgf/sddsds?start=1000&stop=1&stop=2
|
||||||
server
|
server
|
||||||
.Given(Request.WithUrl("/*").UsingGet())
|
.Given(Request.WithUrl("/*").UsingGet())
|
||||||
.RespondWith(Response
|
.RespondWith(Response
|
||||||
.WithStatusCode(200)
|
.WithStatusCode(200)
|
||||||
.WithHeader("Content-Type", "application/json")
|
.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
|
server
|
||||||
|
|||||||
57
src/WireMock/Extensions/DictionaryExtensions.cs
Normal file
57
src/WireMock/Extensions/DictionaryExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,10 +31,10 @@ namespace WireMock
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public RequestMessage Map(HttpListenerRequest listenerRequest)
|
public RequestMessage Map(HttpListenerRequest listenerRequest)
|
||||||
{
|
{
|
||||||
var path = listenerRequest.Url.AbsolutePath;
|
string path = listenerRequest.Url.AbsolutePath;
|
||||||
var query = listenerRequest.Url.Query;
|
string query = listenerRequest.Url.Query;
|
||||||
var verb = listenerRequest.HttpMethod;
|
string verb = listenerRequest.HttpMethod;
|
||||||
var body = GetRequestBody(listenerRequest);
|
string body = GetRequestBody(listenerRequest);
|
||||||
var listenerHeaders = listenerRequest.Headers;
|
var listenerHeaders = listenerRequest.Headers;
|
||||||
var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]);
|
var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]);
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace WireMock.RequestBuilders
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The requests.
|
/// The requests.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Request : CompositeRequestSpec, IVerbRequestBuilder, IHeadersRequestBuilder, IParamsRequestBuilder
|
public class Request : CompositeRequestSpec, IVerbRequestBuilder
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The _request specs.
|
/// The _request specs.
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using WireMock.Extensions;
|
||||||
|
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
||||||
@@ -26,11 +28,6 @@ namespace WireMock
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class RequestMessage
|
public class RequestMessage
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The _params.
|
|
||||||
/// </summary>
|
|
||||||
private readonly IDictionary<string, List<string>> _params = new Dictionary<string, List<string>>();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -58,7 +55,7 @@ namespace WireMock
|
|||||||
query = query.Substring(1);
|
query = query.Substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
_params = query.Split('&').Aggregate(
|
Parameters = query.Split('&').Aggregate(
|
||||||
new Dictionary<string, List<string>>(),
|
new Dictionary<string, List<string>>(),
|
||||||
(dict, term) =>
|
(dict, term) =>
|
||||||
{
|
{
|
||||||
@@ -72,7 +69,19 @@ namespace WireMock
|
|||||||
return dict;
|
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;
|
Path = path;
|
||||||
@@ -88,12 +97,12 @@ namespace WireMock
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (!_params.Any())
|
if (!Parameters.Any())
|
||||||
{
|
{
|
||||||
return Path;
|
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; }
|
public IDictionary<string, string> Headers { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the parameters.
|
/// Gets the query parameters.
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// Gets the body.
|
/// Gets the body.
|
||||||
@@ -133,7 +147,7 @@ namespace WireMock
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public List<string> GetParameter(string key)
|
public List<string> GetParameter(string key)
|
||||||
{
|
{
|
||||||
return _params.ContainsKey(key) ? _params[key] : new List<string>();
|
return Parameters.ContainsKey(key) ? Parameters[key] : new List<string>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,8 +12,8 @@
|
|||||||
/// The body.
|
/// The body.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// The <see cref="IDelayResponseBuilder"/>.
|
/// The <see cref="IResponseBuilder"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
IDelayResponseBuilder WithBody(string body);
|
IResponseBuilder WithBody(string body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,8 +14,8 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// The delay.
|
/// The delay.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// The <see cref="IProvideResponses"/>.
|
/// The <see cref="IResponseBuilder"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
IProvideResponses AfterDelay(TimeSpan delay);
|
IResponseBuilder AfterDelay(TimeSpan delay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,8 +15,8 @@
|
|||||||
/// The value.
|
/// The value.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// The <see cref="IHeadersResponseBuilder"/>.
|
/// The <see cref="IResponseBuilder"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
IHeadersResponseBuilder WithHeader(string name, string value);
|
IResponseBuilder WithHeader(string name, string value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
9
src/WireMock/ResponseBuilders/IResponseBuilder.cs
Normal file
9
src/WireMock/ResponseBuilders/IResponseBuilder.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace WireMock.ResponseBuilders
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The ResponseBuilder interface.
|
||||||
|
/// </summary>
|
||||||
|
public interface IResponseBuilder : ITransformResponseBuilder
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs
Normal file
16
src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using HandlebarsDotNet;
|
||||||
|
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
||||||
@@ -21,7 +23,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The responses.
|
/// The responses.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Response : IHeadersResponseBuilder
|
public class Response : IResponseBuilder
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The _response.
|
/// The _response.
|
||||||
@@ -33,6 +35,8 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private TimeSpan _delay = TimeSpan.Zero;
|
private TimeSpan _delay = TimeSpan.Zero;
|
||||||
|
|
||||||
|
private bool _useTransformer;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="Response"/> class.
|
/// Initializes a new instance of the <see cref="Response"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -47,8 +51,8 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The with Success status code.
|
/// The with Success status code.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The <see cref="IHeadersResponseBuilder"/>.</returns>
|
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||||
public static IHeadersResponseBuilder WithSuccess()
|
public static IResponseBuilder WithSuccess()
|
||||||
{
|
{
|
||||||
return WithStatusCode(200);
|
return WithStatusCode(200);
|
||||||
}
|
}
|
||||||
@@ -56,8 +60,8 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The with NotFound status code.
|
/// The with NotFound status code.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The <see cref="IHeadersResponseBuilder"/>.</returns>
|
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||||
public static IHeadersResponseBuilder WithNotFound()
|
public static IResponseBuilder WithNotFound()
|
||||||
{
|
{
|
||||||
return WithStatusCode(404);
|
return WithStatusCode(404);
|
||||||
}
|
}
|
||||||
@@ -69,9 +73,9 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// The code.
|
/// The code.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// The <see cref="IHeadersResponseBuilder"/>.
|
/// The <see cref="IResponseBuilder"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public static IHeadersResponseBuilder WithStatusCode(int code)
|
public static IResponseBuilder WithStatusCode(int code)
|
||||||
{
|
{
|
||||||
var response = new ResponseMessage { StatusCode = code };
|
var response = new ResponseMessage { StatusCode = code };
|
||||||
return new Response(response);
|
return new Response(response);
|
||||||
@@ -88,7 +92,28 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public async Task<ResponseMessage> ProvideResponse(RequestMessage requestMessage)
|
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);
|
await Task.Delay(_delay);
|
||||||
|
|
||||||
return _responseMessage;
|
return _responseMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,9 +127,9 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// The value.
|
/// The value.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// The <see cref="IHeadersResponseBuilder"/>.
|
/// The <see cref="IResponseBuilder"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public IHeadersResponseBuilder WithHeader(string name, string value)
|
public IResponseBuilder WithHeader(string name, string value)
|
||||||
{
|
{
|
||||||
_responseMessage.AddHeader(name, value);
|
_responseMessage.AddHeader(name, value);
|
||||||
return this;
|
return this;
|
||||||
@@ -117,14 +142,26 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// The body.
|
/// The body.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// The <see cref="IDelayResponseBuilder"/>.
|
/// The <see cref="IResponseBuilder"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public IDelayResponseBuilder WithBody(string body)
|
public IResponseBuilder WithBody(string body)
|
||||||
{
|
{
|
||||||
_responseMessage.Body = body;
|
_responseMessage.Body = body;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The with transformer.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// The <see cref="IResponseBuilder"/>.
|
||||||
|
/// </returns>
|
||||||
|
public IResponseBuilder WithTransformer()
|
||||||
|
{
|
||||||
|
_useTransformer = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The after delay.
|
/// The after delay.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -134,7 +171,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// <returns>
|
/// <returns>
|
||||||
/// The <see cref="IProvideResponses"/>.
|
/// The <see cref="IProvideResponses"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public IProvideResponses AfterDelay(TimeSpan delay)
|
public IResponseBuilder AfterDelay(TimeSpan delay)
|
||||||
{
|
{
|
||||||
_delay = delay;
|
_delay = delay;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -23,15 +23,10 @@ namespace WireMock
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ResponseMessage
|
public class ResponseMessage
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The _headers.
|
|
||||||
/// </summary>
|
|
||||||
private readonly IDictionary<string, string> _headers = new ConcurrentDictionary<string, string>();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the headers.
|
/// Gets the headers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IDictionary<string, string> Headers => _headers;
|
public IDictionary<string, string> Headers { get; set; } = new ConcurrentDictionary<string, string>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the status code.
|
/// Gets or sets the status code.
|
||||||
@@ -54,7 +49,7 @@ namespace WireMock
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void AddHeader(string name, string value)
|
public void AddHeader(string name, string value)
|
||||||
{
|
{
|
||||||
_headers.Add(name, value);
|
Headers.Add(name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,6 +30,9 @@
|
|||||||
|
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"net45": {
|
"net45": {
|
||||||
|
"dependencies": {
|
||||||
|
"Handlebars.Net": "1.8.0"
|
||||||
|
},
|
||||||
"frameworkAssemblies": {
|
"frameworkAssemblies": {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,20 +8,20 @@ using NUnit.Framework;
|
|||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
|
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
||||||
"SA1101:PrefixLocalCallsWithThis",
|
"SA1101:PrefixLocalCallsWithThis",
|
||||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.NamingRules",
|
SuppressMessage("StyleCop.CSharp.NamingRules",
|
||||||
"SA1309:FieldNamesMustNotBeginWithUnderscore",
|
"SA1309:FieldNamesMustNotBeginWithUnderscore",
|
||||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||||
"SA1600:ElementsMustBeDocumented",
|
"SA1600:ElementsMustBeDocumented",
|
||||||
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
|
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||||
"SA1633:FileMustHaveHeader",
|
"SA1633:FileMustHaveHeader",
|
||||||
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
|
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
|
||||||
// ReSharper disable ArrangeThisQualifier
|
// ReSharper disable ArrangeThisQualifier
|
||||||
// ReSharper disable InconsistentNaming
|
// ReSharper disable InconsistentNaming
|
||||||
@@ -68,8 +68,11 @@ namespace WireMock.Net.Tests
|
|||||||
public void Should_map_body_from_original_response()
|
public void Should_map_body_from_original_response()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
var response = new ResponseMessage();
|
var response = new ResponseMessage
|
||||||
response.Body = "Hello !!!";
|
{
|
||||||
|
Body = "Hello !!!"
|
||||||
|
};
|
||||||
|
|
||||||
var httpListenerResponse = CreateHttpListenerResponse();
|
var httpListenerResponse = CreateHttpListenerResponse();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
@@ -78,6 +81,7 @@ namespace WireMock.Net.Tests
|
|||||||
// then
|
// then
|
||||||
var responseMessage = ToResponseMessage(httpListenerResponse);
|
var responseMessage = ToResponseMessage(httpListenerResponse);
|
||||||
Check.That(responseMessage).IsNotNull();
|
Check.That(responseMessage).IsNotNull();
|
||||||
|
|
||||||
var contentTask = responseMessage.Content.ReadAsStringAsync();
|
var contentTask = responseMessage.Content.ReadAsStringAsync();
|
||||||
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
|
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
|
||||||
}
|
}
|
||||||
@@ -104,7 +108,7 @@ namespace WireMock.Net.Tests
|
|||||||
var responseReady = new AutoResetEvent(false);
|
var responseReady = new AutoResetEvent(false);
|
||||||
HttpListenerResponse response = null;
|
HttpListenerResponse response = null;
|
||||||
_server = new TinyHttpServer(
|
_server = new TinyHttpServer(
|
||||||
urlPrefix,
|
urlPrefix,
|
||||||
context =>
|
context =>
|
||||||
{
|
{
|
||||||
response = context.Response;
|
response = context.Response;
|
||||||
|
|||||||
42
test/WireMock.Net.Tests/RequestMessageTests.cs
Normal file
42
test/WireMock.Net.Tests/RequestMessageTests.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,14 +2,15 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using WireMock.RequestBuilders;
|
||||||
|
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||||
"SA1600:ElementsMustBeDocumented",
|
"SA1600:ElementsMustBeDocumented",
|
||||||
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
|
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
|
||||||
[module:
|
[module:
|
||||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||||
"SA1633:FileMustHaveHeader",
|
"SA1633:FileMustHaveHeader",
|
||||||
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
|
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
|
||||||
// ReSharper disable InconsistentNaming
|
// ReSharper disable InconsistentNaming
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
@@ -18,25 +19,277 @@ namespace WireMock.Net.Tests
|
|||||||
public class RequestTests
|
public class RequestTests
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
public void Should_handle_empty_query()
|
public void Should_specify_requests_matching_given_url()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
|
var spec = Request.WithUrl("/foo");
|
||||||
|
|
||||||
|
// when
|
||||||
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
|
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(request.GetParameter("foo")).IsEmpty();
|
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Should_parse_query_params()
|
public void Should_specify_requests_matching_given_url_prefix()
|
||||||
{
|
{
|
||||||
// given
|
// 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
|
// then
|
||||||
Check.That(request.GetParameter("foo")).Contains("bar");
|
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||||
Check.That(request.GetParameter("multi")).Contains("1");
|
}
|
||||||
Check.That(request.GetParameter("multi")).Contains("2");
|
|
||||||
|
[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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -64,8 +64,8 @@
|
|||||||
<Compile Include="HttpListenerResponseMapperTests.cs" />
|
<Compile Include="HttpListenerResponseMapperTests.cs" />
|
||||||
<Compile Include="Http\TinyHttpServerTests.cs" />
|
<Compile Include="Http\TinyHttpServerTests.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="RequestsTests.cs" />
|
|
||||||
<Compile Include="RequestTests.cs" />
|
<Compile Include="RequestTests.cs" />
|
||||||
|
<Compile Include="RequestMessageTests.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
|
|||||||
Reference in New Issue
Block a user