Files
WireMock.Net/test/WireMock.Net.Tests/ObservableLogEntriesTest.cs
Stef Heyenrath d971144426 WireMock.Net version 1.1.x (#363)
* refactor

* rename api

* -preview-01

* logger

* move

* RandomDataGenerator.Net

* .

* ISettings

* renames...

* refactor CommandlineParser logic

* remove standalone

* Remove Interfaces

* Update tests

* WireMock.Net.StandAlone

* .

* fix

* .

* _settings

* Admin

* WireMock.Net.Abstractions

* fix build

* rename WireMockServer

* fix compile errors
2019-12-27 16:01:13 +01:00

110 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using NFluent;
using WireMock.Logging;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests
{
public class ObservableLogEntriesTest
{
[Fact]
public async void WireMockServer_LogEntriesChanged_WithException_Should_LogError()
{
// Assign
string path = $"/log_{Guid.NewGuid()}";
var loggerMock = new Mock<IWireMockLogger>();
loggerMock.Setup(l => l.Error(It.IsAny<string>(), It.IsAny<object[]>()));
var settings = new WireMockServerSettings
{
Logger = loggerMock.Object
};
var server = WireMockServer.Start(settings);
server
.Given(Request.Create()
.WithPath(path)
.UsingGet())
.RespondWith(Response.Create()
.WithBody(@"{ msg: ""Hello world!""}"));
server.LogEntriesChanged += (sender, args) => throw new Exception();
// Act
await new HttpClient().GetAsync($"http://localhost:{server.Ports[0]}{path}");
// Assert
loggerMock.Verify(l => l.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
}
[Fact]
public async void WireMockServer_LogEntriesChanged()
{
// Assign
string path = $"/log_{Guid.NewGuid()}";
var server = WireMockServer.Start();
server
.Given(Request.Create()
.WithPath(path)
.UsingGet())
.RespondWith(Response.Create()
.WithBody(@"{ msg: ""Hello world!""}"));
int count = 0;
server.LogEntriesChanged += (sender, args) => count++;
// Act
await new HttpClient().GetAsync($"http://localhost:{server.Ports[0]}{path}");
// Assert
Check.That(count).Equals(1);
}
[Fact]
public async Task WireMockServer_LogEntriesChanged_Parallel()
{
int expectedCount = 10;
// Assign
string path = $"/log_p_{Guid.NewGuid()}";
var server = WireMockServer.Start();
server
.Given(Request.Create()
.WithPath(path)
.UsingGet())
.RespondWith(Response.Create()
.WithSuccess());
int count = 0;
server.LogEntriesChanged += (sender, args) => count++;
var http = new HttpClient();
// Act
var listOfTasks = new List<Task<HttpResponseMessage>>();
for (var i = 0; i < expectedCount; i++)
{
Thread.Sleep(10);
listOfTasks.Add(http.GetAsync($"{server.Urls[0]}{path}"));
}
var responses = await Task.WhenAll(listOfTasks);
var countResponsesWithStatusNotOk = responses.Count(r => r.StatusCode != HttpStatusCode.OK);
// Assert
Check.That(countResponsesWithStatusNotOk).Equals(0);
Check.That(count).Equals(expectedCount);
}
}
}