Support Microsoft.AspNetCore for net 4.6.1 and up (#185)

* net451

* tests : net462

* fixed tests

* fix tests

* readme

* Code review

* LocalFileSystemHandlerTests

* refactor
This commit is contained in:
Stef Heyenrath
2018-08-17 18:52:29 +02:00
committed by GitHub
parent 01d6dce62d
commit b57d118c3d
69 changed files with 842 additions and 2032 deletions

View File

@@ -1,303 +1,30 @@
using System;
using NFluent;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Moq;
using NFluent;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using Xunit;
using Newtonsoft.Json;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Settings;
using WireMock.Admin.Mappings;
namespace WireMock.Net.Tests
{
public partial class FluentMockServerTests : IDisposable
public class FluentMockServerTests
{
private FluentMockServer _server;
private static string jsonRequestMessage = @"{ ""message"" : ""Hello server"" }";
// For for AppVeyor + OpenCover
private string GetCurrentFolder()
{
string current = Directory.GetCurrentDirectory();
//if (!current.EndsWith("WireMock.Net.Tests"))
// return Path.Combine(current, "test", "WireMock.Net.Tests");
return current;
}
[Fact]
public void FluentMockServer_StartStop()
{
var server1 = FluentMockServer.Start("http://localhost:9091/");
server1.Stop();
var server2 = FluentMockServer.Start("http://localhost:9091/");
server2.Stop();
}
[Fact]
public void FluentMockServer_SaveStaticMappings()
{
// Assign
string guid = "791a3f31-6946-aaaa-8e6f-0237c7441111";
var _staticMappingHandlerMock = new Mock<IFileSystemHandler>();
_staticMappingHandlerMock.Setup(m => m.GetMappingFolder()).Returns("folder");
_staticMappingHandlerMock.Setup(m => m.FolderExists(It.IsAny<string>())).Returns(true);
_staticMappingHandlerMock.Setup(m => m.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()));
_server = FluentMockServer.Start(new FluentMockServerSettings
{
FileSystemHandler = _staticMappingHandlerMock.Object
});
_server
.Given(Request.Create().WithPath($"/foo_{Guid.NewGuid()}"))
.WithGuid(guid)
.RespondWith(Response.Create().WithBody("save test"));
// Act
_server.SaveStaticMappings();
// Assert and Verify
_staticMappingHandlerMock.Verify(m => m.GetMappingFolder(), Times.Once);
_staticMappingHandlerMock.Verify(m => m.FolderExists("folder"), Times.Once);
_staticMappingHandlerMock.Verify(m => m.WriteMappingFile(Path.Combine("folder", guid + ".json"), It.IsAny<string>()), Times.Once);
}
[Fact]
public void FluentMockServer_ReadStaticMapping_WithNonGuidFilename()
{
var guid = Guid.Parse("04ee4872-9efd-4770-90d3-88d445265d0d");
string title = "documentdb_root_title";
_server = FluentMockServer.Start();
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", "documentdb_root.json");
_server.ReadStaticMappingAndAddOrUpdate(folder);
var mappings = _server.Mappings.ToArray();
Check.That(mappings).HasSize(1);
Check.That(mappings.First().RequestMatcher).IsNotNull();
Check.That(mappings.First().Provider).IsNotNull();
Check.That(mappings.First().Guid).Equals(guid);
Check.That(mappings.First().Title).Equals(title);
}
[Fact]
public void FluentMockServer_ReadStaticMapping_WithGuidFilename()
{
string guid = "00000002-ee28-4f29-ae63-1ac9b0802d86";
_server = FluentMockServer.Start();
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
_server.ReadStaticMappingAndAddOrUpdate(folder);
var mappings = _server.Mappings.ToArray();
Check.That(mappings).HasSize(1);
Check.That(mappings.First().RequestMatcher).IsNotNull();
Check.That(mappings.First().Provider).IsNotNull();
Check.That(mappings.First().Guid).Equals(Guid.Parse(guid));
Check.That(mappings.First().Title).IsNullOrEmpty();
}
[Fact]
public void FluentMockServer_ReadStaticMapping_WithResponseBodyFromFile()
{
string guid = "00000002-ee28-4f29-ae63-1ac9b0802d87";
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
string json = File.ReadAllText(folder);
string responseBodyFilePath = Path.Combine(GetCurrentFolder(), "responsebody.json");
dynamic jsonObj = JsonConvert.DeserializeObject(json);
jsonObj["Response"]["BodyAsFile"] = responseBodyFilePath;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText(folder, output);
_server = FluentMockServer.Start();
_server.ReadStaticMappingAndAddOrUpdate(folder);
var mappings = _server.Mappings.ToArray();
Check.That(mappings).HasSize(1);
Check.That(mappings.First().RequestMatcher).IsNotNull();
Check.That(mappings.First().Provider).IsNotNull();
Check.That(mappings.First().Guid).Equals(Guid.Parse(guid));
Check.That(mappings.First().Title).IsNullOrEmpty();
}
[Fact]
public void FluentMockServer_ReadStaticMappings_FolderExistsIsTrue()
{
// Assign
var _staticMappingHandlerMock = new Mock<IFileSystemHandler>();
_staticMappingHandlerMock.Setup(m => m.GetMappingFolder()).Returns("folder");
_staticMappingHandlerMock.Setup(m => m.FolderExists(It.IsAny<string>())).Returns(true);
_staticMappingHandlerMock.Setup(m => m.EnumerateFiles(It.IsAny<string>())).Returns(new string[0]);
_server = FluentMockServer.Start(new FluentMockServerSettings
{
FileSystemHandler = _staticMappingHandlerMock.Object
});
// Act
_server.ReadStaticMappings();
// Assert and Verify
_staticMappingHandlerMock.Verify(m => m.GetMappingFolder(), Times.Once);
_staticMappingHandlerMock.Verify(m => m.FolderExists("folder"), Times.Once);
_staticMappingHandlerMock.Verify(m => m.EnumerateFiles("folder"), Times.Once);
}
[Fact]
public void FluentMockServer_ReadStaticMappingAndAddOrUpdate()
{
// Assign
string mapping = "{\"Request\": {\"Path\": {\"Matchers\": [{\"Name\": \"WildcardMatcher\",\"Pattern\": \"/static/mapping\"}]},\"Methods\": [\"get\"]},\"Response\": {\"BodyAsJson\": { \"body\": \"static mapping\" }}}";
var _staticMappingHandlerMock = new Mock<IFileSystemHandler>();
_staticMappingHandlerMock.Setup(m => m.ReadMappingFile(It.IsAny<string>())).Returns(mapping);
_server = FluentMockServer.Start(new FluentMockServerSettings
{
FileSystemHandler = _staticMappingHandlerMock.Object
});
// Act
_server.ReadStaticMappingAndAddOrUpdate(@"c:\test.json");
// Assert and Verify
_staticMappingHandlerMock.Verify(m => m.ReadMappingFile(@"c:\test.json"), Times.Once);
}
[Fact]
public void FluentMockServer_ReadStaticMappings()
{
_server = FluentMockServer.Start();
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings");
_server.ReadStaticMappings(folder);
var mappings = _server.Mappings.ToArray();
Check.That(mappings).HasSize(3);
}
[Fact]
public void FluentMockServer_Admin_Mappings_WithGuid_Get()
{
Guid guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
_server = FluentMockServer.Start();
_server.Given(Request.Create().WithPath("/foo1").UsingGet()).WithGuid(guid)
.RespondWith(Response.Create().WithStatusCode(201).WithBody("1"));
_server.Given(Request.Create().WithPath("/foo2").UsingGet())
.RespondWith(Response.Create().WithStatusCode(202).WithBody("2"));
var mappings = _server.Mappings.ToArray();
Check.That(mappings).HasSize(2);
}
[Fact]
public void FluentMockServer_Admin_Mappings_WithGuidAsString_Get()
{
string guid = "90356dba-b36c-469a-a17e-669cd84f1f05";
_server = FluentMockServer.Start();
_server.Given(Request.Create().WithPath("/foo100").UsingGet()).WithGuid(guid)
.RespondWith(Response.Create().WithStatusCode(201).WithBody("1"));
var mappings = _server.Mappings.ToArray();
Check.That(mappings).HasSize(1);
}
[Fact]
public void FluentMockServer_Admin_Mappings_Add_SameGuid()
{
var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
_server = FluentMockServer.Start();
var response1 = Response.Create().WithStatusCode(500);
_server.Given(Request.Create().UsingGet())
.WithGuid(guid)
.RespondWith(response1);
var mappings1 = _server.Mappings.ToArray();
Check.That(mappings1).HasSize(1);
Check.That(mappings1.First().Guid).Equals(guid);
var response2 = Response.Create().WithStatusCode(400);
_server.Given(Request.Create().WithPath("/2").UsingGet())
.WithGuid(guid)
.RespondWith(response2);
var mappings2 = _server.Mappings.ToArray();
Check.That(mappings2).HasSize(1);
Check.That(mappings2.First().Guid).Equals(guid);
Check.That(mappings2.First().Provider).Equals(response2);
}
[Fact]
public async Task FluentMockServer_Admin_Mappings_AtPriority()
{
_server = FluentMockServer.Start();
// given
_server.Given(Request.Create().WithPath("/1").UsingGet())
.AtPriority(2)
.RespondWith(Response.Create().WithStatusCode(200));
_server.Given(Request.Create().WithPath("/1").UsingGet())
.AtPriority(1)
.RespondWith(Response.Create().WithStatusCode(400));
var mappings = _server.Mappings.ToArray();
Check.That(mappings).HasSize(2);
// when
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/1");
// then
Check.That((int)response.StatusCode).IsEqualTo(400);
}
[Fact]
public async Task FluentMockServer_Admin_Requests_Get()
{
// given
_server = FluentMockServer.Start();
// when
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
// then
Check.That(_server.LogEntries).HasSize(1);
var requestLogged = _server.LogEntries.First();
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("get");
Check.That(requestLogged.RequestMessage.BodyAsBytes).IsNull();
}
[Fact]
public async Task FluentMockServer_Should_respond_to_request_methodPatch()
{
// given
string path = $"/foo_{Guid.NewGuid()}";
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server.Given(Request.Create().WithPath(path).UsingMethod("patch"))
.RespondWith(Response.Create().WithBody("hello patch"));
@@ -325,7 +52,7 @@ namespace WireMock.Net.Tests
public async Task FluentMockServer_Should_respond_to_request_bodyAsString()
{
// given
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server
.Given(Request.Create()
@@ -346,7 +73,7 @@ namespace WireMock.Net.Tests
public async Task FluentMockServer_Should_respond_to_request_BodyAsJson()
{
// Assign
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server
.Given(Request.Create().UsingAnyMethod())
@@ -363,7 +90,7 @@ namespace WireMock.Net.Tests
public async Task FluentMockServer_Should_respond_to_request_BodyAsJson_Indented()
{
// Assign
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server
.Given(Request.Create().UsingAnyMethod())
@@ -380,7 +107,7 @@ namespace WireMock.Net.Tests
public async Task FluentMockServer_Should_respond_to_request_bodyAsCallback()
{
// given
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server
.Given(Request.Create()
@@ -401,7 +128,7 @@ namespace WireMock.Net.Tests
public async Task FluentMockServer_Should_respond_to_request_bodyAsBase64()
{
// given
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server.Given(Request.Create().WithPath("/foo").UsingGet()).RespondWith(Response.Create().WithBodyFromBase64("SGVsbG8gV29ybGQ/"));
@@ -417,7 +144,7 @@ namespace WireMock.Net.Tests
{
// given
string path = $"/foo_{Guid.NewGuid()}";
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server.Given(Request.Create().WithPath(path).UsingGet()).RespondWith(Response.Create().WithBody(new byte[] { 48, 49 }));
@@ -446,7 +173,7 @@ namespace WireMock.Net.Tests
new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "text/plain" }
};
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
foreach (var item in validMatchersForHelloServerJsonMessage)
{
@@ -473,7 +200,7 @@ namespace WireMock.Net.Tests
{
// given
string path = $"/foo{Guid.NewGuid()}";
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
// when
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + path);
@@ -488,7 +215,7 @@ namespace WireMock.Net.Tests
{
// Assign
string path = $"/bar_{Guid.NewGuid()}";
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
// when
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
@@ -507,7 +234,7 @@ namespace WireMock.Net.Tests
public async Task FluentMockServer_Should_reset_requestlogs()
{
// given
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
// when
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
@@ -522,7 +249,7 @@ namespace WireMock.Net.Tests
{
// given
string path = $"/foo_{Guid.NewGuid()}";
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server
.Given(Request.Create()
@@ -547,7 +274,7 @@ namespace WireMock.Net.Tests
string path = $"/foo_{Guid.NewGuid()}";
string pathToRedirect = $"/bar_{Guid.NewGuid()}";
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server
.Given(Request.Create()
@@ -575,7 +302,7 @@ namespace WireMock.Net.Tests
public async Task FluentMockServer_Should_delay_responses_for_a_given_route()
{
// given
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server
.Given(Request.Create()
@@ -598,7 +325,7 @@ namespace WireMock.Net.Tests
public async Task FluentMockServer_Should_delay_responses()
{
// given
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server.AddGlobalProcessingDelay(TimeSpan.FromMilliseconds(200));
_server
.Given(Request.Create().WithPath("/*"))
@@ -619,7 +346,7 @@ namespace WireMock.Net.Tests
//public async Task Should_proxy_responses_with_client_certificate()
//{
// // given
// _server = FluentMockServer.Start();
// var _server = FluentMockServer.Start();
// _server
// .Given(Request.Create().WithPath("/*"))
// .RespondWith(Response.Create().WithProxy("https://server-that-expects-a-client-certificate", @"\\yourclientcertificatecontainingprivatekey.pfx", "yourclientcertificatepassword"));
@@ -631,34 +358,11 @@ namespace WireMock.Net.Tests
// Check.That(result).Contains("google");
//}
[Fact]
public async Task FluentMockServer_Logging_SetMaxRequestLogCount()
{
// Assign
var client = new HttpClient();
// Act
_server = FluentMockServer.Start();
_server.SetMaxRequestLogCount(2);
await client.GetAsync("http://localhost:" + _server.Ports[0] + "/foo1");
await client.GetAsync("http://localhost:" + _server.Ports[0] + "/foo2");
await client.GetAsync("http://localhost:" + _server.Ports[0] + "/foo3");
// Assert
Check.That(_server.LogEntries).HasSize(2);
var requestLoggedA = _server.LogEntries.First();
Check.That(requestLoggedA.RequestMessage.Path).EndsWith("/foo2");
var requestLoggedB = _server.LogEntries.Last();
Check.That(requestLoggedB.RequestMessage.Path).EndsWith("/foo3");
}
[Fact]
public async Task FluentMockServer_Should_respond_to_request_callback()
{
// Assign
_server = FluentMockServer.Start();
var _server = FluentMockServer.Start();
_server
.Given(Request.Create().WithPath("/foo").UsingGet())
@@ -671,27 +375,25 @@ namespace WireMock.Net.Tests
Check.That(response).IsEqualTo("/fooBar");
}
#if !NET452
[Fact]
public async Task FluentMockServer_Should_exclude_restrictedResponseHeader_for_IOwinResponse()
public async Task FluentMockServer_Should_not_exclude_restrictedResponseHeader_for_ASPNETCORE()
{
_server = FluentMockServer.Start();
// Assign
string path = $"/foo_{Guid.NewGuid()}";
var _server = FluentMockServer.Start();
_server
.Given(Request.Create().WithPath("/foo").UsingGet())
.RespondWith(Response.Create().WithHeader("Keep-Alive", "").WithHeader("test", ""));
.Given(Request.Create().WithPath(path).UsingGet())
.RespondWith(Response.Create().WithHeader("Keep-Alive", "k").WithHeader("test", "t"));
// Act
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + path);
// Assert
Check.That(response.Headers.Contains("test")).IsTrue();
Check.That(response.Headers.Contains("Keep-Alive")).IsFalse();
}
public void Dispose()
{
_server?.Stop();
_serverForProxyForwarding?.Stop();
Check.That(response.Headers.Contains("Keep-Alive")).IsTrue();
}
#endif
}
}
}