Handlebars #4

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

View File

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

View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using NFluent;
using NUnit.Framework;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable InconsistentNaming
namespace WireMock.Net.Tests
{
[TestFixture]
public class RequestMessageTests
{
[Test]
public void Should_handle_empty_query()
{
// given
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(request.GetParameter("foo")).IsEmpty();
}
[Test]
public void Should_parse_query_params()
{
// given
var request = new RequestMessage("/foo", "foo=bar&multi=1&multi=2", "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(request.GetParameter("foo")).Contains("bar");
Check.That(request.GetParameter("multi")).Contains("1");
Check.That(request.GetParameter("multi")).Contains("2");
}
}
}

View File

@@ -2,14 +2,15 @@
using System.Diagnostics.CodeAnalysis;
using NFluent;
using NUnit.Framework;
using WireMock.RequestBuilders;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable InconsistentNaming
namespace WireMock.Net.Tests
@@ -18,25 +19,277 @@ namespace WireMock.Net.Tests
public class RequestTests
{
[Test]
public void Should_handle_empty_query()
public void Should_specify_requests_matching_given_url()
{
// given
var spec = Request.WithUrl("/foo");
// when
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(request.GetParameter("foo")).IsEmpty();
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_parse_query_params()
public void Should_specify_requests_matching_given_url_prefix()
{
// given
var request = new RequestMessage("/foo", "foo=bar&multi=1&multi=2", "blabla", "whatever", new Dictionary<string, string>());
var spec = Request.WithUrl("/foo*");
// when
var request = new RequestMessage("/foo/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(request.GetParameter("foo")).Contains("bar");
Check.That(request.GetParameter("multi")).Contains("1");
Check.That(request.GetParameter("multi")).Contains("2");
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_url()
{
// given
var spec = Request.WithUrl("/foo");
// when
var request = new RequestMessage("/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_path()
{
// given
var spec = Request.WithPath("/foo");
// when
var request = new RequestMessage("/foo", "?param=1", "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_put()
{
// given
var spec = Request.WithUrl("/foo").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_post()
{
// given
var spec = Request.WithUrl("/foo").UsingPost();
// when
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_get()
{
// given
var spec = Request.WithUrl("/foo").UsingGet();
// when
var request = new RequestMessage("/foo", string.Empty, "GET", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_delete()
{
// given
var spec = Request.WithUrl("/foo").UsingDelete();
// when
var request = new RequestMessage("/foo", string.Empty, "DELETE", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_head()
{
// given
var spec = Request.WithUrl("/foo").UsingHead();
// when
var request = new RequestMessage("/foo", string.Empty, "HEAD", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_matching_given_url_but_not_http_method()
{
// given
var spec = Request.WithUrl("/foo").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_matching_given_http_method_but_not_url()
{
// given
var spec = Request.WithUrl("/bar").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_url_and_headers()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers_ignorecase()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "abc", false);
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "ABC" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_header_prefix()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "TaTaTa" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(".*Hello world!.*");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body_as_wildcard()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_body()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_params()
{
// given
var spec = Request.WithPath("/foo").WithParam("bar", "1", "2");
// when
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_params_func()
{
// given
var spec = Request.WithPath("/foo").WithParam(p => p.ContainsKey("bar") && (p["bar"].Contains("1") || p["bar"].Contains("2")));
// when
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_params()
{
// given
var spec = Request.WithPath("/foo").WithParam("bar", "1");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
}
}

View File

@@ -1,295 +0,0 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using NFluent;
using NUnit.Framework;
using WireMock.RequestBuilders;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable InconsistentNaming
namespace WireMock.Net.Tests
{
[TestFixture]
public class RequestsTests
{
[Test]
public void Should_specify_requests_matching_given_url()
{
// given
var spec = Request.WithUrl("/foo");
// when
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_prefix()
{
// given
var spec = Request.WithUrl("/foo*");
// when
var request = new RequestMessage("/foo/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_url()
{
// given
var spec = Request.WithUrl("/foo");
// when
var request = new RequestMessage("/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_path()
{
// given
var spec = Request.WithPath("/foo");
// when
var request = new RequestMessage("/foo", "?param=1", "blabla", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_put()
{
// given
var spec = Request.WithUrl("/foo").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_post()
{
// given
var spec = Request.WithUrl("/foo").UsingPost();
// when
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_get()
{
// given
var spec = Request.WithUrl("/foo").UsingGet();
// when
var request = new RequestMessage("/foo", string.Empty, "GET", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_delete()
{
// given
var spec = Request.WithUrl("/foo").UsingDelete();
// when
var request = new RequestMessage("/foo", string.Empty, "DELETE", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_url_and_method_head()
{
// given
var spec = Request.WithUrl("/foo").UsingHead();
// when
var request = new RequestMessage("/foo", string.Empty, "HEAD", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_matching_given_url_but_not_http_method()
{
// given
var spec = Request.WithUrl("/foo").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_matching_given_http_method_but_not_url()
{
// given
var spec = Request.WithUrl("/bar").UsingPut();
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_url_and_headers()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_exclude_requests_not_matching_given_headers_ignorecase()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "abc", false);
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "ABC" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_header_prefix()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "TaTaTa" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(".*Hello world!.*");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body_as_wildcard()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_body()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test]
public void Should_specify_requests_matching_given_params()
{
// given
var spec = Request.WithPath("/foo").WithParam("bar", "1", "2");
// when
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_params_func()
{
// given
var spec = Request.WithPath("/foo").WithParam(p => p.ContainsKey("bar") && (p["bar"].Contains("1") || p["bar"].Contains("2")));
// when
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_exclude_requests_not_matching_given_params()
{
// given
var spec = Request.WithPath("/foo").WithParam("bar", "1");
// when
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string>());
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
}
}

View File

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