mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-17 23:57:03 +01:00
230 lines
7.2 KiB
C#
230 lines
7.2 KiB
C#
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;
|
|
using WireMock.RequestBuilders;
|
|
using WireMock.ResponseBuilders;
|
|
|
|
[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(Request
|
|
.WithUrl("/foo")
|
|
.UsingGet())
|
|
.RespondWith(Response
|
|
.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).IsNull();
|
|
}
|
|
|
|
[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(Request.WithUrl("/b.*")).ToList();
|
|
Check.That(result).HasSize(1);
|
|
|
|
var requestLogged = result.First();
|
|
Check.That(requestLogged.Path).IsEqualTo("/bar");
|
|
Check.That(requestLogged.Url).IsEqualTo("http://localhost:" + _server.Port + "/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(Request
|
|
.WithUrl("/foo")
|
|
.UsingGet())
|
|
.RespondWith(Response
|
|
.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(Request
|
|
.WithUrl("/foo")
|
|
.UsingGet())
|
|
.RespondWith(Response
|
|
.WithStatusCode(307)
|
|
.WithHeader("Location", "/bar"));
|
|
_server
|
|
.Given(Request
|
|
.WithUrl("/bar")
|
|
.UsingGet())
|
|
.RespondWith(Response
|
|
.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(Request
|
|
.WithUrl("/*"))
|
|
.RespondWith(Response
|
|
.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(Request
|
|
.WithUrl("/*"))
|
|
.RespondWith(Response
|
|
.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();
|
|
}
|
|
}
|
|
}
|