mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-11 21:12:16 +01:00
Initial code (copy)
This commit is contained in:
225
test/WireMock.Net.Tests/FluentMockServerTests.cs
Normal file
225
test/WireMock.Net.Tests/FluentMockServerTests.cs
Normal file
@@ -0,0 +1,225 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using NUnit.Framework;
|
||||
|
||||
[module:
|
||||
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",
|
||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||
[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 ArrangeThisQualifier
|
||||
// ReSharper disable InconsistentNaming
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
[Timeout(5000)]
|
||||
public class FluentMockServerTests
|
||||
{
|
||||
private FluentMockServer _server;
|
||||
|
||||
[Test]
|
||||
public async Task Should_respond_to_request()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Requests
|
||||
.WithUrl("/foo")
|
||||
.UsingGet())
|
||||
.RespondWith(Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||
|
||||
// when
|
||||
var response
|
||||
= await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
||||
|
||||
// then
|
||||
Check.That(response).IsEqualTo(@"{ msg: ""Hello world!""}");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_respond_404_for_unexpected_request()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
// when
|
||||
var response
|
||||
= await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
||||
|
||||
// then
|
||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
||||
Check.That((int)response.StatusCode).IsEqualTo(404);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_record_requests_in_the_requestlogs()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
// when
|
||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
||||
|
||||
// then
|
||||
Check.That(_server.RequestLogs).HasSize(1);
|
||||
var requestLogged = _server.RequestLogs.First();
|
||||
Check.That(requestLogged.Verb).IsEqualTo("get");
|
||||
Check.That(requestLogged.Body).IsEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_find_a_request_satisfying_a_request_spec()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
// when
|
||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/bar");
|
||||
|
||||
// then
|
||||
var result = _server.SearchLogsFor(Requests.WithUrl("/b*"));
|
||||
Check.That(result).HasSize(1);
|
||||
var requestLogged = result.First();
|
||||
Check.That(requestLogged.Url).IsEqualTo("/bar");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_reset_requestlogs()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
// when
|
||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
||||
_server.Reset();
|
||||
|
||||
// then
|
||||
Check.That(_server.RequestLogs).IsEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Should_reset_routes()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Requests
|
||||
.WithUrl("/foo")
|
||||
.UsingGet())
|
||||
.RespondWith(Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||
|
||||
// when
|
||||
_server.Reset();
|
||||
|
||||
// then
|
||||
Check.ThatAsyncCode(() => new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo"))
|
||||
.ThrowsAny();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_respond_a_redirect_without_body()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Requests
|
||||
.WithUrl("/foo")
|
||||
.UsingGet())
|
||||
.RespondWith(Responses
|
||||
.WithStatusCode(307)
|
||||
.WithHeader("Location", "/bar"));
|
||||
_server
|
||||
.Given(Requests
|
||||
.WithUrl("/bar")
|
||||
.UsingGet())
|
||||
.RespondWith(Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody("REDIRECT SUCCESSFUL"));
|
||||
|
||||
// when
|
||||
var response
|
||||
= await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
||||
|
||||
// then
|
||||
Check.That(response).IsEqualTo("REDIRECT SUCCESSFUL");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_delay_responses_for_a_given_route()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Requests
|
||||
.WithUrl("/*"))
|
||||
.RespondWith(Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ msg: ""Hello world!""}")
|
||||
.AfterDelay(TimeSpan.FromMilliseconds(2000)));
|
||||
|
||||
// when
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
||||
watch.Stop();
|
||||
|
||||
// then
|
||||
Check.That(watch.ElapsedMilliseconds).IsGreaterThan(2000);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_delay_responses()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
_server.AddRequestProcessingDelay(TimeSpan.FromMilliseconds(2000));
|
||||
_server
|
||||
.Given(Requests
|
||||
.WithUrl("/*"))
|
||||
.RespondWith(Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||
|
||||
// when
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
||||
watch.Stop();
|
||||
|
||||
// then
|
||||
Check.That(watch.ElapsedMilliseconds).IsGreaterThan(2000);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void ShutdownServer()
|
||||
{
|
||||
_server.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
39
test/WireMock.Net.Tests/Http/TinyHttpServerTests.cs
Normal file
39
test/WireMock.Net.Tests/Http/TinyHttpServerTests.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net.Http;
|
||||
using NFluent;
|
||||
using NUnit.Framework;
|
||||
using WireMock.Http;
|
||||
|
||||
[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.")]
|
||||
|
||||
namespace WireMock.Net.Tests.Http
|
||||
{
|
||||
[TestFixture]
|
||||
public class TinyHttpServerTests
|
||||
{
|
||||
[Test]
|
||||
public void Should_Call_Handler_on_Request()
|
||||
{
|
||||
// given
|
||||
var port = Ports.FindFreeTcpPort();
|
||||
bool called = false;
|
||||
var urlPrefix = "http://localhost:" + port + "/";
|
||||
var server = new TinyHttpServer(urlPrefix, ctx => called = true);
|
||||
server.Start();
|
||||
|
||||
// when
|
||||
var httpClient = new HttpClient();
|
||||
httpClient.GetAsync(urlPrefix).Wait(3000);
|
||||
|
||||
// then
|
||||
Check.That(called).IsTrue();
|
||||
}
|
||||
}
|
||||
}
|
||||
166
test/WireMock.Net.Tests/HttpListenerRequestMapperTests.cs
Normal file
166
test/WireMock.Net.Tests/HttpListenerRequestMapperTests.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using NUnit.Framework;
|
||||
using WireMock.Http;
|
||||
|
||||
[module:
|
||||
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",
|
||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||
[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 ArrangeThisQualifier
|
||||
// ReSharper disable InconsistentNaming
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class HttpListenerRequestMapperTests
|
||||
{
|
||||
private MapperServer _server;
|
||||
|
||||
[SetUp]
|
||||
public void StartListenerServer()
|
||||
{
|
||||
_server = MapperServer.Start();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_map_uri_from_listener_request()
|
||||
{
|
||||
// given
|
||||
var client = new HttpClient();
|
||||
|
||||
// when
|
||||
await client.GetAsync(MapperServer.UrlPrefix + "toto");
|
||||
|
||||
// then
|
||||
Check.That(MapperServer.LastRequest).IsNotNull();
|
||||
Check.That(MapperServer.LastRequest.Url).IsEqualTo("/toto");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_map_verb_from_listener_request()
|
||||
{
|
||||
// given
|
||||
var client = new HttpClient();
|
||||
|
||||
// when
|
||||
await client.PutAsync(MapperServer.UrlPrefix, new StringContent("Hello!"));
|
||||
|
||||
// then
|
||||
Check.That(MapperServer.LastRequest).IsNotNull();
|
||||
Check.That(MapperServer.LastRequest.Verb).IsEqualTo("put");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_map_body_from_listener_request()
|
||||
{
|
||||
// given
|
||||
var client = new HttpClient();
|
||||
|
||||
// when
|
||||
await client.PutAsync(MapperServer.UrlPrefix, new StringContent("Hello!"));
|
||||
|
||||
// then
|
||||
Check.That(MapperServer.LastRequest).IsNotNull();
|
||||
Check.That(MapperServer.LastRequest.Body).IsEqualTo("Hello!");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_map_headers_from_listener_request()
|
||||
{
|
||||
// given
|
||||
var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("X-Alex", "1706");
|
||||
|
||||
// when
|
||||
await client.GetAsync(MapperServer.UrlPrefix);
|
||||
|
||||
// then
|
||||
Check.That(MapperServer.LastRequest).IsNotNull();
|
||||
Check.That(MapperServer.LastRequest.Headers).Not.IsNullOrEmpty();
|
||||
Check.That(MapperServer.LastRequest.Headers.Contains(new KeyValuePair<string, string>("x-alex", "1706"))).IsTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Should_map_params_from_listener_request()
|
||||
{
|
||||
// given
|
||||
var client = new HttpClient();
|
||||
|
||||
// when
|
||||
await client.GetAsync(MapperServer.UrlPrefix + "index.html?id=toto");
|
||||
|
||||
// then
|
||||
Check.That(MapperServer.LastRequest).IsNotNull();
|
||||
Check.That(MapperServer.LastRequest.Path).EndsWith("/index.html");
|
||||
Check.That(MapperServer.LastRequest.GetParameter("id")).HasSize(1);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void StopListenerServer()
|
||||
{
|
||||
_server.Stop();
|
||||
}
|
||||
|
||||
private class MapperServer : TinyHttpServer
|
||||
{
|
||||
private static volatile Request _lastRequest;
|
||||
|
||||
private MapperServer(string urlPrefix, Action<HttpListenerContext> httpHandler) : base(urlPrefix, httpHandler)
|
||||
{
|
||||
}
|
||||
|
||||
public static Request LastRequest
|
||||
{
|
||||
get
|
||||
{
|
||||
return _lastRequest;
|
||||
}
|
||||
|
||||
private set
|
||||
{
|
||||
_lastRequest = value;
|
||||
}
|
||||
}
|
||||
|
||||
public static string UrlPrefix { get; private set; }
|
||||
|
||||
public static new MapperServer Start()
|
||||
{
|
||||
var port = Ports.FindFreeTcpPort();
|
||||
UrlPrefix = "http://localhost:" + port + "/";
|
||||
var server = new MapperServer(
|
||||
UrlPrefix,
|
||||
context =>
|
||||
{
|
||||
LastRequest = new HttpListenerRequestMapper().Map(context.Request);
|
||||
context.Response.Close();
|
||||
});
|
||||
((TinyHttpServer)server).Start();
|
||||
return server;
|
||||
}
|
||||
|
||||
public new void Stop()
|
||||
{
|
||||
base.Stop();
|
||||
LastRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
126
test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs
Normal file
126
test/WireMock.Net.Tests/HttpListenerResponseMapperTests.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using NUnit.Framework;
|
||||
using WireMock.Http;
|
||||
|
||||
[module:
|
||||
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",
|
||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||
[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 ArrangeThisQualifier
|
||||
// ReSharper disable InconsistentNaming
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class HttpListenerResponseMapperTests
|
||||
{
|
||||
private TinyHttpServer _server;
|
||||
private Task<HttpResponseMessage> _responseMsgTask;
|
||||
|
||||
[Test]
|
||||
public void Should_map_status_code_from_original_response()
|
||||
{
|
||||
// given
|
||||
var response = new Response { StatusCode = 404 };
|
||||
var httpListenerResponse = CreateHttpListenerResponse();
|
||||
|
||||
// when
|
||||
new HttpListenerResponseMapper().Map(response, httpListenerResponse);
|
||||
|
||||
// then
|
||||
Check.That(httpListenerResponse.StatusCode).IsEqualTo(404);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Should_map_headers_from_original_response()
|
||||
{
|
||||
// given
|
||||
var response = new Response();
|
||||
response.AddHeader("cache-control", "no-cache");
|
||||
var httpListenerResponse = CreateHttpListenerResponse();
|
||||
|
||||
// when
|
||||
new HttpListenerResponseMapper().Map(response, httpListenerResponse);
|
||||
|
||||
// then
|
||||
Check.That(httpListenerResponse.Headers).HasSize(1);
|
||||
Check.That(httpListenerResponse.Headers.Keys).Contains("cache-control");
|
||||
Check.That(httpListenerResponse.Headers.Get("cache-control")).Contains("no-cache");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Should_map_body_from_original_response()
|
||||
{
|
||||
// given
|
||||
var response = new Response();
|
||||
response.Body = "Hello !!!";
|
||||
var httpListenerResponse = CreateHttpListenerResponse();
|
||||
|
||||
// when
|
||||
new HttpListenerResponseMapper().Map(response, httpListenerResponse);
|
||||
|
||||
// then
|
||||
var responseMessage = ToResponseMessage(httpListenerResponse);
|
||||
Check.That(responseMessage).IsNotNull();
|
||||
var contentTask = responseMessage.Content.ReadAsStringAsync();
|
||||
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void StopServer()
|
||||
{
|
||||
if (_server != null)
|
||||
{
|
||||
_server.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dirty HACK to get HttpListenerResponse instances
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="HttpListenerResponse"/>.
|
||||
/// </returns>
|
||||
public HttpListenerResponse CreateHttpListenerResponse()
|
||||
{
|
||||
var port = Ports.FindFreeTcpPort();
|
||||
var urlPrefix = "http://localhost:" + port + "/";
|
||||
var responseReady = new AutoResetEvent(false);
|
||||
HttpListenerResponse response = null;
|
||||
_server = new TinyHttpServer(
|
||||
urlPrefix,
|
||||
context =>
|
||||
{
|
||||
response = context.Response;
|
||||
responseReady.Set();
|
||||
});
|
||||
_server.Start();
|
||||
_responseMsgTask = new HttpClient().GetAsync(urlPrefix);
|
||||
responseReady.WaitOne();
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpResponseMessage ToResponseMessage(HttpListenerResponse listenerResponse)
|
||||
{
|
||||
listenerResponse.Close();
|
||||
_responseMsgTask.Wait();
|
||||
return _responseMsgTask.Result;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
test/WireMock.Net.Tests/Properties/AssemblyInfo.cs
Normal file
36
test/WireMock.Net.Tests/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("WireMock.Net.Tests")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("WireMock.Net.Tests")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("d8b56d28-33ce-4bef-97d4-7dd546e37f25")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
42
test/WireMock.Net.Tests/RequestTests.cs
Normal file
42
test/WireMock.Net.Tests/RequestTests.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using NFluent;
|
||||
using NUnit.Framework;
|
||||
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1600:ElementsMustBeDocumented",
|
||||
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1633:FileMustHaveHeader",
|
||||
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
|
||||
// ReSharper disable InconsistentNaming
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class RequestTests
|
||||
{
|
||||
[Test]
|
||||
public void Should_handle_empty_query()
|
||||
{
|
||||
// given
|
||||
var request = new Request("/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 Request("/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");
|
||||
}
|
||||
}
|
||||
}
|
||||
215
test/WireMock.Net.Tests/RequestsTests.cs
Normal file
215
test/WireMock.Net.Tests/RequestsTests.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
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 RequestsTests
|
||||
{
|
||||
[Test]
|
||||
public void Should_specify_requests_matching_given_url()
|
||||
{
|
||||
// given
|
||||
var spec = Requests.WithUrl("/foo");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithUrl("/foo*");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithUrl("/foo");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithPath("/foo");
|
||||
|
||||
// when
|
||||
var request = new Request("/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()
|
||||
{
|
||||
// given
|
||||
var spec = Requests.WithUrl("/foo").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new Request("/foo", string.Empty, "PUT", "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 = Requests.WithUrl("/foo").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithUrl("/bar").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata");
|
||||
|
||||
// when
|
||||
var request = new Request("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Should_specify_requests_matching_given_header_prefix()
|
||||
{
|
||||
// given
|
||||
var spec = Requests.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithUrl("/foo").UsingAnyVerb().WithBody("H*o wor?d!");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithPath("/foo").WithParam("bar", "1", "2");
|
||||
|
||||
// when
|
||||
var request = new Request("/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 = Requests.WithPath("/foo").WithParam("bar", "1");
|
||||
|
||||
// when
|
||||
var request = new Request("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string>());
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
48
test/WireMock.Net.Tests/WildcardPatternMatcherTests.cs
Normal file
48
test/WireMock.Net.Tests/WildcardPatternMatcherTests.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
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 WildcardPatternMatcherTests
|
||||
{
|
||||
[Test]
|
||||
public void Should_evaluate_patterns()
|
||||
{
|
||||
// Positive Tests
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*", string.Empty)).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("?", " ")).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*", "a")).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*", "ab")).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("?", "a")).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*?", "abc")).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("?*", "abc")).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*abc", "abc")).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*abc*", "abc")).IsTrue();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*a*bc*", "aXXXbc")).IsTrue();
|
||||
|
||||
// Negative Tests
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*a", string.Empty)).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("a*", string.Empty)).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("?", string.Empty)).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*b*", "a")).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("b*a", "ab")).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("??", "a")).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*?", string.Empty)).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("??*", "a")).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*abc", "abX")).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*abc*", "Xbc")).IsFalse();
|
||||
Check.That(WildcardPatternMatcher.MatchWildcardString("*a*bc*", "ac")).IsFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
85
test/WireMock.Net.Tests/WireMock.Net.Tests.csproj
Normal file
85
test/WireMock.Net.Tests/WireMock.Net.Tests.csproj
Normal file
@@ -0,0 +1,85 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{D8B56D28-33CE-4BEF-97D4-7DD546E37F25}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>WireMock.Net.Tests</RootNamespace>
|
||||
<AssemblyName>WireMock.Net.Tests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Moq, Version=4.5.30.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Moq.4.5.30\lib\net45\Moq.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="NFluent, Version=1.3.1.0, Culture=neutral, PublicKeyToken=18828b37b84b1437, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\NFluent.1.3.1.0\lib\net40\NFluent.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="nunit.framework, Version=3.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\NUnit.3.6.0\lib\net45\nunit.framework.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="WireMock">
|
||||
<HintPath>..\..\src\WireMock\bin\$(Configuration)\net45\WireMock.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="FluentMockServerTests.cs" />
|
||||
<Compile Include="HttpListenerRequestMapperTests.cs" />
|
||||
<Compile Include="HttpListenerResponseMapperTests.cs" />
|
||||
<Compile Include="Http\TinyHttpServerTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="RequestsTests.cs" />
|
||||
<Compile Include="RequestTests.cs" />
|
||||
<Compile Include="WildcardPatternMatcherTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
7
test/WireMock.Net.Tests/packages.config
Normal file
7
test/WireMock.Net.Tests/packages.config
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Castle.Core" version="3.3.3" targetFramework="net452" />
|
||||
<package id="Moq" version="4.5.30" targetFramework="net452" />
|
||||
<package id="NFluent" version="1.3.1.0" targetFramework="net452" />
|
||||
<package id="NUnit" version="3.6.0" targetFramework="net452" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user