diff --git a/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/11111110-a633-40e8-a244-5cb80bc0ab66.json b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/11111110-a633-40e8-a244-5cb80bc0ab66.json index 09216402..9c761369 100644 --- a/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/11111110-a633-40e8-a244-5cb80bc0ab66.json +++ b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/11111110-a633-40e8-a244-5cb80bc0ab66.json @@ -14,8 +14,9 @@ }, "Response": { "BodyAsJson": { "body": "static mapping" }, - "Headers": { - "Content-Type": "application/json" - } + "Headers": { + "Content-Type": "application/json", + "Test-X": [ "test 1", "test 2" ] + } } } \ No newline at end of file diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs index 65d2c2f1..8f002d98 100644 --- a/examples/WireMock.Net.ConsoleApplication/MainApp.cs +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -30,6 +30,13 @@ namespace WireMock.Net.ConsoleApplication // server.AllowPartialMapping(); + server + .Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*")) + .RespondWith(Response.Create() + .WithStatusCode(201) + .WithHeader("MyHeader", "application/json", "application/json2") + .WithBody(@"{ ""result"": ""data posted with 201""}")); + server .Given(Request.Create().WithPath("/file").UsingGet()) .RespondWith(Response.Create() @@ -92,13 +99,6 @@ namespace WireMock.Net.ConsoleApplication .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""data posted with FUNC 201""}")); - server - .Given(Request.Create().WithPath("/data", "/ax").UsingPost().WithHeader("Content-Type", "application/json*")) - .RespondWith(Response.Create() - .WithStatusCode(201) - .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""result"": ""data posted with 201""}")); - server .Given(Request.Create().WithPath("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))) .RespondWith(Response.Create() diff --git a/src/WireMock.Net/Admin/Mappings/ResponseModel.cs b/src/WireMock.Net/Admin/Mappings/ResponseModel.cs index fbefb23d..5627ee82 100644 --- a/src/WireMock.Net/Admin/Mappings/ResponseModel.cs +++ b/src/WireMock.Net/Admin/Mappings/ResponseModel.cs @@ -60,7 +60,7 @@ namespace WireMock.Admin.Mappings /// /// Gets or sets the headers. /// - public IDictionary Headers { get; set; } + public IDictionary Headers { get; set; } /// /// Gets or sets the Headers (Raw). diff --git a/src/WireMock.Net/Admin/Requests/LogRequestModel.cs b/src/WireMock.Net/Admin/Requests/LogRequestModel.cs index 4e7a44f3..c6e04fcc 100644 --- a/src/WireMock.Net/Admin/Requests/LogRequestModel.cs +++ b/src/WireMock.Net/Admin/Requests/LogRequestModel.cs @@ -43,7 +43,7 @@ namespace WireMock.Admin.Requests /// /// Gets or sets the Headers. /// - public IDictionary Headers { get; set; } + public IDictionary> Headers { get; set; } /// /// Gets or sets the Cookies. diff --git a/src/WireMock.Net/Admin/Requests/LogResponseModel.cs b/src/WireMock.Net/Admin/Requests/LogResponseModel.cs index 144248ea..edb88a77 100644 --- a/src/WireMock.Net/Admin/Requests/LogResponseModel.cs +++ b/src/WireMock.Net/Admin/Requests/LogResponseModel.cs @@ -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 /// /// Gets the headers. /// - public IDictionary Headers { get; set; } + public IDictionary> Headers { get; set; } /// /// Gets or sets the body destination (SameAsSource, String or Bytes). diff --git a/src/WireMock.Net/Http/HttpClientHelper.cs b/src/WireMock.Net/Http/HttpClientHelper.cs index b9a15977..9a9794d4 100644 --- a/src/WireMock.Net/Http/HttpClientHelper.cs +++ b/src/WireMock.Net/Http/HttpClientHelper.cs @@ -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]); } } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs index ce9c705f..22ef6f55 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs @@ -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 /// /// The functions /// - public Func, bool>[] Funcs { get; } + public Func, bool>[] Funcs { get; } /// /// The name @@ -59,7 +60,7 @@ namespace WireMock.Matchers.Request /// Initializes a new instance of the class. /// /// The funcs. - public RequestMessageHeaderMatcher([NotNull] params Func, bool>[] funcs) + public RequestMessageHeaderMatcher([NotNull] params Func, 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 list = requestMessage.Headers[Name]; + return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ? } } } \ No newline at end of file diff --git a/src/WireMock.Net/Owin/OwinRequestMapper.cs b/src/WireMock.Net/Owin/OwinRequestMapper.cs index 51c4d891..94923178 100644 --- a/src/WireMock.Net/Owin/OwinRequestMapper.cs +++ b/src/WireMock.Net/Owin/OwinRequestMapper.cs @@ -57,13 +57,13 @@ namespace WireMock.Owin body = bodyEncoding.GetBytes(bodyAsString); } - Dictionary headers = null; + Dictionary headers = null; if (request.Headers.Any()) { - headers = new Dictionary(); + headers = new Dictionary(); foreach (var header in request.Headers) { - headers.Add(header.Key, header.Value.FirstOrDefault()); + headers.Add(header.Key, header.Value); } } diff --git a/src/WireMock.Net/Owin/OwinResponseMapper.cs b/src/WireMock.Net/Owin/OwinResponseMapper.cs index b4b4f19b..87cebb0d 100644 --- a/src/WireMock.Net/Owin/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/OwinResponseMapper.cs @@ -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); diff --git a/src/WireMock.Net/Owin/WireMockMiddleware.cs b/src/WireMock.Net/Owin/WireMockMiddleware.cs index 48f9318c..3f601bf3 100644 --- a/src/WireMock.Net/Owin/WireMockMiddleware.cs +++ b/src/WireMock.Net/Owin/WireMockMiddleware.cs @@ -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 authorization); + if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfect) { response = new ResponseMessage { StatusCode = 401 }; return; diff --git a/src/WireMock.Net/RequestBuilders/IHeadersAndCookiesRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IHeadersAndCookiesRequestBuilder.cs index cd158fdf..5a416755 100644 --- a/src/WireMock.Net/RequestBuilders/IHeadersAndCookiesRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IHeadersAndCookiesRequestBuilder.cs @@ -33,7 +33,7 @@ namespace WireMock.RequestBuilders /// /// The headers funcs. /// The . - IRequestBuilder WithHeader([NotNull] params Func, bool>[] funcs); + IRequestBuilder WithHeader([NotNull] params Func, bool>[] funcs); /// /// The with cookie. diff --git a/src/WireMock.Net/RequestBuilders/Request.cs b/src/WireMock.Net/RequestBuilders/Request.cs index ec87c1d6..8d300ce6 100644 --- a/src/WireMock.Net/RequestBuilders/Request.cs +++ b/src/WireMock.Net/RequestBuilders/Request.cs @@ -395,7 +395,7 @@ namespace WireMock.RequestBuilders /// /// The funcs. /// The . - public IRequestBuilder WithHeader(params Func, bool>[] funcs) + public IRequestBuilder WithHeader(params Func, bool>[] funcs) { Check.NotEmpty(funcs, nameof(funcs)); diff --git a/src/WireMock.Net/RequestMessage.cs b/src/WireMock.Net/RequestMessage.cs index 8424ce54..f672de81 100644 --- a/src/WireMock.Net/RequestMessage.cs +++ b/src/WireMock.Net/RequestMessage.cs @@ -41,7 +41,7 @@ namespace WireMock /// /// Gets the headers. /// - public IDictionary Headers { get; } + public IDictionary> Headers { get; } /// /// Gets the cookies. @@ -79,7 +79,7 @@ namespace WireMock /// The body encoding /// The headers. /// The cookies. - 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 headers = null, [CanBeNull] IDictionary 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 headers = null, [CanBeNull] IDictionary 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(header.Value)); Cookies = cookies; Query = ParseQuery(url.Query); } diff --git a/src/WireMock.Net/ResponseBuilders/IHeadersResponseBuilder.cs b/src/WireMock.Net/ResponseBuilders/IHeadersResponseBuilder.cs index 4a2b8054..edb02a17 100644 --- a/src/WireMock.Net/ResponseBuilders/IHeadersResponseBuilder.cs +++ b/src/WireMock.Net/ResponseBuilders/IHeadersResponseBuilder.cs @@ -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. /// /// The name. - /// The value. + /// The values. /// The . - IResponseBuilder WithHeader([NotNull] string name, string value); + IResponseBuilder WithHeader([NotNull] string name, params string[] values); /// /// The with headers. /// /// The headers. /// The . - IResponseBuilder WithHeaders([NotNull] IDictionary headers); + IResponseBuilder WithHeaders([NotNull] IDictionary headers); + + /// + /// The with headers. + /// + /// The headers. + /// The . + IResponseBuilder WithHeaders([NotNull] IDictionary headers); + + /// + /// The with headers. + /// + /// The headers. + /// The . + IResponseBuilder WithHeaders([NotNull] IDictionary> headers); } } \ No newline at end of file diff --git a/src/WireMock.Net/ResponseBuilders/Response.cs b/src/WireMock.Net/ResponseBuilders/Response.cs index bc9643d3..53f7152d 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.cs @@ -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); } - /// - /// The with header. - /// - /// The name. - /// The value. - /// The . - public IResponseBuilder WithHeader(string name, string value) + /// + public IResponseBuilder WithHeader(string name, params string[] values) { Check.NotNull(name, nameof(name)); - ResponseMessage.AddHeader(name, value); + ResponseMessage.AddHeader(name, values); return this; } - /// + /// public IResponseBuilder WithHeaders(IDictionary headers) + { + Check.NotNull(headers, nameof(headers)); + + ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList(header.Value)); + return this; + } + + /// + public IResponseBuilder WithHeaders(IDictionary headers) + { + Check.NotNull(headers, nameof(headers)); + + ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList(header.Value)); + return this; + } + + /// + public IResponseBuilder WithHeaders(IDictionary> headers) { ResponseMessage.Headers = headers; return this; diff --git a/src/WireMock.Net/ResponseMessage.cs b/src/WireMock.Net/ResponseMessage.cs index 02776513..a4ac7d51 100644 --- a/src/WireMock.Net/ResponseMessage.cs +++ b/src/WireMock.Net/ResponseMessage.cs @@ -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 /// /// Gets the headers. /// - public IDictionary Headers { get; set; } = new ConcurrentDictionary(); + public IDictionary> Headers { get; set; } = new ConcurrentDictionary>(); /// /// Gets or sets the status code. @@ -55,17 +58,29 @@ namespace WireMock public Encoding BodyEncoding { get; set; } = new UTF8Encoding(false); /// - /// The add header. + /// Adds the header. /// - /// - /// The name. - /// - /// - /// The value. - /// + /// The name. + /// The value. public void AddHeader(string name, string value) { - Headers.Add(name, value); + Headers.Add(name, new WireMockList(value)); + } + + /// + /// Adds the header. + /// + /// The name. + /// The values. + public void AddHeader(string name, params string[] values) + { + Check.NotEmpty(values, nameof(values)); + + var newHeaderValues = Headers.TryGetValue(name, out WireMockList existingValues) + ? values.Union(existingValues).ToArray() + : values; + + Headers[name] = new WireMockList(newHeaderValues); } } } \ No newline at end of file diff --git a/src/WireMock.Net/Serialization/MappingConverter.cs b/src/WireMock.Net/Serialization/MappingConverter.cs index cae3399e..a1a76777 100644 --- a/src/WireMock.Net/Serialization/MappingConverter.cs +++ b/src/WireMock.Net/Serialization/MappingConverter.cs @@ -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 matchers) + private static IDictionary Map(IDictionary> dictionary) + { + if (dictionary == null) + { + return null; + } + + var newDictionary = new Dictionary(); + 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 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([CanBeNull] IEnumerable> funcs) + private static string[] Map([CanBeNull] IEnumerable> 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([CanBeNull] Func func) + private static string Map([CanBeNull] Func func) { return func?.ToString(); } diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 1a1b4526..5081c4e8 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -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(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(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(entry.Value)); + } } else if (responseModel.HeadersRaw != null) { @@ -647,7 +660,7 @@ namespace WireMock.Server { Body = JsonConvert.SerializeObject(result, _settings), StatusCode = 200, - Headers = new Dictionary { { "Content-Type", "application/json" } } + Headers = new Dictionary> { { "Content-Type", new WireMockList("application/json") } } }; } diff --git a/src/WireMock.Net/Transformers/ResponseMessageTransformer.cs b/src/WireMock.Net/Transformers/ResponseMessageTransformer.cs index c53d1d5d..f1948328 100644 --- a/src/WireMock.Net/Transformers/ResponseMessageTransformer.cs +++ b/src/WireMock.Net/Transformers/ResponseMessageTransformer.cs @@ -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(); + var newHeaders = new Dictionary>(); 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(templateHeaderValues)); } responseMessage.Headers = newHeaders; diff --git a/src/WireMock.Net/Util/JsonUtils.cs b/src/WireMock.Net/Util/JsonUtils.cs index 3d80c89f..9554a5e6 100644 --- a/src/WireMock.Net/Util/JsonUtils.cs +++ b/src/WireMock.Net/Util/JsonUtils.cs @@ -7,13 +7,12 @@ namespace WireMock.Util public static T ParseJTokenToObject(object value) { if (value == null) + { return default(T); + } - JToken token = value as JToken; - if (token == null) - return default(T); - - return token.ToObject(); + var token = value as JToken; + return token == null ? default(T) : token.ToObject(); } } } \ No newline at end of file diff --git a/src/WireMock.Net/Util/WireMockList.cs b/src/WireMock.Net/Util/WireMockList.cs index 73f4747a..8af519b0 100644 --- a/src/WireMock.Net/Util/WireMockList.cs +++ b/src/WireMock.Net/Util/WireMockList.cs @@ -36,9 +36,6 @@ namespace WireMock.Util /// /// Returns a that represents this instance. /// - /// - /// A that represents this instance. - /// public override string ToString() { if (this != null && this.Any()) diff --git a/test/WireMock.Net.Tests/RequestMessageTests.cs b/test/WireMock.Net.Tests/RequestMessageTests.cs index 839abab6..d3ec7235 100644 --- a/test/WireMock.Net.Tests/RequestMessageTests.cs +++ b/test/WireMock.Net.Tests/RequestMessageTests.cs @@ -8,13 +8,13 @@ namespace WireMock.Net.Tests //[TestFixture] public class RequestMessageTests { - private const string clientIP = "::1"; + private const string ClientIp = "::1"; [Fact] public void Should_handle_empty_query() { // given - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp); // then Check.That(request.GetParameter("not_there")).IsNull(); @@ -26,7 +26,7 @@ namespace WireMock.Net.Tests // given string bodyAsString = "whatever"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); // then Check.That(request.GetParameter("foo")).Contains("bar"); diff --git a/test/WireMock.Net.Tests/RequestTests.ClientIP.cs b/test/WireMock.Net.Tests/RequestTests.ClientIP.cs index b089bdd4..397311fb 100644 --- a/test/WireMock.Net.Tests/RequestTests.ClientIP.cs +++ b/test/WireMock.Net.Tests/RequestTests.ClientIP.cs @@ -1,4 +1,3 @@ - using System; using NFluent; using WireMock.Matchers.Request; diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index 521a7d1d..b28370f3 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -12,7 +12,7 @@ namespace WireMock.Net.Tests //[TestFixture] public partial class RequestTests { - private const string clientIP = "::1"; + private const string ClientIp = "::1"; [Fact] public void Should_specify_requests_matching_given_path() @@ -21,7 +21,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo"); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -33,8 +33,8 @@ namespace WireMock.Net.Tests { var requestBuilder = Request.Create().WithPath("/x1", "/x2"); - var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla", clientIP); - var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla", clientIP); + var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla", ClientIp); + var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla", ClientIp); var requestMatchResult = new RequestMatchResult(); Check.That(requestBuilder.GetMatchingScore(request1, requestMatchResult)).IsEqualTo(1.0); @@ -48,7 +48,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath(url => url.EndsWith("/foo")); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -62,7 +62,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath(new RegexMatcher("^/foo")); // when - var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -76,7 +76,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo"); // when - var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", clientIP); + var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -90,7 +90,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithUrl("*/foo"); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -104,7 +104,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingPut(); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -118,7 +118,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingPost(); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -132,7 +132,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingGet(); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -148,7 +148,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "whatever"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -162,7 +162,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingHead(); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -176,7 +176,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/foo").UsingPut(); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -190,7 +190,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithPath("/bar").UsingPut(); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -206,7 +206,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "whatever"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tata" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new [] { "tata" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -222,7 +222,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "whatever"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tata" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "tata" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -238,7 +238,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "whatever"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "ABC" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "ABC" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -254,7 +254,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "whatever"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "TaTa" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "TaTa" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -268,7 +268,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*"); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, null, null, null, null, new Dictionary { { "session", "abc" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, null, null, null, null, new Dictionary { { "session", "abc" } }); // then var requestMatchResult = new RequestMatchResult(); @@ -284,7 +284,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "Hello world!"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -300,7 +300,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "cat"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -316,7 +316,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "cat"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -332,7 +332,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "caR"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -348,7 +348,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "The car drives in the street."; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -364,7 +364,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "Hello"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -380,7 +380,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "Hello world!"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tatata" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "tatata" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -396,7 +396,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "Hello world!"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -417,7 +417,7 @@ namespace WireMock.Net.Tests xyz "; byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, xmlBodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, xmlBodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -438,7 +438,7 @@ namespace WireMock.Net.Tests xyz "; byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, xmlBodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, xmlBodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -454,7 +454,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -470,7 +470,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); @@ -486,7 +486,7 @@ namespace WireMock.Net.Tests // when string bodyAsString = "xxx"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", "tatata" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "tata" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -500,7 +500,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithParam("bar", "1", "2"); // when - var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -514,7 +514,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithParam("bar"); // when - var request = new RequestMessage(new Uri("http://localhost/foo?bar"), "PUT", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo?bar"), "PUT", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -528,7 +528,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().UsingAnyVerb().WithParam(p => p.ContainsKey("bar")); // when - var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", clientIP); + var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", ClientIp); // then var requestMatchResult = new RequestMatchResult(); @@ -542,7 +542,7 @@ namespace WireMock.Net.Tests var spec = Request.Create().WithParam("bar", "1"); // when - var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT", clientIP); + var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT", ClientIp); // then var requestMatchResult = new RequestMatchResult(); diff --git a/test/WireMock.Net.Tests/ResponseTests.Handlebars.cs b/test/WireMock.Net.Tests/ResponseTests.Handlebars.cs new file mode 100644 index 00000000..c794af20 --- /dev/null +++ b/test/WireMock.Net.Tests/ResponseTests.Handlebars.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using NFluent; +using WireMock.ResponseBuilders; +using Xunit; + +namespace WireMock.Net.Tests +{ + public partial class ResponseTests + { + [Fact] + public async Task Response_ProvideResponse_Handlebars_UrlPathVerb() + { + // given + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + + var response = Response.Create() + .WithBody("test {{request.url}} {{request.path}} {{request.method}}") + .WithTransformer(); + + // act + var responseMessage = await response.ProvideResponseAsync(request); + + // then + Check.That(responseMessage.Body).Equals("test http://localhost/foo /foo post"); + } + + [Fact] + public async Task Response_ProvideResponse_Handlebars_Query() + { + // given + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + + var response = Response.Create() + .WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}") + .WithTransformer(); + + // act + var responseMessage = await response.ProvideResponseAsync(request); + + // then + Check.That(responseMessage.Body).Equals("test keya=1 idx=1 idx=2 keyb=5"); + } + + [Fact] + public async Task Response_ProvideResponse_Handlebars_Header() + { + // given + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "Content-Type", new[] { "text/plain" } } }); + + var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer(); + + // act + var responseMessage = await response.ProvideResponseAsync(request); + + // then + Check.That(responseMessage.Body).Equals("test"); + Check.That(responseMessage.Headers).ContainsKey("x"); + Check.That(responseMessage.Headers["x"]).ContainsExactly("text/plain"); + } + + [Fact] + public async Task Response_ProvideResponse_Handlebars_Headers() + { + // given + string bodyAsString = "abc"; + byte[] body = Encoding.UTF8.GetBytes(bodyAsString); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "Content-Type", new[] { "text/plain" } } }); + + var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}", "{{request.url}}").WithBody("test").WithTransformer(); + + // act + var responseMessage = await response.ProvideResponseAsync(request); + + // then + Check.That(responseMessage.Body).Equals("test"); + Check.That(responseMessage.Headers).ContainsKey("x"); + Check.That(responseMessage.Headers["x"]).Contains("text/plain"); + Check.That(responseMessage.Headers["x"]).Contains("http://localhost/foo"); + } + } +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/ResponseTests.cs b/test/WireMock.Net.Tests/ResponseTests.cs index 6a74dd7e..acf05d75 100644 --- a/test/WireMock.Net.Tests/ResponseTests.cs +++ b/test/WireMock.Net.Tests/ResponseTests.cs @@ -1,73 +1,15 @@ using System; -using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using NFluent; -using Xunit; using WireMock.ResponseBuilders; +using Xunit; namespace WireMock.Net.Tests { - //[TestFixture] - public class ResponseTests + public partial class ResponseTests { - private const string clientIP = "::1"; - - [Fact] - public async Task Response_ProvideResponse_Handlebars_UrlPathVerb() - { - // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); - - var response = Response.Create() - .WithBody("test {{request.url}} {{request.path}} {{request.method}}") - .WithTransformer(); - - // act - var responseMessage = await response.ProvideResponseAsync(request); - - // then - Check.That(responseMessage.Body).Equals("test http://localhost/foo /foo post"); - } - - [Fact] - public async Task Response_ProvideResponse_Handlebars_Query() - { - // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); - - var response = Response.Create() - .WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}") - .WithTransformer(); - - // act - var responseMessage = await response.ProvideResponseAsync(request); - - // then - Check.That(responseMessage.Body).Equals("test keya=1 idx=1 idx=2 keyb=5"); - } - - [Fact] - public async Task Response_ProvideResponse_Handlebars_Headers() - { - // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary { { "Content-Type", "text/plain" } }); - - var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer(); - - // act - var responseMessage = await response.ProvideResponseAsync(request); - - // then - Check.That(responseMessage.Body).Equals("test"); - Check.That(responseMessage.Headers).Contains(new KeyValuePair("x", "text/plain")); - } + private const string ClientIp = "::1"; [Fact] public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_String() @@ -75,7 +17,7 @@ namespace WireMock.Net.Tests // given string bodyAsString = "abc"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.String, Encoding.ASCII); @@ -94,7 +36,7 @@ namespace WireMock.Net.Tests // given string bodyAsString = "abc"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.SameAsSource, Encoding.ASCII); @@ -113,7 +55,7 @@ namespace WireMock.Net.Tests // given string bodyAsString = "abc"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); var response = Response.Create().WithBody("test", null, Encoding.ASCII); @@ -131,7 +73,7 @@ namespace WireMock.Net.Tests // given string bodyAsString = "abc"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII); diff --git a/test/WireMock.Net.Tests/StatefulBehaviorTests.cs b/test/WireMock.Net.Tests/StatefulBehaviorTests.cs index a7c845d8..b6997ed7 100644 --- a/test/WireMock.Net.Tests/StatefulBehaviorTests.cs +++ b/test/WireMock.Net.Tests/StatefulBehaviorTests.cs @@ -7,7 +7,6 @@ using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; using Xunit; -using Xunit.Abstractions; namespace WireMock.Net.Tests {