From d736745aff86a9de828b3e13f0d1a11aa508944a Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sat, 19 Jan 2019 19:27:48 +0100 Subject: [PATCH] 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 --- Directory.Build.props | 2 +- .../WireMock.Net.Console.NETCoreApp2.csproj | 7 +++ .../__admin/mappings/BodyAsFileExample.json | 22 +++++++++ .../__admin/mappings/MyXmlResponse.xml | 3 ++ .../__admin/mappings/array.json | 46 +++++++++++++++++++ .../CustomFileSystemFileHandler.cs | 6 +++ .../MainApp.cs | 17 +++++-- .../Handlers/IFileSystemHandler.cs | 10 +++- .../Handlers/LocalFileSystemHandler.cs | 14 +++++- src/WireMock.Net/Owin/AspNetCoreSelfHost.cs | 4 +- .../Owin/IWireMockMiddlewareOptions.cs | 3 ++ .../Owin/Mappers/OwinResponseMapper.cs | 17 ++++++- src/WireMock.Net/Owin/OwinSelfHost.cs | 3 +- .../Owin/WireMockMiddlewareOptions.cs | 4 ++ src/WireMock.Net/ResponseBuilders/Response.cs | 13 +++--- .../Server/FluentMockServer.Admin.cs | 40 +++++++++------- src/WireMock.Net/Server/FluentMockServer.cs | 1 + .../FluentMockServerTests.Admin.cs | 32 +++++++++---- .../Handlers/LocalFileSystemHandlerTests.cs | 7 +++ .../Owin/Mappers/OwinResponseMapperTests.cs | 33 +++++++------ .../WireMock.Net.Tests.csproj | 6 +++ .../00000002-ee28-4f29-ae63-1ac9b0802d87.json | 4 +- .../__admin/mappings/MyXmlResponse.xml | 3 ++ .../__admin/mappings/array.json | 46 +++++++++++++++++++ 24 files changed, 284 insertions(+), 59 deletions(-) create mode 100644 examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/BodyAsFileExample.json create mode 100644 examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/MyXmlResponse.xml create mode 100644 examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/array.json create mode 100644 test/WireMock.Net.Tests/__admin/mappings/MyXmlResponse.xml create mode 100644 test/WireMock.Net.Tests/__admin/mappings/array.json diff --git a/Directory.Build.props b/Directory.Build.props index 059e75a5..5438effc 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ - 1.0.6.1 + 1.0.7 diff --git a/examples/WireMock.Net.Console.NETCoreApp2/WireMock.Net.Console.NETCoreApp2.csproj b/examples/WireMock.Net.Console.NETCoreApp2/WireMock.Net.Console.NETCoreApp2.csproj index d6e2a54f..e0dc27cb 100644 --- a/examples/WireMock.Net.Console.NETCoreApp2/WireMock.Net.Console.NETCoreApp2.csproj +++ b/examples/WireMock.Net.Console.NETCoreApp2/WireMock.Net.Console.NETCoreApp2.csproj @@ -18,6 +18,10 @@ + + + + @@ -37,6 +41,9 @@ Never + + PreserveNewest + \ No newline at end of file diff --git a/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/BodyAsFileExample.json b/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/BodyAsFileExample.json new file mode 100644 index 00000000..633db99e --- /dev/null +++ b/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/BodyAsFileExample.json @@ -0,0 +1,22 @@ +{ + "Request": { + "Path": { + "Matchers": [ + { + "Name": "WildcardMatcher", + "Pattern": "/bodyasfilexmltest", + "IgnoreCase": false + } + ] + }, + "Methods": [ + "get" + ] + }, + "Response": { + "StatusCode": 200, + "Headers": {"Content-Type": "application/xml"}, + "BodyAsFile": "MyXmlResponse.xml", + "UseTransformer": false + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/MyXmlResponse.xml b/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/MyXmlResponse.xml new file mode 100644 index 00000000..24fd28d3 --- /dev/null +++ b/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/MyXmlResponse.xml @@ -0,0 +1,3 @@ + +world + \ No newline at end of file diff --git a/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/array.json b/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/array.json new file mode 100644 index 00000000..79f338ae --- /dev/null +++ b/examples/WireMock.Net.Console.NETCoreApp2/__admin/mappings/array.json @@ -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" + } + } + } +] \ No newline at end of file diff --git a/examples/WireMock.Net.Console.Net452.Classic/CustomFileSystemFileHandler.cs b/examples/WireMock.Net.Console.Net452.Classic/CustomFileSystemFileHandler.cs index c60ebece..0c558479 100644 --- a/examples/WireMock.Net.Console.Net452.Classic/CustomFileSystemFileHandler.cs +++ b/examples/WireMock.Net.Console.Net452.Classic/CustomFileSystemFileHandler.cs @@ -43,5 +43,11 @@ namespace WireMock.Net.ConsoleApplication { File.WriteAllText(path, text); } + + /// + public byte[] ReadResponseBodyAsFile(string path) + { + return File.ReadAllBytes(Path.GetFileName(path) == path ? Path.Combine(GetMappingFolder(), path) : path); + } } } \ No newline at end of file diff --git a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs index 49d30dc5..4c3ef47b 100644 --- a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs +++ b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs @@ -1,7 +1,7 @@ -using System; +using Newtonsoft.Json; +using System; using System.Globalization; using System.Net; -using Newtonsoft.Json; using WireMock.Logging; using WireMock.Matchers; using WireMock.RequestBuilders; @@ -39,7 +39,7 @@ namespace WireMock.Net.ConsoleApplication server.SetBasicAuthentication("a", "b"); - server.AllowPartialMapping(); + // server.AllowPartialMapping(); server .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) @@ -427,6 +427,17 @@ namespace WireMock.Net.ConsoleApplication .WithTransformer() ); + server + .Given(Request.Create() + .UsingPost() + .WithPath("/xpathsoap") + .WithBody(new XPathMatcher("//*[local-name() = 'getMyData']")) + ) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/xml") + .WithBody("ok") + ); + System.Console.WriteLine("Press any key to stop the server"); System.Console.ReadKey(); server.Stop(); diff --git a/src/WireMock.Net/Handlers/IFileSystemHandler.cs b/src/WireMock.Net/Handlers/IFileSystemHandler.cs index ad6105c6..15268735 100644 --- a/src/WireMock.Net/Handlers/IFileSystemHandler.cs +++ b/src/WireMock.Net/Handlers/IFileSystemHandler.cs @@ -10,7 +10,7 @@ namespace WireMock.Handlers /// /// Gets the folder where the static mappings are located. For local file system, this would be `{CurrentFolder}/__admin/mappings`. /// - /// The foldername. + /// The folder name. string GetMappingFolder(); /// @@ -37,6 +37,7 @@ namespace WireMock.Handlers /// Read a static mapping file as text. /// /// The path (folder + filename with .json extension). + /// The file content as text. string ReadMappingFile(string path); /// @@ -45,5 +46,12 @@ namespace WireMock.Handlers /// The path (folder + filename with .json extension). /// The text. void WriteMappingFile(string path, string text); + + /// + /// Read a response body file as text. + /// + /// The path or filename from the file to read. + /// The file content as bytes. + byte[] ReadResponseBodyAsFile(string path); } } \ No newline at end of file diff --git a/src/WireMock.Net/Handlers/LocalFileSystemHandler.cs b/src/WireMock.Net/Handlers/LocalFileSystemHandler.cs index fca619ba..bfc7007a 100644 --- a/src/WireMock.Net/Handlers/LocalFileSystemHandler.cs +++ b/src/WireMock.Net/Handlers/LocalFileSystemHandler.cs @@ -1,6 +1,6 @@ -using System.Collections.Generic; +using JetBrains.Annotations; +using System.Collections.Generic; using System.IO; -using JetBrains.Annotations; using WireMock.Validation; namespace WireMock.Handlers @@ -58,5 +58,15 @@ namespace WireMock.Handlers File.WriteAllText(path, text); } + + /// + public byte[] ReadResponseBodyAsFile(string path) + { + Check.NotNullOrEmpty(path, nameof(path)); + + // In case the path is a filename, the path will be adjusted to the MappingFolder. + // Else the path will just be as-is. + return File.ReadAllBytes(Path.GetFileName(path) == path ? Path.Combine(GetMappingFolder(), path) : path); + } } } diff --git a/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs b/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs index 27fa1a22..c50ca546 100644 --- a/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs +++ b/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs @@ -8,6 +8,7 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; +using WireMock.Handlers; using WireMock.HttpsCertificate; using WireMock.Logging; using WireMock.Owin.Mappers; @@ -58,7 +59,8 @@ namespace WireMock.Owin _host = new WebHostBuilder() .ConfigureServices(services => { - services.AddSingleton(_options); + services.AddSingleton(_options.FileSystemHandler); + services.AddSingleton(_options); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs index e26e0ace..31e10003 100644 --- a/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.ObjectModel; +using WireMock.Handlers; using WireMock.Logging; using WireMock.Matchers; #if !USE_ASPNETCORE @@ -34,5 +35,7 @@ namespace WireMock.Owin Action PreWireMockMiddlewareInit { get; set; } Action PostWireMockMiddlewareInit { get; set; } + + IFileSystemHandler FileSystemHandler { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs index bed90f27..9d9b3c93 100644 --- a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; +using WireMock.Handlers; using WireMock.Http; using WireMock.Util; +using WireMock.Validation; #if !USE_ASPNETCORE using IResponse = Microsoft.Owin.IOwinResponse; #else @@ -21,6 +22,7 @@ namespace WireMock.Owin.Mappers /// public class OwinResponseMapper : IOwinResponseMapper { + private readonly IFileSystemHandler _fileSystemHandler; private readonly Encoding _utf8NoBom = new UTF8Encoding(false); // https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx @@ -32,6 +34,17 @@ namespace WireMock.Owin.Mappers { HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() } }; + /// + /// Constructor + /// + /// The IFileSystemHandler. + public OwinResponseMapper(IFileSystemHandler fileSystemHandler) + { + Check.NotNull(fileSystemHandler, nameof(fileSystemHandler)); + + _fileSystemHandler = fileSystemHandler; + } + /// public async Task MapAsync(ResponseMessage responseMessage, IResponse response) { @@ -60,7 +73,7 @@ namespace WireMock.Owin.Mappers break; case BodyType.File: - bytes = File.ReadAllBytes(responseMessage.BodyData.BodyAsFile); + bytes = _fileSystemHandler.ReadResponseBodyAsFile(responseMessage.BodyData.BodyAsFile); break; } diff --git a/src/WireMock.Net/Owin/OwinSelfHost.cs b/src/WireMock.Net/Owin/OwinSelfHost.cs index eb4e0d14..67e80f45 100644 --- a/src/WireMock.Net/Owin/OwinSelfHost.cs +++ b/src/WireMock.Net/Owin/OwinSelfHost.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using WireMock.Handlers; using WireMock.Logging; using WireMock.Owin.Mappers; using WireMock.Util; @@ -75,7 +76,7 @@ namespace WireMock.Owin try { var requestMapper = new OwinRequestMapper(); - var responseMapper = new OwinResponseMapper(); + var responseMapper = new OwinResponseMapper(_options.FileSystemHandler); var matcher = new MappingMatcher(_options); Action startup = app => diff --git a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs index cc8741cf..81552f54 100644 --- a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.ObjectModel; +using WireMock.Handlers; using WireMock.Logging; using WireMock.Matchers; using WireMock.Util; @@ -35,5 +36,8 @@ namespace WireMock.Owin public Action PreWireMockMiddlewareInit { get; set; } public Action PostWireMockMiddlewareInit { get; set; } + + /// + public IFileSystemHandler FileSystemHandler { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/ResponseBuilders/Response.cs b/src/WireMock.Net/ResponseBuilders/Response.cs index fd8da1db..3df50690 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.cs @@ -1,13 +1,13 @@ +using JetBrains.Annotations; +using Newtonsoft.Json; using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; -using JetBrains.Annotations; -using Newtonsoft.Json; +using WireMock.Handlers; using WireMock.Http; using WireMock.Settings; using WireMock.Transformers; @@ -21,6 +21,7 @@ namespace WireMock.ResponseBuilders /// public class Response : IResponseBuilder { + private readonly IFileSystemHandler _fileSystemHandler = new LocalFileSystemHandler(); private HttpClient _httpClientForProxy; /// @@ -226,7 +227,7 @@ namespace WireMock.ResponseBuilders if (cache) { ResponseMessage.BodyData.DetectedBodyType = BodyType.Bytes; - ResponseMessage.BodyData.BodyAsBytes = File.ReadAllBytes(filename); + ResponseMessage.BodyData.BodyAsBytes = _fileSystemHandler.ReadResponseBodyAsFile(filename); } else { @@ -255,7 +256,7 @@ namespace WireMock.ResponseBuilders { case BodyDestinationFormat.Bytes: ResponseMessage.BodyData.DetectedBodyType = BodyType.Bytes; - ResponseMessage.BodyData.BodyAsBytes= encoding.GetBytes(body); + ResponseMessage.BodyData.BodyAsBytes = encoding.GetBytes(body); break; case BodyDestinationFormat.Json: @@ -285,7 +286,7 @@ namespace WireMock.ResponseBuilders BodyAsJson = body, BodyAsJsonIndented = indented }; - + return this; } diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 8a0ced75..e7a8011d 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -206,14 +206,17 @@ namespace WireMock.Server string filenameWithoutExtension = Path.GetFileNameWithoutExtension(path); - MappingModel mappingModel = JsonConvert.DeserializeObject(_fileSystemHandler.ReadMappingFile(path)); - if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename)) + var mappingModels = DeserializeObjectToArray(JsonConvert.DeserializeObject(_fileSystemHandler.ReadMappingFile(path))); + foreach (var mappingModel in mappingModels) { - DeserializeAndAddOrUpdateMapping(mappingModel, guidFromFilename, path); - } - else - { - DeserializeAndAddOrUpdateMapping(mappingModel, null, path); + if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename)) + { + DeserializeAndAddOrUpdateMapping(mappingModel, guidFromFilename, path); + } + else + { + DeserializeAndAddOrUpdateMapping(mappingModel, null, path); + } } } #endregion @@ -414,7 +417,7 @@ namespace WireMock.Server { try { - var mappingModels = DeserializeObjectArray(requestMessage); + var mappingModels = DeserializeRequestMessageToArray(requestMessage); if (mappingModels.Length == 1) { Guid? guid = DeserializeAndAddOrUpdateMapping(mappingModels[0]); @@ -802,22 +805,27 @@ namespace WireMock.Server return default(T); } - private T[] DeserializeObjectArray(RequestMessage requestMessage) + private T[] DeserializeRequestMessageToArray(RequestMessage requestMessage) { if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json) { var bodyAsJson = requestMessage.BodyData.BodyAsJson; - if (bodyAsJson is JArray jArray) - { - return jArray.ToObject(); - } - - var value = ((JObject)requestMessage.BodyData.BodyAsJson).ToObject(); - return new[] { value }; + return DeserializeObjectToArray(bodyAsJson); } return default(T[]); } + + private T[] DeserializeObjectToArray(object value) + { + if (value is JArray jArray) + { + return jArray.ToObject(); + } + + var singleResult = ((JObject)value).ToObject(); + return new[] { singleResult }; + } } } \ No newline at end of file diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index f9ec203a..81bc5b2e 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -203,6 +203,7 @@ namespace WireMock.Server Urls = new[] { $"{(settings.UseSSL == true ? "https" : "http")}://localhost:{port}" }; } + _options.FileSystemHandler = settings.FileSystemHandler; _options.PreWireMockMiddlewareInit = settings.PreWireMockMiddlewareInit; _options.PostWireMockMiddlewareInit = settings.PostWireMockMiddlewareInit; _options.Logger = _logger; diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.Admin.cs b/test/WireMock.Net.Tests/FluentMockServerTests.Admin.cs index 97e164a4..17baa808 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.Admin.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.Admin.cs @@ -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] diff --git a/test/WireMock.Net.Tests/Handlers/LocalFileSystemHandlerTests.cs b/test/WireMock.Net.Tests/Handlers/LocalFileSystemHandlerTests.cs index 4bb6897c..24c42956 100644 --- a/test/WireMock.Net.Tests/Handlers/LocalFileSystemHandlerTests.cs +++ b/test/WireMock.Net.Tests/Handlers/LocalFileSystemHandlerTests.cs @@ -33,5 +33,12 @@ namespace WireMock.Net.Tests.Handlers // Act Check.ThatCode(() => _sut.WriteMappingFile(null, null)).Throws(); } + + [Fact] + public void LocalFileSystemHandler_ReadResponseBodyAsFile_Throws() + { + // Act + Check.ThatCode(() => _sut.ReadResponseBodyAsFile(null)).Throws(); + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs b/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs index 10c1b004..696999f6 100644 --- a/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs +++ b/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs @@ -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 _responseMock; private readonly Mock _stream; private readonly Mock _headers; + private readonly Mock _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(); + _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>() @@ -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> { { "h", new WireMockList("x", "y") } } diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index 039835b8..33c73504 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -62,9 +62,15 @@ PreserveNewest + + PreserveNewest + PreserveNewest + + PreserveNewest + diff --git a/test/WireMock.Net.Tests/__admin/mappings/00000002-ee28-4f29-ae63-1ac9b0802d87.json b/test/WireMock.Net.Tests/__admin/mappings/00000002-ee28-4f29-ae63-1ac9b0802d87.json index 97999c1e..20c4b949 100644 --- a/test/WireMock.Net.Tests/__admin/mappings/00000002-ee28-4f29-ae63-1ac9b0802d87.json +++ b/test/WireMock.Net.Tests/__admin/mappings/00000002-ee28-4f29-ae63-1ac9b0802d87.json @@ -16,9 +16,9 @@ }, "Response": { "StatusCode": 200, - "BodyAsFile": "responsebody.json", + "BodyAsFile": "MyXmlResponse.xml", "Headers": { - "Content-Type": "application/json" + "Content-Type": "application/xml" } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/__admin/mappings/MyXmlResponse.xml b/test/WireMock.Net.Tests/__admin/mappings/MyXmlResponse.xml new file mode 100644 index 00000000..24fd28d3 --- /dev/null +++ b/test/WireMock.Net.Tests/__admin/mappings/MyXmlResponse.xml @@ -0,0 +1,3 @@ + +world + \ No newline at end of file diff --git a/test/WireMock.Net.Tests/__admin/mappings/array.json b/test/WireMock.Net.Tests/__admin/mappings/array.json new file mode 100644 index 00000000..79f338ae --- /dev/null +++ b/test/WireMock.Net.Tests/__admin/mappings/array.json @@ -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" + } + } + } +] \ No newline at end of file