diff --git a/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj b/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj index 11c2304b..5448a240 100644 --- a/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj +++ b/examples/WireMock.Net.Console.NETCoreApp/WireMock.Net.Console.NETCoreApp.csproj @@ -18,9 +18,6 @@ PreserveNewest - - PreserveNewest - @@ -28,4 +25,13 @@ + + + PreserveNewest + + + Never + + + \ No newline at end of file diff --git a/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json new file mode 100644 index 00000000..f360d25d --- /dev/null +++ b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json @@ -0,0 +1,36 @@ +{ + "Guid": "791a3f31-6946-4ce7-8e6f-0237c7443275", + "Title": "", + "Priority": 0, + "Request": { + "Path": { + "Matchers": [ + { + "Name": "WildcardMatcher", + "Pattern": "/proxy-google-test-post" + } + ] + }, + "Methods": [ + "post" + ], + "Body": {} + }, + "Response": { + "StatusCode": 404, + "Body": "\n\n \n \n Error 404 (Not Found)!!1\n \n \n

404. That’s an error.\n

The requested URL /proxy-google-test-post was not found on this server. That’s all we know.\n", + "BodyAsBytes": "PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ZW4+CiAgPG1ldGEgY2hhcnNldD11dGYtOD4KICA8bWV0YSBuYW1lPXZpZXdwb3J0IGNvbnRlbnQ9ImluaXRpYWwtc2NhbGU9MSwgbWluaW11bS1zY2FsZT0xLCB3aWR0aD1kZXZpY2Utd2lkdGgiPgogIDx0aXRsZT5FcnJvciA0MDQgKE5vdCBGb3VuZCkhITE8L3RpdGxlPgogIDxzdHlsZT4KICAgICp7bWFyZ2luOjA7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5jb20vaW1hZ2VzL2JyYW5kaW5nL2dvb2dsZWxvZ28vMngvZ29vZ2xlbG9nb19jb2xvcl8xNTB4NTRkcC5wbmcpIDB9fUBtZWRpYSBvbmx5IHNjcmVlbiBhbmQgKC13ZWJraXQtbWluLWRldmljZS1waXhlbC1yYXRpbzoyKXsjbG9nb3tiYWNrZ3JvdW5kOnVybCgvL3d3dy5nb29nbGUuY29tL2ltYWdlcy9icmFuZGluZy9nb29nbGVsb2dvLzJ4L2dvb2dsZWxvZ29fY29sb3JfMTUweDU0ZHAucG5nKSBuby1yZXBlYXQ7LXdlYmtpdC1iYWNrZ3JvdW5kLXNpemU6MTAwJSAxMDAlfX0jbG9nb3tkaXNwbGF5OmlubGluZS1ibG9jaztoZWlnaHQ6NTRweDt3aWR0aDoxNTBweH0KICA8L3N0eWxlPgogIDxhIGhyZWY9Ly93d3cuZ29vZ2xlLmNvbS8+PHNwYW4gaWQ9bG9nbyBhcmlhLWxhYmVsPUdvb2dsZT48L3NwYW4+PC9hPgogIDxwPjxiPjQwNC48L2I+IDxpbnM+VGhhdOKAmXMgYW4gZXJyb3IuPC9pbnM+CiAgPHA+VGhlIHJlcXVlc3RlZCBVUkwgPGNvZGU+L3Byb3h5LWdvb2dsZS10ZXN0LXBvc3Q8L2NvZGU+IHdhcyBub3QgZm91bmQgb24gdGhpcyBzZXJ2ZXIuICA8aW5zPlRoYXTigJlzIGFsbCB3ZSBrbm93LjwvaW5zPgo=", + "BodyEncoding": { + "CodePage": 65001, + "EncodingName": "Unicode (UTF-8)", + "WebName": "utf-8" + }, + "UseTransformer": false, + "Headers": { + "Date": "Wed, 25 Oct 2017 18:57:40 GMT", + "Alt-Svc": "quic=\":443\"; ma=2592000; v=\"39,38,37,35\"", + "Referrer-Policy": "no-referrer", + "Connection": "close" + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/826aff7c-6208-4a3c-923d-575248907db4.json b/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/826aff7c-6208-4a3c-923d-575248907db4.json deleted file mode 100644 index ca2c5440..00000000 --- a/examples/WireMock.Net.Console.NETCoreApp/__admin/mappings/826aff7c-6208-4a3c-923d-575248907db4.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "Guid": "826aff7c-6208-4a3c-923d-575248907db4", - "Priority": 0, - "Request": { - "Path": { - "Matchers": [ - { - "Name": "WildcardMatcher", - "Pattern": "/earth/story/20170510-terrifying-20m-tall-rogue-waves-are-actually-real" - } - ] - }, - "Methods": [ - "get" - ] - }, - "Response": { - "StatusCode": 200, - "Body": " BBC - Homepage \n\n\n\n\n\n\n \n\n\n\n\n \n \n\n\n\n \n\n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n\n \r\n\r\n\r\n \n \n

\n

Accessibility links

\n\n \n Notifications\n \n \n \n \n
Search\n\n
\n
\n \n \n \n \n \n
\n
\n\n\n
\n\n\n\n\n

BBC Homepage

\n\n
\n \n
\n

 

\n

\n News\n

\n

\n Sport\n

\n
\r\n
\r\n

\n Editor’s Picks\n

\n

\n Featured video\n

\n
\n \n
\n
\n
\"'Attention!
\n\n \n \n
\n\n

\n \n 'Attention! You are next to 15 great white sharks' \n

\n\n

\n A sheriff's helicopter crew warns paddle-boarders they are swimming with sharks off the California coast.

\n \n US\n \n
\n\n \n 'Attention! You are next to 15 great white sharks' \n
\n\n \n
\r\n

\n More from around the BBC\n

\n

\n BBC in other languages\n

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n \n\n \n ", - "BodyEncoding": { - "CodePage": 65001, - "EncodingName": "Unicode (UTF-8)", - "WebName": "utf-8" - }, - "UseTransformer": false, - "Headers": { - "Set-Cookie": "BBC-UID=d5e9e11601d6a92baa56889831b1cedf3e0040fa733856b32b0ba4234e28e4740PostmanRuntime/3.0.11-hotfix.2; expires=Tue, 11-May-21 20:10:03 GMT; path=/; domain=.bbc.com", - "Cache-Control": "max-age=60, private", - "Vary": "X-CDN", - "X-Cache-Action": "HIT", - "Date": "Fri, 12 May 2017 20:10:03 GMT", - "ETag": "\"777c5af8426c21563a57166e437d820a\"", - "X-PAL-Host": "pal010.back.live.cwwtf.local:80", - "Connection": "keep-alive", - "X-Cache-Hits": "5", - "Server": "Apache", - "X-Cache-Age": "37", - "X-LB-NoCache": "true" - } - } -} \ No newline at end of file diff --git a/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs b/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs index 5d20a66f..700fa2c5 100644 --- a/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs +++ b/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs @@ -10,11 +10,12 @@ namespace WireMock.Net.Console.Record.NETCoreApp { var server = FluentMockServer.Start(new FluentMockServerSettings { - Urls = new[] { "http://localhost:9095/", "https://localhost:9096/" }, + Urls = new[] { "http://localhost:9091/", "https://localhost:9443/" }, StartAdminInterface = true, + ReadStaticMappings = true, ProxyAndRecordSettings = new ProxyAndRecordSettings { - Url = "https://www.msn.com", + Url = "https://www.google.com", //X509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)", SaveMapping = true } diff --git a/examples/WireMock.Net.Console.Record.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json b/examples/WireMock.Net.Console.Record.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json new file mode 100644 index 00000000..f360d25d --- /dev/null +++ b/examples/WireMock.Net.Console.Record.NETCoreApp/__admin/mappings/791a3f31-6946-4ce7-8e6f-0237c7443275.json @@ -0,0 +1,36 @@ +{ + "Guid": "791a3f31-6946-4ce7-8e6f-0237c7443275", + "Title": "", + "Priority": 0, + "Request": { + "Path": { + "Matchers": [ + { + "Name": "WildcardMatcher", + "Pattern": "/proxy-google-test-post" + } + ] + }, + "Methods": [ + "post" + ], + "Body": {} + }, + "Response": { + "StatusCode": 404, + "Body": "\n\n \n \n Error 404 (Not Found)!!1\n \n \n

404. That’s an error.\n

The requested URL /proxy-google-test-post was not found on this server. That’s all we know.\n", + "BodyAsBytes": "PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ZW4+CiAgPG1ldGEgY2hhcnNldD11dGYtOD4KICA8bWV0YSBuYW1lPXZpZXdwb3J0IGNvbnRlbnQ9ImluaXRpYWwtc2NhbGU9MSwgbWluaW11bS1zY2FsZT0xLCB3aWR0aD1kZXZpY2Utd2lkdGgiPgogIDx0aXRsZT5FcnJvciA0MDQgKE5vdCBGb3VuZCkhITE8L3RpdGxlPgogIDxzdHlsZT4KICAgICp7bWFyZ2luOjA7cGFkZGluZzowfWh0bWwsY29kZXtmb250OjE1cHgvMjJweCBhcmlhbCxzYW5zLXNlcmlmfWh0bWx7YmFja2dyb3VuZDojZmZmO2NvbG9yOiMyMjI7cGFkZGluZzoxNXB4fWJvZHl7bWFyZ2luOjclIGF1dG8gMDttYXgtd2lkdGg6MzkwcHg7bWluLWhlaWdodDoxODBweDtwYWRkaW5nOjMwcHggMCAxNXB4fSogPiBib2R5e2JhY2tncm91bmQ6dXJsKC8vd3d3Lmdvb2dsZS5jb20vaW1hZ2VzL2Vycm9ycy9yb2JvdC5wbmcpIDEwMCUgNXB4IG5vLXJlcGVhdDtwYWRkaW5nLXJpZ2h0OjIwNXB4fXB7bWFyZ2luOjExcHggMCAyMnB4O292ZXJmbG93OmhpZGRlbn1pbnN7Y29sb3I6Izc3Nzt0ZXh0LWRlY29yYXRpb246bm9uZX1hIGltZ3tib3JkZXI6MH1AbWVkaWEgc2NyZWVuIGFuZCAobWF4LXdpZHRoOjc3MnB4KXtib2R5e2JhY2tncm91bmQ6bm9uZTttYXJnaW4tdG9wOjA7bWF4LXdpZHRoOm5vbmU7cGFkZGluZy1yaWdodDowfX0jbG9nb3tiYWNrZ3JvdW5kOnVybCgvL3d3dy5nb29nbGUuY29tL2ltYWdlcy9icmFuZGlu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NvZGU+IHdhcyBub3QgZm91bmQgb24gdGhpcyBzZXJ2ZXIuICA8aW5zPlRoYXTigJlzIGFsbCB3ZSBrbm93LjwvaW5zPgo=", + "BodyEncoding": { + "CodePage": 65001, + "EncodingName": "Unicode (UTF-8)", + "WebName": "utf-8" + }, + "UseTransformer": false, + "Headers": { + "Date": "Wed, 25 Oct 2017 18:57:40 GMT", + "Alt-Svc": "quic=\":443\"; ma=2592000; v=\"39,38,37,35\"", + "Referrer-Policy": "no-referrer", + "Connection": "close" + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs index 72b6d5f0..fcfcef67 100644 --- a/examples/WireMock.Net.ConsoleApplication/MainApp.cs +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -20,7 +20,7 @@ namespace WireMock.Net.ConsoleApplication { Urls = new[] { url1, url2, url3 }, StartAdminInterface = true, - ReadStaticMappings = false, + ReadStaticMappings = true, PreWireMockMiddlewareInit = app => { System.Console.WriteLine($"PreWireMockMiddlewareInit : {app.GetType()}"); }, PostWireMockMiddlewareInit = app => { System.Console.WriteLine($"PostWireMockMiddlewareInit : {app.GetType()}"); } }); @@ -30,6 +30,18 @@ namespace WireMock.Net.ConsoleApplication // server.AllowPartialMapping(); + server + .Given(Request.Create().WithPath("/file").UsingGet()) + .RespondWith(Response.Create() + .WithBodyFromFile(@"c:\temp\x.json", false) + ); + + server + .Given(Request.Create().WithPath("/filecache").UsingGet()) + .RespondWith(Response.Create() + .WithBodyFromFile(@"c:\temp\x.json") + ); + server .Given(Request.Create().WithHeader("ProxyThis", "true") .UsingGet()) diff --git a/src/WireMock.Net/Admin/Mappings/ResponseModel.cs b/src/WireMock.Net/Admin/Mappings/ResponseModel.cs index cd12d37a..fbefb23d 100644 --- a/src/WireMock.Net/Admin/Mappings/ResponseModel.cs +++ b/src/WireMock.Net/Admin/Mappings/ResponseModel.cs @@ -10,9 +10,6 @@ namespace WireMock.Admin.Mappings ///

/// Gets or sets the HTTP status. /// - /// - /// The HTTP status. - /// public int? StatusCode { get; set; } /// @@ -23,79 +20,61 @@ namespace WireMock.Admin.Mappings /// /// Gets or sets the body. /// - /// - /// The body. - /// public string Body { get; set; } /// /// Gets or sets the body. /// - /// - /// The body. - /// public string BodyFromBase64 { get; set; } /// /// Gets or sets the body (as JSON object). /// - /// - /// The body. - /// public object BodyAsJson { get; set; } /// /// Gets or sets the body (as bytearray). /// - /// - /// The body. - /// public byte[] BodyAsBytes { get; set; } + /// + /// Gets or sets the body as a file. + /// + public string BodyAsFile { get; set; } + + /// + /// Is the body as file cached? + /// + public bool? BodyAsFileIsCached { get; set; } + /// /// Gets or sets the body encoding. /// - /// - /// The body encoding. - /// public EncodingModel BodyEncoding { get; set; } /// /// Gets or sets a value indicating whether [use transformer]. /// - /// - /// true if [use transformer]; otherwise, false. - /// public bool UseTransformer { get; set; } /// /// Gets or sets the headers. /// - /// - /// The headers. - /// public IDictionary Headers { get; set; } /// /// Gets or sets the Headers (Raw). /// - /// - /// The Headers (Raw). - /// public string HeadersRaw { get; set; } /// /// Gets or sets the delay in milliseconds. /// - /// - /// The delay in milliseconds. - /// public int? Delay { get; set; } /// /// Gets or sets the Proxy URL. /// - /// ProxyUrl public string ProxyUrl { get; set; } /// diff --git a/src/WireMock.Net/Admin/Requests/LogRequestModel.cs b/src/WireMock.Net/Admin/Requests/LogRequestModel.cs index 77a97b78..4e7a44f3 100644 --- a/src/WireMock.Net/Admin/Requests/LogRequestModel.cs +++ b/src/WireMock.Net/Admin/Requests/LogRequestModel.cs @@ -23,17 +23,11 @@ namespace WireMock.Admin.Requests /// /// Gets or sets the Path. /// - /// - /// The Path. - /// public string Path { get; set; } /// /// Gets or sets the absolete URL. /// - /// - /// The absolute URL. - /// public string AbsoluteUrl { get; set; } /// @@ -44,41 +38,26 @@ namespace WireMock.Admin.Requests /// /// Gets or sets the method. /// - /// - /// The method. - /// public string Method { get; set; } /// /// Gets or sets the Headers. /// - /// - /// The Headers. - /// public IDictionary Headers { get; set; } /// /// Gets or sets the Cookies. /// - /// - /// The Cookies. - /// public IDictionary Cookies { get; set; } /// /// Gets or sets the body. /// - /// - /// The body. - /// public string Body { get; set; } /// /// Gets or sets the body encoding. /// - /// - /// The body encoding. - /// public EncodingModel BodyEncoding { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Admin/Requests/LogResponseModel.cs b/src/WireMock.Net/Admin/Requests/LogResponseModel.cs index 4d39fc1d..144248ea 100644 --- a/src/WireMock.Net/Admin/Requests/LogResponseModel.cs +++ b/src/WireMock.Net/Admin/Requests/LogResponseModel.cs @@ -29,10 +29,20 @@ namespace WireMock.Admin.Requests public string Body { get; set; } /// - /// Gets or sets the body. + /// Gets or sets the body as bytes. /// public byte[] BodyAsBytes { get; set; } + /// + /// Gets or sets the body as file. + /// + public string BodyAsFile { get; set; } + + /// + /// Is the body as file cached? + /// + public bool? BodyAsFileIsCached { get; set; } + /// /// Gets or sets the original body. /// diff --git a/src/WireMock.Net/Http/HttpClientHelper.cs b/src/WireMock.Net/Http/HttpClientHelper.cs index d4366c3b..b9a15977 100644 --- a/src/WireMock.Net/Http/HttpClientHelper.cs +++ b/src/WireMock.Net/Http/HttpClientHelper.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -27,7 +26,7 @@ namespace WireMock.Http { ClientCertificateOptions = ClientCertificateOption.Manual, ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true, - AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate + AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate }; var x509Certificate2 = CertificateUtil.GetCertificate(clientX509Certificate2ThumbprintOrSubjectName); diff --git a/src/WireMock.Net/Owin/OwinResponseMapper.cs b/src/WireMock.Net/Owin/OwinResponseMapper.cs index 9abbfaed..b4b4f19b 100644 --- a/src/WireMock.Net/Owin/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/OwinResponseMapper.cs @@ -39,7 +39,7 @@ namespace WireMock.Owin } responseMessage.Headers.Where(h => h.Key != HttpKnownHeaderNames.ContentType).ToList().ForEach(pair => response.Headers.Append(pair.Key, pair.Value)); - if (responseMessage.Body == null && responseMessage.BodyAsBytes == null) + if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == null) { return; } @@ -49,6 +49,14 @@ namespace WireMock.Owin await response.Body.WriteAsync(responseMessage.BodyAsBytes, 0, responseMessage.BodyAsBytes.Length); return; } + + if (responseMessage.BodyAsFile != null) + { + byte[] bytes = File.ReadAllBytes(responseMessage.BodyAsFile); + + await response.Body.WriteAsync(bytes, 0, bytes.Length); + return; + } Encoding encoding = responseMessage.BodyEncoding ?? _utf8NoBom; using (var writer = new StreamWriter(response.Body, encoding)) diff --git a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs index edd84b86..1f27fa23 100644 --- a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs +++ b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs @@ -9,14 +9,6 @@ namespace WireMock.ResponseBuilders /// public interface IBodyResponseBuilder : ITransformResponseBuilder { - ///// - ///// WithBody : Create a string response based on a string. - ///// - ///// The body. - ///// The body encoding. - ///// A . - //// IResponseBuilder WithBody([NotNull] string body, [CanBeNull] Encoding encoding = null); - /// /// WithBody : Create a ... response based on a string. /// @@ -51,5 +43,13 @@ namespace WireMock.ResponseBuilders /// A . [Obsolete] IResponseBuilder WithBodyFromBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null); + + /// + /// WithBodyFromFile : Create a ... response based on a File. + /// + /// The filename. + /// Defines if this file is cached in memory or retrieved from disk everytime the response is created. + /// A . + IResponseBuilder WithBodyFromFile([NotNull] string filename, bool cache = true); } } \ No newline at end of file diff --git a/src/WireMock.Net/ResponseBuilders/Response.cs b/src/WireMock.Net/ResponseBuilders/Response.cs index 41604c78..bc9643d3 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; @@ -139,11 +140,7 @@ namespace WireMock.ResponseBuilders return this; } - /// - /// The with headers. - /// - /// The headers. - /// + /// public IResponseBuilder WithHeaders(IDictionary headers) { ResponseMessage.Headers = headers; @@ -175,6 +172,30 @@ namespace WireMock.ResponseBuilders return this; } + /// + public IResponseBuilder WithBodyFromFile(string filename, bool cache = true) + { + Check.NotNull(filename, nameof(filename)); + + ResponseMessage.BodyEncoding = null; + ResponseMessage.BodyAsFileIsCached = cache; + + if (cache) + { + ResponseMessage.Body = null; + ResponseMessage.BodyAsBytes = File.ReadAllBytes(filename); + ResponseMessage.BodyAsFile = null; + } + else + { + ResponseMessage.Body = null; + ResponseMessage.BodyAsBytes = null; + ResponseMessage.BodyAsFile = filename; + } + + return this; + } + /// public IResponseBuilder WithBody(string body, string destination = BodyDestinationFormat.SameAsSource, Encoding encoding = null) { diff --git a/src/WireMock.Net/ResponseMessage.cs b/src/WireMock.Net/ResponseMessage.cs index 9c2bdc15..02776513 100644 --- a/src/WireMock.Net/ResponseMessage.cs +++ b/src/WireMock.Net/ResponseMessage.cs @@ -30,15 +30,25 @@ namespace WireMock public string BodyDestination { get; set; } /// - /// Gets or sets the body. + /// Gets or sets the body as a string. /// public string Body { get; set; } /// - /// Gets or sets the body. + /// Gets or sets the body as bytes. /// public byte[] BodyAsBytes { get; set; } + /// + /// Gets or sets the body as a file. + /// + public string BodyAsFile { get; set; } + + /// + /// Is the body as file cached? + /// + public bool? BodyAsFileIsCached { get; set; } + /// /// Gets or sets the body encoding. /// diff --git a/src/WireMock.Net/Serialization/MappingConverter.cs b/src/WireMock.Net/Serialization/MappingConverter.cs index 47661662..cae3399e 100644 --- a/src/WireMock.Net/Serialization/MappingConverter.cs +++ b/src/WireMock.Net/Serialization/MappingConverter.cs @@ -98,6 +98,8 @@ namespace WireMock.Serialization mappingModel.Response.BodyDestination = null; mappingModel.Response.Body = null; mappingModel.Response.BodyAsBytes = null; + mappingModel.Response.BodyAsFile = null; + mappingModel.Response.BodyAsFileIsCached = null; mappingModel.Response.UseTransformer = false; mappingModel.Response.BodyEncoding = null; mappingModel.Response.ProxyUrl = response.ProxyUrl; @@ -109,6 +111,8 @@ namespace WireMock.Serialization mappingModel.Response.Headers = response.ResponseMessage.Headers; mappingModel.Response.Body = response.ResponseMessage.Body; mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes; + mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile; + mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyAsFileIsCached; mappingModel.Response.UseTransformer = response.UseTransformer; mappingModel.Response.BodyEncoding = response.ResponseMessage.BodyEncoding != null ? new EncodingModel diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 482b16e3..1a1b4526 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -416,6 +416,8 @@ namespace WireMock.Server Body = logEntry.ResponseMessage.Body, BodyAsBytes = logEntry.ResponseMessage.BodyAsBytes, BodyOriginal = logEntry.ResponseMessage.BodyOriginal, + BodyAsFile = logEntry.ResponseMessage.BodyAsFile, + BodyAsFileIsCached = logEntry.ResponseMessage.BodyAsFileIsCached, Headers = logEntry.ResponseMessage.Headers, BodyEncoding = logEntry.ResponseMessage.BodyEncoding != null ? new EncodingModel {