From 8dfcf7288f81e08d63af82241318942422eb2746 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Wed, 18 Jan 2017 22:47:40 +0100 Subject: [PATCH] Handlebars #4 --- .../Program.cs | 6 +- .../Extensions/DictionaryExtensions.cs | 57 ++++ src/WireMock/HttpListenerRequestMapper.cs | 8 +- src/WireMock/RequestBuilders/Request.cs | 2 +- src/WireMock/RequestMessage.cs | 40 ++- .../ResponseBuilders/IBodyResponseBuilder.cs | 4 +- .../ResponseBuilders/IDelayResponseBuilder.cs | 4 +- .../IHeadersResponseBuilder.cs | 4 +- .../ResponseBuilders/IResponseBuilder.cs | 9 + .../ITransformResponseBuilder.cs | 16 + src/WireMock/ResponseBuilders/Response.cs | 61 +++- src/WireMock/ResponseMessage.cs | 9 +- src/WireMock/project.json | 3 + .../HttpListenerResponseMapperTests.cs | 26 +- .../WireMock.Net.Tests/RequestMessageTests.cs | 42 +++ test/WireMock.Net.Tests/RequestTests.cs | 275 +++++++++++++++- test/WireMock.Net.Tests/RequestsTests.cs | 295 ------------------ .../WireMock.Net.Tests.csproj | 2 +- 18 files changed, 501 insertions(+), 362 deletions(-) create mode 100644 src/WireMock/Extensions/DictionaryExtensions.cs create mode 100644 src/WireMock/ResponseBuilders/IResponseBuilder.cs create mode 100644 src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs create mode 100644 test/WireMock.Net.Tests/RequestMessageTests.cs delete mode 100644 test/WireMock.Net.Tests/RequestsTests.cs diff --git a/examples/WireMock.Net.ConsoleApplication/Program.cs b/examples/WireMock.Net.ConsoleApplication/Program.cs index e312b7df..660d6d94 100644 --- a/examples/WireMock.Net.ConsoleApplication/Program.cs +++ b/examples/WireMock.Net.ConsoleApplication/Program.cs @@ -24,12 +24,16 @@ namespace WireMock.Net.ConsoleApplication .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""/x with FUNC 200""}")); + // http://localhost:8080/gffgfgf/sddsds?start=1000&stop=1&stop=2 server .Given(Request.WithUrl("/*").UsingGet()) .RespondWith(Response .WithStatusCode(200) .WithHeader("Content-Type", "application/json") - .WithBody(@"{ ""msg"": ""Hello world!""}") + .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}") + .WithBody(@"{""msg"": ""Hello world! : {{request.url}} : {{request.path}} : {{request.query.start}} : {{request.query.stop.[0]}}""") + .AfterDelay(TimeSpan.FromMilliseconds(100)) + .WithTransformer() ); server diff --git a/src/WireMock/Extensions/DictionaryExtensions.cs b/src/WireMock/Extensions/DictionaryExtensions.cs new file mode 100644 index 00000000..e95db0c2 --- /dev/null +++ b/src/WireMock/Extensions/DictionaryExtensions.cs @@ -0,0 +1,57 @@ +using System.Collections; +using System.Collections.Generic; +using System.Dynamic; + +namespace WireMock.Extensions +{ + /// + /// Dictionary Extensions + /// + public static class DictionaryExtensions + { + public static dynamic ToExpandoObject(this IDictionary dictionary) + { + dynamic expando = new ExpandoObject(); + var expandoDic = (IDictionary)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; + 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(); + foreach (var item in (ICollection)kvp.Value) + { + var objects = item as IDictionary; + 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; + } + } +} \ No newline at end of file diff --git a/src/WireMock/HttpListenerRequestMapper.cs b/src/WireMock/HttpListenerRequestMapper.cs index b5dc0267..d89ce59a 100644 --- a/src/WireMock/HttpListenerRequestMapper.cs +++ b/src/WireMock/HttpListenerRequestMapper.cs @@ -31,10 +31,10 @@ namespace WireMock /// public RequestMessage Map(HttpListenerRequest listenerRequest) { - var path = listenerRequest.Url.AbsolutePath; - var query = listenerRequest.Url.Query; - var verb = listenerRequest.HttpMethod; - var body = GetRequestBody(listenerRequest); + string path = listenerRequest.Url.AbsolutePath; + string query = listenerRequest.Url.Query; + string verb = listenerRequest.HttpMethod; + string body = GetRequestBody(listenerRequest); var listenerHeaders = listenerRequest.Headers; var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]); diff --git a/src/WireMock/RequestBuilders/Request.cs b/src/WireMock/RequestBuilders/Request.cs index 76a4d177..3e783a51 100644 --- a/src/WireMock/RequestBuilders/Request.cs +++ b/src/WireMock/RequestBuilders/Request.cs @@ -27,7 +27,7 @@ namespace WireMock.RequestBuilders /// /// The requests. /// - public class Request : CompositeRequestSpec, IVerbRequestBuilder, IHeadersRequestBuilder, IParamsRequestBuilder + public class Request : CompositeRequestSpec, IVerbRequestBuilder { /// /// The _request specs. diff --git a/src/WireMock/RequestMessage.cs b/src/WireMock/RequestMessage.cs index 224b5152..e1a2c906 100644 --- a/src/WireMock/RequestMessage.cs +++ b/src/WireMock/RequestMessage.cs @@ -1,6 +1,8 @@ -using System.Collections.Generic; +using System.Collections.Concurrent; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using WireMock.Extensions; [module: SuppressMessage("StyleCop.CSharp.ReadabilityRules", @@ -26,11 +28,6 @@ namespace WireMock /// public class RequestMessage { - /// - /// The _params. - /// - private readonly IDictionary> _params = new Dictionary>(); - /// /// Initializes a new instance of the class. /// @@ -58,7 +55,7 @@ namespace WireMock query = query.Substring(1); } - _params = query.Split('&').Aggregate( + Parameters = query.Split('&').Aggregate( new Dictionary>(), (dict, term) => { @@ -72,7 +69,19 @@ namespace WireMock return dict; }); - Parameters = _params; + var tmpDictionary = new Dictionary(); + foreach (var parameter in Parameters.Where(p => p.Value.Any())) + { + if (parameter.Value.Count == 1) + { + tmpDictionary.Add(parameter.Key, parameter.Value.First()); + } + else + { + tmpDictionary.Add(parameter.Key, parameter.Value); + } + } + Query = tmpDictionary.ToExpandoObject(); } Path = path; @@ -88,12 +97,12 @@ namespace WireMock { get { - if (!_params.Any()) + if (!Parameters.Any()) { return Path; } - return Path + "?" + string.Join("&", _params.SelectMany(kv => kv.Value.Select(value => kv.Key + "=" + value))); + return Path + "?" + string.Join("&", Parameters.SelectMany(kv => kv.Value.Select(value => kv.Key + "=" + value))); } } @@ -113,9 +122,14 @@ namespace WireMock public IDictionary Headers { get; } /// - /// Gets the parameters. + /// Gets the query parameters. /// - public IDictionary> Parameters { get; } + public IDictionary> Parameters { get; } = new Dictionary>(); + + /// + /// Gets the query as object. + /// + public dynamic Query { get; } /// /// Gets the body. @@ -133,7 +147,7 @@ namespace WireMock /// public List GetParameter(string key) { - return _params.ContainsKey(key) ? _params[key] : new List(); + return Parameters.ContainsKey(key) ? Parameters[key] : new List(); } } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs index 1cb611b8..2c09d673 100644 --- a/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/IBodyResponseBuilder.cs @@ -12,8 +12,8 @@ /// The body. /// /// - /// The . + /// The . /// - IDelayResponseBuilder WithBody(string body); + IResponseBuilder WithBody(string body); } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/IDelayResponseBuilder.cs b/src/WireMock/ResponseBuilders/IDelayResponseBuilder.cs index 12efae83..37e5115e 100644 --- a/src/WireMock/ResponseBuilders/IDelayResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/IDelayResponseBuilder.cs @@ -14,8 +14,8 @@ namespace WireMock.ResponseBuilders /// The delay. /// /// - /// The . + /// The . /// - IProvideResponses AfterDelay(TimeSpan delay); + IResponseBuilder AfterDelay(TimeSpan delay); } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/IHeadersResponseBuilder.cs b/src/WireMock/ResponseBuilders/IHeadersResponseBuilder.cs index eaf8dd76..cf87e2c5 100644 --- a/src/WireMock/ResponseBuilders/IHeadersResponseBuilder.cs +++ b/src/WireMock/ResponseBuilders/IHeadersResponseBuilder.cs @@ -15,8 +15,8 @@ /// The value. /// /// - /// The . + /// The . /// - IHeadersResponseBuilder WithHeader(string name, string value); + IResponseBuilder WithHeader(string name, string value); } } \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/IResponseBuilder.cs b/src/WireMock/ResponseBuilders/IResponseBuilder.cs new file mode 100644 index 00000000..6a115a26 --- /dev/null +++ b/src/WireMock/ResponseBuilders/IResponseBuilder.cs @@ -0,0 +1,9 @@ +namespace WireMock.ResponseBuilders +{ + /// + /// The ResponseBuilder interface. + /// + public interface IResponseBuilder : ITransformResponseBuilder + { + } +} \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs b/src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs new file mode 100644 index 00000000..8c0b3d5b --- /dev/null +++ b/src/WireMock/ResponseBuilders/ITransformResponseBuilder.cs @@ -0,0 +1,16 @@ +namespace WireMock.ResponseBuilders +{ + /// + /// The BodyResponseBuilder interface. + /// + public interface ITransformResponseBuilder : IHeadersResponseBuilder + { + /// + /// The with transformer. + /// + /// + /// The . + /// + IResponseBuilder WithTransformer(); + } +} \ No newline at end of file diff --git a/src/WireMock/ResponseBuilders/Response.cs b/src/WireMock/ResponseBuilders/Response.cs index 2b0af198..b69f3975 100644 --- a/src/WireMock/ResponseBuilders/Response.cs +++ b/src/WireMock/ResponseBuilders/Response.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; +using HandlebarsDotNet; [module: SuppressMessage("StyleCop.CSharp.ReadabilityRules", @@ -21,7 +23,7 @@ namespace WireMock.ResponseBuilders /// /// The responses. /// - public class Response : IHeadersResponseBuilder + public class Response : IResponseBuilder { /// /// The _response. @@ -33,6 +35,8 @@ namespace WireMock.ResponseBuilders /// private TimeSpan _delay = TimeSpan.Zero; + private bool _useTransformer; + /// /// Initializes a new instance of the class. /// @@ -47,8 +51,8 @@ namespace WireMock.ResponseBuilders /// /// The with Success status code. /// - /// The . - public static IHeadersResponseBuilder WithSuccess() + /// The . + public static IResponseBuilder WithSuccess() { return WithStatusCode(200); } @@ -56,8 +60,8 @@ namespace WireMock.ResponseBuilders /// /// The with NotFound status code. /// - /// The . - public static IHeadersResponseBuilder WithNotFound() + /// The . + public static IResponseBuilder WithNotFound() { return WithStatusCode(404); } @@ -69,9 +73,9 @@ namespace WireMock.ResponseBuilders /// The code. /// /// - /// The . + /// The . /// - public static IHeadersResponseBuilder WithStatusCode(int code) + public static IResponseBuilder WithStatusCode(int code) { var response = new ResponseMessage { StatusCode = code }; return new Response(response); @@ -88,7 +92,28 @@ namespace WireMock.ResponseBuilders /// public async Task 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(); + foreach (var header in _responseMessage.Headers) + { + var templateHeaderKey = Handlebars.Compile(header.Key); + var templateHeaderValue = Handlebars.Compile(header.Value); + + newHeaders.Add(templateHeaderKey(template), templateHeaderValue(template)); + } + _responseMessage.Headers = newHeaders; + } + await Task.Delay(_delay); + return _responseMessage; } @@ -102,9 +127,9 @@ namespace WireMock.ResponseBuilders /// The value. /// /// - /// The . + /// The . /// - public IHeadersResponseBuilder WithHeader(string name, string value) + public IResponseBuilder WithHeader(string name, string value) { _responseMessage.AddHeader(name, value); return this; @@ -117,14 +142,26 @@ namespace WireMock.ResponseBuilders /// The body. /// /// - /// The . + /// The . /// - public IDelayResponseBuilder WithBody(string body) + public IResponseBuilder WithBody(string body) { _responseMessage.Body = body; return this; } + /// + /// The with transformer. + /// + /// + /// The . + /// + public IResponseBuilder WithTransformer() + { + _useTransformer = true; + return this; + } + /// /// The after delay. /// @@ -134,7 +171,7 @@ namespace WireMock.ResponseBuilders /// /// The . /// - public IProvideResponses AfterDelay(TimeSpan delay) + public IResponseBuilder AfterDelay(TimeSpan delay) { _delay = delay; return this; diff --git a/src/WireMock/ResponseMessage.cs b/src/WireMock/ResponseMessage.cs index 4836aa17..6989745b 100644 --- a/src/WireMock/ResponseMessage.cs +++ b/src/WireMock/ResponseMessage.cs @@ -23,15 +23,10 @@ namespace WireMock /// public class ResponseMessage { - /// - /// The _headers. - /// - private readonly IDictionary _headers = new ConcurrentDictionary(); - /// /// Gets the headers. /// - public IDictionary Headers => _headers; + public IDictionary Headers { get; set; } = new ConcurrentDictionary(); /// /// Gets or sets the status code. @@ -54,7 +49,7 @@ namespace WireMock /// public void AddHeader(string name, string value) { - _headers.Add(name, value); + Headers.Add(name, value); } } } \ No newline at end of file diff --git a/src/WireMock/project.json b/src/WireMock/project.json index 7b60f667..a9e0dd19 100644 --- a/src/WireMock/project.json +++ b/src/WireMock/project.json @@ -30,6 +30,9 @@ "frameworks": { "net45": { + "dependencies": { + "Handlebars.Net": "1.8.0" + }, "frameworkAssemblies": { } } diff --git a/test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs b/test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs index 19bb011c..495fe72d 100644 --- a/test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs +++ b/test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs @@ -8,20 +8,20 @@ using NUnit.Framework; using WireMock.Http; [module: - SuppressMessage("StyleCop.CSharp.ReadabilityRules", - "SA1101:PrefixLocalCallsWithThis", + SuppressMessage("StyleCop.CSharp.ReadabilityRules", + "SA1101:PrefixLocalCallsWithThis", Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")] [module: - SuppressMessage("StyleCop.CSharp.NamingRules", - "SA1309:FieldNamesMustNotBeginWithUnderscore", + SuppressMessage("StyleCop.CSharp.NamingRules", + "SA1309:FieldNamesMustNotBeginWithUnderscore", Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")] [module: - SuppressMessage("StyleCop.CSharp.DocumentationRules", - "SA1600:ElementsMustBeDocumented", + SuppressMessage("StyleCop.CSharp.DocumentationRules", + "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here, as it's a tests class.")] [module: - SuppressMessage("StyleCop.CSharp.DocumentationRules", - "SA1633:FileMustHaveHeader", + SuppressMessage("StyleCop.CSharp.DocumentationRules", + "SA1633:FileMustHaveHeader", Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")] // ReSharper disable ArrangeThisQualifier // ReSharper disable InconsistentNaming @@ -68,8 +68,11 @@ namespace WireMock.Net.Tests public void Should_map_body_from_original_response() { // given - var response = new ResponseMessage(); - response.Body = "Hello !!!"; + var response = new ResponseMessage + { + Body = "Hello !!!" + }; + var httpListenerResponse = CreateHttpListenerResponse(); // when @@ -78,6 +81,7 @@ namespace WireMock.Net.Tests // then var responseMessage = ToResponseMessage(httpListenerResponse); Check.That(responseMessage).IsNotNull(); + var contentTask = responseMessage.Content.ReadAsStringAsync(); Check.That(contentTask.Result).IsEqualTo("Hello !!!"); } @@ -104,7 +108,7 @@ namespace WireMock.Net.Tests var responseReady = new AutoResetEvent(false); HttpListenerResponse response = null; _server = new TinyHttpServer( - urlPrefix, + urlPrefix, context => { response = context.Response; diff --git a/test/WireMock.Net.Tests/RequestMessageTests.cs b/test/WireMock.Net.Tests/RequestMessageTests.cs new file mode 100644 index 00000000..6012256e --- /dev/null +++ b/test/WireMock.Net.Tests/RequestMessageTests.cs @@ -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()); + + // 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()); + + // then + Check.That(request.GetParameter("foo")).Contains("bar"); + Check.That(request.GetParameter("multi")).Contains("1"); + Check.That(request.GetParameter("multi")).Contains("2"); + } + } +} diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index 2575d606..ef621ffb 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -2,14 +2,15 @@ using System.Diagnostics.CodeAnalysis; using NFluent; using NUnit.Framework; +using WireMock.RequestBuilders; [module: - SuppressMessage("StyleCop.CSharp.DocumentationRules", - "SA1600:ElementsMustBeDocumented", + SuppressMessage("StyleCop.CSharp.DocumentationRules", + "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here, as it's a tests class.")] [module: - SuppressMessage("StyleCop.CSharp.DocumentationRules", - "SA1633:FileMustHaveHeader", + SuppressMessage("StyleCop.CSharp.DocumentationRules", + "SA1633:FileMustHaveHeader", Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")] // ReSharper disable InconsistentNaming namespace WireMock.Net.Tests @@ -18,25 +19,277 @@ namespace WireMock.Net.Tests public class RequestTests { [Test] - public void Should_handle_empty_query() + public void Should_specify_requests_matching_given_url() { // given + var spec = Request.WithUrl("/foo"); + + // when var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary()); // then - Check.That(request.GetParameter("foo")).IsEmpty(); + Check.That(spec.IsSatisfiedBy(request)).IsTrue(); } [Test] - public void Should_parse_query_params() + public void Should_specify_requests_matching_given_url_prefix() { // given - var request = new RequestMessage("/foo", "foo=bar&multi=1&multi=2", "blabla", "whatever", new Dictionary()); + var spec = Request.WithUrl("/foo*"); + + // when + var request = new RequestMessage("/foo/bar", string.Empty, "blabla", "whatever", new Dictionary()); // then - Check.That(request.GetParameter("foo")).Contains("bar"); - Check.That(request.GetParameter("multi")).Contains("1"); - Check.That(request.GetParameter("multi")).Contains("2"); + Check.That(spec.IsSatisfiedBy(request)).IsTrue(); + } + + [Test] + public void Should_exclude_requests_not_matching_given_url() + { + // given + var spec = Request.WithUrl("/foo"); + + // when + var request = new RequestMessage("/bar", string.Empty, "blabla", "whatever", new Dictionary()); + + // 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()); + + // 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()); + + // 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()); + + // 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()); + + // 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()); + + // 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()); + + // 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()); + + // 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()); + + // 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 { { "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 { { "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 { { "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 { { "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 { { "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 { { "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 { { "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()); + + // 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()); + + // 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()); + + // then + Check.That(spec.IsSatisfiedBy(request)).IsFalse(); } } } + diff --git a/test/WireMock.Net.Tests/RequestsTests.cs b/test/WireMock.Net.Tests/RequestsTests.cs deleted file mode 100644 index d754f90a..00000000 --- a/test/WireMock.Net.Tests/RequestsTests.cs +++ /dev/null @@ -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()); - - // 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()); - - // 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()); - - // 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()); - - // 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()); - - // 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()); - - // 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()); - - // 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()); - - // 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()); - - // 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()); - - // 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()); - - // 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 { { "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 { { "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 { { "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 { { "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 { { "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 { { "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 { { "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()); - - // 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()); - - // 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()); - - // then - Check.That(spec.IsSatisfiedBy(request)).IsFalse(); - } - } -} - diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index 5993db14..1f6017e3 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -64,8 +64,8 @@ - +