mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-28 20:32:13 +01:00
Support json path in the response (#170)
* jsonpath in response * Fix tests * Support also BodyAsJson * Fix Sonar Issue * Add example (zubinix2) * Fix batch file * Solve CodeFactor issues * netcoreapp2.0;netcoreapp2.1 * 1.0.4.8
This commit is contained in:
@@ -1,3 +1,10 @@
|
|||||||
|
# 1.0.4.8 (23 July 2018)
|
||||||
|
|
||||||
|
- [#167](https://github.com/WireMock-Net/WireMock.Net/issues/167) - Feature: Support for JsonPath in the response (with HandleBars) +feature
|
||||||
|
|
||||||
|
Commits: bd5e99b082...4e0b22db08
|
||||||
|
|
||||||
|
|
||||||
# 1.0.4.7 (19 July 2018)
|
# 1.0.4.7 (19 July 2018)
|
||||||
|
|
||||||
- [#169](https://github.com/WireMock-Net/WireMock.Net/pull/169) - Fix for Restricted Response headers contributed by Stef Heyenrath ([StefH](https://github.com/StefH)) +fix
|
- [#169](https://github.com/WireMock-Net/WireMock.Net/pull/169) - Fix for Restricted Response headers contributed by Stef Heyenrath ([StefH](https://github.com/StefH)) +fix
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
https://github.com/GitTools/GitReleaseNotes
|
https://github.com/GitTools/GitReleaseNotes
|
||||||
|
|
||||||
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.4.7
|
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.4.8
|
||||||
|
|
||||||
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /allTags
|
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /allTags
|
||||||
|
|||||||
32
README.md
32
README.md
@@ -4,27 +4,27 @@ A C# .NET version based on [mock4net](https://github.com/alexvictoor/mock4net) w
|
|||||||
### Info
|
### Info
|
||||||
| | |
|
| | |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| **Chat** | [](https://gitter.im/wiremock_dotnet/Lobby) |
|
| ***Project*** | |
|
||||||
| **Issues** | [](https://github.com/WireMock-Net/WireMock.Net/issues) |
|
| **Chat** | [](https://gitter.im/wiremock_dotnet/Lobby) |
|
||||||
| **Build** | [](https://ci.appveyor.com/project/StefH/wiremock-net) |
|
| **Issues** | [](https://github.com/WireMock-Net/WireMock.Net/issues) |
|
||||||
| | |
|
|
||||||
| ***Sonar*** | |
|
|
||||||
| **Sonar Quality Gate** | [](https://sonarcloud.io/project/issues?id=wiremock) |
|
|
||||||
| **Sonar Bugs** | [](https://sonarcloud.io/project/issues?id=wiremock&resolved=false&types=BUG) |
|
|
||||||
| **Sonar Code Smells** | [](https://sonarcloud.io/project/issues?id=wiremock&resolved=false&types=CODE_SMELL) |
|
|
||||||
| **Sonar Coverage** | [](https://sonarcloud.io/component_measures?id=wiremock&metric=coverage) |
|
|
||||||
| **Coveralls.io** | [](https://coveralls.io/github/WireMock-Net/WireMock.Net?branch=master) |
|
|
||||||
| | |
|
| | |
|
||||||
|
| ***Quality*** | |
|
||||||
|
| **Build** | [](https://ci.appveyor.com/project/StefH/wiremock-net) |
|
||||||
|
| **CodeFactor** | [](https://www.codefactor.io/repository/github/wiremock-net/wiremock.net)
|
||||||
|
| **Sonar Quality Gate** | [](https://sonarcloud.io/project/issues?id=wiremock) |
|
||||||
|
| **Sonar Bugs** | [](https://sonarcloud.io/project/issues?id=wiremock&resolved=false&types=BUG) |
|
||||||
|
| **Sonar Code Smells** | [](https://sonarcloud.io/project/issues?id=wiremock&resolved=false&types=CODE_SMELL) |
|
||||||
|
| **Sonar Coverage** | [](https://sonarcloud.io/component_measures?id=wiremock&metric=coverage) |
|
||||||
|
| **Coveralls** | [](https://coveralls.io/github/WireMock-Net/WireMock.Net?branch=master) |
|
||||||
|
| |
|
||||||
| ***Nuget*** | |
|
| ***Nuget*** | |
|
||||||
| **WireMock.Net** | [](https://www.nuget.org/packages/WireMock.Net) |
|
| **WireMock.Net** | [](https://www.nuget.org/packages/WireMock.Net) |
|
||||||
| **WireMock.Net.StandAlone** | [](https://www.nuget.org/packages/WireMock.Net.StandAlone) |
|
| **WireMock.Net.StandAlone** | [](https://www.nuget.org/packages/WireMock.Net.StandAlone) |
|
||||||
|
|
||||||
### Frameworks
|
### Frameworks
|
||||||
The following frameworks are supported:
|
The following frameworks are supported:
|
||||||
- net 4.5.2 and up
|
- net 4.5.2 and up & net 4.6 and up
|
||||||
- net 4.6 and up
|
- netstandard 1.3 & netstandard 2.0
|
||||||
- netstandard 1.3
|
|
||||||
- netstandard 2.0
|
|
||||||
|
|
||||||
## Build info
|
## Build info
|
||||||
To build you need:
|
To build you need:
|
||||||
|
|||||||
@@ -256,6 +256,46 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
.WithDelay(TimeSpan.FromMilliseconds(100))
|
.WithDelay(TimeSpan.FromMilliseconds(100))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/jsonpathtestToken").UsingPost())
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody("{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}")
|
||||||
|
.WithTransformer()
|
||||||
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/zubinix").UsingPost())
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody("{ \"result\": \"{{JsonPath.SelectToken request.bodyAsJson \"username\"}}\" }")
|
||||||
|
.WithTransformer()
|
||||||
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/zubinix2").UsingPost())
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBodyAsJson(new { path = "{{request.path}}", result = "{{JsonPath.SelectToken request.bodyAsJson \"username\"}}" })
|
||||||
|
.WithTransformer()
|
||||||
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/jsonpathtestTokenJson").UsingPost())
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBodyAsJson(new { status = "OK", url = "{{request.url}}", transformed = "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" } )
|
||||||
|
.WithTransformer()
|
||||||
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/jsonpathtestTokens").UsingPost())
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody("[{{#JsonPath.SelectTokens request.body \"$..Products[?(@.Price >= 50)].Name\"}} { \"idx\":{{id}}, \"value\":\"{{value}}\" }, {{/JsonPath.SelectTokens}} {} ]")
|
||||||
|
.WithTransformer()
|
||||||
|
);
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
.WithPath("/state1")
|
.WithPath("/state1")
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
@echo off
|
@echo off
|
||||||
call uninstall.bat
|
call Service-Uninstall.bat
|
||||||
|
|
||||||
SET mypath=%~dp0
|
SET mypath=%~dp0
|
||||||
SET targetpath=C:\Services\WireMock.Net.Service\
|
SET targetpath=C:\Services\WireMock.Net.Service\
|
||||||
|
|||||||
@@ -1,19 +1,25 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
<TargetFrameworks>netcoreapp2.0;netcoreapp2.1</TargetFrameworks>
|
||||||
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
|
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
|
||||||
<StartupObject>WireMock.Net.WebApplication.Program</StartupObject>
|
<StartupObject>WireMock.Net.WebApplication.Program</StartupObject>
|
||||||
<AssemblyName>WireMock.Net.WebApplication</AssemblyName>
|
<AssemblyName>WireMock.Net.WebApplication</AssemblyName>
|
||||||
<RootNamespace>WireMock.Net.WebApplication</RootNamespace>
|
<RootNamespace>WireMock.Net.WebApplication</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0'">
|
||||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
|
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
|
||||||
|
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.2" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\src\WireMock.Net.StandAlone\WireMock.Net.StandAlone.csproj" />
|
<ProjectReference Include="..\..\src\WireMock.Net.StandAlone\WireMock.Net.StandAlone.csproj" />
|
||||||
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Description>Lightweight StandAlone Http Mocking Server for .Net.</Description>
|
<Description>Lightweight StandAlone Http Mocking Server for .Net.</Description>
|
||||||
<AssemblyTitle>WireMock.Net.StandAlone</AssemblyTitle>
|
<AssemblyTitle>WireMock.Net.StandAlone</AssemblyTitle>
|
||||||
<Version>1.0.4.7</Version>
|
<Version>1.0.4.8-preview-01</Version>
|
||||||
<Authors>Stef Heyenrath</Authors>
|
<Authors>Stef Heyenrath</Authors>
|
||||||
<TargetFrameworks>net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
|
|||||||
@@ -73,10 +73,21 @@ namespace WireMock.Matchers
|
|||||||
JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);
|
JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);
|
||||||
|
|
||||||
// Check if JToken or string or object
|
// Check if JToken or string or object
|
||||||
JToken jtokenValue =
|
JToken jtokenValue;
|
||||||
Value is JToken tokenValue ? tokenValue :
|
switch (Value)
|
||||||
Value is string stringValue ? JToken.Parse(stringValue) :
|
{
|
||||||
JObject.FromObject(input);
|
case JToken tokenValue:
|
||||||
|
jtokenValue = tokenValue;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case string stringValue:
|
||||||
|
jtokenValue = JToken.Parse(stringValue);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
jtokenValue = JObject.FromObject(Value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
match = JToken.DeepEquals(jtokenValue, jtokenInput);
|
match = JToken.DeepEquals(jtokenValue, jtokenInput);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
|
namespace WireMock.Matchers
|
||||||
namespace WireMock.Matchers
|
|
||||||
{
|
{
|
||||||
internal static class MatchBehaviourHelper
|
internal static class MatchBehaviourHelper
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -76,6 +76,11 @@ namespace WireMock.Owin
|
|||||||
#endif
|
#endif
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
if (responseMessage == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
response.StatusCode = responseMessage.StatusCode;
|
response.StatusCode = responseMessage.StatusCode;
|
||||||
|
|
||||||
byte[] bytes = null;
|
byte[] bytes = null;
|
||||||
|
|||||||
@@ -701,7 +701,6 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
responseBuilder = responseBuilder.WithBodyFromBase64(responseModel.BodyFromBase64, ToEncoding(responseModel.BodyEncoding));
|
responseBuilder = responseBuilder.WithBodyFromBase64(responseModel.BodyFromBase64, ToEncoding(responseModel.BodyEncoding));
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (responseModel.BodyAsFile != null)
|
else if (responseModel.BodyAsFile != null)
|
||||||
{
|
{
|
||||||
responseBuilder = responseBuilder.WithBodyFromFile(responseModel.BodyAsFile);
|
responseBuilder = responseBuilder.WithBodyFromFile(responseModel.BodyAsFile);
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ using WireMock.Owin;
|
|||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseProviders;
|
using WireMock.ResponseProviders;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
using WireMock.Transformers;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.Server
|
namespace WireMock.Server
|
||||||
@@ -387,7 +388,6 @@ namespace WireMock.Server
|
|||||||
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
|
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
|
||||||
{
|
{
|
||||||
_options.MaxRequestLogCount = maxRequestLogCount;
|
_options.MaxRequestLogCount = maxRequestLogCount;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
89
src/WireMock.Net/Transformers/HandleBarsHelpers.cs
Normal file
89
src/WireMock.Net/Transformers/HandleBarsHelpers.cs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using HandlebarsDotNet;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using WireMock.Validation;
|
||||||
|
|
||||||
|
namespace WireMock.Transformers
|
||||||
|
{
|
||||||
|
internal static class HandlebarsHelpers
|
||||||
|
{
|
||||||
|
public static void Register()
|
||||||
|
{
|
||||||
|
Handlebars.RegisterHelper("JsonPath.SelectToken", (writer, context, arguments) =>
|
||||||
|
{
|
||||||
|
(JObject valueToProcess, string jsonpath) = Parse(arguments);
|
||||||
|
|
||||||
|
JToken result = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = valueToProcess.SelectToken(jsonpath);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
// Ignore JsonException and return
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result != null)
|
||||||
|
{
|
||||||
|
writer.WriteSafeString(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Handlebars.RegisterHelper("JsonPath.SelectTokens", (writer, options, context, arguments) =>
|
||||||
|
{
|
||||||
|
(JObject valueToProcess, string jsonpath) = Parse(arguments);
|
||||||
|
|
||||||
|
IEnumerable<JToken> values = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
values = valueToProcess.SelectTokens(jsonpath);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
// Ignore JsonException and return
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (values == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int id = 0;
|
||||||
|
foreach (JToken value in values)
|
||||||
|
{
|
||||||
|
options.Template(writer, new { id, value });
|
||||||
|
id++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static (JObject valueToProcess, string jsonpath) Parse(object[] arguments)
|
||||||
|
{
|
||||||
|
Check.Condition(arguments, args => args.Length == 2, nameof(arguments));
|
||||||
|
Check.NotNull(arguments[0], "arguments[0]");
|
||||||
|
Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]");
|
||||||
|
|
||||||
|
JObject valueToProcess;
|
||||||
|
|
||||||
|
switch (arguments[0])
|
||||||
|
{
|
||||||
|
case string jsonAsString:
|
||||||
|
valueToProcess = JObject.Parse(jsonAsString);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JObject jsonAsJObject:
|
||||||
|
valueToProcess = jsonAsJObject;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException($"The value '{arguments[0]}' with type {arguments[0].GetType()} cannot be used in Handlebars JsonPath.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return (valueToProcess, arguments[1] as string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,18 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using HandlebarsDotNet;
|
using HandlebarsDotNet;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.Transformers
|
namespace WireMock.Transformers
|
||||||
{
|
{
|
||||||
internal static class ResponseMessageTransformer
|
internal static class ResponseMessageTransformer
|
||||||
{
|
{
|
||||||
|
static ResponseMessageTransformer()
|
||||||
|
{
|
||||||
|
HandlebarsHelpers.Register();
|
||||||
|
}
|
||||||
|
|
||||||
public static ResponseMessage Transform(RequestMessage requestMessage, ResponseMessage original)
|
public static ResponseMessage Transform(RequestMessage requestMessage, ResponseMessage original)
|
||||||
{
|
{
|
||||||
bool bodyIsJson = original.BodyAsJson != null;
|
bool bodyIsJson = original.BodyAsJson != null;
|
||||||
@@ -20,21 +26,13 @@ namespace WireMock.Transformers
|
|||||||
|
|
||||||
var template = new { request = requestMessage };
|
var template = new { request = requestMessage };
|
||||||
|
|
||||||
// Body
|
if (!bodyIsJson)
|
||||||
Formatting formatting = original.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None;
|
|
||||||
string body = bodyIsJson ? JsonConvert.SerializeObject(original.BodyAsJson, formatting) : original.Body;
|
|
||||||
if (body != null)
|
|
||||||
{
|
{
|
||||||
var templateBody = Handlebars.Compile(body);
|
TransformBodyAsString(template, original, responseMessage);
|
||||||
|
}
|
||||||
if (!bodyIsJson)
|
else
|
||||||
{
|
{
|
||||||
responseMessage.Body = templateBody(template);
|
TransformBodyAsJson(template, original, responseMessage);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(templateBody(template));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Headers
|
// Headers
|
||||||
@@ -54,5 +52,79 @@ namespace WireMock.Transformers
|
|||||||
|
|
||||||
return responseMessage;
|
return responseMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void TransformBodyAsJson(object template, ResponseMessage original, ResponseMessage responseMessage)
|
||||||
|
{
|
||||||
|
JObject jobject;
|
||||||
|
switch (original.BodyAsJson)
|
||||||
|
{
|
||||||
|
case JObject bodyAsJObject:
|
||||||
|
jobject = bodyAsJObject;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
jobject = JObject.FromObject(original.BodyAsJson);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
WalkNode(jobject, template);
|
||||||
|
|
||||||
|
responseMessage.BodyAsJson = jobject;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void WalkNode(JToken node, object template)
|
||||||
|
{
|
||||||
|
if (node.Type == JTokenType.Object)
|
||||||
|
{
|
||||||
|
// In case of Object, loop all children.
|
||||||
|
foreach (JProperty child in node.Children<JProperty>())
|
||||||
|
{
|
||||||
|
WalkNode(child.Value, template);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (node.Type == JTokenType.Array)
|
||||||
|
{
|
||||||
|
// In case of Array, loop all items.
|
||||||
|
foreach (JToken child in node.Children())
|
||||||
|
{
|
||||||
|
WalkNode(child, template);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (node.Type == JTokenType.String)
|
||||||
|
{
|
||||||
|
// In case of string, try to transform the value.
|
||||||
|
string stringValue = node.Value<string>();
|
||||||
|
if (string.IsNullOrEmpty(stringValue))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var templateForStringValue = Handlebars.Compile(stringValue);
|
||||||
|
string transformedString = templateForStringValue(template);
|
||||||
|
if (!string.Equals(stringValue, transformedString))
|
||||||
|
{
|
||||||
|
JToken value;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Try to convert this string into a real JsonObject
|
||||||
|
value = JToken.Parse(transformedString);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
// Ignore JsonException and just convert to JToken
|
||||||
|
value = transformedString;
|
||||||
|
}
|
||||||
|
|
||||||
|
node.Replace(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void TransformBodyAsString(object template, ResponseMessage original, ResponseMessage responseMessage)
|
||||||
|
{
|
||||||
|
var templateBody = Handlebars.Compile(original.Body);
|
||||||
|
|
||||||
|
responseMessage.Body = templateBody(template);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Description>Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.</Description>
|
<Description>Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.</Description>
|
||||||
<AssemblyTitle>WireMock.Net</AssemblyTitle>
|
<AssemblyTitle>WireMock.Net</AssemblyTitle>
|
||||||
<Version>1.0.4.7</Version>
|
<Version>1.0.4.8-preview-01</Version>
|
||||||
<Authors>Stef Heyenrath</Authors>
|
<Authors>Stef Heyenrath</Authors>
|
||||||
<TargetFrameworks>net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
@@ -41,7 +41,7 @@
|
|||||||
<PackageReference Include="JetBrains.Annotations" Version="11.1.0">
|
<PackageReference Include="JetBrains.Annotations" Version="11.1.0">
|
||||||
<PrivateAssets>All</PrivateAssets>
|
<PrivateAssets>All</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Handlebars.Net" Version="1.9.0" />
|
<PackageReference Include="Handlebars.Net" Version="1.9.5" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||||
<PackageReference Include="SimMetrics.Net" Version="1.0.4" />
|
<PackageReference Include="SimMetrics.Net" Version="1.0.4" />
|
||||||
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
||||||
@@ -53,6 +53,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.6" />
|
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.6" />
|
||||||
<PackageReference Include="XPath2" Version="1.0.5.1" />
|
<PackageReference Include="XPath2" Version="1.0.5.1" />
|
||||||
<Reference Include="System.Net.Http.WebRequest" />
|
<Reference Include="System.Net.Http.WebRequest" />
|
||||||
|
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
|
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
|
||||||
@@ -62,6 +63,7 @@
|
|||||||
<PackageReference Include="Microsoft.Owin.Hosting" Version="4.0.0" />
|
<PackageReference Include="Microsoft.Owin.Hosting" Version="4.0.0" />
|
||||||
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
||||||
<PackageReference Include="XPath2" Version="1.0.5.1" />
|
<PackageReference Include="XPath2" Version="1.0.5.1" />
|
||||||
|
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
|
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
|
||||||
@@ -69,6 +71,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="1.1.3" />
|
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="1.1.3" />
|
||||||
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
|
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
|
||||||
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
|
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
|
||||||
|
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
|
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
|
||||||
|
|||||||
@@ -585,7 +585,6 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(response).IsEqualTo("/fooBar");
|
Check.That(response).IsEqualTo("/fooBar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task FluentMockServer_Should_exclude_restrictedResponseHeader_for_IOwinResponse()
|
public async Task FluentMockServer_Should_exclude_restrictedResponseHeader_for_IOwinResponse()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ namespace WireMock.Net.Tests.Matchers
|
|||||||
public void JsonMatcher_IsMatch_JObject1()
|
public void JsonMatcher_IsMatch_JObject1()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var matcher = new JsonMatcher(new { Id = 1, Name = "test" });
|
var matcher = new JsonMatcher(new { Id = 1, Name = "Test" });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var jobject = new JObject
|
var jobject = new JObject
|
||||||
@@ -83,7 +83,7 @@ namespace WireMock.Net.Tests.Matchers
|
|||||||
public void JsonMatcher_IsMatch_JObject2()
|
public void JsonMatcher_IsMatch_JObject2()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var matcher = new JsonMatcher(new { Id = 1, Name = "test" });
|
var matcher = new JsonMatcher(new { Id = 1, Name = "Test" });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Owin;
|
using Microsoft.Owin;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Models;
|
using WireMock.Models;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
@@ -19,7 +20,7 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_WithBodyAsJson()
|
public async Task Response_ProvideResponse_Handlebars_WithBodyAsJson()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
||||||
var bodyData = new BodyData
|
var bodyData = new BodyData
|
||||||
{
|
{
|
||||||
@@ -32,17 +33,17 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
.WithBodyAsJson(new { x = "test {{request.url}}" })
|
.WithBodyAsJson(new { x = "test {{request.url}}" })
|
||||||
.WithTransformer();
|
.WithTransformer();
|
||||||
|
|
||||||
// act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(JsonConvert.SerializeObject(responseMessage.BodyAsJson)).Equals("{\"x\":\"test http://localhost/foo\"}");
|
Check.That(JsonConvert.SerializeObject(responseMessage.BodyAsJson)).Equals("{\"x\":\"test http://localhost/foo\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
var body = new BodyData
|
var body = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsString = "whatever"
|
BodyAsString = "whatever"
|
||||||
@@ -53,17 +54,17 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
|
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
|
||||||
.WithTransformer();
|
.WithTransformer();
|
||||||
|
|
||||||
// act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(responseMessage.Body).Equals("test http://localhost/foo /foo post");
|
Check.That(responseMessage.Body).Equals("test http://localhost/foo /foo post");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_UrlPath()
|
public async Task Response_ProvideResponse_Handlebars_UrlPath()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
var urlDetails = UrlUtils.Parse(new Uri("http://localhost/wiremock/a/b"), new PathString("/wiremock"));
|
var urlDetails = UrlUtils.Parse(new Uri("http://localhost/wiremock/a/b"), new PathString("/wiremock"));
|
||||||
var request = new RequestMessage(urlDetails, "POST", ClientIp);
|
var request = new RequestMessage(urlDetails, "POST", ClientIp);
|
||||||
|
|
||||||
@@ -71,17 +72,17 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
.WithBody("{{request.url}} {{request.absoluteurl}} {{request.path}} {{request.absolutepath}}")
|
.WithBody("{{request.url}} {{request.absoluteurl}} {{request.path}} {{request.absolutepath}}")
|
||||||
.WithTransformer();
|
.WithTransformer();
|
||||||
|
|
||||||
// act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(responseMessage.Body).Equals("http://localhost/a/b http://localhost/wiremock/a/b /a/b /wiremock/a/b");
|
Check.That(responseMessage.Body).Equals("http://localhost/a/b http://localhost/wiremock/a/b /a/b /wiremock/a/b");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_PathSegments()
|
public async Task Response_ProvideResponse_Handlebars_PathSegments()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
var urlDetails = UrlUtils.Parse(new Uri("http://localhost/wiremock/a/b"), new PathString("/wiremock"));
|
var urlDetails = UrlUtils.Parse(new Uri("http://localhost/wiremock/a/b"), new PathString("/wiremock"));
|
||||||
var request = new RequestMessage(urlDetails, "POST", ClientIp);
|
var request = new RequestMessage(urlDetails, "POST", ClientIp);
|
||||||
|
|
||||||
@@ -89,17 +90,17 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
.WithBody("{{request.pathsegments.[0]}} {{request.absolutepathsegments.[0]}}")
|
.WithBody("{{request.pathsegments.[0]}} {{request.absolutepathsegments.[0]}}")
|
||||||
.WithTransformer();
|
.WithTransformer();
|
||||||
|
|
||||||
// act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(responseMessage.Body).Equals("a wiremock");
|
Check.That(responseMessage.Body).Equals("a wiremock");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_Query()
|
public async Task Response_ProvideResponse_Handlebars_Query()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
var body = new BodyData
|
var body = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsString = "abc"
|
BodyAsString = "abc"
|
||||||
@@ -110,17 +111,17 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
.WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}")
|
.WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}")
|
||||||
.WithTransformer();
|
.WithTransformer();
|
||||||
|
|
||||||
// act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(responseMessage.Body).Equals("test keya=1 idx=1 idx=2 keyb=5");
|
Check.That(responseMessage.Body).Equals("test keya=1 idx=1 idx=2 keyb=5");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_Header()
|
public async Task Response_ProvideResponse_Handlebars_Header()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
var body = new BodyData
|
var body = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsString = "abc"
|
BodyAsString = "abc"
|
||||||
@@ -129,10 +130,10 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
|
|
||||||
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer();
|
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer();
|
||||||
|
|
||||||
// act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(responseMessage.Body).Equals("test");
|
Check.That(responseMessage.Body).Equals("test");
|
||||||
Check.That(responseMessage.Headers).ContainsKey("x");
|
Check.That(responseMessage.Headers).ContainsKey("x");
|
||||||
Check.That(responseMessage.Headers["x"]).ContainsExactly("text/plain");
|
Check.That(responseMessage.Headers["x"]).ContainsExactly("text/plain");
|
||||||
@@ -141,7 +142,7 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_Headers()
|
public async Task Response_ProvideResponse_Handlebars_Headers()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
var body = new BodyData
|
var body = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsString = "abc"
|
BodyAsString = "abc"
|
||||||
@@ -150,10 +151,10 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
|
|
||||||
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}", "{{request.url}}").WithBody("test").WithTransformer();
|
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}", "{{request.url}}").WithBody("test").WithTransformer();
|
||||||
|
|
||||||
// act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(responseMessage.Body).Equals("test");
|
Check.That(responseMessage.Body).Equals("test");
|
||||||
Check.That(responseMessage.Headers).ContainsKey("x");
|
Check.That(responseMessage.Headers).ContainsKey("x");
|
||||||
Check.That(responseMessage.Headers["x"]).Contains("text/plain");
|
Check.That(responseMessage.Headers["x"]).Contains("text/plain");
|
||||||
@@ -163,7 +164,7 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_Origin_Port_Protocol_Host()
|
public async Task Response_ProvideResponse_Handlebars_Origin_Port_Protocol_Host()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
var body = new BodyData
|
var body = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsString = "abc"
|
BodyAsString = "abc"
|
||||||
@@ -174,11 +175,298 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
.WithBody("test {{request.origin}} {{request.port}} {{request.protocol}} {{request.host}}")
|
.WithBody("test {{request.origin}} {{request.port}} {{request.protocol}} {{request.host}}")
|
||||||
.WithTransformer();
|
.WithTransformer();
|
||||||
|
|
||||||
// act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
// then
|
// Assert
|
||||||
Check.That(responseMessage.Body).Equals("test http://localhost:1234 1234 http localhost");
|
Check.That(responseMessage.Body).Equals("test http://localhost:1234 1234 http localhost");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_Handlebars_JsonPath_SelectToken_ResponseBodyAsJson()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsString = @"{
|
||||||
|
""Stores"": [
|
||||||
|
""Lambton Quay"",
|
||||||
|
""Willis Street""
|
||||||
|
],
|
||||||
|
""Manufacturers"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Acme Co"",
|
||||||
|
""Products"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Anvil"",
|
||||||
|
""Price"": 50
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Name"": ""Contoso"",
|
||||||
|
""Products"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Elbow Grease"",
|
||||||
|
""Price"": 99.95
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Name"": ""Headlight Fluid"",
|
||||||
|
""Price"": 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}"
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBodyAsJson(new { x = "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" })
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
JObject j = JObject.FromObject(responseMessage.BodyAsJson);
|
||||||
|
Check.That(j["x"]).IsNotNull();
|
||||||
|
Check.That(j["x"]["Name"].ToString()).Equals("Acme Co");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_Handlebars_JsonPath_SelectToken_Request_BodyAsString()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsString = @"{
|
||||||
|
""Stores"": [
|
||||||
|
""Lambton Quay"",
|
||||||
|
""Willis Street""
|
||||||
|
],
|
||||||
|
""Manufacturers"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Acme Co"",
|
||||||
|
""Products"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Anvil"",
|
||||||
|
""Price"": 50
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Name"": ""Contoso"",
|
||||||
|
""Products"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Elbow Grease"",
|
||||||
|
""Price"": 99.95
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Name"": ""Headlight Fluid"",
|
||||||
|
""Price"": 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}"
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody("{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}")
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(responseMessage.Body).Equals("{\r\n \"Name\": \"Acme Co\",\r\n \"Products\": [\r\n {\r\n \"Name\": \"Anvil\",\r\n \"Price\": 50\r\n }\r\n ]\r\n}");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_Handlebars_JsonPath_SelectToken_Request_BodyAsJObject()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsJson = JObject.Parse(@"{
|
||||||
|
'Stores': [
|
||||||
|
'Lambton Quay',
|
||||||
|
'Willis Street'
|
||||||
|
],
|
||||||
|
'Manufacturers': [
|
||||||
|
{
|
||||||
|
'Name': 'Acme Co',
|
||||||
|
'Products': [
|
||||||
|
{
|
||||||
|
'Name': 'Anvil',
|
||||||
|
'Price': 50
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'Name': 'Contoso',
|
||||||
|
'Products': [
|
||||||
|
{
|
||||||
|
'Name': 'Elbow Grease',
|
||||||
|
'Price': 99.95
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'Name': 'Headlight Fluid',
|
||||||
|
'Price': 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}")
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody("{{JsonPath.SelectToken request.bodyAsJson \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}")
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(responseMessage.Body).Equals("{\r\n \"Name\": \"Acme Co\",\r\n \"Products\": [\r\n {\r\n \"Name\": \"Anvil\",\r\n \"Price\": 50\r\n }\r\n ]\r\n}");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_Handlebars_JsonPath_SelectTokens_Request_BodyAsString()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsString = @"{
|
||||||
|
""Stores"": [
|
||||||
|
""Lambton Quay"",
|
||||||
|
""Willis Street""
|
||||||
|
],
|
||||||
|
""Manufacturers"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Acme Co"",
|
||||||
|
""Products"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Anvil"",
|
||||||
|
""Price"": 50
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Name"": ""Contoso"",
|
||||||
|
""Products"": [
|
||||||
|
{
|
||||||
|
""Name"": ""Elbow Grease"",
|
||||||
|
""Price"": 99.95
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Name"": ""Headlight Fluid"",
|
||||||
|
""Price"": 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}"
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody("{{#JsonPath.SelectTokens request.body \"$..Products[?(@.Price >= 50)].Name\"}}{{id}} {{value}},{{/JsonPath.SelectTokens}}")
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(responseMessage.Body).Equals("0 Anvil,1 Elbow Grease,");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_Handlebars_JsonPath_SelectTokens_Request_BodyAsJObject()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsJson = JObject.Parse(@"{
|
||||||
|
'Stores': [
|
||||||
|
'Lambton Quay',
|
||||||
|
'Willis Street'
|
||||||
|
],
|
||||||
|
'Manufacturers': [
|
||||||
|
{
|
||||||
|
'Name': 'Acme Co',
|
||||||
|
'Products': [
|
||||||
|
{
|
||||||
|
'Name': 'Anvil',
|
||||||
|
'Price': 50
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'Name': 'Contoso',
|
||||||
|
'Products': [
|
||||||
|
{
|
||||||
|
'Name': 'Elbow Grease',
|
||||||
|
'Price': 99.95
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'Name': 'Headlight Fluid',
|
||||||
|
'Price': 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}")
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody("{{#JsonPath.SelectTokens request.bodyAsJson \"$..Products[?(@.Price >= 50)].Name\"}}{{id}} {{value}},{{/JsonPath.SelectTokens}}")
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(responseMessage.Body).Equals("0 Anvil,1 Elbow Grease,");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Response_ProvideResponse_Handlebars_JsonPath_SelectTokens_Throws()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsJson = JObject.Parse(@"{
|
||||||
|
'Stores': [
|
||||||
|
'Lambton Quay',
|
||||||
|
'Willis Street'
|
||||||
|
]
|
||||||
|
}")
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody("{{#JsonPath.SelectTokens request.body \"$..Products[?(@.Price >= 50)].Name\"}}{{id}} {{value}},{{/JsonPath.SelectTokens}}")
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Check.ThatAsyncCode(() => response.ProvideResponseAsync(request)).Throws<ArgumentNullException>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using NFluent;
|
||||||
using NFluent;
|
|
||||||
using WireMock.Models;
|
using WireMock.Models;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|||||||
Reference in New Issue
Block a user