Add ability to provide multiple values for headers in response (#59)

* Add ability to provide multiple values for headers

* Updated json model
This commit is contained in:
Oleksandr Liakhevych
2017-10-27 22:49:03 +03:00
committed by Stef Heyenrath
parent a96b7bca1e
commit d134684bcb
27 changed files with 305 additions and 187 deletions

View File

@@ -60,7 +60,7 @@ namespace WireMock.Admin.Mappings
/// <summary>
/// Gets or sets the headers.
/// </summary>
public IDictionary<string, string> Headers { get; set; }
public IDictionary<string, object> Headers { get; set; }
/// <summary>
/// Gets or sets the Headers (Raw).

View File

@@ -43,7 +43,7 @@ namespace WireMock.Admin.Requests
/// <summary>
/// Gets or sets the Headers.
/// </summary>
public IDictionary<string, string> Headers { get; set; }
public IDictionary<string, WireMockList<string>> Headers { get; set; }
/// <summary>
/// Gets or sets the Cookies.

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using WireMock.Admin.Mappings;
using WireMock.Util;
namespace WireMock.Admin.Requests
{
@@ -16,7 +17,7 @@ namespace WireMock.Admin.Requests
/// <summary>
/// Gets the headers.
/// </summary>
public IDictionary<string, string> Headers { get; set; }
public IDictionary<string, WireMockList<string>> Headers { get; set; }
/// <summary>
/// Gets or sets the body destination (SameAsSource, String or Bytes).

View File

@@ -52,7 +52,7 @@ namespace WireMock.Http
{
foreach (var headerName in requestMessage.Headers.Keys.Where(k => k.ToUpper() != "HOST"))
{
httpRequestMessage.Headers.TryAddWithoutValidation(headerName, new[] { requestMessage.Headers[headerName] });
httpRequestMessage.Headers.TryAddWithoutValidation(headerName, requestMessage.Headers[headerName]);
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock.Matchers.Request
@@ -14,7 +15,7 @@ namespace WireMock.Matchers.Request
/// <summary>
/// The functions
/// </summary>
public Func<IDictionary<string, string>, bool>[] Funcs { get; }
public Func<IDictionary<string, string[]>, bool>[] Funcs { get; }
/// <summary>
/// The name
@@ -59,7 +60,7 @@ namespace WireMock.Matchers.Request
/// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
/// </summary>
/// <param name="funcs">The funcs.</param>
public RequestMessageHeaderMatcher([NotNull] params Func<IDictionary<string, string>, bool>[] funcs)
public RequestMessageHeaderMatcher([NotNull] params Func<IDictionary<string, string[]>, bool>[] funcs)
{
Check.NotNull(funcs, nameof(funcs));
@@ -86,7 +87,7 @@ namespace WireMock.Matchers.Request
return MatchScores.Mismatch;
if (Funcs != null)
return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Headers)));
return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Headers.ToDictionary(entry => entry.Key, entry => entry.Value.ToArray()))));
if (Matchers == null)
return MatchScores.Mismatch;
@@ -94,8 +95,8 @@ namespace WireMock.Matchers.Request
if (!requestMessage.Headers.ContainsKey(Name))
return MatchScores.Mismatch;
string value = requestMessage.Headers[Name];
return Matchers.Max(m => m.IsMatch(value));
WireMockList<string> list = requestMessage.Headers[Name];
return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ?
}
}
}

View File

@@ -57,13 +57,13 @@ namespace WireMock.Owin
body = bodyEncoding.GetBytes(bodyAsString);
}
Dictionary<string, string> headers = null;
Dictionary<string, string[]> headers = null;
if (request.Headers.Any())
{
headers = new Dictionary<string, string>();
headers = new Dictionary<string, string[]>();
foreach (var header in request.Headers)
{
headers.Add(header.Key, header.Value.FirstOrDefault());
headers.Add(header.Key, header.Value);
}
}

View File

@@ -35,9 +35,16 @@ namespace WireMock.Owin
if (responseMessage.Headers.ContainsKey(HttpKnownHeaderNames.ContentType))
{
response.ContentType = responseMessage.Headers[HttpKnownHeaderNames.ContentType];
response.ContentType = responseMessage.Headers[HttpKnownHeaderNames.ContentType].FirstOrDefault();
}
responseMessage.Headers.Where(h => h.Key != HttpKnownHeaderNames.ContentType).ToList().ForEach(pair => response.Headers.Append(pair.Key, pair.Value));
var headers = responseMessage.Headers.Where(h => h.Key != HttpKnownHeaderNames.ContentType).ToList();
#if !NETSTANDARD
headers.ForEach(pair => response.Headers.AppendValues(pair.Key, pair.Value.ToArray()));
#else
headers.ForEach(pair => response.Headers.Append(pair.Key, pair.Value.ToArray()));
#endif
if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == null)
{
@@ -49,7 +56,7 @@ namespace WireMock.Owin
await response.Body.WriteAsync(responseMessage.BodyAsBytes, 0, responseMessage.BodyAsBytes.Length);
return;
}
if (responseMessage.BodyAsFile != null)
{
byte[] bytes = File.ReadAllBytes(responseMessage.BodyAsFile);

View File

@@ -4,6 +4,7 @@ using WireMock.Logging;
using WireMock.Matchers.Request;
using System.Linq;
using WireMock.Matchers;
using WireMock.Util;
#if !NETSTANDARD
using Microsoft.Owin;
#else
@@ -101,8 +102,8 @@ namespace WireMock.Owin
if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
{
bool present = request.Headers.TryGetValue("Authorization", out string authorization);
if (!present || _options.AuthorizationMatcher.IsMatch(authorization) < MatchScores.Perfect)
bool present = request.Headers.TryGetValue("Authorization", out WireMockList<string> authorization);
if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfect)
{
response = new ResponseMessage { StatusCode = 401 };
return;

View File

@@ -33,7 +33,7 @@ namespace WireMock.RequestBuilders
/// </summary>
/// <param name="funcs">The headers funcs.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithHeader([NotNull] params Func<IDictionary<string, string>, bool>[] funcs);
IRequestBuilder WithHeader([NotNull] params Func<IDictionary<string, string[]>, bool>[] funcs);
/// <summary>
/// The with cookie.

View File

@@ -395,7 +395,7 @@ namespace WireMock.RequestBuilders
/// </summary>
/// <param name="funcs">The funcs.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithHeader(params Func<IDictionary<string, string>, bool>[] funcs)
public IRequestBuilder WithHeader(params Func<IDictionary<string, string[]>, bool>[] funcs)
{
Check.NotEmpty(funcs, nameof(funcs));

View File

@@ -41,7 +41,7 @@ namespace WireMock
/// <summary>
/// Gets the headers.
/// </summary>
public IDictionary<string, string> Headers { get; }
public IDictionary<string, WireMockList<string>> Headers { get; }
/// <summary>
/// Gets the cookies.
@@ -79,7 +79,7 @@ namespace WireMock
/// <param name="bodyEncoding">The body encoding</param>
/// <param name="headers">The headers.</param>
/// <param name="cookies">The cookies.</param>
public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] Encoding bodyEncoding = null, [CanBeNull] IDictionary<string, string> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] Encoding bodyEncoding = null, [CanBeNull] IDictionary<string, string[]> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
{
Check.NotNull(url, nameof(url));
Check.NotNull(method, nameof(method));
@@ -92,7 +92,7 @@ namespace WireMock
BodyAsBytes = bodyAsBytes;
Body = body;
BodyEncoding = bodyEncoding;
Headers = headers;
Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
Cookies = cookies;
Query = ParseQuery(url.Query);
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using WireMock.Util;
namespace WireMock.ResponseBuilders
{
@@ -12,15 +13,29 @@ namespace WireMock.ResponseBuilders
/// The with header.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
/// <param name="values">The values.</param>
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithHeader([NotNull] string name, string value);
IResponseBuilder WithHeader([NotNull] string name, params string[] values);
/// <summary>
/// The with headers.
/// </summary>
/// <param name="headers">The headers.</param>
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithHeaders([NotNull] IDictionary<string,string> headers);
IResponseBuilder WithHeaders([NotNull] IDictionary<string, string> headers);
/// <summary>
/// The with headers.
/// </summary>
/// <param name="headers">The headers.</param>
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithHeaders([NotNull] IDictionary<string, string[]> headers);
/// <summary>
/// The with headers.
/// </summary>
/// <param name="headers">The headers.</param>
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithHeaders([NotNull] IDictionary<string, WireMockList<string>> headers);
}
}

View File

@@ -1,14 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using WireMock.Validation;
using WireMock.Http;
using WireMock.Transformers;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock.ResponseBuilders
{
@@ -126,22 +128,35 @@ namespace WireMock.ResponseBuilders
return WithStatusCode((int)HttpStatusCode.NotFound);
}
/// <summary>
/// The with header.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
public IResponseBuilder WithHeader(string name, string value)
/// <inheritdoc cref="IHeadersResponseBuilder.WithHeader(string, string[])"/>
public IResponseBuilder WithHeader(string name, params string[] values)
{
Check.NotNull(name, nameof(name));
ResponseMessage.AddHeader(name, value);
ResponseMessage.AddHeader(name, values);
return this;
}
/// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders"/>
/// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, string})"/>
public IResponseBuilder WithHeaders(IDictionary<string, string> headers)
{
Check.NotNull(headers, nameof(headers));
ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
return this;
}
/// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, string[]})"/>
public IResponseBuilder WithHeaders(IDictionary<string, string[]> headers)
{
Check.NotNull(headers, nameof(headers));
ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
return this;
}
/// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, WireMockList{string}})"/>
public IResponseBuilder WithHeaders(IDictionary<string, WireMockList<string>> headers)
{
ResponseMessage.Headers = headers;
return this;

View File

@@ -1,6 +1,9 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock
{
@@ -12,7 +15,7 @@ namespace WireMock
/// <summary>
/// Gets the headers.
/// </summary>
public IDictionary<string, string> Headers { get; set; } = new ConcurrentDictionary<string, string>();
public IDictionary<string, WireMockList<string>> Headers { get; set; } = new ConcurrentDictionary<string, WireMockList<string>>();
/// <summary>
/// Gets or sets the status code.
@@ -55,17 +58,29 @@ namespace WireMock
public Encoding BodyEncoding { get; set; } = new UTF8Encoding(false);
/// <summary>
/// The add header.
/// Adds the header.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="value">
/// The value.
/// </param>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
public void AddHeader(string name, string value)
{
Headers.Add(name, value);
Headers.Add(name, new WireMockList<string>(value));
}
/// <summary>
/// Adds the header.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="values">The values.</param>
public void AddHeader(string name, params string[] values)
{
Check.NotEmpty(values, nameof(values));
var newHeaderValues = Headers.TryGetValue(name, out WireMockList<string> existingValues)
? values.Union(existingValues).ToArray()
: values;
Headers[name] = new WireMockList<string>(newHeaderValues);
}
}
}

View File

@@ -8,6 +8,7 @@ using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Util;
namespace WireMock.Serialization
{
@@ -108,7 +109,7 @@ namespace WireMock.Serialization
{
mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
mappingModel.Response.Headers = response.ResponseMessage.Headers;
mappingModel.Response.Headers = Map(response.ResponseMessage.Headers);
mappingModel.Response.Body = response.ResponseMessage.Body;
mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes;
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile;
@@ -127,7 +128,24 @@ namespace WireMock.Serialization
return mappingModel;
}
public static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
private static IDictionary<string, object> Map(IDictionary<string, WireMockList<string>> dictionary)
{
if (dictionary == null)
{
return null;
}
var newDictionary = new Dictionary<string, object>();
foreach (var entry in dictionary)
{
object value = entry.Value.Count == 1 ? (object)entry.Value.ToString() : entry.Value;
newDictionary.Add(entry.Key, value);
}
return newDictionary;
}
private static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
{
if (matchers == null || !matchers.Any())
return null;
@@ -135,7 +153,7 @@ namespace WireMock.Serialization
return matchers.Select(Map).Where(x => x != null).ToArray();
}
public static MatcherModel Map([CanBeNull] IMatcher matcher)
private static MatcherModel Map([CanBeNull] IMatcher matcher)
{
if (matcher == null)
return null;
@@ -150,7 +168,7 @@ namespace WireMock.Serialization
};
}
public static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs)
private static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs)
{
if (funcs == null || !funcs.Any())
return null;
@@ -158,7 +176,7 @@ namespace WireMock.Serialization
return funcs.Select(Map).Where(x => x != null).ToArray();
}
public static string Map<T>([CanBeNull] Func<T, bool> func)
private static string Map<T>([CanBeNull] Func<T, bool> func)
{
return func?.ToString();
}

View File

@@ -3,22 +3,22 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using WireMock.Admin.Mappings;
using WireMock.Admin.Requests;
using WireMock.Admin.Settings;
using WireMock.Http;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Serialization;
using WireMock.Settings;
using WireMock.Util;
using WireMock.Validation;
using WireMock.Http;
using System.Threading.Tasks;
using WireMock.Settings;
using WireMock.Serialization;
namespace WireMock.Server
{
@@ -519,12 +519,15 @@ namespace WireMock.Server
{
string path = requestModel.Path as string;
if (path != null)
{
requestBuilder = requestBuilder.WithPath(path);
}
else
{
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
if (pathModel?.Matchers != null)
requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(MappingConverter.Map).ToArray());
requestBuilder =
requestBuilder.WithPath(pathModel.Matchers.Select(MappingConverter.Map).ToArray());
}
}
@@ -532,17 +535,22 @@ namespace WireMock.Server
{
string url = requestModel.Url as string;
if (url != null)
{
requestBuilder = requestBuilder.WithUrl(url);
}
else
{
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
if (urlModel?.Matchers != null)
requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(MappingConverter.Map).ToArray());
requestBuilder =
requestBuilder.WithUrl(urlModel.Matchers.Select(MappingConverter.Map).ToArray());
}
}
if (requestModel.Methods != null)
{
requestBuilder = requestBuilder.UsingVerb(requestModel.Methods);
}
if (requestModel.Headers != null)
{
@@ -603,7 +611,12 @@ namespace WireMock.Server
if (responseModel.Headers != null)
{
responseBuilder = responseBuilder.WithHeaders(responseModel.Headers);
foreach (var entry in responseModel.Headers)
{
responseBuilder = entry.Value is string value ?
responseBuilder.WithHeader(entry.Key, value) :
responseBuilder.WithHeader(entry.Key, JsonUtils.ParseJTokenToObject<string[]>(entry.Value));
}
}
else if (responseModel.HeadersRaw != null)
{
@@ -647,7 +660,7 @@ namespace WireMock.Server
{
Body = JsonConvert.SerializeObject(result, _settings),
StatusCode = 200,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
Headers = new Dictionary<string, WireMockList<string>> { { "Content-Type", new WireMockList<string>("application/json") } }
};
}

View File

@@ -1,5 +1,7 @@
using HandlebarsDotNet;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using HandlebarsDotNet;
using WireMock.Util;
namespace WireMock.Transformers
{
@@ -16,13 +18,16 @@ namespace WireMock.Transformers
responseMessage.Body = templateBody(template);
// Headers
var newHeaders = new Dictionary<string, string>();
var newHeaders = new Dictionary<string, WireMockList<string>>();
foreach (var header in original.Headers)
{
var templateHeaderKey = Handlebars.Compile(header.Key);
var templateHeaderValue = Handlebars.Compile(header.Value);
var templateHeaderValues = header.Value
.Select(Handlebars.Compile)
.Select(func => func(template))
.ToArray();
newHeaders.Add(templateHeaderKey(template), templateHeaderValue(template));
newHeaders.Add(templateHeaderKey(template), new WireMockList<string>(templateHeaderValues));
}
responseMessage.Headers = newHeaders;

View File

@@ -7,13 +7,12 @@ namespace WireMock.Util
public static T ParseJTokenToObject<T>(object value)
{
if (value == null)
{
return default(T);
}
JToken token = value as JToken;
if (token == null)
return default(T);
return token.ToObject<T>();
var token = value as JToken;
return token == null ? default(T) : token.ToObject<T>();
}
}
}

View File

@@ -36,9 +36,6 @@ namespace WireMock.Util
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="string" /> that represents this instance.
/// </returns>
public override string ToString()
{
if (this != null && this.Any())