Fix BodyAsFile to also allow relative paths (#244)

* Read only .json files as static mapping files and fix current folder for BodyAsFile

* include .json again

* LocalFileSystemHandler_ReadResponseBodyAsFile_Throws

* Read array from static mappings folder

* xml soap example
This commit is contained in:
Stef Heyenrath
2019-01-19 19:27:48 +01:00
committed by GitHub
parent 34abd05d19
commit d736745aff
24 changed files with 284 additions and 59 deletions

View File

@@ -46,7 +46,7 @@ namespace WireMock.Net.Tests
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings");
server.ReadStaticMappings(folder);
Check.That(server.Mappings).HasSize(3);
Check.That(server.Mappings).HasSize(5);
// Act
server.ResetMappings();
@@ -92,8 +92,8 @@ namespace WireMock.Net.Tests
var server = FluentMockServer.Start();
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", "documentdb_root.json");
server.ReadStaticMappingAndAddOrUpdate(folder);
string path = Path.Combine(GetCurrentFolder(), "__admin", "mappings", "documentdb_root.json");
server.ReadStaticMappingAndAddOrUpdate(path);
var mappings = server.Mappings.ToArray();
Check.That(mappings).HasSize(1);
@@ -110,8 +110,8 @@ namespace WireMock.Net.Tests
string guid = "00000002-ee28-4f29-ae63-1ac9b0802d86";
var server = FluentMockServer.Start();
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
server.ReadStaticMappingAndAddOrUpdate(folder);
string path = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
server.ReadStaticMappingAndAddOrUpdate(path);
var mappings = server.Mappings.ToArray();
Check.That(mappings).HasSize(1);
@@ -122,13 +122,25 @@ namespace WireMock.Net.Tests
Check.That(mappings.First().Title).IsNullOrEmpty();
}
[Fact]
public void FluentMockServer_Admin_ReadStaticMapping_WithArray()
{
var server = FluentMockServer.Start();
string path = Path.Combine(GetCurrentFolder(), "__admin", "mappings", "array.json");
server.ReadStaticMappingAndAddOrUpdate(path);
var mappings = server.Mappings.ToArray();
Check.That(mappings).HasSize(2);
}
[Fact]
public void FluentMockServer_Admin_ReadStaticMapping_WithResponseBodyFromFile()
{
string guid = "00000002-ee28-4f29-ae63-1ac9b0802d87";
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
string json = File.ReadAllText(folder);
string path = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
string json = File.ReadAllText(path);
string responseBodyFilePath = Path.Combine(GetCurrentFolder(), "responsebody.json");
@@ -136,10 +148,10 @@ namespace WireMock.Net.Tests
jsonObj["Response"]["BodyAsFile"] = responseBodyFilePath;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText(folder, output);
File.WriteAllText(path, output);
var server = FluentMockServer.Start();
server.ReadStaticMappingAndAddOrUpdate(folder);
server.ReadStaticMappingAndAddOrUpdate(path);
var mappings = server.Mappings.ToArray();
Check.That(mappings).HasSize(1);
@@ -202,7 +214,7 @@ namespace WireMock.Net.Tests
server.ReadStaticMappings(folder);
var mappings = server.Mappings.ToArray();
Check.That(mappings).HasSize(3);
Check.That(mappings).HasSize(5);
}
[Fact]

View File

@@ -33,5 +33,12 @@ namespace WireMock.Net.Tests.Handlers
// Act
Check.ThatCode(() => _sut.WriteMappingFile(null, null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_ReadResponseBodyAsFile_Throws()
{
// Act
Check.ThatCode(() => _sut.ReadResponseBodyAsFile(null)).Throws<ArgumentNullException>();
}
}
}

View File

@@ -4,6 +4,7 @@ using Xunit;
using Moq;
using System.Threading.Tasks;
using System.Threading;
using WireMock.Handlers;
using WireMock.Owin.Mappers;
using WireMock.Util;
#if NET452
@@ -26,6 +27,7 @@ namespace WireMock.Net.Tests.Owin.Mappers
private readonly Mock<IResponse> _responseMock;
private readonly Mock<Stream> _stream;
private readonly Mock<IHeaderDictionary> _headers;
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
public OwinResponseMapperTests()
{
@@ -46,20 +48,23 @@ namespace WireMock.Net.Tests.Owin.Mappers
_responseMock.SetupGet(r => r.Body).Returns(_stream.Object);
_responseMock.SetupGet(r => r.Headers).Returns(_headers.Object);
_sut = new OwinResponseMapper();
_fileSystemHandlerMock = new Mock<IFileSystemHandler>();
_fileSystemHandlerMock.SetupAllProperties();
_sut = new OwinResponseMapper(_fileSystemHandlerMock.Object);
}
[Fact]
public async void OwinResponseMapper_MapAsync_Null()
public async Task OwinResponseMapper_MapAsync_Null()
{
// Act
await _sut.MapAsync(null, _responseMock.Object);
}
[Fact]
public async void OwinResponseMapper_MapAsync_StatusCode()
public async Task OwinResponseMapper_MapAsync_StatusCode()
{
// Assign
// Arrange
var responseMessage = new ResponseMessage
{
StatusCode = 302
@@ -73,9 +78,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
}
[Fact]
public async void OwinResponseMapper_MapAsync_NoBody()
public async Task OwinResponseMapper_MapAsync_NoBody()
{
// Assign
// Arrange
var responseMessage = new ResponseMessage
{
Headers = new Dictionary<string, WireMockList<string>>()
@@ -89,9 +94,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
}
[Fact]
public async void OwinResponseMapper_MapAsync_Body()
public async Task OwinResponseMapper_MapAsync_Body()
{
// Assign
// Arrange
string body = "abc";
var responseMessage = new ResponseMessage
{
@@ -107,9 +112,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
}
[Fact]
public async void OwinResponseMapper_MapAsync_BodyAsBytes()
public async Task OwinResponseMapper_MapAsync_BodyAsBytes()
{
// Assign
// Arrange
var bytes = new byte[] { 48, 49 };
var responseMessage = new ResponseMessage
{
@@ -125,9 +130,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
}
[Fact]
public async void OwinResponseMapper_MapAsync_BodyAsJson()
public async Task OwinResponseMapper_MapAsync_BodyAsJson()
{
// Assign
// Arrange
var json = new { t = "x", i = (string)null };
var responseMessage = new ResponseMessage
{
@@ -143,9 +148,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
}
[Fact]
public async void OwinResponseMapper_MapAsync_SetResponseHeaders()
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
{
// Assign
// Arrange
var responseMessage = new ResponseMessage
{
Headers = new Dictionary<string, WireMockList<string>> { { "h", new WireMockList<string>("x", "y") } }

View File

@@ -62,9 +62,15 @@
<None Update="__admin\mappings\00000002-ee28-4f29-ae63-1ac9b0802d87.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="__admin\mappings\array.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="__admin\mappings\documentdb_root.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="__admin\mappings\MyXmlResponse.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>

View File

@@ -16,9 +16,9 @@
},
"Response": {
"StatusCode": 200,
"BodyAsFile": "responsebody.json",
"BodyAsFile": "MyXmlResponse.xml",
"Headers": {
"Content-Type": "application/json"
"Content-Type": "application/xml"
}
}
}

View File

@@ -0,0 +1,3 @@
<xml>
<hello>world</hello>
</xml>

View File

@@ -0,0 +1,46 @@
[
{
"Title": "1",
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/mappings_static_1"
}
]
},
"Methods": [
"get"
]
},
"Response": {
"BodyAsJson": { "result": "mappings static_1" },
"Headers": {
"Content-Type": "application/json"
}
}
},
{
"Title": "2",
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/mappings_static_2"
}
]
},
"Methods": [
"get"
]
},
"Response": {
"BodyAsJson": { "result": "mappings static_2" },
"Headers": {
"Content-Type": "application/json"
}
}
}
]