mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-19 15:31:39 +02:00
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:
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.0.6.1</VersionPrefix>
|
<VersionPrefix>1.0.7</VersionPrefix>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<Choose>
|
<Choose>
|
||||||
|
|||||||
@@ -18,6 +18,10 @@
|
|||||||
</Content>
|
</Content>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="__admin\mappings\array.json" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
||||||
<PackageReference Include="log4net" Version="2.0.8" />
|
<PackageReference Include="log4net" Version="2.0.8" />
|
||||||
@@ -37,6 +41,9 @@
|
|||||||
<None Update="__admin\mappings\791a3f31-6946-4ce7-8e6f-0237c7443275.json">
|
<None Update="__admin\mappings\791a3f31-6946-4ce7-8e6f-0237c7443275.json">
|
||||||
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="__admin\mappings\MyXmlResponse.xml">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
<xml>
|
||||||
|
<hello>world</hello>
|
||||||
|
</xml>
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
@@ -43,5 +43,11 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
{
|
{
|
||||||
File.WriteAllText(path, text);
|
File.WriteAllText(path, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IFileSystemHandler.ReadResponseBodyAsFile"/>
|
||||||
|
public byte[] ReadResponseBodyAsFile(string path)
|
||||||
|
{
|
||||||
|
return File.ReadAllBytes(Path.GetFileName(path) == path ? Path.Combine(GetMappingFolder(), path) : path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
@@ -39,7 +39,7 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
|
|
||||||
server.SetBasicAuthentication("a", "b");
|
server.SetBasicAuthentication("a", "b");
|
||||||
|
|
||||||
server.AllowPartialMapping();
|
// server.AllowPartialMapping();
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet())
|
.Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet())
|
||||||
@@ -427,6 +427,17 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
.WithTransformer()
|
.WithTransformer()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.UsingPost()
|
||||||
|
.WithPath("/xpathsoap")
|
||||||
|
.WithBody(new XPathMatcher("//*[local-name() = 'getMyData']"))
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/xml")
|
||||||
|
.WithBody("<xml>ok</xml>")
|
||||||
|
);
|
||||||
|
|
||||||
System.Console.WriteLine("Press any key to stop the server");
|
System.Console.WriteLine("Press any key to stop the server");
|
||||||
System.Console.ReadKey();
|
System.Console.ReadKey();
|
||||||
server.Stop();
|
server.Stop();
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace WireMock.Handlers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the folder where the static mappings are located. For local file system, this would be `{CurrentFolder}/__admin/mappings`.
|
/// Gets the folder where the static mappings are located. For local file system, this would be `{CurrentFolder}/__admin/mappings`.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The foldername.</returns>
|
/// <returns>The folder name.</returns>
|
||||||
string GetMappingFolder();
|
string GetMappingFolder();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -37,6 +37,7 @@ namespace WireMock.Handlers
|
|||||||
/// Read a static mapping file as text.
|
/// Read a static mapping file as text.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path">The path (folder + filename with .json extension).</param>
|
/// <param name="path">The path (folder + filename with .json extension).</param>
|
||||||
|
/// <returns>The file content as text.</returns>
|
||||||
string ReadMappingFile(string path);
|
string ReadMappingFile(string path);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -45,5 +46,12 @@ namespace WireMock.Handlers
|
|||||||
/// <param name="path">The path (folder + filename with .json extension).</param>
|
/// <param name="path">The path (folder + filename with .json extension).</param>
|
||||||
/// <param name="text">The text.</param>
|
/// <param name="text">The text.</param>
|
||||||
void WriteMappingFile(string path, string text);
|
void WriteMappingFile(string path, string text);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a response body file as text.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">The path or filename from the file to read.</param>
|
||||||
|
/// <returns>The file content as bytes.</returns>
|
||||||
|
byte[] ReadResponseBodyAsFile(string path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using JetBrains.Annotations;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using JetBrains.Annotations;
|
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.Handlers
|
namespace WireMock.Handlers
|
||||||
@@ -58,5 +58,15 @@ namespace WireMock.Handlers
|
|||||||
|
|
||||||
File.WriteAllText(path, text);
|
File.WriteAllText(path, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IFileSystemHandler.ReadResponseBodyAsFile"/>
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using JetBrains.Annotations;
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using WireMock.Handlers;
|
||||||
using WireMock.HttpsCertificate;
|
using WireMock.HttpsCertificate;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Owin.Mappers;
|
using WireMock.Owin.Mappers;
|
||||||
@@ -58,7 +59,8 @@ namespace WireMock.Owin
|
|||||||
_host = new WebHostBuilder()
|
_host = new WebHostBuilder()
|
||||||
.ConfigureServices(services =>
|
.ConfigureServices(services =>
|
||||||
{
|
{
|
||||||
services.AddSingleton<IWireMockMiddlewareOptions>(_options);
|
services.AddSingleton(_options.FileSystemHandler);
|
||||||
|
services.AddSingleton(_options);
|
||||||
services.AddSingleton<IMappingMatcher, MappingMatcher>();
|
services.AddSingleton<IMappingMatcher, MappingMatcher>();
|
||||||
services.AddSingleton<IOwinRequestMapper, OwinRequestMapper>();
|
services.AddSingleton<IOwinRequestMapper, OwinRequestMapper>();
|
||||||
services.AddSingleton<IOwinResponseMapper, OwinResponseMapper>();
|
services.AddSingleton<IOwinResponseMapper, OwinResponseMapper>();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using WireMock.Handlers;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
#if !USE_ASPNETCORE
|
#if !USE_ASPNETCORE
|
||||||
@@ -34,5 +35,7 @@ namespace WireMock.Owin
|
|||||||
Action<IAppBuilder> PreWireMockMiddlewareInit { get; set; }
|
Action<IAppBuilder> PreWireMockMiddlewareInit { get; set; }
|
||||||
|
|
||||||
Action<IAppBuilder> PostWireMockMiddlewareInit { get; set; }
|
Action<IAppBuilder> PostWireMockMiddlewareInit { get; set; }
|
||||||
|
|
||||||
|
IFileSystemHandler FileSystemHandler { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using WireMock.Handlers;
|
||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
|
using WireMock.Validation;
|
||||||
#if !USE_ASPNETCORE
|
#if !USE_ASPNETCORE
|
||||||
using IResponse = Microsoft.Owin.IOwinResponse;
|
using IResponse = Microsoft.Owin.IOwinResponse;
|
||||||
#else
|
#else
|
||||||
@@ -21,6 +22,7 @@ namespace WireMock.Owin.Mappers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class OwinResponseMapper : IOwinResponseMapper
|
public class OwinResponseMapper : IOwinResponseMapper
|
||||||
{
|
{
|
||||||
|
private readonly IFileSystemHandler _fileSystemHandler;
|
||||||
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
||||||
|
|
||||||
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
|
// 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() }
|
{ HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileSystemHandler">The IFileSystemHandler.</param>
|
||||||
|
public OwinResponseMapper(IFileSystemHandler fileSystemHandler)
|
||||||
|
{
|
||||||
|
Check.NotNull(fileSystemHandler, nameof(fileSystemHandler));
|
||||||
|
|
||||||
|
_fileSystemHandler = fileSystemHandler;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IOwinResponseMapper.MapAsync"/>
|
/// <inheritdoc cref="IOwinResponseMapper.MapAsync"/>
|
||||||
public async Task MapAsync(ResponseMessage responseMessage, IResponse response)
|
public async Task MapAsync(ResponseMessage responseMessage, IResponse response)
|
||||||
{
|
{
|
||||||
@@ -60,7 +73,7 @@ namespace WireMock.Owin.Mappers
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case BodyType.File:
|
case BodyType.File:
|
||||||
bytes = File.ReadAllBytes(responseMessage.BodyData.BodyAsFile);
|
bytes = _fileSystemHandler.ReadResponseBodyAsFile(responseMessage.BodyData.BodyAsFile);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using WireMock.Handlers;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Owin.Mappers;
|
using WireMock.Owin.Mappers;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
@@ -75,7 +76,7 @@ namespace WireMock.Owin
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var requestMapper = new OwinRequestMapper();
|
var requestMapper = new OwinRequestMapper();
|
||||||
var responseMapper = new OwinResponseMapper();
|
var responseMapper = new OwinResponseMapper(_options.FileSystemHandler);
|
||||||
var matcher = new MappingMatcher(_options);
|
var matcher = new MappingMatcher(_options);
|
||||||
|
|
||||||
Action<IAppBuilder> startup = app =>
|
Action<IAppBuilder> startup = app =>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using WireMock.Handlers;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
@@ -35,5 +36,8 @@ namespace WireMock.Owin
|
|||||||
public Action<IAppBuilder> PreWireMockMiddlewareInit { get; set; }
|
public Action<IAppBuilder> PreWireMockMiddlewareInit { get; set; }
|
||||||
|
|
||||||
public Action<IAppBuilder> PostWireMockMiddlewareInit { get; set; }
|
public Action<IAppBuilder> PostWireMockMiddlewareInit { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockMiddlewareOptions.FileSystemHandler"/>
|
||||||
|
public IFileSystemHandler FileSystemHandler { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
|
using JetBrains.Annotations;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using JetBrains.Annotations;
|
using WireMock.Handlers;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
using WireMock.Transformers;
|
using WireMock.Transformers;
|
||||||
@@ -21,6 +21,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Response : IResponseBuilder
|
public class Response : IResponseBuilder
|
||||||
{
|
{
|
||||||
|
private readonly IFileSystemHandler _fileSystemHandler = new LocalFileSystemHandler();
|
||||||
private HttpClient _httpClientForProxy;
|
private HttpClient _httpClientForProxy;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -226,7 +227,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
if (cache)
|
if (cache)
|
||||||
{
|
{
|
||||||
ResponseMessage.BodyData.DetectedBodyType = BodyType.Bytes;
|
ResponseMessage.BodyData.DetectedBodyType = BodyType.Bytes;
|
||||||
ResponseMessage.BodyData.BodyAsBytes = File.ReadAllBytes(filename);
|
ResponseMessage.BodyData.BodyAsBytes = _fileSystemHandler.ReadResponseBodyAsFile(filename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -255,7 +256,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
{
|
{
|
||||||
case BodyDestinationFormat.Bytes:
|
case BodyDestinationFormat.Bytes:
|
||||||
ResponseMessage.BodyData.DetectedBodyType = BodyType.Bytes;
|
ResponseMessage.BodyData.DetectedBodyType = BodyType.Bytes;
|
||||||
ResponseMessage.BodyData.BodyAsBytes= encoding.GetBytes(body);
|
ResponseMessage.BodyData.BodyAsBytes = encoding.GetBytes(body);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BodyDestinationFormat.Json:
|
case BodyDestinationFormat.Json:
|
||||||
@@ -285,7 +286,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
BodyAsJson = body,
|
BodyAsJson = body,
|
||||||
BodyAsJsonIndented = indented
|
BodyAsJsonIndented = indented
|
||||||
};
|
};
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -206,14 +206,17 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(path);
|
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(path);
|
||||||
|
|
||||||
MappingModel mappingModel = JsonConvert.DeserializeObject<MappingModel>(_fileSystemHandler.ReadMappingFile(path));
|
var mappingModels = DeserializeObjectToArray<MappingModel>(JsonConvert.DeserializeObject(_fileSystemHandler.ReadMappingFile(path)));
|
||||||
if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
|
foreach (var mappingModel in mappingModels)
|
||||||
{
|
{
|
||||||
DeserializeAndAddOrUpdateMapping(mappingModel, guidFromFilename, path);
|
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
|
||||||
}
|
{
|
||||||
else
|
DeserializeAndAddOrUpdateMapping(mappingModel, guidFromFilename, path);
|
||||||
{
|
}
|
||||||
DeserializeAndAddOrUpdateMapping(mappingModel, null, path);
|
else
|
||||||
|
{
|
||||||
|
DeserializeAndAddOrUpdateMapping(mappingModel, null, path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -414,7 +417,7 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var mappingModels = DeserializeObjectArray<MappingModel>(requestMessage);
|
var mappingModels = DeserializeRequestMessageToArray<MappingModel>(requestMessage);
|
||||||
if (mappingModels.Length == 1)
|
if (mappingModels.Length == 1)
|
||||||
{
|
{
|
||||||
Guid? guid = DeserializeAndAddOrUpdateMapping(mappingModels[0]);
|
Guid? guid = DeserializeAndAddOrUpdateMapping(mappingModels[0]);
|
||||||
@@ -802,22 +805,27 @@ namespace WireMock.Server
|
|||||||
return default(T);
|
return default(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
private T[] DeserializeObjectArray<T>(RequestMessage requestMessage)
|
private T[] DeserializeRequestMessageToArray<T>(RequestMessage requestMessage)
|
||||||
{
|
{
|
||||||
if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json)
|
if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json)
|
||||||
{
|
{
|
||||||
var bodyAsJson = requestMessage.BodyData.BodyAsJson;
|
var bodyAsJson = requestMessage.BodyData.BodyAsJson;
|
||||||
|
|
||||||
if (bodyAsJson is JArray jArray)
|
return DeserializeObjectToArray<T>(bodyAsJson);
|
||||||
{
|
|
||||||
return jArray.ToObject<T[]>();
|
|
||||||
}
|
|
||||||
|
|
||||||
var value = ((JObject)requestMessage.BodyData.BodyAsJson).ToObject<T>();
|
|
||||||
return new[] { value };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return default(T[]);
|
return default(T[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private T[] DeserializeObjectToArray<T>(object value)
|
||||||
|
{
|
||||||
|
if (value is JArray jArray)
|
||||||
|
{
|
||||||
|
return jArray.ToObject<T[]>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var singleResult = ((JObject)value).ToObject<T>();
|
||||||
|
return new[] { singleResult };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,6 +203,7 @@ namespace WireMock.Server
|
|||||||
Urls = new[] { $"{(settings.UseSSL == true ? "https" : "http")}://localhost:{port}" };
|
Urls = new[] { $"{(settings.UseSSL == true ? "https" : "http")}://localhost:{port}" };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_options.FileSystemHandler = settings.FileSystemHandler;
|
||||||
_options.PreWireMockMiddlewareInit = settings.PreWireMockMiddlewareInit;
|
_options.PreWireMockMiddlewareInit = settings.PreWireMockMiddlewareInit;
|
||||||
_options.PostWireMockMiddlewareInit = settings.PostWireMockMiddlewareInit;
|
_options.PostWireMockMiddlewareInit = settings.PostWireMockMiddlewareInit;
|
||||||
_options.Logger = _logger;
|
_options.Logger = _logger;
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace WireMock.Net.Tests
|
|||||||
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings");
|
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings");
|
||||||
server.ReadStaticMappings(folder);
|
server.ReadStaticMappings(folder);
|
||||||
|
|
||||||
Check.That(server.Mappings).HasSize(3);
|
Check.That(server.Mappings).HasSize(5);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
server.ResetMappings();
|
server.ResetMappings();
|
||||||
@@ -92,8 +92,8 @@ namespace WireMock.Net.Tests
|
|||||||
|
|
||||||
var server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
|
|
||||||
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", "documentdb_root.json");
|
string path = Path.Combine(GetCurrentFolder(), "__admin", "mappings", "documentdb_root.json");
|
||||||
server.ReadStaticMappingAndAddOrUpdate(folder);
|
server.ReadStaticMappingAndAddOrUpdate(path);
|
||||||
|
|
||||||
var mappings = server.Mappings.ToArray();
|
var mappings = server.Mappings.ToArray();
|
||||||
Check.That(mappings).HasSize(1);
|
Check.That(mappings).HasSize(1);
|
||||||
@@ -110,8 +110,8 @@ namespace WireMock.Net.Tests
|
|||||||
string guid = "00000002-ee28-4f29-ae63-1ac9b0802d86";
|
string guid = "00000002-ee28-4f29-ae63-1ac9b0802d86";
|
||||||
|
|
||||||
var server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
|
string path = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
|
||||||
server.ReadStaticMappingAndAddOrUpdate(folder);
|
server.ReadStaticMappingAndAddOrUpdate(path);
|
||||||
|
|
||||||
var mappings = server.Mappings.ToArray();
|
var mappings = server.Mappings.ToArray();
|
||||||
Check.That(mappings).HasSize(1);
|
Check.That(mappings).HasSize(1);
|
||||||
@@ -122,13 +122,25 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(mappings.First().Title).IsNullOrEmpty();
|
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]
|
[Fact]
|
||||||
public void FluentMockServer_Admin_ReadStaticMapping_WithResponseBodyFromFile()
|
public void FluentMockServer_Admin_ReadStaticMapping_WithResponseBodyFromFile()
|
||||||
{
|
{
|
||||||
string guid = "00000002-ee28-4f29-ae63-1ac9b0802d87";
|
string guid = "00000002-ee28-4f29-ae63-1ac9b0802d87";
|
||||||
|
|
||||||
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
|
string path = Path.Combine(GetCurrentFolder(), "__admin", "mappings", guid + ".json");
|
||||||
string json = File.ReadAllText(folder);
|
string json = File.ReadAllText(path);
|
||||||
|
|
||||||
string responseBodyFilePath = Path.Combine(GetCurrentFolder(), "responsebody.json");
|
string responseBodyFilePath = Path.Combine(GetCurrentFolder(), "responsebody.json");
|
||||||
|
|
||||||
@@ -136,10 +148,10 @@ namespace WireMock.Net.Tests
|
|||||||
jsonObj["Response"]["BodyAsFile"] = responseBodyFilePath;
|
jsonObj["Response"]["BodyAsFile"] = responseBodyFilePath;
|
||||||
|
|
||||||
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
|
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
|
||||||
File.WriteAllText(folder, output);
|
File.WriteAllText(path, output);
|
||||||
|
|
||||||
var server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
server.ReadStaticMappingAndAddOrUpdate(folder);
|
server.ReadStaticMappingAndAddOrUpdate(path);
|
||||||
|
|
||||||
var mappings = server.Mappings.ToArray();
|
var mappings = server.Mappings.ToArray();
|
||||||
Check.That(mappings).HasSize(1);
|
Check.That(mappings).HasSize(1);
|
||||||
@@ -202,7 +214,7 @@ namespace WireMock.Net.Tests
|
|||||||
server.ReadStaticMappings(folder);
|
server.ReadStaticMappings(folder);
|
||||||
|
|
||||||
var mappings = server.Mappings.ToArray();
|
var mappings = server.Mappings.ToArray();
|
||||||
Check.That(mappings).HasSize(3);
|
Check.That(mappings).HasSize(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -33,5 +33,12 @@ namespace WireMock.Net.Tests.Handlers
|
|||||||
// Act
|
// Act
|
||||||
Check.ThatCode(() => _sut.WriteMappingFile(null, null)).Throws<ArgumentNullException>();
|
Check.ThatCode(() => _sut.WriteMappingFile(null, null)).Throws<ArgumentNullException>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void LocalFileSystemHandler_ReadResponseBodyAsFile_Throws()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
Check.ThatCode(() => _sut.ReadResponseBodyAsFile(null)).Throws<ArgumentNullException>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ using Xunit;
|
|||||||
using Moq;
|
using Moq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using WireMock.Handlers;
|
||||||
using WireMock.Owin.Mappers;
|
using WireMock.Owin.Mappers;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
#if NET452
|
#if NET452
|
||||||
@@ -26,6 +27,7 @@ namespace WireMock.Net.Tests.Owin.Mappers
|
|||||||
private readonly Mock<IResponse> _responseMock;
|
private readonly Mock<IResponse> _responseMock;
|
||||||
private readonly Mock<Stream> _stream;
|
private readonly Mock<Stream> _stream;
|
||||||
private readonly Mock<IHeaderDictionary> _headers;
|
private readonly Mock<IHeaderDictionary> _headers;
|
||||||
|
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
|
||||||
|
|
||||||
public OwinResponseMapperTests()
|
public OwinResponseMapperTests()
|
||||||
{
|
{
|
||||||
@@ -46,20 +48,23 @@ namespace WireMock.Net.Tests.Owin.Mappers
|
|||||||
_responseMock.SetupGet(r => r.Body).Returns(_stream.Object);
|
_responseMock.SetupGet(r => r.Body).Returns(_stream.Object);
|
||||||
_responseMock.SetupGet(r => r.Headers).Returns(_headers.Object);
|
_responseMock.SetupGet(r => r.Headers).Returns(_headers.Object);
|
||||||
|
|
||||||
_sut = new OwinResponseMapper();
|
_fileSystemHandlerMock = new Mock<IFileSystemHandler>();
|
||||||
|
_fileSystemHandlerMock.SetupAllProperties();
|
||||||
|
|
||||||
|
_sut = new OwinResponseMapper(_fileSystemHandlerMock.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void OwinResponseMapper_MapAsync_Null()
|
public async Task OwinResponseMapper_MapAsync_Null()
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(null, _responseMock.Object);
|
await _sut.MapAsync(null, _responseMock.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void OwinResponseMapper_MapAsync_StatusCode()
|
public async Task OwinResponseMapper_MapAsync_StatusCode()
|
||||||
{
|
{
|
||||||
// Assign
|
// Arrange
|
||||||
var responseMessage = new ResponseMessage
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
StatusCode = 302
|
StatusCode = 302
|
||||||
@@ -73,9 +78,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void OwinResponseMapper_MapAsync_NoBody()
|
public async Task OwinResponseMapper_MapAsync_NoBody()
|
||||||
{
|
{
|
||||||
// Assign
|
// Arrange
|
||||||
var responseMessage = new ResponseMessage
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
Headers = new Dictionary<string, WireMockList<string>>()
|
Headers = new Dictionary<string, WireMockList<string>>()
|
||||||
@@ -89,9 +94,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void OwinResponseMapper_MapAsync_Body()
|
public async Task OwinResponseMapper_MapAsync_Body()
|
||||||
{
|
{
|
||||||
// Assign
|
// Arrange
|
||||||
string body = "abc";
|
string body = "abc";
|
||||||
var responseMessage = new ResponseMessage
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
@@ -107,9 +112,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void OwinResponseMapper_MapAsync_BodyAsBytes()
|
public async Task OwinResponseMapper_MapAsync_BodyAsBytes()
|
||||||
{
|
{
|
||||||
// Assign
|
// Arrange
|
||||||
var bytes = new byte[] { 48, 49 };
|
var bytes = new byte[] { 48, 49 };
|
||||||
var responseMessage = new ResponseMessage
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
@@ -125,9 +130,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void OwinResponseMapper_MapAsync_BodyAsJson()
|
public async Task OwinResponseMapper_MapAsync_BodyAsJson()
|
||||||
{
|
{
|
||||||
// Assign
|
// Arrange
|
||||||
var json = new { t = "x", i = (string)null };
|
var json = new { t = "x", i = (string)null };
|
||||||
var responseMessage = new ResponseMessage
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
@@ -143,9 +148,9 @@ namespace WireMock.Net.Tests.Owin.Mappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void OwinResponseMapper_MapAsync_SetResponseHeaders()
|
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
|
||||||
{
|
{
|
||||||
// Assign
|
// Arrange
|
||||||
var responseMessage = new ResponseMessage
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
Headers = new Dictionary<string, WireMockList<string>> { { "h", new WireMockList<string>("x", "y") } }
|
Headers = new Dictionary<string, WireMockList<string>> { { "h", new WireMockList<string>("x", "y") } }
|
||||||
|
|||||||
@@ -62,9 +62,15 @@
|
|||||||
<None Update="__admin\mappings\00000002-ee28-4f29-ae63-1ac9b0802d87.json">
|
<None Update="__admin\mappings\00000002-ee28-4f29-ae63-1ac9b0802d87.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="__admin\mappings\array.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Update="__admin\mappings\documentdb_root.json">
|
<None Update="__admin\mappings\documentdb_root.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="__admin\mappings\MyXmlResponse.xml">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -16,9 +16,9 @@
|
|||||||
},
|
},
|
||||||
"Response": {
|
"Response": {
|
||||||
"StatusCode": 200,
|
"StatusCode": 200,
|
||||||
"BodyAsFile": "responsebody.json",
|
"BodyAsFile": "MyXmlResponse.xml",
|
||||||
"Headers": {
|
"Headers": {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/xml"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
<xml>
|
||||||
|
<hello>world</hello>
|
||||||
|
</xml>
|
||||||
46
test/WireMock.Net.Tests/__admin/mappings/array.json
Normal file
46
test/WireMock.Net.Tests/__admin/mappings/array.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user