mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-14 07:33:33 +01:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1269fb178f | ||
|
|
7426bf76ee | ||
|
|
36c9d95abb | ||
|
|
674fa89c3e | ||
|
|
61cdc13fae | ||
|
|
c344b73f45 | ||
|
|
2ac9ca207a | ||
|
|
f099f3a288 | ||
|
|
02b607cc95 | ||
|
|
7ac89e85b7 |
16
CHANGELOG.md
16
CHANGELOG.md
@@ -1,3 +1,15 @@
|
||||
# 1.5.18 (09 March 2023)
|
||||
- [#893](https://github.com/WireMock-Net/WireMock.Net/pull/893) - Add 'Data' to response which can be used during transforming the response [feature] contributed by [StefH](https://github.com/StefH)
|
||||
- [#896](https://github.com/WireMock-Net/WireMock.Net/pull/896) - Bump Microsoft.Owin from 2.0.2 to 4.2.2 in /examples/WireMock.Net.Service [dependencies] contributed by [dependabot[bot]](https://github.com/apps/dependabot)
|
||||
- [#900](https://github.com/WireMock-Net/WireMock.Net/pull/900) - ProxySettings : Add logic to not save some requests depending on HttpMethods [feature] contributed by [StefH](https://github.com/StefH)
|
||||
- [#897](https://github.com/WireMock-Net/WireMock.Net/issues/897) - WebHostBuilder.ConfigureServices method not found when using nunit3testadapter 4.4.0 [bug]
|
||||
- [#899](https://github.com/WireMock-Net/WireMock.Net/issues/899) - Ignore OPTIONS request when using proxyandrecord [feature]
|
||||
|
||||
# 1.5.17 (25 February 2023)
|
||||
- [#881](https://github.com/WireMock-Net/WireMock.Net/pull/881) - Add WithBodyAsJson builder method with accepts a Func [feature] contributed by [StefH](https://github.com/StefH)
|
||||
- [#882](https://github.com/WireMock-Net/WireMock.Net/pull/882) - Add example code to test HTTP Status 400 and 500 [test] contributed by [StefH](https://github.com/StefH)
|
||||
- [#890](https://github.com/WireMock-Net/WireMock.Net/pull/890) - AdminApiMappingBuilder [feature] contributed by [StefH](https://github.com/StefH)
|
||||
|
||||
# 1.5.16 (01 February 2023)
|
||||
- [#880](https://github.com/WireMock-Net/WireMock.Net/pull/880) - Add `WithProxy(string proxyUrl, X509Certificate2 certificate)` [feature] contributed by [StefH](https://github.com/StefH)
|
||||
- [#879](https://github.com/WireMock-Net/WireMock.Net/issues/879) - Possibility to pass a X509Certificate2 to WithProxy() or specifiy certificate loading options [feature]
|
||||
@@ -651,8 +663,8 @@
|
||||
- [#263](https://github.com/WireMock-Net/WireMock.Net/issues/263) - Content-Type multipart/form-data is not serialized in proxy and recording mode [bug]
|
||||
|
||||
# 1.0.11.0 (30 March 2019)
|
||||
- [#261](https://github.com/WireMock-Net/WireMock.Net/pull/261) - Fix BodyAsJson transform bug in ResponseMessageTransformer contributed by [psypilat](https://github.com/psypilat)
|
||||
- [#262](https://github.com/WireMock-Net/WireMock.Net/pull/262) - Add ProvideResponse_WithJsonBodyAndTransform test contributed by [psypilat](https://github.com/psypilat)
|
||||
- [#261](https://github.com/WireMock-Net/WireMock.Net/pull/261) - Fix BodyAsJson transform bug in ResponseMessageTransformer contributed by [ghost](https://github.com/ghost)
|
||||
- [#262](https://github.com/WireMock-Net/WireMock.Net/pull/262) - Add ProvideResponse_WithJsonBodyAndTransform test contributed by [ghost](https://github.com/ghost)
|
||||
|
||||
# 1.0.10.0 (27 March 2019)
|
||||
- [#260](https://github.com/WireMock-Net/WireMock.Net/pull/260) - Fix Response.Delay property serialization [bug] contributed by [StefH](https://github.com/StefH)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>1.5.16</VersionPrefix>
|
||||
<VersionPrefix>1.5.18</VersionPrefix>
|
||||
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
||||
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
rem https://github.com/StefH/GitHubReleaseNotes
|
||||
|
||||
SET version=1.5.16
|
||||
SET version=1.5.18
|
||||
|
||||
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc duplicate --version %version% --token %GH_TOKEN%
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
# 1.5.16 (01 February 2023)
|
||||
- #880 Add `WithProxy(string proxyUrl, X509Certificate2 certificate)` [feature]
|
||||
- #879 Possibility to pass a X509Certificate2 to WithProxy() or specifiy certificate loading options [feature]
|
||||
# 1.5.18 (09 March 2023)
|
||||
- #893 Add 'Data' to response which can be used during transforming the response [feature]
|
||||
- #896 Bump Microsoft.Owin from 2.0.2 to 4.2.2 in /examples/WireMock.Net.Service [dependencies]
|
||||
- #900 ProxySettings : Add logic to not save some requests depending on HttpMethods [feature]
|
||||
- #897 WebHostBuilder.ConfigureServices method not found when using nunit3testadapter 4.4.0 [bug]
|
||||
- #899 Ignore OPTIONS request when using proxyandrecord [feature]
|
||||
|
||||
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md
|
||||
@@ -1,78 +1,125 @@
|
||||
using Newtonsoft.Json;
|
||||
using RestEase;
|
||||
using System;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using RestEase;
|
||||
using WireMock.Admin.Settings;
|
||||
using WireMock.Client;
|
||||
using WireMock.Client.Extensions;
|
||||
|
||||
namespace WireMock.Net.Client
|
||||
namespace WireMock.Net.Client;
|
||||
|
||||
class Program
|
||||
{
|
||||
class Program
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
// Create an implementation of the IWireMockAdminApi and pass in the base URL for the API.
|
||||
var api = RestClient.For<IWireMockAdminApi>("http://localhost:9091");
|
||||
|
||||
// await api.ResetMappingsAsync().ConfigureAwait(false);
|
||||
|
||||
var mappingBuilder = api.GetMappingBuilder();
|
||||
mappingBuilder.Given(m => m
|
||||
.WithTitle("This is my title 1")
|
||||
.WithRequest(req => req
|
||||
.UsingGet()
|
||||
.WithPath("/bla1")
|
||||
)
|
||||
.WithResponse(rsp => rsp
|
||||
.WithBody("x1")
|
||||
.WithHeaders(h => h.Add("h1", "v1"))
|
||||
)
|
||||
);
|
||||
|
||||
mappingBuilder.Given(m => m
|
||||
.WithTitle("This is my title 2")
|
||||
.WithRequest(req => req
|
||||
.UsingGet()
|
||||
.WithPath("/bla2")
|
||||
)
|
||||
.WithResponse(rsp => rsp
|
||||
.WithBody("x2")
|
||||
.WithHeaders(h => h.Add("h2", "v2"))
|
||||
)
|
||||
);
|
||||
|
||||
mappingBuilder.Given(m => m
|
||||
.WithTitle("This is my title 3")
|
||||
.WithRequest(req => req
|
||||
.UsingGet()
|
||||
.WithPath("/bla3")
|
||||
)
|
||||
.WithResponse(rsp => rsp
|
||||
.WithBodyAsJson(new
|
||||
{
|
||||
x = "test"
|
||||
}, true)
|
||||
)
|
||||
);
|
||||
|
||||
var result = await mappingBuilder.BuildAndPostAsync().ConfigureAwait(false);
|
||||
Console.WriteLine($"result = {JsonConvert.SerializeObject(result)}");
|
||||
|
||||
var mappings = await api.GetMappingsAsync();
|
||||
Console.WriteLine($"mappings = {JsonConvert.SerializeObject(mappings)}");
|
||||
|
||||
// Set BASIC Auth
|
||||
var value = Convert.ToBase64String(Encoding.ASCII.GetBytes("a:b"));
|
||||
api.Authorization = new AuthenticationHeaderValue("Basic", value);
|
||||
|
||||
var settings1 = await api.GetSettingsAsync();
|
||||
Console.WriteLine($"settings1 = {JsonConvert.SerializeObject(settings1)}");
|
||||
|
||||
var settingsViaBuilder = new SettingsModelBuilder()
|
||||
.WithGlobalProcessingDelay(1077)
|
||||
.WithoutGlobalProcessingDelay()
|
||||
.Build();
|
||||
|
||||
settings1.GlobalProcessingDelay = 1077;
|
||||
api.PostSettingsAsync(settings1).Wait();
|
||||
|
||||
var settings2 = await api.GetSettingsAsync();
|
||||
Console.WriteLine($"settings2 = {JsonConvert.SerializeObject(settings2)}");
|
||||
|
||||
mappings = await api.GetMappingsAsync();
|
||||
Console.WriteLine($"mappings = {JsonConvert.SerializeObject(mappings)}");
|
||||
|
||||
try
|
||||
{
|
||||
// Create an implementation of the IWireMockAdminApi and pass in the base URL for the API.
|
||||
var api = RestClient.For<IWireMockAdminApi>("http://localhost:9091");
|
||||
|
||||
// Set BASIC Auth
|
||||
var value = Convert.ToBase64String(Encoding.ASCII.GetBytes("a:b"));
|
||||
api.Authorization = new AuthenticationHeaderValue("Basic", value);
|
||||
|
||||
var settings1 = await api.GetSettingsAsync();
|
||||
Console.WriteLine($"settings1 = {JsonConvert.SerializeObject(settings1)}");
|
||||
|
||||
var settingsViaBuilder = new SettingsModelBuilder()
|
||||
.WithGlobalProcessingDelay(1077)
|
||||
.WithoutGlobalProcessingDelay()
|
||||
.Build();
|
||||
|
||||
settings1.GlobalProcessingDelay = 1077;
|
||||
api.PostSettingsAsync(settings1).Wait();
|
||||
|
||||
var settings2 = await api.GetSettingsAsync();
|
||||
Console.WriteLine($"settings2 = {JsonConvert.SerializeObject(settings2)}");
|
||||
|
||||
var mappings = await api.GetMappingsAsync();
|
||||
Console.WriteLine($"mappings = {JsonConvert.SerializeObject(mappings)}");
|
||||
|
||||
try
|
||||
{
|
||||
var guid = Guid.Parse("11111110-a633-40e8-a244-5cb80bc0ab66");
|
||||
var mapping = await api.GetMappingAsync(guid);
|
||||
Console.WriteLine($"mapping = {JsonConvert.SerializeObject(mapping)}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
|
||||
var request = await api.GetRequestsAsync();
|
||||
Console.WriteLine($"request = {JsonConvert.SerializeObject(request)}");
|
||||
|
||||
//var deleteRequestsAsync = api.DeleteRequestsAsync().Result;
|
||||
//Console.WriteLine($"DeleteRequestsAsync = {deleteRequestsAsync.Status}");
|
||||
|
||||
//var resetRequestsAsync = api.ResetRequestsAsync().Result;
|
||||
//Console.WriteLine($"ResetRequestsAsync = {resetRequestsAsync.Status}");
|
||||
|
||||
var scenarioStates = await api.GetScenariosAsync();
|
||||
Console.WriteLine($"GetScenariosAsync = {JsonConvert.SerializeObject(scenarioStates)}");
|
||||
|
||||
var postFileResult = await api.PostFileAsync("1.cs", "C# Hello");
|
||||
Console.WriteLine($"postFileResult = {JsonConvert.SerializeObject(postFileResult)}");
|
||||
|
||||
var getFileResult = await api.GetFileAsync("1.cs");
|
||||
Console.WriteLine($"getFileResult = {getFileResult}");
|
||||
|
||||
var resetMappingsAsync = await api.ResetMappingsAsync();
|
||||
Console.WriteLine($"resetMappingsAsync = {resetMappingsAsync.Status}");
|
||||
|
||||
var resetMappingsAndReloadStaticMappingsAsync = await api.ResetMappingsAsync(true);
|
||||
Console.WriteLine($"resetMappingsAndReloadStaticMappingsAsync = {resetMappingsAndReloadStaticMappingsAsync.Status}");
|
||||
|
||||
Console.WriteLine("Press any key to quit");
|
||||
Console.ReadKey();
|
||||
var guid = Guid.Parse("11111110-a633-40e8-a244-5cb80bc0ab66");
|
||||
var mapping = await api.GetMappingAsync(guid);
|
||||
Console.WriteLine($"mapping = {JsonConvert.SerializeObject(mapping)}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
|
||||
var request = await api.GetRequestsAsync();
|
||||
Console.WriteLine($"request = {JsonConvert.SerializeObject(request)}");
|
||||
|
||||
//var deleteRequestsAsync = api.DeleteRequestsAsync().Result;
|
||||
//Console.WriteLine($"DeleteRequestsAsync = {deleteRequestsAsync.Status}");
|
||||
|
||||
//var resetRequestsAsync = api.ResetRequestsAsync().Result;
|
||||
//Console.WriteLine($"ResetRequestsAsync = {resetRequestsAsync.Status}");
|
||||
|
||||
var scenarioStates = await api.GetScenariosAsync();
|
||||
Console.WriteLine($"GetScenariosAsync = {JsonConvert.SerializeObject(scenarioStates)}");
|
||||
|
||||
var postFileResult = await api.PostFileAsync("1.cs", "C# Hello");
|
||||
Console.WriteLine($"postFileResult = {JsonConvert.SerializeObject(postFileResult)}");
|
||||
|
||||
var getFileResult = await api.GetFileAsync("1.cs");
|
||||
Console.WriteLine($"getFileResult = {getFileResult}");
|
||||
|
||||
var resetMappingsAsync = await api.ResetMappingsAsync();
|
||||
Console.WriteLine($"resetMappingsAsync = {resetMappingsAsync.Status}");
|
||||
|
||||
var resetMappingsAndReloadStaticMappingsAsync = await api.ResetMappingsAsync(true);
|
||||
Console.WriteLine($"resetMappingsAndReloadStaticMappingsAsync = {resetMappingsAndReloadStaticMappingsAsync.Status}");
|
||||
|
||||
Console.WriteLine("Press any key to quit");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ApplicationIcon>../../resources/WireMock.Net-Logo.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ApplicationIcon>../../resources/WireMock.Net-Logo.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="RestEase" Version="1.5.7" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.Abstractions\WireMock.Net.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.RestClient\WireMock.Net.RestClient.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.RestClient\WireMock.Net.RestClient.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers" Version="2.*" />
|
||||
<!--<PackageReference Include="Handlebars.Net.Helpers" Version="2.*" />-->
|
||||
<PackageReference Include="log4net" Version="2.0.15" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
@@ -33,6 +35,11 @@ namespace WireMock.Net.ConsoleApplication
|
||||
}
|
||||
}
|
||||
|
||||
public class Todo
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public static class MainApp
|
||||
{
|
||||
public static void Run()
|
||||
@@ -51,8 +58,31 @@ namespace WireMock.Net.ConsoleApplication
|
||||
var json = mappingBuilder.ToJson();
|
||||
System.Console.WriteLine("mappingBuilder : Json = {0}", json);
|
||||
|
||||
var s = WireMockServer.Start();
|
||||
s.Stop();
|
||||
var todos = new Dictionary<int, Todo>();
|
||||
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("todos")
|
||||
.UsingGet()
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithBodyAsJson(todos.Values)
|
||||
);
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingGet()
|
||||
.WithPath("todos")
|
||||
.WithParam("id")
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithBodyAsJson(rm => todos[int.Parse(rm.Query!["id"].ToString())])
|
||||
);
|
||||
|
||||
var httpClient = server.CreateClient();
|
||||
//server.Stop();
|
||||
|
||||
var httpAndHttpsWithPort = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
@@ -71,7 +101,7 @@ namespace WireMock.Net.ConsoleApplication
|
||||
string url2 = "http://localhost:9092/";
|
||||
string url3 = "https://localhost:9443/";
|
||||
|
||||
var server = WireMockServer.Start(new WireMockServerSettings
|
||||
server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
AllowCSharpCodeMatcher = true,
|
||||
Urls = new[] { url1, url2, url3 },
|
||||
@@ -106,6 +136,31 @@ namespace WireMock.Net.ConsoleApplication
|
||||
//server.SetAzureADAuthentication("6c2a4722-f3b9-4970-b8fc-fac41e29stef", "8587fde1-7824-42c7-8592-faf92b04stef");
|
||||
|
||||
// server.AllowPartialMapping();
|
||||
|
||||
// 400 ms
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/slow/400")
|
||||
.UsingPost())
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
.WithStatusCode(400)
|
||||
.WithBody("return 400")
|
||||
.WithHeader("Content-Type", "text/plain")
|
||||
);
|
||||
// 4 sec
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/slow/500")
|
||||
.UsingPost())
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
.WithStatusCode(500)
|
||||
.WithBody("return 500")
|
||||
.WithHeader("Content-Type", "text/plain")
|
||||
);
|
||||
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
@@ -588,6 +643,7 @@ namespace WireMock.Net.ConsoleApplication
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("Content-Type", "application/json")
|
||||
.WithBodyAsJson(new { Id = "5bdf076c-5654-4b3e-842c-7caf1fabf8c9" }));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/random200or505").UsingGet())
|
||||
.RespondWith(Response.Create().WithCallback(request =>
|
||||
@@ -595,7 +651,11 @@ namespace WireMock.Net.ConsoleApplication
|
||||
int code = new Random().Next(1, 2) == 1 ? 505 : 200;
|
||||
return new ResponseMessage
|
||||
{
|
||||
BodyData = new BodyData { BodyAsString = "random200or505:" + code, DetectedBodyType = Types.BodyType.String },
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "random200or505:" + code + ", HeadersFromRequest = " + string.Join(",", request.Headers),
|
||||
DetectedBodyType = Types.BodyType.String,
|
||||
},
|
||||
StatusCode = code
|
||||
};
|
||||
}));
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
|
||||
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
|
||||
<package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.2.3" targetFramework="net452" />
|
||||
<package id="Microsoft.Owin" version="2.0.2" targetFramework="net452" />
|
||||
<package id="Microsoft.Owin" version="4.2.2" targetFramework="net452" />
|
||||
<package id="Microsoft.Owin.Host.HttpListener" version="2.0.2" targetFramework="net452" />
|
||||
<package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net452" />
|
||||
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net452" />
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using WireMock.Models;
|
||||
|
||||
namespace WireMock.Admin.Mappings;
|
||||
@@ -84,4 +85,13 @@ public class MappingModel
|
||||
/// Fire and forget for webhooks.
|
||||
/// </summary>
|
||||
public bool? UseWebhooksFireAndForget { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Data Object which can be used when WithTransformer is used.
|
||||
/// e.g. lookup an path in this object using
|
||||
/// <example>
|
||||
/// lookup data "1"
|
||||
/// </example>
|
||||
/// </summary>
|
||||
public object? Data { get; set; }
|
||||
}
|
||||
@@ -1,121 +1,120 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WireMock.Admin.Mappings
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// ResponseModel
|
||||
/// </summary>
|
||||
[FluentBuilder.AutoGenerateBuilder]
|
||||
public class ResponseModel
|
||||
{
|
||||
/// <summary>
|
||||
/// ResponseModel
|
||||
/// Gets or sets the HTTP status.
|
||||
/// </summary>
|
||||
[FluentBuilder.AutoGenerateBuilder]
|
||||
public class ResponseModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP status.
|
||||
/// </summary>
|
||||
public object? StatusCode { get; set; }
|
||||
public object? StatusCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body destination (SameAsSource, String or Bytes).
|
||||
/// </summary>
|
||||
public string? BodyDestination { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the body destination (SameAsSource, String or Bytes).
|
||||
/// </summary>
|
||||
public string? BodyDestination { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body.
|
||||
/// </summary>
|
||||
public string? Body { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the body.
|
||||
/// </summary>
|
||||
public string? Body { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body (as JSON object).
|
||||
/// </summary>
|
||||
public object? BodyAsJson { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the body (as JSON object).
|
||||
/// </summary>
|
||||
public object? BodyAsJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
|
||||
/// </summary>
|
||||
public bool? BodyAsJsonIndented { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
|
||||
/// </summary>
|
||||
public bool? BodyAsJsonIndented { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body (as bytearray).
|
||||
/// </summary>
|
||||
public byte[]? BodyAsBytes { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the body (as bytearray).
|
||||
/// </summary>
|
||||
public byte[]? BodyAsBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body as a file.
|
||||
/// </summary>
|
||||
public string? BodyAsFile { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the body as a file.
|
||||
/// </summary>
|
||||
public string? BodyAsFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Is the body as file cached?
|
||||
/// </summary>
|
||||
public bool? BodyAsFileIsCached { get; set; }
|
||||
/// <summary>
|
||||
/// Is the body as file cached?
|
||||
/// </summary>
|
||||
public bool? BodyAsFileIsCached { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body encoding.
|
||||
/// </summary>
|
||||
public EncodingModel? BodyEncoding { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the body encoding.
|
||||
/// </summary>
|
||||
public EncodingModel? BodyEncoding { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Use ResponseMessage Transformer.
|
||||
/// </summary>
|
||||
public bool? UseTransformer { get; set; }
|
||||
/// <summary>
|
||||
/// Use ResponseMessage Transformer.
|
||||
/// </summary>
|
||||
public bool? UseTransformer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the transformer.
|
||||
/// </summary>
|
||||
public string? TransformerType { get; set; }
|
||||
/// <summary>
|
||||
/// Gets the type of the transformer.
|
||||
/// </summary>
|
||||
public string? TransformerType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Use the Handlebars transformer for the content from the referenced BodyAsFile.
|
||||
/// </summary>
|
||||
public bool? UseTransformerForBodyAsFile { get; set; }
|
||||
/// <summary>
|
||||
/// Use the Handlebars transformer for the content from the referenced BodyAsFile.
|
||||
/// </summary>
|
||||
public bool? UseTransformerForBodyAsFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The ReplaceNodeOptions to use when transforming a JSON node.
|
||||
/// </summary>
|
||||
public string? TransformerReplaceNodeOptions { get; set; }
|
||||
/// <summary>
|
||||
/// The ReplaceNodeOptions to use when transforming a JSON node.
|
||||
/// </summary>
|
||||
public string? TransformerReplaceNodeOptions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the headers.
|
||||
/// </summary>
|
||||
public IDictionary<string, object>? Headers { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the headers.
|
||||
/// </summary>
|
||||
public IDictionary<string, object>? Headers { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Headers (Raw).
|
||||
/// </summary>
|
||||
public string? HeadersRaw { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the Headers (Raw).
|
||||
/// </summary>
|
||||
public string? HeadersRaw { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? Delay { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? Delay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the minimum random delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? MinimumRandomDelay { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the minimum random delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? MinimumRandomDelay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum random delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? MaximumRandomDelay { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum random delay in milliseconds.
|
||||
/// </summary>
|
||||
public int? MaximumRandomDelay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Proxy URL.
|
||||
/// </summary>
|
||||
public string? ProxyUrl { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the Proxy URL.
|
||||
/// </summary>
|
||||
public string? ProxyUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The client X509Certificate2 Thumbprint or SubjectName to use.
|
||||
/// </summary>
|
||||
public string? X509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||
/// <summary>
|
||||
/// The client X509Certificate2 Thumbprint or SubjectName to use.
|
||||
/// </summary>
|
||||
public string? X509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the fault.
|
||||
/// </summary>
|
||||
public FaultModel? Fault { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the fault.
|
||||
/// </summary>
|
||||
public FaultModel? Fault { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the WebProxy settings.
|
||||
/// </summary>
|
||||
public WebProxyModel? WebProxy { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets or sets the WebProxy settings.
|
||||
/// </summary>
|
||||
public WebProxyModel? WebProxy { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// RequestModelBuilder
|
||||
/// </summary>
|
||||
public partial class RequestModelBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// UsingConnect: add HTTP Method matching on `CONNECT`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingConnect() => WithMethods("CONNECT");
|
||||
|
||||
/// <summary>
|
||||
/// UsingDelete: add HTTP Method matching on `DELETE`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingDelete() => WithMethods("DELETE");
|
||||
|
||||
/// <summary>
|
||||
/// UsingGet: add HTTP Method matching on `GET`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingGet() => WithMethods("GET");
|
||||
|
||||
/// <summary>
|
||||
/// UsingHead: Add HTTP Method matching on `HEAD`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingHead() => WithMethods("HEAD");
|
||||
|
||||
/// <summary>
|
||||
/// UsingPost: add HTTP Method matching on `POST`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingPost() => WithMethods("POST");
|
||||
|
||||
/// <summary>
|
||||
/// UsingPatch: add HTTP Method matching on `PATCH`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingPatch() => WithMethods("PATCH");
|
||||
|
||||
/// <summary>
|
||||
/// UsingPut: add HTTP Method matching on `OPTIONS`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingOptions() => WithMethods("OPTIONS");
|
||||
|
||||
/// <summary>
|
||||
/// UsingPut: add HTTP Method matching on `PUT`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingPut() => WithMethods("PUT");
|
||||
|
||||
/// <summary>
|
||||
/// UsingTrace: add HTTP Method matching on `TRACE`.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingTrace() => WithMethods("TRACE");
|
||||
|
||||
/// <summary>
|
||||
/// UsingAnyMethod: add HTTP Method matching on any method.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="RequestModelBuilder"/>.</returns>
|
||||
public RequestModelBuilder UsingAnyMethod() => this;
|
||||
|
||||
/// <summary>
|
||||
/// Set the ClientIP.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithClientIP(string value) => WithClientIP(() => value);
|
||||
|
||||
/// <summary>
|
||||
/// Set the ClientIP.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithClientIP(ClientIPModel value) => WithClientIP(() => value);
|
||||
|
||||
/// <summary>
|
||||
/// Set the ClientIP.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithClientIP(Action<ClientIPModelBuilder> action)
|
||||
{
|
||||
return WithClientIP(() =>
|
||||
{
|
||||
var builder = new ClientIPModelBuilder();
|
||||
action(builder);
|
||||
return builder.Build();
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the Path.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithPath(string value) => WithPath(() => value);
|
||||
|
||||
/// <summary>
|
||||
/// Set the Path.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithPath(PathModel value) => WithPath(() => value);
|
||||
|
||||
/// <summary>
|
||||
/// Set the Path.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithPath(Action<PathModelBuilder> action)
|
||||
{
|
||||
return WithPath(() =>
|
||||
{
|
||||
var builder = new PathModelBuilder();
|
||||
action(builder);
|
||||
return builder.Build();
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the Url.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithUrl(string value) => WithUrl(() => value);
|
||||
|
||||
/// <summary>
|
||||
/// Set the Url.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithUrl(UrlModel value) => WithUrl(() => value);
|
||||
|
||||
/// <summary>
|
||||
/// Set the Url.
|
||||
/// </summary>
|
||||
public RequestModelBuilder WithUrl(Action<UrlModelBuilder> action)
|
||||
{
|
||||
return WithUrl(() =>
|
||||
{
|
||||
var builder = new UrlModelBuilder();
|
||||
action(builder);
|
||||
return builder.Build();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// ResponseModelBuilder
|
||||
/// </summary>
|
||||
public partial class ResponseModelBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// Set the StatusCode.
|
||||
/// </summary>
|
||||
public ResponseModelBuilder WithStatusCode(int value) => WithStatusCode(() => value);
|
||||
|
||||
/// <summary>
|
||||
/// Set the StatusCode.
|
||||
/// </summary>
|
||||
public ResponseModelBuilder WithStatusCode(HttpStatusCode value) => WithStatusCode(() => value);
|
||||
|
||||
/// <summary>
|
||||
/// Set the Delay.
|
||||
/// </summary>
|
||||
public ResponseModelBuilder WithDelay(TimeSpan value) => WithDelay((int) value.TotalMilliseconds);
|
||||
|
||||
/// <summary>
|
||||
/// Set the MinimumRandomDelay.
|
||||
/// </summary>
|
||||
public ResponseModelBuilder WithMinimumRandomDelay(TimeSpan value) => WithMinimumRandomDelay((int)value.TotalMilliseconds);
|
||||
|
||||
/// <summary>
|
||||
/// Set the MaximumRandomDelay.
|
||||
/// </summary>
|
||||
public ResponseModelBuilder WithMaximumRandomDelay(TimeSpan value) => WithMaximumRandomDelay((int)value.TotalMilliseconds);
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public interface IResponseMessage
|
||||
/// Gets or sets the status code.
|
||||
/// </summary>
|
||||
object? StatusCode { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Adds the header.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
|
||||
namespace WireMock.Client.Builders;
|
||||
|
||||
/// <summary>
|
||||
/// AdminApiMappingBuilder
|
||||
/// </summary>
|
||||
public class AdminApiMappingBuilder
|
||||
{
|
||||
private readonly List<Action<MappingModelBuilder>> _mappingModelBuilderActions = new();
|
||||
|
||||
private readonly IWireMockAdminApi _api;
|
||||
|
||||
/// <summary>
|
||||
/// AdminApiMappingBuilder
|
||||
/// </summary>
|
||||
/// <param name="api">The <see cref="IWireMockAdminApi"/>.</param>
|
||||
public AdminApiMappingBuilder(IWireMockAdminApi api)
|
||||
{
|
||||
_api = Guard.NotNull(api);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The Given
|
||||
/// </summary>
|
||||
/// <param name="mappingModelBuilderAction">The action.</param>
|
||||
public void Given(Action<MappingModelBuilder> mappingModelBuilderAction)
|
||||
{
|
||||
_mappingModelBuilderActions.Add(Guard.NotNull(mappingModelBuilderAction));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build the mappings and post these using the <see cref="IWireMockAdminApi"/> to the WireMock.Net server.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional CancellationToken.</param>
|
||||
/// <returns><see cref="StatusModel"/></returns>
|
||||
public Task<StatusModel> BuildAndPostAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var modelMappings = new List<MappingModel>();
|
||||
|
||||
foreach (var mappingModelBuilderAction in _mappingModelBuilderActions)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var mappingModelBuilder = new MappingModelBuilder();
|
||||
mappingModelBuilderAction(mappingModelBuilder);
|
||||
|
||||
modelMappings.Add(mappingModelBuilder.Build());
|
||||
}
|
||||
|
||||
return _api.PostMappingsAsync(modelMappings, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Text;
|
||||
using JsonConverter.Abstractions;
|
||||
using JsonConverter.Newtonsoft.Json;
|
||||
using WireMock.Admin.Mappings;
|
||||
|
||||
namespace WireMock.Client.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// ResponseModelBuilder
|
||||
/// </summary>
|
||||
public static class ResponseModelBuilderExtensions
|
||||
{
|
||||
private static readonly Encoding Utf8NoBom = new UTF8Encoding(false);
|
||||
private static readonly IJsonConverter JsonConverter = new NewtonsoftJsonConverter();
|
||||
|
||||
/// <summary>
|
||||
/// WithBodyAsJson
|
||||
/// </summary>
|
||||
/// <param name="builder">The ResponseModelBuilder.</param>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <param name="encoding">The body encoding.</param>
|
||||
/// <param name="indented">Define whether child objects to be indented.</param>
|
||||
public static ResponseModelBuilder WithBodyAsJson(this ResponseModelBuilder builder, object body, Encoding? encoding = null, bool? indented = null)
|
||||
{
|
||||
return builder.WithBodyAsBytes(() =>
|
||||
{
|
||||
var options = new JsonConverterOptions
|
||||
{
|
||||
WriteIndented = indented == true
|
||||
};
|
||||
var jsonBody = JsonConverter.Serialize(body, options);
|
||||
return (encoding ?? Utf8NoBom).GetBytes(jsonBody);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WithBodyAsJson
|
||||
/// </summary>
|
||||
/// <param name="builder">The ResponseModelBuilder.</param>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <param name="indented">Define whether child objects to be indented.</param>
|
||||
public static ResponseModelBuilder WithBodyAsJson(this ResponseModelBuilder builder, object body, bool indented)
|
||||
{
|
||||
return builder.WithBodyAsJson(body, null, indented);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using WireMock.Client.Builders;
|
||||
|
||||
namespace WireMock.Client.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Some extensions for <see cref="IWireMockAdminApi"/>.
|
||||
/// </summary>
|
||||
public static class WireMockAdminApiExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Get a new <see cref="AdminApiMappingBuilder"/> for the <see cref="IWireMockAdminApi"/>.
|
||||
/// </summary>
|
||||
/// <param name="api">See <see cref="IWireMockAdminApi"/>.</param>
|
||||
/// <returns></returns>
|
||||
public static AdminApiMappingBuilder GetMappingBuilder(this IWireMockAdminApi api)
|
||||
{
|
||||
return new AdminApiMappingBuilder(api);
|
||||
}
|
||||
}
|
||||
@@ -1,250 +1,283 @@
|
||||
using RestEase;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using RestEase;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Admin.Requests;
|
||||
using WireMock.Admin.Scenarios;
|
||||
using WireMock.Admin.Settings;
|
||||
using WireMock.Types;
|
||||
|
||||
namespace WireMock.Client
|
||||
namespace WireMock.Client;
|
||||
|
||||
/// <summary>
|
||||
/// The RestEase interface which defines all admin commands.
|
||||
/// </summary>
|
||||
[BasePath("__admin")]
|
||||
public interface IWireMockAdminApi
|
||||
{
|
||||
/// <summary>
|
||||
/// The RestEase interface which defines all admin commands.
|
||||
/// Authentication header
|
||||
/// </summary>
|
||||
[BasePath("__admin")]
|
||||
public interface IWireMockAdminApi
|
||||
{
|
||||
/// <summary>
|
||||
/// Authentication header
|
||||
/// </summary>
|
||||
[Header("Authorization")]
|
||||
AuthenticationHeaderValue Authorization { get; set; }
|
||||
[Header("Authorization")]
|
||||
AuthenticationHeaderValue Authorization { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get the settings.
|
||||
/// </summary>
|
||||
/// <returns>SettingsModel</returns>
|
||||
[Get("settings")]
|
||||
Task<SettingsModel> GetSettingsAsync();
|
||||
/// <summary>
|
||||
/// Get the settings.
|
||||
/// </summary>
|
||||
/// <returns>SettingsModel</returns>
|
||||
[Get("settings")]
|
||||
Task<SettingsModel> GetSettingsAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Update the settings.
|
||||
/// </summary>
|
||||
/// <param name="settings">SettingsModel</param>
|
||||
[Put("settings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PutSettingsAsync([Body] SettingsModel settings);
|
||||
/// <summary>
|
||||
/// Update the settings.
|
||||
/// </summary>
|
||||
/// <param name="settings">SettingsModel</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Put("settings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PutSettingsAsync([Body] SettingsModel settings, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Update the settings
|
||||
/// </summary>
|
||||
/// <param name="settings">SettingsModel</param>
|
||||
[Post("settings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PostSettingsAsync([Body] SettingsModel settings);
|
||||
/// <summary>
|
||||
/// Update the settings
|
||||
/// </summary>
|
||||
/// <param name="settings">SettingsModel</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("settings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PostSettingsAsync([Body] SettingsModel settings, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get the mappings.
|
||||
/// </summary>
|
||||
/// <returns>MappingModels</returns>
|
||||
[Get("mappings")]
|
||||
Task<IList<MappingModel>> GetMappingsAsync();
|
||||
/// <summary>
|
||||
/// Get the mappings.
|
||||
/// </summary>
|
||||
/// <returns>MappingModels</returns>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Get("mappings")]
|
||||
Task<IList<MappingModel>> GetMappingsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get the C# code from all mappings
|
||||
/// </summary>
|
||||
/// <returns>C# code</returns>
|
||||
[Get("mappings/code")]
|
||||
Task<string> GetMappingsCodeAsync([Query] MappingConverterType mappingConverterType = MappingConverterType.Server);
|
||||
|
||||
/// <summary>
|
||||
/// Add a new mapping.
|
||||
/// </summary>
|
||||
/// <param name="mapping">MappingModel</param>
|
||||
[Post("mappings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PostMappingAsync([Body] MappingModel mapping);
|
||||
/// <summary>
|
||||
/// Get the C# code from all mappings
|
||||
/// </summary>
|
||||
/// <returns>C# code</returns>
|
||||
/// <param name="mappingConverterType">The <see cref="MappingConverterType"/>, default is Server.</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Get("mappings/code")]
|
||||
Task<string> GetMappingsCodeAsync([Query] MappingConverterType mappingConverterType = MappingConverterType.Server, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Add new mappings.
|
||||
/// </summary>
|
||||
/// <param name="mappings">MappingModels</param>
|
||||
[Post("mappings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PostMappingsAsync([Body] IList<MappingModel> mappings);
|
||||
/// <summary>
|
||||
/// Add a new mapping.
|
||||
/// </summary>
|
||||
/// <param name="mapping">MappingModel</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("mappings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PostMappingAsync([Body] MappingModel mapping, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete all mappings.
|
||||
/// </summary>
|
||||
[Delete("mappings")]
|
||||
Task<StatusModel> DeleteMappingsAsync();
|
||||
/// <summary>
|
||||
/// Add new mappings.
|
||||
/// </summary>
|
||||
/// <param name="mappings">MappingModels</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("mappings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PostMappingsAsync([Body] IList<MappingModel> mappings, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete mappings according to GUIDs
|
||||
/// </summary>
|
||||
/// <param name="mappings">MappingModels</param>
|
||||
[Delete("mappings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> DeleteMappingsAsync([Body] IList<MappingModel> mappings);
|
||||
/// <summary>
|
||||
/// Delete all mappings.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Delete("mappings")]
|
||||
Task<StatusModel> DeleteMappingsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete (reset) all mappings.
|
||||
/// </summary>
|
||||
/// <param name="reloadStaticMappings">A value indicating whether to reload the static mappings after the reset.</param>
|
||||
[Post("mappings/reset")]
|
||||
Task<StatusModel> ResetMappingsAsync(bool? reloadStaticMappings = false);
|
||||
/// <summary>
|
||||
/// Delete mappings according to GUIDs
|
||||
/// </summary>
|
||||
/// <param name="mappings">MappingModels</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Delete("mappings")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> DeleteMappingsAsync([Body] IList<MappingModel> mappings, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get a mapping based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <returns>MappingModel</returns>
|
||||
[Get("mappings/{guid}")]
|
||||
Task<MappingModel> GetMappingAsync([Path] Guid guid);
|
||||
/// <summary>
|
||||
/// Delete (reset) all mappings.
|
||||
/// </summary>
|
||||
/// <param name="reloadStaticMappings">A value indicating whether to reload the static mappings after the reset.</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("mappings/reset")]
|
||||
Task<StatusModel> ResetMappingsAsync(bool? reloadStaticMappings = false, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get the C# code from a mapping based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <param name="mappingConverterType">The optional mappingConverterType (can be Server or Builder)</param>
|
||||
/// <returns>C# code</returns>
|
||||
[Get("mappings/code/{guid}")]
|
||||
Task<string> GetMappingCodeAsync([Path] Guid guid, [Query] MappingConverterType mappingConverterType = MappingConverterType.Server);
|
||||
|
||||
/// <summary>
|
||||
/// Update a mapping based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <param name="mapping">MappingModel</param>
|
||||
[Put("mappings/{guid}")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PutMappingAsync([Path] Guid guid, [Body] MappingModel mapping);
|
||||
/// <summary>
|
||||
/// Get a mapping based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <returns>MappingModel</returns>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Get("mappings/{guid}")]
|
||||
Task<MappingModel> GetMappingAsync([Path] Guid guid, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete a mapping based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
[Delete("mappings/{guid}")]
|
||||
Task<StatusModel> DeleteMappingAsync([Path] Guid guid);
|
||||
/// <summary>
|
||||
/// Get the C# code from a mapping based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <param name="mappingConverterType">The optional mappingConverterType (can be Server or Builder)</param>
|
||||
/// <returns>C# code</returns>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Get("mappings/code/{guid}")]
|
||||
Task<string> GetMappingCodeAsync([Path] Guid guid, [Query] MappingConverterType mappingConverterType = MappingConverterType.Server, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Save the mappings
|
||||
/// </summary>
|
||||
[Post("mappings/save")]
|
||||
Task<StatusModel> SaveMappingAsync();
|
||||
/// <summary>
|
||||
/// Update a mapping based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <param name="mapping">MappingModel</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Put("mappings/{guid}")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<StatusModel> PutMappingAsync([Path] Guid guid, [Body] MappingModel mapping, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get the requests.
|
||||
/// </summary>
|
||||
/// <returns>LogRequestModels</returns>
|
||||
[Get("requests")]
|
||||
Task<IList<LogEntryModel>> GetRequestsAsync();
|
||||
/// <summary>
|
||||
/// Delete a mapping based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Delete("mappings/{guid}")]
|
||||
Task<StatusModel> DeleteMappingAsync([Path] Guid guid, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete all requests.
|
||||
/// </summary>
|
||||
[Delete("requests")]
|
||||
Task<StatusModel> DeleteRequestsAsync();
|
||||
/// <summary>
|
||||
/// Save the mappings
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("mappings/save")]
|
||||
Task<StatusModel> SaveMappingAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete (reset) all requests.
|
||||
/// </summary>
|
||||
[Post("requests/reset")]
|
||||
Task<StatusModel> ResetRequestsAsync();
|
||||
/// <summary>
|
||||
/// Get the requests.
|
||||
/// </summary>
|
||||
/// <returns>LogRequestModels</returns>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Get("requests")]
|
||||
Task<IList<LogEntryModel>> GetRequestsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get a request based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <returns>MappingModel</returns>
|
||||
[Get("requests/{guid}")]
|
||||
Task<LogEntryModel> GetRequestAsync([Path] Guid guid);
|
||||
/// <summary>
|
||||
/// Delete all requests.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Delete("requests")]
|
||||
Task<StatusModel> DeleteRequestsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete a request based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
[Delete("requests/{guid}")]
|
||||
Task<StatusModel> DeleteRequestAsync([Path] Guid guid);
|
||||
/// <summary>
|
||||
/// Delete (reset) all requests.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("requests/reset")]
|
||||
Task<StatusModel> ResetRequestsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Find a request based on the criteria
|
||||
/// </summary>
|
||||
/// <param name="model">The RequestModel</param>
|
||||
[Post("requests/find")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<IList<LogEntryModel>> FindRequestsAsync([Body] RequestModel model);
|
||||
/// <summary>
|
||||
/// Get a request based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <returns>MappingModel</returns>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Get("requests/{guid}")]
|
||||
Task<LogEntryModel> GetRequestAsync([Path] Guid guid, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get all scenarios
|
||||
/// </summary>
|
||||
[Get("scenarios")]
|
||||
Task<IList<ScenarioStateModel>> GetScenariosAsync();
|
||||
/// <summary>
|
||||
/// Delete a request based on the guid
|
||||
/// </summary>
|
||||
/// <param name="guid">The Guid</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Delete("requests/{guid}")]
|
||||
Task<StatusModel> DeleteRequestAsync([Path] Guid guid, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete (reset) all scenarios
|
||||
/// </summary>
|
||||
[Delete("scenarios")]
|
||||
Task<StatusModel> DeleteScenariosAsync();
|
||||
/// <summary>
|
||||
/// Find a request based on the criteria
|
||||
/// </summary>
|
||||
/// <param name="model">The RequestModel</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("requests/find")]
|
||||
[Header("Content-Type", "application/json")]
|
||||
Task<IList<LogEntryModel>> FindRequestsAsync([Body] RequestModel model, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete (reset) all scenarios
|
||||
/// </summary>
|
||||
[Post("scenarios")]
|
||||
Task<StatusModel> ResetScenariosAsync();
|
||||
/// <summary>
|
||||
/// Get all scenarios
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Get("scenarios")]
|
||||
Task<IList<ScenarioStateModel>> GetScenariosAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete (reset) a specific scenario
|
||||
/// </summary>
|
||||
[Delete("scenarios/{name}")]
|
||||
[AllowAnyStatusCode]
|
||||
Task<StatusModel> DeleteScenarioAsync([Path] string name);
|
||||
/// <summary>
|
||||
/// Delete (reset) all scenarios
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Delete("scenarios")]
|
||||
Task<StatusModel> DeleteScenariosAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete (reset) all scenarios
|
||||
/// </summary>
|
||||
[Post("scenarios/{name}/reset")]
|
||||
[AllowAnyStatusCode]
|
||||
Task<StatusModel> ResetScenarioAsync([Path] string name);
|
||||
/// <summary>
|
||||
/// Delete (reset) all scenarios
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("scenarios")]
|
||||
Task<StatusModel> ResetScenariosAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new File
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
/// <param name="body">The body</param>
|
||||
[Post("files/{filename}")]
|
||||
Task<StatusModel> PostFileAsync([Path] string filename, [Body] string body);
|
||||
/// <summary>
|
||||
/// Delete (reset) a specific scenario
|
||||
/// </summary>
|
||||
/// <param name="name">Scenario name.</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Delete("scenarios/{name}")]
|
||||
[AllowAnyStatusCode]
|
||||
Task<StatusModel> DeleteScenarioAsync([Path] string name, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Update an existing File
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
/// <param name="body">The body</param>
|
||||
[Put("files/{filename}")]
|
||||
Task<StatusModel> PutFileAsync([Path] string filename, [Body] string body);
|
||||
/// <summary>
|
||||
/// Delete (reset) all scenarios
|
||||
/// </summary>
|
||||
/// <param name="name">Scenario name.</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("scenarios/{name}/reset")]
|
||||
[AllowAnyStatusCode]
|
||||
Task<StatusModel> ResetScenarioAsync([Path] string name, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get the content of an existing File
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
[Get("files/{filename}")]
|
||||
Task<string> GetFileAsync([Path] string filename);
|
||||
/// <summary>
|
||||
/// Create a new File
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
/// <param name="body">The body</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Post("files/{filename}")]
|
||||
Task<StatusModel> PostFileAsync([Path] string filename, [Body] string body, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete an existing File
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
[Delete("files/{filename}")]
|
||||
Task<StatusModel> DeleteFileAsync([Path] string filename);
|
||||
/// <summary>
|
||||
/// Update an existing File
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
/// <param name="body">The body</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Put("files/{filename}")]
|
||||
Task<StatusModel> PutFileAsync([Path] string filename, [Body] string body, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Check if a file exists
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
[Head("files/{filename}")]
|
||||
Task FileExistsAsync([Path] string filename);
|
||||
}
|
||||
/// <summary>
|
||||
/// Get the content of an existing File
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Get("files/{filename}")]
|
||||
Task<string> GetFileAsync([Path] string filename, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Delete an existing File
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Delete("files/{filename}")]
|
||||
Task<StatusModel> DeleteFileAsync([Path] string filename, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Check if a file exists
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename</param>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
[Head("files/{filename}")]
|
||||
Task FileExistsAsync([Path] string filename, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -30,8 +30,10 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.3.0" />
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
|
||||
<PackageReference Include="RestEase" Version="1.5.7" />
|
||||
<PackageReference Include="Stef.Validation" Version="0.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
@@ -120,7 +121,16 @@ public interface IMapping
|
||||
/// <summary>
|
||||
/// Use Fire and Forget for the defined webhook(s). [Optional]
|
||||
/// </summary>
|
||||
public bool? UseWebhooksFireAndForget { get; set; }
|
||||
bool? UseWebhooksFireAndForget { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Data Object which can be used when WithTransformer is used.
|
||||
/// e.g. lookup an path in this object using
|
||||
/// <example>
|
||||
/// lookup data "1"
|
||||
/// </example>
|
||||
/// </summary>
|
||||
object? Data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// ProvideResponseAsync
|
||||
|
||||
10
src/WireMock.Net/Json/DynamicJsonClassOptions.cs
Normal file
10
src/WireMock.Net/Json/DynamicJsonClassOptions.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq
|
||||
|
||||
namespace WireMock.Json;
|
||||
|
||||
internal class DynamicJsonClassOptions
|
||||
{
|
||||
public IntegerBehavior IntegerConvertBehavior { get; set; } = IntegerBehavior.UseLong;
|
||||
|
||||
public FloatBehavior FloatConvertBehavior { get; set; } = FloatBehavior.UseDouble;
|
||||
}
|
||||
15
src/WireMock.Net/Json/DynamicPropertyWithValue.cs
Normal file
15
src/WireMock.Net/Json/DynamicPropertyWithValue.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq
|
||||
|
||||
using System.Linq.Dynamic.Core;
|
||||
|
||||
namespace WireMock.Json;
|
||||
|
||||
public class DynamicPropertyWithValue : DynamicProperty
|
||||
{
|
||||
public object? Value { get; }
|
||||
|
||||
public DynamicPropertyWithValue(string name, object? value) : base(name, value?.GetType() ?? typeof(object))
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
24
src/WireMock.Net/Json/FloatBehavior.cs
Normal file
24
src/WireMock.Net/Json/FloatBehavior.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq
|
||||
|
||||
namespace WireMock.Json;
|
||||
|
||||
/// <summary>
|
||||
/// Enum to define how to convert an Float in the Json Object.
|
||||
/// </summary>
|
||||
internal enum FloatBehavior
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert all Float types in the Json Object to a double. (default)
|
||||
/// </summary>
|
||||
UseDouble = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Convert all Float types in the Json Object to a float (unless overflow).
|
||||
/// </summary>
|
||||
UseFloat = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Convert all Float types in the Json Object to a decimal (unless overflow).
|
||||
/// </summary>
|
||||
UseDecimal = 2
|
||||
}
|
||||
20
src/WireMock.Net/Json/IntegerBehavior.cs
Normal file
20
src/WireMock.Net/Json/IntegerBehavior.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq
|
||||
|
||||
namespace WireMock.Json;
|
||||
|
||||
/// <summary>
|
||||
/// Enum to define how to convert an Integer in the Json Object.
|
||||
/// </summary>
|
||||
internal enum IntegerBehavior
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert all Integer types in the Json Object to a int (unless overflow).
|
||||
/// (default)
|
||||
/// </summary>
|
||||
UseInt = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Convert all Integer types in the Json Object to a long.
|
||||
/// </summary>
|
||||
UseLong = 1
|
||||
}
|
||||
202
src/WireMock.Net/Json/JObjectExtensions.cs
Normal file
202
src/WireMock.Net/Json/JObjectExtensions.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
// Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Dynamic.Core;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace WireMock.Json;
|
||||
|
||||
internal static class JObjectExtensions
|
||||
{
|
||||
private class JTokenResolvers : Dictionary<JTokenType, Func<JToken, DynamicJsonClassOptions?, object?>>
|
||||
{
|
||||
}
|
||||
|
||||
private static readonly JTokenResolvers Resolvers = new()
|
||||
{
|
||||
{ JTokenType.Array, ConvertJTokenArray },
|
||||
{ JTokenType.Boolean, (jToken, _) => jToken.Value<bool>() },
|
||||
{ JTokenType.Bytes, (jToken, _) => jToken.Value<byte[]>() },
|
||||
{ JTokenType.Date, (jToken, _) => jToken.Value<DateTime>() },
|
||||
{ JTokenType.Float, ConvertJTokenFloat },
|
||||
{ JTokenType.Guid, (jToken, _) => jToken.Value<Guid>() },
|
||||
{ JTokenType.Integer, ConvertJTokenInteger },
|
||||
{ JTokenType.None, (_, _) => null },
|
||||
{ JTokenType.Null, (_, _) => null },
|
||||
{ JTokenType.Object, ConvertJObject },
|
||||
{ JTokenType.Property, ConvertJTokenProperty },
|
||||
{ JTokenType.String, (jToken, _) => jToken.Value<string>() },
|
||||
{ JTokenType.TimeSpan, (jToken, _) => jToken.Value<TimeSpan>() },
|
||||
{ JTokenType.Undefined, (_, _) => null },
|
||||
{ JTokenType.Uri, (o, _) => o.Value<Uri>() },
|
||||
};
|
||||
|
||||
internal static DynamicClass? ToDynamicJsonClass(this JObject? src, DynamicJsonClassOptions? options = null)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var dynamicPropertyWithValues = new List<DynamicPropertyWithValue>();
|
||||
|
||||
foreach (var prop in src.Properties())
|
||||
{
|
||||
var value = Resolvers[prop.Type](prop.Value, options);
|
||||
if (value != null)
|
||||
{
|
||||
dynamicPropertyWithValues.Add(new DynamicPropertyWithValue(prop.Name, value));
|
||||
}
|
||||
}
|
||||
|
||||
return CreateInstance(dynamicPropertyWithValues);
|
||||
}
|
||||
|
||||
internal static IEnumerable ToDynamicClassArray(this JArray? src, DynamicJsonClassOptions? options = null)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
return new object?[0];
|
||||
}
|
||||
|
||||
return ConvertJTokenArray(src, options);
|
||||
}
|
||||
|
||||
private static object? ConvertJObject(JToken arg, DynamicJsonClassOptions? options = null)
|
||||
{
|
||||
if (arg is JObject asJObject)
|
||||
{
|
||||
return asJObject.ToDynamicJsonClass(options);
|
||||
}
|
||||
|
||||
return GetResolverFor(arg)(arg, options);
|
||||
}
|
||||
|
||||
private static object PassThrough(JToken arg, DynamicJsonClassOptions? options)
|
||||
{
|
||||
return arg;
|
||||
}
|
||||
|
||||
private static Func<JToken, DynamicJsonClassOptions?, object?> GetResolverFor(JToken arg)
|
||||
{
|
||||
return Resolvers.TryGetValue(arg.Type, out var result) ? result : PassThrough;
|
||||
}
|
||||
|
||||
private static object ConvertJTokenFloat(JToken arg, DynamicJsonClassOptions? options = null)
|
||||
{
|
||||
if (arg.Type != JTokenType.Float)
|
||||
{
|
||||
throw new InvalidOperationException($"Unable to convert {nameof(JToken)} of type: {arg.Type} to double or float.");
|
||||
}
|
||||
|
||||
if (options?.FloatConvertBehavior == FloatBehavior.UseFloat)
|
||||
{
|
||||
try
|
||||
{
|
||||
return arg.Value<float>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return arg.Value<double>();
|
||||
}
|
||||
}
|
||||
|
||||
if (options?.FloatConvertBehavior == FloatBehavior.UseDecimal)
|
||||
{
|
||||
try
|
||||
{
|
||||
return arg.Value<decimal>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return arg.Value<double>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return arg.Value<double>();
|
||||
}
|
||||
|
||||
private static object ConvertJTokenInteger(JToken arg, DynamicJsonClassOptions? options = null)
|
||||
{
|
||||
if (arg.Type != JTokenType.Integer)
|
||||
{
|
||||
throw new InvalidOperationException($"Unable to convert {nameof(JToken)} of type: {arg.Type} to long or int.");
|
||||
}
|
||||
|
||||
var longValue = arg.Value<long>();
|
||||
|
||||
if (options is null || options.IntegerConvertBehavior == IntegerBehavior.UseInt)
|
||||
{
|
||||
if (longValue is >= int.MinValue and <= int.MaxValue)
|
||||
{
|
||||
return Convert.ToInt32(longValue);
|
||||
}
|
||||
}
|
||||
|
||||
return longValue;
|
||||
}
|
||||
|
||||
private static object? ConvertJTokenProperty(JToken arg, DynamicJsonClassOptions? options = null)
|
||||
{
|
||||
var resolver = GetResolverFor(arg);
|
||||
if (resolver is null)
|
||||
{
|
||||
throw new InvalidOperationException($"Unable to handle {nameof(JToken)} of type: {arg.Type}.");
|
||||
}
|
||||
|
||||
return resolver(arg, options);
|
||||
}
|
||||
|
||||
private static IEnumerable ConvertJTokenArray(JToken arg, DynamicJsonClassOptions? options = null)
|
||||
{
|
||||
if (arg is not JArray array)
|
||||
{
|
||||
throw new InvalidOperationException($"Unable to convert {nameof(JToken)} of type: {arg.Type} to {nameof(JArray)}.");
|
||||
}
|
||||
|
||||
var result = new List<object?>();
|
||||
foreach (var item in array)
|
||||
{
|
||||
result.Add(ConvertJObject(item));
|
||||
}
|
||||
|
||||
var distinctType = FindSameTypeOf(result);
|
||||
return distinctType == null ? result.ToArray() : ConvertToTypedArray(result, distinctType);
|
||||
}
|
||||
|
||||
private static Type? FindSameTypeOf(IEnumerable<object?> src)
|
||||
{
|
||||
var types = src.Select(o => o?.GetType()).Distinct().OfType<Type>().ToArray();
|
||||
return types.Length == 1 ? types[0] : null;
|
||||
}
|
||||
|
||||
private static IEnumerable ConvertToTypedArray(IEnumerable<object?> src, Type newType)
|
||||
{
|
||||
var method = ConvertToTypedArrayGenericMethod.MakeGenericMethod(newType);
|
||||
return (IEnumerable)method.Invoke(null, new object[] { src })!;
|
||||
}
|
||||
|
||||
private static readonly MethodInfo ConvertToTypedArrayGenericMethod = typeof(JObjectExtensions).GetMethod(nameof(ConvertToTypedArrayGeneric), BindingFlags.NonPublic | BindingFlags.Static)!;
|
||||
|
||||
private static T[] ConvertToTypedArrayGeneric<T>(IEnumerable<object> src)
|
||||
{
|
||||
return src.Cast<T>().ToArray();
|
||||
}
|
||||
|
||||
public static DynamicClass CreateInstance(IList<DynamicPropertyWithValue> dynamicPropertiesWithValue, bool createParameterCtor = true)
|
||||
{
|
||||
var type = DynamicClassFactory.CreateType(dynamicPropertiesWithValue.Cast<DynamicProperty>().ToArray(), createParameterCtor);
|
||||
var dynamicClass = (DynamicClass)Activator.CreateInstance(type);
|
||||
foreach (var dynamicPropertyWithValue in dynamicPropertiesWithValue.Where(p => p.Value != null))
|
||||
{
|
||||
dynamicClass.SetDynamicPropertyValue(dynamicPropertyWithValue.Name, dynamicPropertyWithValue.Value!);
|
||||
}
|
||||
|
||||
return dynamicClass;
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,9 @@ public class Mapping : IMapping
|
||||
/// <inheritdoc />
|
||||
public ITimeSettings? TimeSettings { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public object? Data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Mapping"/> class.
|
||||
/// </summary>
|
||||
@@ -91,6 +94,7 @@ public class Mapping : IMapping
|
||||
/// <param name="webhooks">The Webhooks. [Optional]</param>
|
||||
/// <param name="useWebhooksFireAndForget">Use Fire and Forget for the defined webhook(s). [Optional]</param>
|
||||
/// <param name="timeSettings">The TimeSettings. [Optional]</param>
|
||||
/// <param name="data">The data object. [Optional]</param>
|
||||
public Mapping(
|
||||
Guid guid,
|
||||
DateTime updatedAt,
|
||||
@@ -107,7 +111,8 @@ public class Mapping : IMapping
|
||||
int? stateTimes,
|
||||
IWebhook[]? webhooks,
|
||||
bool? useWebhooksFireAndForget,
|
||||
ITimeSettings? timeSettings)
|
||||
ITimeSettings? timeSettings,
|
||||
object? data)
|
||||
{
|
||||
Guid = guid;
|
||||
UpdatedAt = updatedAt;
|
||||
@@ -125,6 +130,7 @@ public class Mapping : IMapping
|
||||
Webhooks = webhooks;
|
||||
UseWebhooksFireAndForget = useWebhooksFireAndForget;
|
||||
TimeSettings = timeSettings;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IMapping.ProvideResponseAsync" />
|
||||
|
||||
@@ -5,8 +5,8 @@ using AnyOfTypes;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Json;
|
||||
using WireMock.Models;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Matchers;
|
||||
|
||||
@@ -100,38 +100,55 @@ public class LinqMatcher : IObjectMatcher, IStringMatcher
|
||||
{
|
||||
double match = MatchScores.Mismatch;
|
||||
|
||||
JObject value;
|
||||
switch (input)
|
||||
JArray jArray;
|
||||
try
|
||||
{
|
||||
case JObject valueAsJObject:
|
||||
value = valueAsJObject;
|
||||
break;
|
||||
|
||||
case { } valueAsObject:
|
||||
value = JObject.FromObject(valueAsObject);
|
||||
break;
|
||||
|
||||
default:
|
||||
return MatchScores.Mismatch;
|
||||
jArray = new JArray { input };
|
||||
}
|
||||
catch
|
||||
{
|
||||
jArray = new JArray { JToken.FromObject(input) };
|
||||
}
|
||||
|
||||
//enumerable = jArray.ToDynamicClassArray();
|
||||
|
||||
//JObject value;
|
||||
//switch (input)
|
||||
//{
|
||||
// case JObject valueAsJObject:
|
||||
// value = valueAsJObject;
|
||||
// break;
|
||||
|
||||
// case { } valueAsObject:
|
||||
// value = JObject.FromObject(valueAsObject);
|
||||
// break;
|
||||
|
||||
// default:
|
||||
// return MatchScores.Mismatch;
|
||||
//}
|
||||
|
||||
// Convert a single object to a Queryable JObject-list with 1 entry.
|
||||
var queryable1 = new[] { value }.AsQueryable();
|
||||
//var queryable1 = new[] { value }.AsQueryable();
|
||||
var queryable = jArray.ToDynamicClassArray().AsQueryable();
|
||||
|
||||
try
|
||||
{
|
||||
// Generate the DynamicLinq select statement.
|
||||
string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value);
|
||||
//string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value);
|
||||
|
||||
// Execute DynamicLinq Select statement.
|
||||
var queryable2 = queryable1.Select(dynamicSelect);
|
||||
//var queryable2 = queryable1.Select(dynamicSelect);
|
||||
|
||||
// Use the Any(...) method to check if the result matches.
|
||||
match = MatchScores.ToScore(_patterns.Select(pattern => queryable2.Any(pattern)).ToArray(), MatchOperator);
|
||||
|
||||
var patternsAsStringArray = _patterns.Select(p => p.GetPattern()).ToArray();
|
||||
var scores = patternsAsStringArray.Select(p => queryable.Any(p)).ToArray();
|
||||
|
||||
match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern.GetPattern())).ToArray(), MatchOperator);
|
||||
|
||||
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
|
||||
}
|
||||
catch
|
||||
catch (Exception e)
|
||||
{
|
||||
if (ThrowException)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
namespace WireMock.Matchers;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Stef.Validation;
|
||||
using WireMock.Http;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Serialization;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Util;
|
||||
@@ -47,12 +49,33 @@ internal class ProxyHelper
|
||||
var responseMessage = await HttpResponseMessageHelper.CreateAsync(httpResponseMessage, requiredUri, originalUri, deserializeJson, decompressGzipAndDeflate).ConfigureAwait(false);
|
||||
|
||||
IMapping? newMapping = null;
|
||||
if (HttpStatusRangeParser.IsMatch(proxyAndRecordSettings.SaveMappingForStatusCodePattern, responseMessage.StatusCode) &&
|
||||
(proxyAndRecordSettings.SaveMapping || proxyAndRecordSettings.SaveMappingToFile))
|
||||
|
||||
var saveMappingSettings = proxyAndRecordSettings.SaveMappingSettings;
|
||||
|
||||
bool save = true;
|
||||
if (saveMappingSettings != null)
|
||||
{
|
||||
save &= Check(saveMappingSettings.StatusCodePattern,
|
||||
() => HttpStatusRangeParser.IsMatch(saveMappingSettings.StatusCodePattern, responseMessage.StatusCode)
|
||||
);
|
||||
|
||||
save &= Check(saveMappingSettings.HttpMethods,
|
||||
() => saveMappingSettings.HttpMethods.Value.Contains(requestMessage.Method, StringComparer.OrdinalIgnoreCase)
|
||||
);
|
||||
}
|
||||
|
||||
if (save && (proxyAndRecordSettings.SaveMapping || proxyAndRecordSettings.SaveMappingToFile))
|
||||
{
|
||||
newMapping = _proxyMappingConverter.ToMapping(mapping, proxyAndRecordSettings, requestMessage, responseMessage);
|
||||
}
|
||||
|
||||
return (responseMessage, newMapping);
|
||||
}
|
||||
|
||||
private static bool Check<T>(ProxySaveMappingSetting<T>? saveMappingSetting, Func<bool> action)
|
||||
{
|
||||
var isMatch = saveMappingSetting is null || action();
|
||||
var matchBehaviour = saveMappingSetting?.MatchBehaviour ?? MatchBehaviour.AcceptOnMatch;
|
||||
return isMatch == (matchBehaviour == MatchBehaviour.AcceptOnMatch);
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
|
||||
IResponseBuilder WithBody(Func<IRequestMessage, string> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null);
|
||||
|
||||
/// <summary>
|
||||
/// WithBody : Create a ... response based on a callback function.
|
||||
/// WithBody : Create a ... response based on a async callback function.
|
||||
/// </summary>
|
||||
/// <param name="bodyFactory">The async delegate to build the body.</param>
|
||||
/// <param name="destination">The Body Destination format (SameAsSource, String or Bytes).</param>
|
||||
@@ -51,7 +51,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
|
||||
/// </summary>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <param name="encoding">The body encoding.</param>
|
||||
/// <param name="indented">Use JSON indented.</param>
|
||||
/// <param name="indented">Define whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithBodyAsJson(object body, Encoding? encoding = null, bool? indented = null);
|
||||
|
||||
@@ -63,6 +63,22 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithBodyAsJson(object body, bool indented);
|
||||
|
||||
/// <summary>
|
||||
/// WithBodyAsJson : Create a ... response based on a callback function.
|
||||
/// </summary>
|
||||
/// <param name="bodyFactory">The delegate to build the body.</param>
|
||||
/// <param name="encoding">The body encoding.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithBodyAsJson(Func<IRequestMessage, object> bodyFactory, Encoding? encoding = null);
|
||||
|
||||
/// <summary>
|
||||
/// WithBodyAsJson : Create a ... response based on a async callback function.
|
||||
/// </summary>
|
||||
/// <param name="bodyFactory">The async delegate to build the body.</param>
|
||||
/// <param name="encoding">The body encoding.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithBodyAsJson(Func<IRequestMessage, Task<object>> bodyFactory, Encoding? encoding = null);
|
||||
|
||||
/// <summary>
|
||||
/// WithBodyFromFile : Create a ... response based on a File.
|
||||
/// </summary>
|
||||
|
||||
@@ -13,7 +13,7 @@ public partial class Response
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBody(Func<IRequestMessage, string> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null)
|
||||
{
|
||||
Guard.NotNull(bodyFactory, nameof(bodyFactory));
|
||||
Guard.NotNull(bodyFactory);
|
||||
|
||||
return WithCallbackInternal(true, req => new ResponseMessage
|
||||
{
|
||||
@@ -30,7 +30,7 @@ public partial class Response
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBody(Func<IRequestMessage, Task<string>> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null)
|
||||
{
|
||||
Guard.NotNull(bodyFactory, nameof(bodyFactory));
|
||||
Guard.NotNull(bodyFactory);
|
||||
|
||||
return WithCallbackInternal(true, async req => new ResponseMessage
|
||||
{
|
||||
@@ -70,7 +70,7 @@ public partial class Response
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/>
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBodyFromFile(string filename, bool cache = true)
|
||||
{
|
||||
Guard.NotNull(filename);
|
||||
@@ -127,7 +127,7 @@ public partial class Response
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson(object, Encoding, bool?)"/>
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBodyAsJson(object body, Encoding? encoding = null, bool? indented = null)
|
||||
{
|
||||
Guard.NotNull(body);
|
||||
@@ -144,12 +144,46 @@ public partial class Response
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson(object, bool)"/>
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBodyAsJson(object body, bool indented)
|
||||
{
|
||||
return WithBodyAsJson(body, null, indented);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBodyAsJson(Func<IRequestMessage, object> bodyFactory, Encoding? encoding = null)
|
||||
{
|
||||
Guard.NotNull(bodyFactory);
|
||||
|
||||
return WithCallbackInternal(true, req => new ResponseMessage
|
||||
{
|
||||
BodyData = new BodyData
|
||||
{
|
||||
Encoding = encoding ?? Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json,
|
||||
BodyAsJson = bodyFactory(req),
|
||||
IsFuncUsed = "Func<IRequestMessage, object>"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBodyAsJson(Func<IRequestMessage, Task<object>> bodyFactory, Encoding? encoding = null)
|
||||
{
|
||||
Guard.NotNull(bodyFactory);
|
||||
|
||||
return WithCallbackInternal(true, async req => new ResponseMessage
|
||||
{
|
||||
BodyData = new BodyData
|
||||
{
|
||||
Encoding = encoding ?? Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json,
|
||||
BodyAsJson = await bodyFactory(req).ConfigureAwait(false),
|
||||
IsFuncUsed = "Func<IRequestMessage, Task<object>>"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBody(object body, IJsonConverter converter, JsonConverterOptions? options = null)
|
||||
{
|
||||
|
||||
@@ -107,14 +107,12 @@ public partial class Response : IResponseBuilder
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Response"/> class.
|
||||
/// </summary>
|
||||
/// <param name="responseMessage">
|
||||
/// The response.
|
||||
/// </param>
|
||||
/// <param name="responseMessage">The response.</param>
|
||||
private Response(ResponseMessage responseMessage)
|
||||
{
|
||||
ResponseMessage = responseMessage;
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc cref="IStatusCodeResponseBuilder.WithStatusCode(int)"/>
|
||||
[PublicAPI]
|
||||
public IResponseBuilder WithStatusCode(int code)
|
||||
|
||||
@@ -181,6 +181,7 @@ internal class MappingConverter
|
||||
Scenario = mapping.Scenario,
|
||||
WhenStateIs = mapping.ExecutionConditionState,
|
||||
SetStateTo = mapping.NextState,
|
||||
Data = mapping.Data,
|
||||
Request = new RequestModel
|
||||
{
|
||||
Headers = headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
|
||||
|
||||
@@ -26,8 +26,12 @@ internal class ProxyMappingConverter
|
||||
_dateTimeUtils = Guard.NotNull(dateTimeUtils);
|
||||
}
|
||||
|
||||
public IMapping ToMapping(IMapping? mapping, ProxyAndRecordSettings proxyAndRecordSettings, IRequestMessage requestMessage, ResponseMessage responseMessage)
|
||||
public IMapping? ToMapping(IMapping? mapping, ProxyAndRecordSettings proxyAndRecordSettings, IRequestMessage requestMessage, ResponseMessage responseMessage)
|
||||
{
|
||||
var useDefinedRequestMatchers = proxyAndRecordSettings.UseDefinedRequestMatchers;
|
||||
var excludedHeaders = new List<string>(proxyAndRecordSettings.ExcludedHeaders ?? new string[] { }) { "Cookie" };
|
||||
var excludedCookies = proxyAndRecordSettings.ExcludedCookies ?? new string[0];
|
||||
|
||||
var request = (Request?)mapping?.RequestMatcher;
|
||||
var clientIPMatcher = request?.GetRequestMessageMatcher<RequestMessageClientIPMatcher>();
|
||||
var pathMatcher = request?.GetRequestMessageMatcher<RequestMessagePathMatcher>();
|
||||
@@ -37,11 +41,6 @@ internal class ProxyMappingConverter
|
||||
var methodMatcher = request?.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
|
||||
var bodyMatcher = request?.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
|
||||
|
||||
var useDefinedRequestMatchers = proxyAndRecordSettings.UseDefinedRequestMatchers;
|
||||
|
||||
var excludedHeaders = new List<string>(proxyAndRecordSettings.ExcludedHeaders ?? new string[] { }) { "Cookie" };
|
||||
var excludedCookies = proxyAndRecordSettings.ExcludedCookies ?? new string[] { };
|
||||
|
||||
var newRequest = Request.Create();
|
||||
|
||||
// ClientIP
|
||||
@@ -178,7 +177,8 @@ internal class ProxyMappingConverter
|
||||
stateTimes: null,
|
||||
webhooks: null,
|
||||
useWebhooksFireAndForget: null,
|
||||
timeSettings: null
|
||||
timeSettings: null,
|
||||
data: mapping?.Data
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -166,4 +166,14 @@ public interface IRespondWithAProvider
|
||||
bool useTransformer = true,
|
||||
TransformerType transformerType = TransformerType.Handlebars
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Data Object which can be used when WithTransformer is used.
|
||||
/// e.g. lookup an path in this object using
|
||||
/// <param name="data">The data dictionary object.</param>
|
||||
/// <example>
|
||||
/// lookup data "1"
|
||||
/// </example>
|
||||
/// </summary>
|
||||
IRespondWithAProvider WithData(object data);
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
using Stef.Validation;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
@@ -39,6 +40,8 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
|
||||
public ITimeSettings? TimeSettings { get; private set; }
|
||||
|
||||
public object? Data { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RespondWithAProvider"/> class.
|
||||
/// </summary>
|
||||
@@ -88,10 +91,20 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
_timesInSameState,
|
||||
Webhooks,
|
||||
_useWebhookFireAndForget,
|
||||
TimeSettings);
|
||||
TimeSettings,
|
||||
Data);
|
||||
|
||||
_registrationCallback(mapping, _saveToFile);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public IRespondWithAProvider WithData(object data)
|
||||
{
|
||||
Data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IRespondWithAProvider WithGuid(string guid)
|
||||
{
|
||||
|
||||
@@ -46,7 +46,7 @@ public partial class WireMockServer
|
||||
}
|
||||
|
||||
var respondProvider = Given(requestBuilder, mappingModel.SaveToFile == true);
|
||||
|
||||
|
||||
if (guid != null)
|
||||
{
|
||||
respondProvider = respondProvider.WithGuid(guid.Value);
|
||||
@@ -56,6 +56,11 @@ public partial class WireMockServer
|
||||
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value);
|
||||
}
|
||||
|
||||
if (mappingModel.Data != null)
|
||||
{
|
||||
respondProvider = respondProvider.WithData(mappingModel.Data);
|
||||
}
|
||||
|
||||
var timeSettings = TimeSettingsMapper.Map(mappingModel.TimeSettings);
|
||||
if (timeSettings != null)
|
||||
{
|
||||
|
||||
@@ -28,9 +28,28 @@ public class ProxyAndRecordSettings : HttpClientSettings
|
||||
/// <summary>
|
||||
/// Only save request/response to the internal Mappings if the status code is included in this pattern. (Note that SaveMapping must also be set to true.)
|
||||
/// The pattern can contain a single value like "200", but also ranges like "2xx", "100,300,600" or "100-299,6xx" are supported.
|
||||
///
|
||||
/// Deprecated : use SaveMappingSettings.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public string SaveMappingForStatusCodePattern { get; set; } = "*";
|
||||
public string SaveMappingForStatusCodePattern
|
||||
{
|
||||
set
|
||||
{
|
||||
if (SaveMappingSettings is null)
|
||||
{
|
||||
SaveMappingSettings = new ProxySaveMappingSettings();
|
||||
}
|
||||
|
||||
SaveMappingSettings.StatusCodePattern = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Additional SaveMappingSettings.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public ProxySaveMappingSettings? SaveMappingSettings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines a list from headers which will be excluded from the saved mappings.
|
||||
|
||||
20
src/WireMock.Net/Settings/ProxySaveMappingSetting.cs
Normal file
20
src/WireMock.Net/Settings/ProxySaveMappingSetting.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Settings;
|
||||
|
||||
public class ProxySaveMappingSetting<T>
|
||||
{
|
||||
public MatchBehaviour MatchBehaviour { get; } = MatchBehaviour.AcceptOnMatch;
|
||||
|
||||
public T Value { get; private set; }
|
||||
|
||||
public ProxySaveMappingSetting(T value, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||
{
|
||||
Value = value;
|
||||
MatchBehaviour = matchBehaviour;
|
||||
}
|
||||
|
||||
public static implicit operator ProxySaveMappingSetting<T>(T value) => new ProxySaveMappingSetting<T>(value);
|
||||
|
||||
public static implicit operator T(ProxySaveMappingSetting<T> @this) => @this.Value;
|
||||
}
|
||||
22
src/WireMock.Net/Settings/ProxySaveMappingSettings.cs
Normal file
22
src/WireMock.Net/Settings/ProxySaveMappingSettings.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace WireMock.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// ProxySaveMappingSettings
|
||||
/// </summary>
|
||||
public class ProxySaveMappingSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Only save request/response to the internal Mappings if the status code is included in this pattern. (Note that SaveMapping must also be set to true.)
|
||||
/// The pattern can contain a single value like "200", but also ranges like "2xx", "100,300,600" or "100-299,6xx" are supported.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public ProxySaveMappingSetting<string>? StatusCodePattern { get; set; } = "*";
|
||||
|
||||
/// <summary>
|
||||
/// Only save these Http Methods. (Note that SaveMapping must also be set to true.)
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public ProxySaveMappingSetting<string[]>? HttpMethods { get; set; }
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Stef.Validation;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
@@ -111,7 +112,12 @@ public static class WireMockServerSettingsParser
|
||||
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
||||
UseDefinedRequestMatchers = parser.GetBoolValue(nameof(ProxyAndRecordSettings.UseDefinedRequestMatchers)),
|
||||
AppendGuidToSavedMappingFile = parser.GetBoolValue(nameof(ProxyAndRecordSettings.AppendGuidToSavedMappingFile)),
|
||||
Url = proxyUrl!
|
||||
Url = proxyUrl!,
|
||||
SaveMappingSettings = new ProxySaveMappingSettings
|
||||
{
|
||||
StatusCodePattern = parser.GetStringValue("SaveMappingForStatusCodePattern", "*"),
|
||||
// HttpMethods = new ProxySaveMappingSetting<string[]>(parser.GetValues("DoNotSaveMappingForHttpMethods", new string[0]), MatchBehaviour.RejectOnMatch)
|
||||
}
|
||||
};
|
||||
|
||||
string? proxyAddress = parser.GetStringValue("WebProxyAddress");
|
||||
|
||||
@@ -10,4 +10,6 @@ internal struct TransformModel
|
||||
public IRequestMessage request { get; set; }
|
||||
|
||||
public IResponseMessage? response { get; set; }
|
||||
|
||||
public object data { get; set; }
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
@@ -115,7 +116,8 @@ internal class Transformer : ITransformer
|
||||
{
|
||||
mapping = mapping,
|
||||
request = request,
|
||||
response = response
|
||||
response = response,
|
||||
data = mapping.Data ?? new { }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -11,38 +9,6 @@ namespace WireMock.Util;
|
||||
|
||||
internal static class JsonUtils
|
||||
{
|
||||
public static Type CreateTypeFromJObject(JObject instance, string? fullName = null)
|
||||
{
|
||||
static Type ConvertType(JToken value, string? propertyName = null)
|
||||
{
|
||||
var type = value.Type;
|
||||
return type switch
|
||||
{
|
||||
JTokenType.Array => value.HasValues ? ConvertType(value.First!, propertyName).MakeArrayType() : typeof(object).MakeArrayType(),
|
||||
JTokenType.Boolean => typeof(bool),
|
||||
JTokenType.Bytes => typeof(byte[]),
|
||||
JTokenType.Date => typeof(DateTime),
|
||||
JTokenType.Guid => typeof(Guid),
|
||||
JTokenType.Float => typeof(float),
|
||||
JTokenType.Integer => typeof(long),
|
||||
JTokenType.Null => typeof(object),
|
||||
JTokenType.Object => CreateTypeFromJObject((JObject)value, propertyName),
|
||||
JTokenType.String => typeof(string),
|
||||
JTokenType.TimeSpan => typeof(TimeSpan),
|
||||
JTokenType.Uri => typeof(string),
|
||||
_ => typeof(object)
|
||||
};
|
||||
}
|
||||
|
||||
var properties = new Dictionary<string, Type>();
|
||||
foreach (var item in instance.Properties())
|
||||
{
|
||||
properties.Add(item.Name, ConvertType(item.Value, item.Name));
|
||||
}
|
||||
|
||||
return TypeBuilderUtils.BuildType(properties, fullName) ?? throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
public static bool TryParseAsJObject(string? strInput, [NotNullWhen(true)] out JObject? value)
|
||||
{
|
||||
value = null;
|
||||
@@ -140,100 +106,4 @@ internal static class JsonUtils
|
||||
_ => throw new NotSupportedException($"Unable to convert value to {typeof(T)}.")
|
||||
};
|
||||
}
|
||||
|
||||
public static string GenerateDynamicLinqStatement(JToken jsonObject)
|
||||
{
|
||||
var lines = new List<string>();
|
||||
WalkNode(jsonObject, null, null, lines);
|
||||
|
||||
return lines.First();
|
||||
}
|
||||
|
||||
private static void WalkNode(JToken node, string? path, string? propertyName, List<string> lines)
|
||||
{
|
||||
switch (node.Type)
|
||||
{
|
||||
case JTokenType.Object:
|
||||
ProcessObject(node, propertyName, lines);
|
||||
break;
|
||||
|
||||
case JTokenType.Array:
|
||||
ProcessArray(node, propertyName, lines);
|
||||
break;
|
||||
|
||||
default:
|
||||
ProcessItem(node, path ?? "it", propertyName, lines);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void ProcessObject(JToken node, string? propertyName, List<string> lines)
|
||||
{
|
||||
var items = new List<string>();
|
||||
var text = new StringBuilder("new (");
|
||||
|
||||
// In case of Object, loop all children. Do a ToArray() to avoid `Collection was modified` exceptions.
|
||||
foreach (var child in node.Children<JProperty>().ToArray())
|
||||
{
|
||||
WalkNode(child.Value, child.Path, child.Name, items);
|
||||
}
|
||||
|
||||
text.Append(string.Join(", ", items));
|
||||
text.Append(")");
|
||||
|
||||
if (!string.IsNullOrEmpty(propertyName))
|
||||
{
|
||||
text.AppendFormat(" as {0}", propertyName);
|
||||
}
|
||||
|
||||
lines.Add(text.ToString());
|
||||
}
|
||||
|
||||
private static void ProcessArray(JToken node, string? propertyName, List<string> lines)
|
||||
{
|
||||
var items = new List<string>();
|
||||
var text = new StringBuilder("(new [] { ");
|
||||
|
||||
// In case of Array, loop all items. Do a ToArray() to avoid `Collection was modified` exceptions.
|
||||
var idx = 0;
|
||||
foreach (var child in node.Children().ToArray())
|
||||
{
|
||||
WalkNode(child, $"{node.Path}[{idx}]", null, items);
|
||||
idx++;
|
||||
}
|
||||
|
||||
text.Append(string.Join(", ", items));
|
||||
text.Append("})");
|
||||
|
||||
if (!string.IsNullOrEmpty(propertyName))
|
||||
{
|
||||
text.AppendFormat(" as {0}", propertyName);
|
||||
}
|
||||
|
||||
lines.Add(text.ToString());
|
||||
}
|
||||
|
||||
private static void ProcessItem(JToken node, string path, string? propertyName, List<string> lines)
|
||||
{
|
||||
var castText = node.Type switch
|
||||
{
|
||||
JTokenType.Boolean => $"bool({path})",
|
||||
JTokenType.Date => $"DateTime({path})",
|
||||
JTokenType.Float => $"double({path})",
|
||||
JTokenType.Guid => $"Guid({path})",
|
||||
JTokenType.Integer => $"long({path})",
|
||||
JTokenType.Null => "null",
|
||||
JTokenType.String => $"string({path})",
|
||||
JTokenType.TimeSpan => $"TimeSpan({path})",
|
||||
JTokenType.Uri => $"Uri({path})",
|
||||
_ => throw new NotSupportedException($"JTokenType '{node.Type}' cannot be converted to a Dynamic Linq cast operator.")
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(propertyName))
|
||||
{
|
||||
castText += $" as {propertyName}";
|
||||
}
|
||||
|
||||
lines.Add(castText);
|
||||
}
|
||||
}
|
||||
@@ -53,13 +53,12 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All" />
|
||||
<PackageReference Include="JsonConverter.Abstractions" Version="0.3.0" />
|
||||
<PackageReference Include="JsonConverter.Abstractions" Version="0.4.0" />
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="NJsonSchema.Extensions" Version="0.1.0" />
|
||||
<PackageReference Include="NSwag.Core" Version="13.16.1" />
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.23" />
|
||||
<PackageReference Include="JmesPath.Net" Version="1.0.125" />
|
||||
<PackageReference Include="AnyOf" Version="0.3.0" />
|
||||
<PackageReference Include="TinyMapper" Version="3.0.3" />
|
||||
@@ -177,13 +176,13 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Handlebars.Net.Helpers" Version="2.3.12" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.DynamicLinq" Version="2.3.12" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.Humanizer" Version="2.3.12" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.Json" Version="2.3.12" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.Random" Version="2.3.12" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.XPath" Version="2.3.12" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.Xeger" Version="2.3.12" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers" Version="2.3.15-preview-01" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.DynamicLinq" Version="2.3.15-preview-01" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.Humanizer" Version="2.3.15-preview-01" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.Json" Version="2.3.15-preview-01" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.Random" Version="2.3.15-preview-01" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.XPath" Version="2.3.15-preview-01" />
|
||||
<PackageReference Include="Handlebars.Net.Helpers.Xeger" Version="2.3.15-preview-01" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -177,7 +177,7 @@ public class WireMockMiddlewareTests
|
||||
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
|
||||
_mappingMock.SetupGet(m => m.Settings).Returns(settings);
|
||||
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null);
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, null);
|
||||
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));
|
||||
|
||||
var requestBuilder = Request.Create().UsingAnyMethod();
|
||||
@@ -231,7 +231,7 @@ public class WireMockMiddlewareTests
|
||||
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
|
||||
_mappingMock.SetupGet(m => m.Settings).Returns(settings);
|
||||
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), _updatedAt, "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null);
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), _updatedAt, "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, data: null);
|
||||
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));
|
||||
|
||||
var requestBuilder = Request.Create().UsingAnyMethod();
|
||||
|
||||
@@ -120,28 +120,6 @@ public class ResponseWithBodyTests
|
||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBody_Object_Indented()
|
||||
{
|
||||
// given
|
||||
var body = new BodyData
|
||||
{
|
||||
DetectedBodyType = BodyType.String,
|
||||
BodyAsString = "abc"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
object x = new { message = "Hello" };
|
||||
var responseBuilder = Response.Create().WithBodyAsJson(x, true);
|
||||
|
||||
// act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// then
|
||||
Check.That(response.Message.BodyData.BodyAsJson).Equals(x);
|
||||
Check.That(response.Message.BodyData.BodyAsJsonIndented).IsEqualTo(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBody_String_SameAsSource_Encoding()
|
||||
{
|
||||
@@ -196,6 +174,70 @@ public class ResponseWithBodyTests
|
||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBodyAsJson_Object_Indented()
|
||||
{
|
||||
// given
|
||||
var body = new BodyData
|
||||
{
|
||||
DetectedBodyType = BodyType.String,
|
||||
BodyAsString = "abc"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
object x = new { message = "Hello" };
|
||||
var responseBuilder = Response.Create().WithBodyAsJson(x, true);
|
||||
|
||||
// act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// then
|
||||
Check.That(response.Message.BodyData.BodyAsJson).Equals(x);
|
||||
Check.That(response.Message.BodyData.BodyAsJsonIndented).IsEqualTo(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBodyAsJson_FuncObject()
|
||||
{
|
||||
// Arrange
|
||||
var requestBody = new BodyData
|
||||
{
|
||||
DetectedBodyType = BodyType.String,
|
||||
BodyAsString = "abc"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, requestBody);
|
||||
|
||||
object responseBody = new { message = "Hello" };
|
||||
var responseBuilder = Response.Create().WithBodyAsJson(requestMessage => responseBody);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsJson.Should().BeEquivalentTo(responseBody);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBodyAsJson_AsyncFuncObject()
|
||||
{
|
||||
// Arrange
|
||||
var requestBody = new BodyData
|
||||
{
|
||||
DetectedBodyType = BodyType.String,
|
||||
BodyAsString = "abc"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, requestBody);
|
||||
|
||||
object responseBody = new { message = "Hello" };
|
||||
var responseBuilder = Response.Create().WithBodyAsJson(requestMessage => Task.FromResult(responseBody));
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsJson.Should().BeEquivalentTo(responseBody);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithJsonBodyAndTransform()
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -279,4 +280,38 @@ public class ResponseWithCallbackTests
|
||||
response.Message.BodyData.BodyAsString.Should().Be("/fooBar");
|
||||
response.Message.StatusCode.Should().Be(302);
|
||||
}
|
||||
|
||||
// https://github.com/WireMock-Net/WireMock.Net/issues/898
|
||||
[Fact]
|
||||
public async Task Response_WithCallback_WithRequestHeaders_Should_BeAccessibleInCallback()
|
||||
{
|
||||
// Assign
|
||||
var headerKey = "headerKey";
|
||||
var headerValues = new[] { "abc" };
|
||||
var requestHeaders = new Dictionary<string, string[]>
|
||||
{
|
||||
{ headerKey, headerValues }
|
||||
};
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, requestHeaders);
|
||||
var responseBuilder = Response.Create()
|
||||
.WithCallback(request =>
|
||||
{
|
||||
var headersFromRequest = request!.Headers![headerKey].ToList();
|
||||
headersFromRequest.Add("extra");
|
||||
|
||||
return new ResponseMessage
|
||||
{
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
{ headerKey, new WireMockList<string>(headersFromRequest) }
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, requestMessage, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.Message.Headers![headerKey].Should().Contain("extra");
|
||||
}
|
||||
}
|
||||
@@ -163,22 +163,6 @@ public class ResponseWithHandlebarsLinqTests
|
||||
Check.ThatAsyncCode(() => responseBuilder.ProvideResponseAsync(mappingMock.Object, request, _settings)).Throws<ArgumentException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Response_ProvideResponse_Handlebars_Linq1_Throws_ArgumentNullException()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData();
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", "::1", body);
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBodyAsJson(new { x = "{{Linq request.body 'Name'}}" })
|
||||
.WithTransformer();
|
||||
|
||||
// Act
|
||||
Check.ThatAsyncCode(() => responseBuilder.ProvideResponseAsync(mappingMock.Object, request, _settings)).Throws<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Response_ProvideResponse_Handlebars_Linq1_Throws_HandlebarsException()
|
||||
{
|
||||
|
||||
@@ -782,4 +782,31 @@ public class ResponseWithTransformerTests
|
||||
response.Message.BodyData!.BodyAsString.Should().Be(text);
|
||||
response.Message.BodyData.Encoding.Should().Be(enc);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/wiremock-data/1", "one")]
|
||||
[InlineData("/wiremock-data/2", "two")]
|
||||
[InlineData("/wiremock-data/3", "N/A")]
|
||||
public async Task Response_ProvideResponse_Handlebars_DataObject(string path, string expected)
|
||||
{
|
||||
// Arrange
|
||||
var request = new RequestMessage(new UrlDetails("https://localhost" + path), "POST", ClientIp);
|
||||
var data = new Dictionary<string, object?>
|
||||
{
|
||||
{ "1", "one" },
|
||||
{ "2", "two" }
|
||||
};
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{lookup data request.PathSegments.[1] 'N/A'}}")
|
||||
.WithTransformer();
|
||||
|
||||
_mappingMock.SetupGet(m => m.Data).Returns(data);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsString.Should().Be(expected);
|
||||
}
|
||||
}
|
||||
@@ -119,7 +119,25 @@ public partial class MappingConverterTests
|
||||
.WithDelay(12345)
|
||||
.WithTransformer();
|
||||
|
||||
return new Mapping(guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
return new Mapping(
|
||||
guid,
|
||||
_updatedAt,
|
||||
string.Empty,
|
||||
string.Empty,
|
||||
null,
|
||||
_settings,
|
||||
request,
|
||||
response,
|
||||
42,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
false,
|
||||
null,
|
||||
data: null
|
||||
);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -57,7 +57,7 @@ public partial class MappingConverterTests
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -130,7 +130,7 @@ public partial class MappingConverterTests
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -168,7 +168,7 @@ public partial class MappingConverterTests
|
||||
var description = "my-description";
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -188,7 +188,7 @@ public partial class MappingConverterTests
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -217,7 +217,7 @@ public partial class MappingConverterTests
|
||||
End = end,
|
||||
TTL = ttl
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -248,7 +248,7 @@ public partial class MappingConverterTests
|
||||
{
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(test.Delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -266,7 +266,7 @@ public partial class MappingConverterTests
|
||||
var delay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(delay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -286,7 +286,7 @@ public partial class MappingConverterTests
|
||||
int minimumDelay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -309,7 +309,7 @@ public partial class MappingConverterTests
|
||||
int maximumDelay = 2000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay, maximumDelay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#if !(NET452 || NET461 || NETCOREAPP3_1)
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using VerifyTests;
|
||||
using VerifyXunit;
|
||||
@@ -70,7 +71,7 @@ public class ProxyMappingConverterTests
|
||||
var responseMessage = new ResponseMessage();
|
||||
|
||||
// Act
|
||||
var proxyMapping = _sut.ToMapping(mappingMock.Object, proxyAndRecordSettings, requestMessageMock.Object, responseMessage);
|
||||
var proxyMapping = _sut.ToMapping(mappingMock.Object, proxyAndRecordSettings, requestMessageMock.Object, responseMessage)!;
|
||||
|
||||
// Assert
|
||||
var model = _mappingConverter.ToMappingModel(proxyMapping);
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>../../src/WireMock.Net/WireMock.Net.snk</AssemblyOriginatorKeyFile>
|
||||
<!--<DelaySign>true</DelaySign>-->
|
||||
|
||||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
|
||||
|
||||
<!--https://developercommunity.visualstudio.com/content/problem/26347/unit-tests-fail-with-fileloadexception-newtonsoftj-1.html-->
|
||||
@@ -24,6 +24,10 @@
|
||||
<DefineConstants>NETFRAMEWORK</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Util\JsonUtilsTests.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- https://stackoverflow.com/questions/59406201/filenesting-not-working-for-class-or-shared-library-projects -->
|
||||
<ProjectCapability Include="ConfigurableFileNesting" />
|
||||
@@ -59,12 +63,13 @@
|
||||
</PackageReference>
|
||||
|
||||
<PackageReference Include="Moq" Version="4.17.2" />
|
||||
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.1-preview-02" />
|
||||
<PackageReference Include="System.Threading" Version="4.3.0" />
|
||||
<PackageReference Include="RestEase" Version="1.5.7" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
||||
<PackageReference Include="NFluent" Version="2.8.0" />
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.23" />
|
||||
<PackageReference Include="AnyOf" Version="0.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -93,7 +98,8 @@
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' != 'net452'">
|
||||
<PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.3.0" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.4.0" />
|
||||
<!--<PackageReference Include="JsonConverter.NewtonSoft.Json" Version="0.4.0" />-->
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -113,8 +119,8 @@
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="client_cert.pfx">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<DependentUpon>WireMockServerTests.ClientCertificate.cs</DependentUpon>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<DependentUpon>WireMockServerTests.ClientCertificate.cs</DependentUpon>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
@@ -123,7 +129,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Pact\files\" />
|
||||
<Folder Include="Pact\files\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -248,6 +248,45 @@ public class WireMockServerProxyTests
|
||||
fileSystemHandlerMock.Verify(f => f.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Proxy_With_DoNotSaveMappingForHttpMethod_Should_Not_SaveMapping()
|
||||
{
|
||||
// Assign
|
||||
var fileSystemHandlerMock = new Mock<IFileSystemHandler>();
|
||||
fileSystemHandlerMock.Setup(f => f.GetMappingFolder()).Returns("m");
|
||||
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://www.google.com",
|
||||
SaveMapping = true,
|
||||
SaveMappingToFile = true,
|
||||
SaveMappingSettings = new ProxySaveMappingSettings
|
||||
{
|
||||
HttpMethods = new ProxySaveMappingSetting<string[]>(new string[] { "GET" }, MatchBehaviour.RejectOnMatch) // To make sure that we don't want this mapping
|
||||
}
|
||||
},
|
||||
FileSystemHandler = fileSystemHandlerMock.Object
|
||||
};
|
||||
var server = WireMockServer.Start(settings);
|
||||
|
||||
// Act
|
||||
var requestMessage = new HttpRequestMessage
|
||||
{
|
||||
Method = HttpMethod.Get,
|
||||
RequestUri = new Uri(server.Urls[0])
|
||||
};
|
||||
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
|
||||
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
server.Mappings.Should().HaveCount(1);
|
||||
|
||||
// Verify
|
||||
fileSystemHandlerMock.Verify(f => f.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Proxy_Should_log_proxied_requests()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user