mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-19 23:41:41 +02:00
Fix unit-tests
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
# WireMock.Net
|
# WireMock.Net
|
||||||
A C# .NET version based on https://github.com/alexvictoor/WireMock which tries to mimic the functionality from http://WireMock.org
|
A C# .NET version based on https://github.com/alexvictoor/WireMock which mimics the functionality from http://WireMock.org
|
||||||
|
|
||||||
[](https://ci.appveyor.com/project/StefH/wiremock-net)
|
[](https://ci.appveyor.com/project/StefH/wiremock-net)
|
||||||
|
|
||||||
[](https://www.nuget.org/packages/WireMock.Net)
|
[](https://www.nuget.org/packages/WireMock.Net)
|
||||||
|
|
||||||
Based on class HttpListener from the .net framework, it is very lightweight and have no external dependencies.
|
Based on class HttpListener from the .net framework, it is very lightweight and have no external dependencies.
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace WireMock.Http
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The ports.
|
/// The ports.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Ports
|
public static class PortUtil
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The find free TCP port.
|
/// The find free TCP port.
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.Http
|
namespace WireMock.Http
|
||||||
{
|
{
|
||||||
@@ -24,20 +27,43 @@ namespace WireMock.Http
|
|||||||
/// </value>
|
/// </value>
|
||||||
public bool IsStarted { get; private set; }
|
public bool IsStarted { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the url.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The urls.
|
||||||
|
/// </value>
|
||||||
|
public List<Uri> Urls { get; } = new List<Uri>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the ports.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The ports.
|
||||||
|
/// </value>
|
||||||
|
public List<int> Ports { get; } = new List<int>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="TinyHttpServer"/> class.
|
/// Initializes a new instance of the <see cref="TinyHttpServer"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="urls">The urls.</param>
|
/// <param name="uriPrefixes">The uriPrefixes.</param>
|
||||||
/// <param name="httpHandler">The http handler.</param>
|
/// <param name="httpHandler">The http handler.</param>
|
||||||
public TinyHttpServer(string[] urls, Action<HttpListenerContext> httpHandler)
|
public TinyHttpServer([NotNull] Action<HttpListenerContext> httpHandler, [NotNull] params string[] uriPrefixes)
|
||||||
{
|
{
|
||||||
|
Check.NotNull(httpHandler, nameof(httpHandler));
|
||||||
|
Check.NotEmpty(uriPrefixes, nameof(uriPrefixes));
|
||||||
|
|
||||||
_httpHandler = httpHandler;
|
_httpHandler = httpHandler;
|
||||||
|
|
||||||
// Create a listener.
|
// Create a listener.
|
||||||
_listener = new HttpListener();
|
_listener = new HttpListener();
|
||||||
foreach (string urlPrefix in urls)
|
foreach (string uriPrefix in uriPrefixes)
|
||||||
{
|
{
|
||||||
_listener.Prefixes.Add(urlPrefix);
|
var uri = new Uri(uriPrefix);
|
||||||
|
Urls.Add(uri);
|
||||||
|
Ports.Add(uri.Port);
|
||||||
|
|
||||||
|
_listener.Prefixes.Add(uriPrefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using JetBrains.Annotations;
|
|
||||||
|
|
||||||
namespace WireMock.ResponseBuilders
|
namespace WireMock.ResponseBuilders
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Collections;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace WireMock.ResponseBuilders
|
namespace WireMock.ResponseBuilders
|
||||||
|
|||||||
@@ -32,6 +32,14 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
private TimeSpan _requestProcessingDelay = TimeSpan.Zero;
|
private TimeSpan _requestProcessingDelay = TimeSpan.Zero;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the ports.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The ports.
|
||||||
|
/// </value>
|
||||||
|
public List<int> Ports { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the urls.
|
/// Gets the urls.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -77,7 +85,7 @@ namespace WireMock.Server
|
|||||||
Check.Condition(port, p => p >= 0, nameof(port));
|
Check.Condition(port, p => p >= 0, nameof(port));
|
||||||
|
|
||||||
if (port == 0)
|
if (port == 0)
|
||||||
port = Ports.FindFreeTcpPort();
|
port = PortUtil.FindFreeTcpPort();
|
||||||
|
|
||||||
return new FluentMockServer(false, port, ssl);
|
return new FluentMockServer(false, port, ssl);
|
||||||
}
|
}
|
||||||
@@ -107,7 +115,7 @@ namespace WireMock.Server
|
|||||||
Check.Condition(port, p => p >= 0, nameof(port));
|
Check.Condition(port, p => p >= 0, nameof(port));
|
||||||
|
|
||||||
if (port == 0)
|
if (port == 0)
|
||||||
port = Ports.FindFreeTcpPort();
|
port = PortUtil.FindFreeTcpPort();
|
||||||
|
|
||||||
return new FluentMockServer(true, port, ssl);
|
return new FluentMockServer(true, port, ssl);
|
||||||
}
|
}
|
||||||
@@ -133,7 +141,9 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
Urls = urls;
|
Urls = urls;
|
||||||
|
|
||||||
_httpServer = new TinyHttpServer(urls, HandleRequestAsync);
|
_httpServer = new TinyHttpServer(HandleRequestAsync, urls);
|
||||||
|
Ports = _httpServer.Ports;
|
||||||
|
|
||||||
_httpServer.Start();
|
_httpServer.Start();
|
||||||
|
|
||||||
if (startAdminInterface)
|
if (startAdminInterface)
|
||||||
@@ -142,7 +152,6 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stop this server.
|
/// Stop this server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"version": "1.0.0.0",
|
"version": "1.0.1.0",
|
||||||
"title": "WireMock.Net",
|
"title": "WireMock.Net",
|
||||||
"description": "Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.",
|
"description": "Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.",
|
||||||
"authors": [ "Alexandre Victoor", "Stef Heyenrath" ],
|
"authors": [ "Alexandre Victoor", "Stef Heyenrath" ],
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
"projectUrl": "https://github.com/StefH/WireMock.Net",
|
"projectUrl": "https://github.com/StefH/WireMock.Net",
|
||||||
"iconUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/WireMock.Net-Logo.png",
|
"iconUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/WireMock.Net-Logo.png",
|
||||||
"licenseUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/LICENSE",
|
"licenseUrl": "https://raw.githubusercontent.com/StefH/WireMock.Net/master/LICENSE",
|
||||||
"releaseNotes": "First version"
|
"releaseNotes": "Added Admin-Interface"
|
||||||
},
|
},
|
||||||
|
|
||||||
"buildOptions": {
|
"buildOptions": {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ namespace WireMock.Net.Tests
|
|||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(_server.LogEntries).HasSize(1);
|
Check.That(_server.LogEntries).HasSize(1);
|
||||||
@@ -95,7 +95,7 @@ namespace WireMock.Net.Tests
|
|||||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(response).IsEqualTo(@"{ msg: ""Hello world!""}");
|
Check.That(response).IsEqualTo(@"{ msg: ""Hello world!""}");
|
||||||
@@ -110,7 +110,7 @@ namespace WireMock.Net.Tests
|
|||||||
_server.Given(Request.Create().WithPath("/foo").UsingGet()).RespondWith(Response.Create().WithBodyAsBase64("SGVsbG8gV29ybGQ/"));
|
_server.Given(Request.Create().WithPath("/foo").UsingGet()).RespondWith(Response.Create().WithBodyAsBase64("SGVsbG8gV29ybGQ/"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(response).IsEqualTo("Hello World?");
|
Check.That(response).IsEqualTo("Hello World?");
|
||||||
@@ -123,7 +123,7 @@ namespace WireMock.Net.Tests
|
|||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
||||||
@@ -137,8 +137,8 @@ namespace WireMock.Net.Tests
|
|||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/bar");
|
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/bar");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var result = _server.SearchLogsFor(Request.Create().WithPath(new RegexMatcher("^/b.*"))).ToList();
|
var result = _server.SearchLogsFor(Request.Create().WithPath(new RegexMatcher("^/b.*"))).ToList();
|
||||||
@@ -146,7 +146,7 @@ namespace WireMock.Net.Tests
|
|||||||
|
|
||||||
var requestLogged = result.First();
|
var requestLogged = result.First();
|
||||||
Check.That(requestLogged.RequestMessage.Path).IsEqualTo("/bar");
|
Check.That(requestLogged.RequestMessage.Path).IsEqualTo("/bar");
|
||||||
Check.That(requestLogged.RequestMessage.Url).IsEqualTo("http://localhost:" + _server.Port + "/bar");
|
Check.That(requestLogged.RequestMessage.Url).IsEqualTo("http://localhost:" + _server.Ports[0] + "/bar");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -156,7 +156,7 @@ namespace WireMock.Net.Tests
|
|||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
|
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
_server.Reset();
|
_server.Reset();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
@@ -180,7 +180,7 @@ namespace WireMock.Net.Tests
|
|||||||
_server.Reset();
|
_server.Reset();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.ThatAsyncCode(() => new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo"))
|
Check.ThatAsyncCode(() => new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo"))
|
||||||
.ThrowsAny();
|
.ThrowsAny();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +206,7 @@ namespace WireMock.Net.Tests
|
|||||||
.WithBody("REDIRECT SUCCESSFUL"));
|
.WithBody("REDIRECT SUCCESSFUL"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(response).IsEqualTo("REDIRECT SUCCESSFUL");
|
Check.That(response).IsEqualTo("REDIRECT SUCCESSFUL");
|
||||||
@@ -228,7 +228,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
var watch = new Stopwatch();
|
var watch = new Stopwatch();
|
||||||
watch.Start();
|
watch.Start();
|
||||||
await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
@@ -248,7 +248,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
var watch = new Stopwatch();
|
var watch = new Stopwatch();
|
||||||
watch.Start();
|
watch.Start();
|
||||||
await new HttpClient().GetStringAsync("http://localhost:" + _server.Port + "/foo");
|
await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ namespace WireMock.Net.Tests.Http
|
|||||||
public void Should_call_handler_on_request()
|
public void Should_call_handler_on_request()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
var port = Ports.FindFreeTcpPort();
|
var port = PortUtil.FindFreeTcpPort();
|
||||||
bool called = false;
|
bool called = false;
|
||||||
var urlPrefix = "http://localhost:" + port + "/";
|
var urlPrefix = "http://localhost:" + port + "/";
|
||||||
var server = new TinyHttpServer(urlPrefix, ctx => called = true);
|
var server = new TinyHttpServer(ctx => called = true, urlPrefix);
|
||||||
server.Start();
|
server.Start();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ namespace WireMock.Net.Tests
|
|||||||
{
|
{
|
||||||
private static volatile RequestMessage _lastRequestMessage;
|
private static volatile RequestMessage _lastRequestMessage;
|
||||||
|
|
||||||
private MapperServer(string urlPrefix, Action<HttpListenerContext> httpHandler) : base(urlPrefix, httpHandler)
|
private MapperServer(Action<HttpListenerContext> httpHandler, string urlPrefix) : base(httpHandler, urlPrefix)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,16 +125,16 @@ namespace WireMock.Net.Tests
|
|||||||
|
|
||||||
public new static MapperServer Start()
|
public new static MapperServer Start()
|
||||||
{
|
{
|
||||||
var port = Ports.FindFreeTcpPort();
|
int port = PortUtil.FindFreeTcpPort();
|
||||||
UrlPrefix = "http://localhost:" + port + "/";
|
UrlPrefix = "http://localhost:" + port + "/";
|
||||||
var server = new MapperServer(
|
var server = new MapperServer(
|
||||||
UrlPrefix,
|
|
||||||
context =>
|
context =>
|
||||||
{
|
{
|
||||||
LastRequestMessage = new HttpListenerRequestMapper().Map(context.Request);
|
LastRequestMessage = new HttpListenerRequestMapper().Map(context.Request);
|
||||||
context.Response.Close();
|
context.Response.Close();
|
||||||
});
|
}, UrlPrefix);
|
||||||
((TinyHttpServer)server).Start();
|
((TinyHttpServer)server).Start();
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,10 +70,7 @@ namespace WireMock.Net.Tests
|
|||||||
[TearDown]
|
[TearDown]
|
||||||
public void StopServer()
|
public void StopServer()
|
||||||
{
|
{
|
||||||
if (_server != null)
|
_server?.Stop();
|
||||||
{
|
|
||||||
_server.Stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -84,17 +81,16 @@ namespace WireMock.Net.Tests
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public HttpListenerResponse CreateHttpListenerResponse()
|
public HttpListenerResponse CreateHttpListenerResponse()
|
||||||
{
|
{
|
||||||
var port = Ports.FindFreeTcpPort();
|
var port = PortUtil.FindFreeTcpPort();
|
||||||
var urlPrefix = "http://localhost:" + port + "/";
|
var urlPrefix = "http://localhost:" + port + "/";
|
||||||
var responseReady = new AutoResetEvent(false);
|
var responseReady = new AutoResetEvent(false);
|
||||||
HttpListenerResponse response = null;
|
HttpListenerResponse response = null;
|
||||||
_server = new TinyHttpServer(
|
_server = new TinyHttpServer(
|
||||||
urlPrefix,
|
|
||||||
context =>
|
context =>
|
||||||
{
|
{
|
||||||
response = context.Response;
|
response = context.Response;
|
||||||
responseReady.Set();
|
responseReady.Set();
|
||||||
});
|
}, urlPrefix);
|
||||||
_server.Start();
|
_server.Start();
|
||||||
_responseMsgTask = new HttpClient().GetAsync(urlPrefix);
|
_responseMsgTask = new HttpClient().GetAsync(urlPrefix);
|
||||||
responseReady.WaitOne();
|
responseReady.WaitOne();
|
||||||
|
|||||||
Reference in New Issue
Block a user