mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 06:29:57 +02:00
Update the logic for ProxyAndRecord (#747)
* . * set * . * . * . * . * prio * appsettings * TinyMapperUtils * set * p * nullable * . * , * fs * . * . * --PreferProxyMapping
This commit is contained in:
@@ -13,6 +13,8 @@
|
|||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<RepositoryUrl>https://github.com/WireMock-Net/WireMock.Net</RepositoryUrl>
|
<RepositoryUrl>https://github.com/WireMock-Net/WireMock.Net</RepositoryUrl>
|
||||||
<ApplicationIcon>../../resources/WireMock.Net-Logo.ico</ApplicationIcon>
|
<ApplicationIcon>../../resources/WireMock.Net-Logo.ico</ApplicationIcon>
|
||||||
|
<LangVersion>Latest</LangVersion>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
|
<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
|
||||||
@@ -24,6 +26,10 @@
|
|||||||
<!--<None Include="../../PackageReadme.md" Pack="true" PackagePath=""/>-->
|
<!--<None Include="../../PackageReadme.md" Pack="true" PackagePath=""/>-->
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(TargetFramework)' != 'net45'">
|
||||||
|
<!--<Nullable>enable</Nullable>-->
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<Choose>
|
<Choose>
|
||||||
<!-- The environment variable `Prerelease` is set in the azure-pipelines.yml file. -->
|
<!-- The environment variable `Prerelease` is set in the azure-pipelines.yml file. -->
|
||||||
<When Condition=" '$(Prerelease)' != '' ">
|
<When Condition=" '$(Prerelease)' != '' ">
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Client", "exam
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.NETCoreApp", "examples\WireMock.Net.Console.NETCoreApp\WireMock.Net.Console.NETCoreApp.csproj", "{FE281639-B014-4C8A-96FA-141164A74713}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.NETCoreApp", "examples\WireMock.Net.Console.NETCoreApp\WireMock.Net.Console.NETCoreApp.csproj", "{FE281639-B014-4C8A-96FA-141164A74713}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.Proxy.NETCoreApp", "examples\WireMock.Net.Console.Record.NETCoreApp\WireMock.Net.Console.Proxy.NETCoreApp.csproj", "{1995E414-F197-4AB4-90C2-68D806B5AF59}"
|
|
||||||
EndProject
|
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.StandAlone.NETCoreApp", "examples\WireMock.Net.StandAlone.NETCoreApp\WireMock.Net.StandAlone.NETCoreApp.csproj", "{10E16614-61CA-48D8-8BDD-664C13913DED}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.StandAlone.NETCoreApp", "examples\WireMock.Net.StandAlone.NETCoreApp\WireMock.Net.StandAlone.NETCoreApp.csproj", "{10E16614-61CA-48D8-8BDD-664C13913DED}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.StandAlone.Net452", "examples\WireMock.Net.StandAlone.Net452\WireMock.Net.StandAlone.Net452.csproj", "{668F689E-57B4-422E-8846-C0FF643CA999}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.StandAlone.Net452", "examples\WireMock.Net.StandAlone.Net452\WireMock.Net.StandAlone.Net452.csproj", "{668F689E-57B4-422E-8846-C0FF643CA999}"
|
||||||
@@ -101,6 +99,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.NET6",
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.WebApplication.NET6", "examples\WireMock.Net.WebApplication.NET6\WireMock.Net.WebApplication.NET6.csproj", "{3F7AA023-6833-4856-A08A-4B5717B592B8}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.WebApplication.NET6", "examples\WireMock.Net.WebApplication.NET6\WireMock.Net.WebApplication.NET6.csproj", "{3F7AA023-6833-4856-A08A-4B5717B592B8}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.Proxy.NETCoreApp", "examples\WireMock.Net.Console.Proxy.NETCoreApp\WireMock.Net.Console.Proxy.NETCoreApp.csproj", "{670C7562-C154-442E-A249-7D26849BCD13}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -143,10 +143,6 @@ Global
|
|||||||
{FE281639-B014-4C8A-96FA-141164A74713}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{FE281639-B014-4C8A-96FA-141164A74713}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{FE281639-B014-4C8A-96FA-141164A74713}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{FE281639-B014-4C8A-96FA-141164A74713}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{FE281639-B014-4C8A-96FA-141164A74713}.Release|Any CPU.Build.0 = Release|Any CPU
|
{FE281639-B014-4C8A-96FA-141164A74713}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{1995E414-F197-4AB4-90C2-68D806B5AF59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{1995E414-F197-4AB4-90C2-68D806B5AF59}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{1995E414-F197-4AB4-90C2-68D806B5AF59}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{1995E414-F197-4AB4-90C2-68D806B5AF59}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{10E16614-61CA-48D8-8BDD-664C13913DED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{10E16614-61CA-48D8-8BDD-664C13913DED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{10E16614-61CA-48D8-8BDD-664C13913DED}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{10E16614-61CA-48D8-8BDD-664C13913DED}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{10E16614-61CA-48D8-8BDD-664C13913DED}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{10E16614-61CA-48D8-8BDD-664C13913DED}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
@@ -239,6 +235,10 @@ Global
|
|||||||
{3F7AA023-6833-4856-A08A-4B5717B592B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{3F7AA023-6833-4856-A08A-4B5717B592B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{3F7AA023-6833-4856-A08A-4B5717B592B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{3F7AA023-6833-4856-A08A-4B5717B592B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{3F7AA023-6833-4856-A08A-4B5717B592B8}.Release|Any CPU.Build.0 = Release|Any CPU
|
{3F7AA023-6833-4856-A08A-4B5717B592B8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{670C7562-C154-442E-A249-7D26849BCD13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{670C7562-C154-442E-A249-7D26849BCD13}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{670C7562-C154-442E-A249-7D26849BCD13}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{670C7562-C154-442E-A249-7D26849BCD13}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -253,7 +253,6 @@ Global
|
|||||||
{41C19451-E980-4ED4-A011-DA7A1C23FC05} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
{41C19451-E980-4ED4-A011-DA7A1C23FC05} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
{74D91AD0-D96D-4FD2-AEC5-CC49D38346C0} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
{74D91AD0-D96D-4FD2-AEC5-CC49D38346C0} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
{FE281639-B014-4C8A-96FA-141164A74713} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
{FE281639-B014-4C8A-96FA-141164A74713} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
{1995E414-F197-4AB4-90C2-68D806B5AF59} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
|
||||||
{10E16614-61CA-48D8-8BDD-664C13913DED} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
{10E16614-61CA-48D8-8BDD-664C13913DED} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
{668F689E-57B4-422E-8846-C0FF643CA999} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
{668F689E-57B4-422E-8846-C0FF643CA999} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
{26433A8F-BF01-4962-97EB-81BFFBB61096} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
{26433A8F-BF01-4962-97EB-81BFFBB61096} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
@@ -279,6 +278,7 @@ Global
|
|||||||
{3BA5109E-5F30-4CC2-B699-02EC82560AA6} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
|
{3BA5109E-5F30-4CC2-B699-02EC82560AA6} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
|
||||||
{2215055B-594E-4C2F-99B2-6DF337F02893} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
{2215055B-594E-4C2F-99B2-6DF337F02893} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
{3F7AA023-6833-4856-A08A-4B5717B592B8} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
{3F7AA023-6833-4856-A08A-4B5717B592B8} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
|
{670C7562-C154-442E-A249-7D26849BCD13} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458}
|
SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458}
|
||||||
|
|||||||
@@ -0,0 +1,169 @@
|
|||||||
|
{
|
||||||
|
"Guid": "501d5907-7f73-46dc-a1c6-1a48f39b103a",
|
||||||
|
"Title": "",
|
||||||
|
"Request": {
|
||||||
|
"Path": {
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "/post",
|
||||||
|
"IgnoreCase": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Methods": [
|
||||||
|
"POST"
|
||||||
|
],
|
||||||
|
"Headers": [
|
||||||
|
{
|
||||||
|
"Name": "Accept",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "*/*",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Connection",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "keep-alive",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Host",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "localhost:9091",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "User-Agent",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "PostmanRuntime/7.29.0",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Accept-Encoding",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "gzip, deflate, br",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Authorization",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "BASIC 1234",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Content-Type",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "application/vnd.contoso+json",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Content-Length",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "16",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "postmanecho",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "post",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Test123",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "WildcardMatcher",
|
||||||
|
"Pattern": "application/vnd.contoso+json",
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Body": {
|
||||||
|
"Matcher": {
|
||||||
|
"Name": "JsonMatcher",
|
||||||
|
"Pattern": {
|
||||||
|
"test": 42
|
||||||
|
},
|
||||||
|
"IgnoreCase": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"StatusCode": 200,
|
||||||
|
"BodyAsJson": {
|
||||||
|
"args": {},
|
||||||
|
"data": {
|
||||||
|
"test": "Stef"
|
||||||
|
},
|
||||||
|
"files": {},
|
||||||
|
"form": {},
|
||||||
|
"headers": {
|
||||||
|
"x-forwarded-proto": "http",
|
||||||
|
"x-forwarded-port": "80",
|
||||||
|
"host": "postman-echo.com",
|
||||||
|
"x-amzn-trace-id": "Root=1-624a96e3-023cf5a15966164e7156c20d",
|
||||||
|
"content-length": "11",
|
||||||
|
"accept": "*/*",
|
||||||
|
"user-agent": "PostmanRuntime/7.29.0",
|
||||||
|
"accept-encoding": "gzip, deflate, br",
|
||||||
|
"authorization": "BASIC 1234",
|
||||||
|
"cookie": "sails.sid=s%3AMpD3hNdjm1Ilte_ml7vgmEopThEhwAbr.HDn7RFcO3qK7oVXIxK3Pxk23g1qHDrNZIN4NozK5oog",
|
||||||
|
"postmanecho": "post",
|
||||||
|
"test123": "application/vnd.contoso+json",
|
||||||
|
"postman-token": "050b1136-19ed-4fbe-ad2a-338a20237321",
|
||||||
|
"content-type": "application/vnd.contoso+json"
|
||||||
|
},
|
||||||
|
"json": {
|
||||||
|
"test": "Stef"
|
||||||
|
},
|
||||||
|
"url": "http://postman-echo.com/post"
|
||||||
|
},
|
||||||
|
"Headers": {
|
||||||
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
"Content-Length": "663",
|
||||||
|
"Date": "Mon, 04 Apr 2022 06:57:39 GMT",
|
||||||
|
"Connection": "keep-alive",
|
||||||
|
"ETag": "W/\"297-FimzXopdNUy6DBi49iOW6Tm9q0o\"",
|
||||||
|
"Vary": "Accept-Encoding",
|
||||||
|
"Set-Cookie": "sails.sid=s%3AhtpOsTOJqbHuXmMAZT6cyHLR6FBx1XhV.0loKIsdu5GJKgjJeha16NVWYl%2B0BvPeEQYHcLBCLJ20; Path=/; HttpOnly"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
53
examples/WireMock.Net.Console.Proxy.NETCoreApp/Program.cs
Normal file
53
examples/WireMock.Net.Console.Proxy.NETCoreApp/Program.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using WireMock.Logging;
|
||||||
|
using WireMock.RequestBuilders;
|
||||||
|
using WireMock.ResponseBuilders;
|
||||||
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Console.Proxy.NETCoreApp;
|
||||||
|
|
||||||
|
static class Program
|
||||||
|
{
|
||||||
|
static void Main(params string[] args)
|
||||||
|
{
|
||||||
|
var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
|
{
|
||||||
|
Logger = new WireMockConsoleLogger(),
|
||||||
|
Urls = new[] { "http://localhost:9091/", "https://localhost:9443/" },
|
||||||
|
StartAdminInterface = true,
|
||||||
|
ReadStaticMappings = true,
|
||||||
|
WatchStaticMappings = true,
|
||||||
|
WatchStaticMappingsInSubdirectories = true,
|
||||||
|
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
|
{
|
||||||
|
Url = "http://postman-echo.com/post",
|
||||||
|
SaveMapping = true,
|
||||||
|
SaveMappingToFile = true,
|
||||||
|
ExcludedHeaders = new[] { "Postman-Token" },
|
||||||
|
ExcludedCookies = new[] { "sails.sid" }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//server
|
||||||
|
// .Given(Request.Create().UsingGet())
|
||||||
|
// .RespondWith(Response.Create()
|
||||||
|
// .WithProxy(new ProxyAndRecordSettings
|
||||||
|
// {
|
||||||
|
// Url = "http://postman-echo.com/post",
|
||||||
|
// SaveMapping = true,
|
||||||
|
// SaveMappingToFile = true
|
||||||
|
// }));
|
||||||
|
|
||||||
|
System.Console.WriteLine("Press any key to stop the server");
|
||||||
|
System.Console.ReadKey();
|
||||||
|
server.Stop();
|
||||||
|
|
||||||
|
System.Console.WriteLine("Displaying all requests");
|
||||||
|
var allRequests = server.LogEntries;
|
||||||
|
System.Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented));
|
||||||
|
|
||||||
|
System.Console.WriteLine("Press any key to quit");
|
||||||
|
System.Console.ReadKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<ApplicationIcon>../../resources/WireMock.Net-Logo.ico</ApplicationIcon>
|
<ApplicationIcon>../../resources/WireMock.Net-Logo.ico</ApplicationIcon>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
using WireMock.RequestBuilders;
|
|
||||||
using WireMock.ResponseBuilders;
|
|
||||||
using WireMock.Server;
|
|
||||||
using WireMock.Settings;
|
|
||||||
|
|
||||||
namespace WireMock.Net.Console.Proxy.NETCoreApp
|
|
||||||
{
|
|
||||||
static class Program
|
|
||||||
{
|
|
||||||
static void Main(params string[] args)
|
|
||||||
{
|
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
|
||||||
{
|
|
||||||
Urls = new[] { "http://localhost:9091/", "https://localhost:9443/" },
|
|
||||||
StartAdminInterface = true,
|
|
||||||
ReadStaticMappings = false,
|
|
||||||
//ProxyAndRecordSettings = new ProxyAndRecordSettings
|
|
||||||
//{
|
|
||||||
// Url = "https://www.google.com",
|
|
||||||
// //ClientX509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)",
|
|
||||||
// SaveMapping = true,
|
|
||||||
// SaveMappingToFile = false,
|
|
||||||
// ExcludedHeaders = new [] { "dnt", "Content-Length" }
|
|
||||||
//}
|
|
||||||
});
|
|
||||||
|
|
||||||
server
|
|
||||||
.Given(Request.Create().UsingGet())
|
|
||||||
.RespondWith(Response.Create()
|
|
||||||
.WithProxy(new ProxyAndRecordSettings
|
|
||||||
{
|
|
||||||
Url = "http://postman-echo.com/post",
|
|
||||||
SaveMapping = true,
|
|
||||||
SaveMappingToFile = true
|
|
||||||
}));
|
|
||||||
|
|
||||||
System.Console.WriteLine("Press any key to stop the server");
|
|
||||||
System.Console.ReadKey();
|
|
||||||
server.Stop();
|
|
||||||
|
|
||||||
System.Console.WriteLine("Displaying all requests");
|
|
||||||
var allRequests = server.LogEntries;
|
|
||||||
System.Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented));
|
|
||||||
|
|
||||||
System.Console.WriteLine("Press any key to quit");
|
|
||||||
System.Console.ReadKey();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +1,27 @@
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WireMock.Net.WebApplication
|
namespace WireMock.Net.WebApplication;
|
||||||
|
|
||||||
|
public class App : IHostedService
|
||||||
{
|
{
|
||||||
public class App : IHostedService
|
private readonly IWireMockService _service;
|
||||||
{
|
|
||||||
private readonly IWireMockService _service;
|
|
||||||
|
|
||||||
public App(IWireMockService service)
|
public App(IWireMockService service)
|
||||||
{
|
{
|
||||||
_service = service;
|
_service = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_service.Start();
|
_service.Start();
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_service.Stop();
|
_service.Stop();
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
namespace WireMock.Net.WebApplication
|
namespace WireMock.Net.WebApplication;
|
||||||
{
|
|
||||||
public interface IWireMockService
|
|
||||||
{
|
|
||||||
void Start();
|
|
||||||
|
|
||||||
void Stop();
|
public interface IWireMockService
|
||||||
}
|
{
|
||||||
|
void Start();
|
||||||
|
|
||||||
|
void Stop();
|
||||||
}
|
}
|
||||||
@@ -1,30 +1,29 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.WebApplication
|
namespace WireMock.Net.WebApplication;
|
||||||
|
|
||||||
|
public class Program
|
||||||
{
|
{
|
||||||
public class Program
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
CreateHostBuilder(args).Build().Run();
|
||||||
{
|
}
|
||||||
CreateHostBuilder(args).Build().Run();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IHostBuilder CreateHostBuilder(string[] args)
|
private static IHostBuilder CreateHostBuilder(string[] args)
|
||||||
=> Host.CreateDefaultBuilder(args)
|
=> Host.CreateDefaultBuilder(args)
|
||||||
.ConfigureServices((host, services) => ConfigureServices(services, host.Configuration));
|
.ConfigureServices((host, services) => ConfigureServices(services, host.Configuration));
|
||||||
|
|
||||||
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
|
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
services.AddLogging(logging => logging.AddConsole().AddDebug());
|
services.AddLogging(logging => logging.AddConsole().AddDebug());
|
||||||
|
|
||||||
services.AddTransient<IWireMockService, WireMockService>();
|
services.AddTransient<IWireMockService, WireMockService>();
|
||||||
services.Configure<WireMockServerSettings>(configuration.GetSection("WireMockServerSettings"));
|
services.Configure<WireMockServerSettings>(configuration.GetSection("WireMockServerSettings"));
|
||||||
|
|
||||||
services.AddHostedService<App>();
|
services.AddHostedService<App>();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,83 +1,81 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
using WireMock.Admin.Requests;
|
using WireMock.Admin.Requests;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.WebApplication
|
namespace WireMock.Net.WebApplication;
|
||||||
|
|
||||||
|
public class WireMockService : IWireMockService
|
||||||
{
|
{
|
||||||
public class WireMockService : IWireMockService
|
private WireMockServer? _server;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly WireMockServerSettings _settings;
|
||||||
|
|
||||||
|
private class Logger : IWireMockLogger
|
||||||
{
|
{
|
||||||
private WireMockServer _server;
|
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly WireMockServerSettings _settings;
|
|
||||||
|
|
||||||
private class Logger : IWireMockLogger
|
public Logger(ILogger logger)
|
||||||
{
|
|
||||||
private readonly ILogger _logger;
|
|
||||||
|
|
||||||
public Logger(ILogger logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Debug(string formatString, params object[] args)
|
|
||||||
{
|
|
||||||
_logger.LogDebug(formatString, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Info(string formatString, params object[] args)
|
|
||||||
{
|
|
||||||
_logger.LogInformation(formatString, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Warn(string formatString, params object[] args)
|
|
||||||
{
|
|
||||||
_logger.LogWarning(formatString, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Error(string formatString, params object[] args)
|
|
||||||
{
|
|
||||||
_logger.LogError(formatString, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminrequest)
|
|
||||||
{
|
|
||||||
string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented);
|
|
||||||
_logger.LogDebug("Admin[{0}] {1}", isAdminrequest, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Error(string formatString, Exception exception)
|
|
||||||
{
|
|
||||||
_logger.LogError(formatString, exception.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public WireMockService(ILogger<WireMockService> logger, IOptions<WireMockServerSettings> settings)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_settings = settings.Value;
|
|
||||||
|
|
||||||
_settings.Logger = new Logger(logger);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start()
|
public void Debug(string formatString, params object[] args)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("WireMock.Net server starting");
|
_logger.LogDebug(formatString, args);
|
||||||
|
|
||||||
_server = WireMockServer.Start(_settings);
|
|
||||||
|
|
||||||
_logger.LogInformation($"WireMock.Net server settings {JsonConvert.SerializeObject(_settings)}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public void Info(string formatString, params object[] args)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("WireMock.Net server stopping");
|
_logger.LogInformation(formatString, args);
|
||||||
_server?.Stop();
|
}
|
||||||
|
|
||||||
|
public void Warn(string formatString, params object[] args)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(formatString, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Error(string formatString, params object[] args)
|
||||||
|
{
|
||||||
|
_logger.LogError(formatString, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminrequest)
|
||||||
|
{
|
||||||
|
string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented);
|
||||||
|
_logger.LogDebug("Admin[{0}] {1}", isAdminrequest, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Error(string formatString, Exception exception)
|
||||||
|
{
|
||||||
|
_logger.LogError(formatString, exception.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public WireMockService(ILogger<WireMockService> logger, IOptions<WireMockServerSettings> settings)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_settings = settings.Value;
|
||||||
|
|
||||||
|
_settings.Logger = new Logger(logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("WireMock.Net server starting");
|
||||||
|
|
||||||
|
_server = WireMockServer.Start(_settings);
|
||||||
|
|
||||||
|
_logger.LogInformation($"WireMock.Net server settings {JsonConvert.SerializeObject(_settings)}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Stop()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("WireMock.Net server stopping");
|
||||||
|
_server?.Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,29 +1,29 @@
|
|||||||
{
|
{
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"IncludeScopes": false,
|
"IncludeScopes": false,
|
||||||
"Debug": {
|
"Debug": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Debug"
|
"Default": "Debug"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"Console": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Debug"
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"Console": {
|
"WireMockServerSettings": {
|
||||||
"LogLevel": {
|
"StartAdminInterface": true,
|
||||||
"Default": "Debug"
|
"Urls": [
|
||||||
}
|
"https://localhost:8081/"
|
||||||
|
],
|
||||||
|
"AllowPartialMapping": false,
|
||||||
|
"HandleRequestsSynchronously": true,
|
||||||
|
"ThrowExceptionWhenMatcherFails": true,
|
||||||
|
"ProxyAndRecordSettings": {
|
||||||
|
"Url": "http://postman-echo.com/post",
|
||||||
|
"SaveMapping": true,
|
||||||
|
"SaveMappingToFile": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"WireMockServerSettings": {
|
|
||||||
"StartAdminInterface": true,
|
|
||||||
"Urls": [
|
|
||||||
"https://localhost:8081/"
|
|
||||||
],
|
|
||||||
"AllowPartialMapping": false,
|
|
||||||
"HandleRequestsSynchronously": true,
|
|
||||||
"ThrowExceptionWhenMatcherFails": true,
|
|
||||||
"ProxyAndRecordSettings": {
|
|
||||||
"Url": "https://support.smartbear.com/",
|
|
||||||
"SaveMapping": true,
|
|
||||||
"SaveMappingToFile": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,9 @@
|
|||||||
"iisSettings": {
|
"iisSettings": {
|
||||||
"windowsAuthentication": false,
|
"windowsAuthentication": false,
|
||||||
"anonymousAuthentication": true,
|
"anonymousAuthentication": true,
|
||||||
"iis": {
|
|
||||||
"applicationUrl": "http://localhost//wiremock",
|
|
||||||
"sslPort": 0
|
|
||||||
},
|
|
||||||
"iisExpress": {
|
"iisExpress": {
|
||||||
"applicationUrl": "http://localhost:56513/",
|
"applicationUrl": "http://localhost:60097/",
|
||||||
"sslPort": 0
|
"sslPort": 44321
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"profiles": {
|
"profiles": {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
|
<!--<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>-->
|
||||||
<StartupObject>WireMock.Net.WebApplication.Program</StartupObject>
|
<StartupObject>WireMock.Net.WebApplication.Program</StartupObject>
|
||||||
<AssemblyName>WireMock.Net.WebApplication</AssemblyName>
|
<AssemblyName>WireMock.Net.WebApplication</AssemblyName>
|
||||||
<RootNamespace>WireMock.Net.WebApplication</RootNamespace>
|
<RootNamespace>WireMock.Net.WebApplication</RootNamespace>
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ namespace WireMock.Admin.Mappings
|
|||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The priority.
|
/// The priority. (A low value means higher priority.)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int? Priority { get; set; }
|
public int? Priority { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
|
namespace WireMock.Admin.Settings;
|
||||||
|
|
||||||
|
[FluentBuilder.AutoGenerateBuilder]
|
||||||
|
public class ProxyAndRecordSettingsModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The clientCertificate thumbprint or subject name fragment to use.
|
||||||
|
/// Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
|
||||||
|
/// </summary>
|
||||||
|
public string ClientX509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines the WebProxySettings.
|
||||||
|
/// </summary>
|
||||||
|
public WebProxySettingsModel WebProxySettings { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Proxy requests should follow redirection (30x).
|
||||||
|
/// </summary>
|
||||||
|
public bool? AllowAutoRedirect { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The URL to proxy.
|
||||||
|
/// </summary>
|
||||||
|
public string Url { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Save the mapping for each request/response to the internal Mappings.
|
||||||
|
/// </summary>
|
||||||
|
public bool SaveMapping { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Save the mapping for each request/response also to a file. (Note that SaveMapping must also be set to true.)
|
||||||
|
/// </summary>
|
||||||
|
public bool SaveMappingToFile { get; set; }
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public string SaveMappingForStatusCodePattern { get; set; } = "*";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines a list from headers which will be excluded from the saved mappings.
|
||||||
|
/// </summary>
|
||||||
|
public string[] ExcludedHeaders { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines a list of cookies which will be excluded from the saved mappings.
|
||||||
|
/// </summary>
|
||||||
|
public string[] ExcludedCookies { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
|
||||||
|
/// </summary>
|
||||||
|
// public bool PreferProxyMapping { get; set; }
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
using WireMock.Types;
|
|
||||||
|
|
||||||
namespace WireMock.Admin.Settings
|
namespace WireMock.Admin.Settings
|
||||||
{
|
{
|
||||||
@@ -55,9 +55,29 @@ namespace WireMock.Admin.Settings
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool? SaveUnmatchedRequests { get; set; }
|
public bool? SaveUnmatchedRequests { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets if the static mappings should be read at startup.
|
||||||
|
/// </summary>
|
||||||
|
public bool? ReadStaticMappings { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Watch the static mapping files + folder for changes when running.
|
||||||
|
/// </summary>
|
||||||
|
public bool? WatchStaticMappings { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A value indicating whether subdirectories within the static mappings path should be monitored.
|
||||||
|
/// </summary>
|
||||||
|
public bool? WatchStaticMappingsInSubdirectories { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Policies to use when using CORS. By default CORS is disabled. [Optional]
|
/// Policies to use when using CORS. By default CORS is disabled. [Optional]
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string CorsPolicyOptions { get; set; }
|
public string CorsPolicyOptions { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The proxy and record settings.
|
||||||
|
/// </summary>
|
||||||
|
public ProxyAndRecordSettingsModel ProxyAndRecordSettings { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
namespace WireMock.Admin.Settings
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// WebProxySettings
|
||||||
|
/// </summary>
|
||||||
|
[FluentBuilder.AutoGenerateBuilder]
|
||||||
|
public class WebProxySettingsModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A string instance that contains the address of the proxy server.
|
||||||
|
/// </summary>
|
||||||
|
public string Address { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The user name associated with the credentials.
|
||||||
|
/// </summary>
|
||||||
|
public string UserName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The password for the user name associated with the credentials.
|
||||||
|
/// </summary>
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,4 +41,11 @@
|
|||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!--<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
|
||||||
|
<PackageReference Include="Nullable" Version="1.2.1">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>-->
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
9
src/WireMock.Net/Constants/WireMockConstants.cs
Normal file
9
src/WireMock.Net/Constants/WireMockConstants.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace WireMock.Constants
|
||||||
|
{
|
||||||
|
internal static class WireMockConstants
|
||||||
|
{
|
||||||
|
public const int AdminPriority = int.MinValue;
|
||||||
|
public const int MinPriority = -1_000_000;
|
||||||
|
public const int ProxyPriority = -2_000_000;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,7 +34,7 @@ namespace WireMock
|
|||||||
string Path { get; set; }
|
string Path { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the priority.
|
/// Gets the priority. (A low value means higher priority.)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
int Priority { get; }
|
int Priority { get; }
|
||||||
|
|
||||||
@@ -91,6 +91,14 @@ namespace WireMock
|
|||||||
/// </value>
|
/// </value>
|
||||||
bool IsAdminInterface { get; }
|
bool IsAdminInterface { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether this mapping is a Proxy Mapping.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// <c>true</c> if this mapping is a Proxy Mapping; otherwise, <c>false</c>.
|
||||||
|
/// </value>
|
||||||
|
bool IsProxy { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether this mapping to be logged.
|
/// Gets a value indicating whether this mapping to be logged.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ namespace WireMock
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
|
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool IsProxy => Provider is ProxyAsyncResponseProvider;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool LogMapping => !(Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider);
|
public bool LogMapping => !(Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
@@ -13,13 +14,14 @@ namespace WireMock.Serialization
|
|||||||
|
|
||||||
public MappingToFileSaver(WireMockServerSettings settings, MappingConverter mappingConverter)
|
public MappingToFileSaver(WireMockServerSettings settings, MappingConverter mappingConverter)
|
||||||
{
|
{
|
||||||
Guard.NotNull(settings, nameof(settings));
|
Guard.NotNull(settings);
|
||||||
|
Guard.NotNull(mappingConverter);
|
||||||
|
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
_mappingConverter = mappingConverter;
|
_mappingConverter = mappingConverter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SaveMappingToFile(IMapping mapping, string folder = null)
|
public void SaveMappingToFile(IMapping mapping, [CanBeNull] string folder = null)
|
||||||
{
|
{
|
||||||
if (folder == null)
|
if (folder == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using WireMock.Settings;
|
|||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
|
using WireMock.Constants;
|
||||||
|
|
||||||
namespace WireMock.Server
|
namespace WireMock.Server
|
||||||
{
|
{
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -20,511 +20,512 @@ using WireMock.ResponseProviders;
|
|||||||
using WireMock.Serialization;
|
using WireMock.Serialization;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Server
|
namespace WireMock.Server;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The fluent mock server.
|
||||||
|
/// </summary>
|
||||||
|
public partial class WireMockServer : IWireMockServer
|
||||||
{
|
{
|
||||||
|
private const int ServerStartDelayInMs = 100;
|
||||||
|
|
||||||
|
private readonly WireMockServerSettings _settings;
|
||||||
|
private readonly IOwinSelfHost _httpServer;
|
||||||
|
private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||||
|
private readonly MappingConverter _mappingConverter;
|
||||||
|
private readonly MatcherMapper _matcherMapper;
|
||||||
|
private readonly MappingToFileSaver _mappingToFileSaver;
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.IsStarted" />
|
||||||
|
[PublicAPI]
|
||||||
|
public bool IsStarted => _httpServer != null && _httpServer.IsStarted;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
[PublicAPI]
|
||||||
|
public List<int> Ports { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
[PublicAPI]
|
||||||
|
public int Port => Ports?.FirstOrDefault() ?? default(int);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
[PublicAPI]
|
||||||
|
public string[] Urls { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
[PublicAPI]
|
||||||
|
public string Url => Urls?.FirstOrDefault();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The fluent mock server.
|
/// Gets the mappings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class WireMockServer : IWireMockServer
|
[PublicAPI]
|
||||||
|
public IEnumerable<IMapping> Mappings => _options.Mappings.Values.ToArray();
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.MappingModels" />
|
||||||
|
[PublicAPI]
|
||||||
|
public IEnumerable<MappingModel> MappingModels => ToMappingModels();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the scenarios.
|
||||||
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
|
public ConcurrentDictionary<string, ScenarioState> Scenarios => new ConcurrentDictionary<string, ScenarioState>(_options.Scenarios);
|
||||||
|
|
||||||
|
#region IDisposable Members
|
||||||
|
/// <summary>
|
||||||
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
{
|
{
|
||||||
private const int ServerStartDelayInMs = 100;
|
Dispose(true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
private readonly WireMockServerSettings _settings;
|
/// <summary>
|
||||||
private readonly IOwinSelfHost _httpServer;
|
/// Releases unmanaged and - optionally - managed resources.
|
||||||
private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
/// </summary>
|
||||||
private readonly MappingConverter _mappingConverter;
|
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
||||||
private readonly MatcherMapper _matcherMapper;
|
protected virtual void Dispose(bool disposing)
|
||||||
private readonly MappingToFileSaver _mappingToFileSaver;
|
{
|
||||||
|
DisposeEnhancedFileSystemWatcher();
|
||||||
|
_httpServer?.StopAsync();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.IsStarted" />
|
#region Start/Stop
|
||||||
[PublicAPI]
|
/// <summary>
|
||||||
public bool IsStarted => _httpServer != null && _httpServer.IsStarted;
|
/// Starts this WireMockServer with the specified settings.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="settings">The WireMockServerSettings.</param>
|
||||||
|
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public static WireMockServer Start([NotNull] WireMockServerSettings settings)
|
||||||
|
{
|
||||||
|
Guard.NotNull(settings, nameof(settings));
|
||||||
|
|
||||||
/// <inheritdoc />
|
return new WireMockServer(settings);
|
||||||
[PublicAPI]
|
}
|
||||||
public List<int> Ports { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
[PublicAPI]
|
/// Start this WireMockServer.
|
||||||
public int Port => Ports?.FirstOrDefault() ?? default(int);
|
/// </summary>
|
||||||
|
/// <param name="port">The port.</param>
|
||||||
/// <inheritdoc />
|
/// <param name="ssl">The SSL support.</param>
|
||||||
[PublicAPI]
|
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||||
public string[] Urls { get; }
|
[PublicAPI]
|
||||||
|
public static WireMockServer Start([CanBeNull] int? port = 0, bool ssl = false)
|
||||||
/// <inheritdoc />
|
{
|
||||||
[PublicAPI]
|
return new WireMockServer(new WireMockServerSettings
|
||||||
public string Url => Urls?.FirstOrDefault();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the mappings.
|
|
||||||
/// </summary>
|
|
||||||
[PublicAPI]
|
|
||||||
public IEnumerable<IMapping> Mappings => _options.Mappings.Values.ToArray();
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.MappingModels" />
|
|
||||||
[PublicAPI]
|
|
||||||
public IEnumerable<MappingModel> MappingModels => ToMappingModels();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the scenarios.
|
|
||||||
/// </summary>
|
|
||||||
[PublicAPI]
|
|
||||||
public ConcurrentDictionary<string, ScenarioState> Scenarios => new ConcurrentDictionary<string, ScenarioState>(_options.Scenarios);
|
|
||||||
|
|
||||||
#region IDisposable Members
|
|
||||||
/// <summary>
|
|
||||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
||||||
/// </summary>
|
|
||||||
public void Dispose()
|
|
||||||
{
|
{
|
||||||
Dispose(true);
|
Port = port,
|
||||||
GC.SuppressFinalize(this);
|
UseSSL = ssl
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Start this WireMockServer.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="urls">The urls to listen on.</param>
|
||||||
|
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public static WireMockServer Start(params string[] urls)
|
||||||
|
{
|
||||||
|
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||||
|
|
||||||
|
return new WireMockServer(new WireMockServerSettings
|
||||||
|
{
|
||||||
|
Urls = urls
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Start this WireMockServer with the admin interface.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="port">The port.</param>
|
||||||
|
/// <param name="ssl">The SSL support.</param>
|
||||||
|
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public static WireMockServer StartWithAdminInterface(int? port = 0, bool ssl = false)
|
||||||
|
{
|
||||||
|
return new WireMockServer(new WireMockServerSettings
|
||||||
|
{
|
||||||
|
Port = port,
|
||||||
|
UseSSL = ssl,
|
||||||
|
StartAdminInterface = true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Start this WireMockServer with the admin interface.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="urls">The urls.</param>
|
||||||
|
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public static WireMockServer StartWithAdminInterface(params string[] urls)
|
||||||
|
{
|
||||||
|
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||||
|
|
||||||
|
return new WireMockServer(new WireMockServerSettings
|
||||||
|
{
|
||||||
|
Urls = urls,
|
||||||
|
StartAdminInterface = true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Start this WireMockServer with the admin interface and read static mappings.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="urls">The urls.</param>
|
||||||
|
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public static WireMockServer StartWithAdminInterfaceAndReadStaticMappings(params string[] urls)
|
||||||
|
{
|
||||||
|
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||||
|
|
||||||
|
return new WireMockServer(new WireMockServerSettings
|
||||||
|
{
|
||||||
|
Urls = urls,
|
||||||
|
StartAdminInterface = true,
|
||||||
|
ReadStaticMappings = true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="WireMockServer"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="settings">The settings.</param>
|
||||||
|
/// <exception cref="WireMockException">
|
||||||
|
/// Service start failed with error: {_httpServer.RunningException.Message}
|
||||||
|
/// or
|
||||||
|
/// Service start failed with error: {startTask.Exception.Message}
|
||||||
|
/// </exception>
|
||||||
|
/// <exception cref="TimeoutException">Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}</exception>
|
||||||
|
protected WireMockServer(WireMockServerSettings settings)
|
||||||
|
{
|
||||||
|
_settings = settings;
|
||||||
|
|
||||||
|
// Set default values if not provided
|
||||||
|
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
||||||
|
_settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler();
|
||||||
|
|
||||||
|
_settings.Logger.Info("By Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)");
|
||||||
|
_settings.Logger.Debug("Server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||||
|
|
||||||
|
HostUrlOptions urlOptions;
|
||||||
|
if (settings.Urls != null)
|
||||||
|
{
|
||||||
|
urlOptions = new HostUrlOptions
|
||||||
|
{
|
||||||
|
Urls = settings.Urls
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
urlOptions = new HostUrlOptions
|
||||||
|
{
|
||||||
|
UseSSL = settings.UseSSL == true,
|
||||||
|
Port = settings.Port
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
_options.FileSystemHandler = _settings.FileSystemHandler;
|
||||||
/// Releases unmanaged and - optionally - managed resources.
|
_options.PreWireMockMiddlewareInit = _settings.PreWireMockMiddlewareInit;
|
||||||
/// </summary>
|
_options.PostWireMockMiddlewareInit = _settings.PostWireMockMiddlewareInit;
|
||||||
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
_options.Logger = _settings.Logger;
|
||||||
protected virtual void Dispose(bool disposing)
|
_options.DisableJsonBodyParsing = _settings.DisableJsonBodyParsing;
|
||||||
{
|
_options.HandleRequestsSynchronously = settings.HandleRequestsSynchronously;
|
||||||
DisposeEnhancedFileSystemWatcher();
|
_options.SaveUnmatchedRequests = settings.SaveUnmatchedRequests;
|
||||||
_httpServer?.StopAsync();
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Start/Stop
|
if (settings.CustomCertificateDefined)
|
||||||
/// <summary>
|
|
||||||
/// Starts this WireMockServer with the specified settings.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="settings">The WireMockServerSettings.</param>
|
|
||||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
|
||||||
[PublicAPI]
|
|
||||||
public static WireMockServer Start([NotNull] WireMockServerSettings settings)
|
|
||||||
{
|
{
|
||||||
Guard.NotNull(settings, nameof(settings));
|
_options.X509StoreName = settings.CertificateSettings.X509StoreName;
|
||||||
|
_options.X509StoreLocation = settings.CertificateSettings.X509StoreLocation;
|
||||||
return new WireMockServer(settings);
|
_options.X509ThumbprintOrSubjectName = settings.CertificateSettings.X509StoreThumbprintOrSubjectName;
|
||||||
|
_options.X509CertificateFilePath = settings.CertificateSettings.X509CertificateFilePath;
|
||||||
|
_options.X509CertificatePassword = settings.CertificateSettings.X509CertificatePassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
_matcherMapper = new MatcherMapper(_settings);
|
||||||
/// Start this WireMockServer.
|
_mappingConverter = new MappingConverter(_matcherMapper);
|
||||||
/// </summary>
|
_mappingToFileSaver = new MappingToFileSaver(_settings, _mappingConverter);
|
||||||
/// <param name="port">The port.</param>
|
|
||||||
/// <param name="ssl">The SSL support.</param>
|
|
||||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
|
||||||
[PublicAPI]
|
|
||||||
public static WireMockServer Start([CanBeNull] int? port = 0, bool ssl = false)
|
|
||||||
{
|
|
||||||
return new WireMockServer(new WireMockServerSettings
|
|
||||||
{
|
|
||||||
Port = port,
|
|
||||||
UseSSL = ssl
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Start this WireMockServer.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="urls">The urls to listen on.</param>
|
|
||||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
|
||||||
[PublicAPI]
|
|
||||||
public static WireMockServer Start(params string[] urls)
|
|
||||||
{
|
|
||||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
|
||||||
|
|
||||||
return new WireMockServer(new WireMockServerSettings
|
|
||||||
{
|
|
||||||
Urls = urls
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Start this WireMockServer with the admin interface.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="port">The port.</param>
|
|
||||||
/// <param name="ssl">The SSL support.</param>
|
|
||||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
|
||||||
[PublicAPI]
|
|
||||||
public static WireMockServer StartWithAdminInterface(int? port = 0, bool ssl = false)
|
|
||||||
{
|
|
||||||
return new WireMockServer(new WireMockServerSettings
|
|
||||||
{
|
|
||||||
Port = port,
|
|
||||||
UseSSL = ssl,
|
|
||||||
StartAdminInterface = true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Start this WireMockServer with the admin interface.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="urls">The urls.</param>
|
|
||||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
|
||||||
[PublicAPI]
|
|
||||||
public static WireMockServer StartWithAdminInterface(params string[] urls)
|
|
||||||
{
|
|
||||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
|
||||||
|
|
||||||
return new WireMockServer(new WireMockServerSettings
|
|
||||||
{
|
|
||||||
Urls = urls,
|
|
||||||
StartAdminInterface = true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Start this WireMockServer with the admin interface and read static mappings.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="urls">The urls.</param>
|
|
||||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
|
||||||
[PublicAPI]
|
|
||||||
public static WireMockServer StartWithAdminInterfaceAndReadStaticMappings(params string[] urls)
|
|
||||||
{
|
|
||||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
|
||||||
|
|
||||||
return new WireMockServer(new WireMockServerSettings
|
|
||||||
{
|
|
||||||
Urls = urls,
|
|
||||||
StartAdminInterface = true,
|
|
||||||
ReadStaticMappings = true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="WireMockServer"/> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="settings">The settings.</param>
|
|
||||||
/// <exception cref="WireMockException">
|
|
||||||
/// Service start failed with error: {_httpServer.RunningException.Message}
|
|
||||||
/// or
|
|
||||||
/// Service start failed with error: {startTask.Exception.Message}
|
|
||||||
/// </exception>
|
|
||||||
/// <exception cref="TimeoutException">Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}</exception>
|
|
||||||
protected WireMockServer(WireMockServerSettings settings)
|
|
||||||
{
|
|
||||||
_settings = settings;
|
|
||||||
|
|
||||||
// Set default values if not provided
|
|
||||||
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
|
||||||
_settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler();
|
|
||||||
|
|
||||||
_settings.Logger.Info("By Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)");
|
|
||||||
_settings.Logger.Debug("Server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
|
|
||||||
|
|
||||||
HostUrlOptions urlOptions;
|
|
||||||
if (settings.Urls != null)
|
|
||||||
{
|
|
||||||
urlOptions = new HostUrlOptions
|
|
||||||
{
|
|
||||||
Urls = settings.Urls
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
urlOptions = new HostUrlOptions
|
|
||||||
{
|
|
||||||
UseSSL = settings.UseSSL == true,
|
|
||||||
Port = settings.Port
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
_options.FileSystemHandler = _settings.FileSystemHandler;
|
|
||||||
_options.PreWireMockMiddlewareInit = _settings.PreWireMockMiddlewareInit;
|
|
||||||
_options.PostWireMockMiddlewareInit = _settings.PostWireMockMiddlewareInit;
|
|
||||||
_options.Logger = _settings.Logger;
|
|
||||||
_options.DisableJsonBodyParsing = _settings.DisableJsonBodyParsing;
|
|
||||||
_options.HandleRequestsSynchronously = settings.HandleRequestsSynchronously;
|
|
||||||
_options.SaveUnmatchedRequests = settings.SaveUnmatchedRequests;
|
|
||||||
|
|
||||||
if (settings.CustomCertificateDefined)
|
|
||||||
{
|
|
||||||
_options.X509StoreName = settings.CertificateSettings.X509StoreName;
|
|
||||||
_options.X509StoreLocation = settings.CertificateSettings.X509StoreLocation;
|
|
||||||
_options.X509ThumbprintOrSubjectName = settings.CertificateSettings.X509StoreThumbprintOrSubjectName;
|
|
||||||
_options.X509CertificateFilePath = settings.CertificateSettings.X509CertificateFilePath;
|
|
||||||
_options.X509CertificatePassword = settings.CertificateSettings.X509CertificatePassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
_matcherMapper = new MatcherMapper(_settings);
|
|
||||||
_mappingConverter = new MappingConverter(_matcherMapper);
|
|
||||||
_mappingToFileSaver = new MappingToFileSaver(_settings, _mappingConverter);
|
|
||||||
|
|
||||||
#if USE_ASPNETCORE
|
#if USE_ASPNETCORE
|
||||||
_options.AdditionalServiceRegistration = _settings.AdditionalServiceRegistration;
|
_options.AdditionalServiceRegistration = _settings.AdditionalServiceRegistration;
|
||||||
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
||||||
|
|
||||||
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
||||||
#else
|
#else
|
||||||
_httpServer = new OwinSelfHost(_options, urlOptions);
|
_httpServer = new OwinSelfHost(_options, urlOptions);
|
||||||
#endif
|
#endif
|
||||||
var startTask = _httpServer.StartAsync();
|
var startTask = _httpServer.StartAsync();
|
||||||
|
|
||||||
using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout))
|
using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout))
|
||||||
|
{
|
||||||
|
while (!_httpServer.IsStarted)
|
||||||
{
|
{
|
||||||
while (!_httpServer.IsStarted)
|
// Throw exception if service start fails
|
||||||
|
if (_httpServer.RunningException != null)
|
||||||
{
|
{
|
||||||
// Throw exception if service start fails
|
throw new WireMockException($"Service start failed with error: {_httpServer.RunningException.Message}", _httpServer.RunningException);
|
||||||
if (_httpServer.RunningException != null)
|
}
|
||||||
|
|
||||||
|
if (ctsStartTimeout.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
// In case of an aggregate exception, throw the exception.
|
||||||
|
if (startTask.Exception != null)
|
||||||
{
|
{
|
||||||
throw new WireMockException($"Service start failed with error: {_httpServer.RunningException.Message}", _httpServer.RunningException);
|
throw new WireMockException($"Service start failed with error: {startTask.Exception.Message}", startTask.Exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctsStartTimeout.IsCancellationRequested)
|
// Else throw TimeoutException
|
||||||
{
|
throw new TimeoutException($"Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}");
|
||||||
// In case of an aggregate exception, throw the exception.
|
|
||||||
if (startTask.Exception != null)
|
|
||||||
{
|
|
||||||
throw new WireMockException($"Service start failed with error: {startTask.Exception.Message}", startTask.Exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Else throw TimeoutException
|
|
||||||
throw new TimeoutException($"Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}");
|
|
||||||
}
|
|
||||||
|
|
||||||
ctsStartTimeout.Token.WaitHandle.WaitOne(ServerStartDelayInMs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Urls = _httpServer.Urls.ToArray();
|
ctsStartTimeout.Token.WaitHandle.WaitOne(ServerStartDelayInMs);
|
||||||
Ports = _httpServer.Ports;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings.AllowBodyForAllHttpMethods == true)
|
Urls = _httpServer.Urls.ToArray();
|
||||||
{
|
Ports = _httpServer.Ports;
|
||||||
_options.AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods;
|
|
||||||
_settings.Logger.Info("AllowBodyForAllHttpMethods is set to True");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings.AllowOnlyDefinedHttpStatusCodeInResponse == true)
|
|
||||||
{
|
|
||||||
_options.AllowOnlyDefinedHttpStatusCodeInResponse = _settings.AllowOnlyDefinedHttpStatusCodeInResponse;
|
|
||||||
_settings.Logger.Info("AllowOnlyDefinedHttpStatusCodeInResponse is set to True");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings.AllowPartialMapping == true)
|
|
||||||
{
|
|
||||||
AllowPartialMapping();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings.StartAdminInterface == true)
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(settings.AdminUsername) && !string.IsNullOrEmpty(settings.AdminPassword))
|
|
||||||
{
|
|
||||||
SetBasicAuthentication(settings.AdminUsername, settings.AdminPassword);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(settings.AdminAzureADTenant) && !string.IsNullOrEmpty(settings.AdminAzureADAudience))
|
|
||||||
{
|
|
||||||
SetAzureADAuthentication(settings.AdminAzureADTenant, settings.AdminAzureADAudience);
|
|
||||||
}
|
|
||||||
|
|
||||||
InitAdmin();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings.ReadStaticMappings == true)
|
|
||||||
{
|
|
||||||
ReadStaticMappings();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings.WatchStaticMappings == true)
|
|
||||||
{
|
|
||||||
WatchStaticMappings();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings.ProxyAndRecordSettings != null)
|
|
||||||
{
|
|
||||||
InitProxyAndRecord(settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings.RequestLogExpirationDuration != null)
|
|
||||||
{
|
|
||||||
SetRequestLogExpirationDuration(settings.RequestLogExpirationDuration);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings.MaxRequestLogCount != null)
|
|
||||||
{
|
|
||||||
SetMaxRequestLogCount(settings.MaxRequestLogCount);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.Stop" />
|
InitSettings(settings);
|
||||||
[PublicAPI]
|
}
|
||||||
public void Stop()
|
|
||||||
{
|
|
||||||
var result = _httpServer?.StopAsync();
|
|
||||||
result?.Wait(); // wait for stop to actually happen
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.AddCatchAllMapping" />
|
/// <inheritdoc cref="IWireMockServer.Stop" />
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void AddCatchAllMapping()
|
public void Stop()
|
||||||
|
{
|
||||||
|
var result = _httpServer?.StopAsync();
|
||||||
|
result?.Wait(); // wait for stop to actually happen
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.AddCatchAllMapping" />
|
||||||
|
[PublicAPI]
|
||||||
|
public void AddCatchAllMapping()
|
||||||
|
{
|
||||||
|
Given(Request.Create().WithPath("/*").UsingAnyMethod())
|
||||||
|
.WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05"))
|
||||||
|
.AtPriority(1000)
|
||||||
|
.RespondWith(new DynamicResponseProvider(request => ResponseMessageBuilder.Create("No matching mapping found", 404)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.Reset" />
|
||||||
|
[PublicAPI]
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
ResetLogEntries();
|
||||||
|
|
||||||
|
ResetMappings();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.ResetMappings" />
|
||||||
|
[PublicAPI]
|
||||||
|
public void ResetMappings()
|
||||||
|
{
|
||||||
|
foreach (var nonAdmin in _options.Mappings.ToArray().Where(m => !m.Value.IsAdminInterface))
|
||||||
{
|
{
|
||||||
Given(Request.Create().WithPath("/*").UsingAnyMethod())
|
_options.Mappings.TryRemove(nonAdmin.Key, out _);
|
||||||
.WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05"))
|
}
|
||||||
.AtPriority(1000)
|
}
|
||||||
.RespondWith(new DynamicResponseProvider(request => ResponseMessageBuilder.Create("No matching mapping found", 404)));
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.DeleteMapping" />
|
||||||
|
[PublicAPI]
|
||||||
|
public bool DeleteMapping(Guid guid)
|
||||||
|
{
|
||||||
|
// Check a mapping exists with the same GUID, if so, remove it.
|
||||||
|
if (_options.Mappings.ContainsKey(guid))
|
||||||
|
{
|
||||||
|
return _options.Mappings.TryRemove(guid, out _);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.Reset" />
|
return false;
|
||||||
[PublicAPI]
|
}
|
||||||
public void Reset()
|
|
||||||
{
|
|
||||||
ResetLogEntries();
|
|
||||||
|
|
||||||
ResetMappings();
|
private bool DeleteMapping(string path)
|
||||||
}
|
{
|
||||||
|
// Check a mapping exists with the same path, if so, remove it.
|
||||||
|
var mapping = _options.Mappings.ToArray().FirstOrDefault(entry => string.Equals(entry.Value.Path, path, StringComparison.OrdinalIgnoreCase));
|
||||||
|
return DeleteMapping(mapping.Key);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.ResetMappings" />
|
/// <inheritdoc cref="IWireMockServer.AddGlobalProcessingDelay" />
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void ResetMappings()
|
public void AddGlobalProcessingDelay(TimeSpan delay)
|
||||||
{
|
{
|
||||||
foreach (var nonAdmin in _options.Mappings.ToArray().Where(m => !m.Value.IsAdminInterface))
|
_options.RequestProcessingDelay = delay;
|
||||||
{
|
}
|
||||||
_options.Mappings.TryRemove(nonAdmin.Key, out _);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.DeleteMapping" />
|
/// <inheritdoc cref="IWireMockServer.AllowPartialMapping" />
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public bool DeleteMapping(Guid guid)
|
public void AllowPartialMapping(bool allow = true)
|
||||||
{
|
{
|
||||||
// Check a mapping exists with the same GUID, if so, remove it.
|
_settings.Logger.Info("AllowPartialMapping is set to {0}", allow);
|
||||||
if (_options.Mappings.ContainsKey(guid))
|
_options.AllowPartialMapping = allow;
|
||||||
{
|
}
|
||||||
return _options.Mappings.TryRemove(guid, out _);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
/// <inheritdoc cref="IWireMockServer.SetAzureADAuthentication(string, string)" />
|
||||||
}
|
[PublicAPI]
|
||||||
|
public void SetAzureADAuthentication([NotNull] string tenant, [NotNull] string audience)
|
||||||
private bool DeleteMapping(string path)
|
{
|
||||||
{
|
Guard.NotNull(tenant, nameof(tenant));
|
||||||
// Check a mapping exists with the same path, if so, remove it.
|
Guard.NotNull(audience, nameof(audience));
|
||||||
var mapping = _options.Mappings.ToArray().FirstOrDefault(entry => string.Equals(entry.Value.Path, path, StringComparison.OrdinalIgnoreCase));
|
|
||||||
return DeleteMapping(mapping.Key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.AddGlobalProcessingDelay" />
|
|
||||||
[PublicAPI]
|
|
||||||
public void AddGlobalProcessingDelay(TimeSpan delay)
|
|
||||||
{
|
|
||||||
_options.RequestProcessingDelay = delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.AllowPartialMapping" />
|
|
||||||
[PublicAPI]
|
|
||||||
public void AllowPartialMapping(bool allow = true)
|
|
||||||
{
|
|
||||||
_settings.Logger.Info("AllowPartialMapping is set to {0}", allow);
|
|
||||||
_options.AllowPartialMapping = allow;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.SetAzureADAuthentication(string, string)" />
|
|
||||||
[PublicAPI]
|
|
||||||
public void SetAzureADAuthentication([NotNull] string tenant, [NotNull] string audience)
|
|
||||||
{
|
|
||||||
Guard.NotNull(tenant, nameof(tenant));
|
|
||||||
Guard.NotNull(audience, nameof(audience));
|
|
||||||
|
|
||||||
#if NETSTANDARD1_3
|
#if NETSTANDARD1_3
|
||||||
throw new NotSupportedException("AzureADAuthentication is not supported for NETStandard 1.3");
|
throw new NotSupportedException("AzureADAuthentication is not supported for NETStandard 1.3");
|
||||||
#else
|
#else
|
||||||
_options.AuthenticationMatcher = new AzureADAuthenticationMatcher(tenant, audience);
|
_options.AuthenticationMatcher = new AzureADAuthenticationMatcher(tenant, audience);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.SetBasicAuthentication(string, string)" />
|
||||||
|
[PublicAPI]
|
||||||
|
public void SetBasicAuthentication([NotNull] string username, [NotNull] string password)
|
||||||
|
{
|
||||||
|
Guard.NotNull(username, nameof(username));
|
||||||
|
Guard.NotNull(password, nameof(password));
|
||||||
|
|
||||||
|
_options.AuthenticationMatcher = new BasicAuthenticationMatcher(username, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.RemoveAuthentication" />
|
||||||
|
[PublicAPI]
|
||||||
|
public void RemoveAuthentication()
|
||||||
|
{
|
||||||
|
_options.AuthenticationMatcher = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.SetMaxRequestLogCount" />
|
||||||
|
[PublicAPI]
|
||||||
|
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
|
||||||
|
{
|
||||||
|
_options.MaxRequestLogCount = maxRequestLogCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.SetRequestLogExpirationDuration" />
|
||||||
|
[PublicAPI]
|
||||||
|
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
|
||||||
|
{
|
||||||
|
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.ResetScenarios" />
|
||||||
|
[PublicAPI]
|
||||||
|
public void ResetScenarios()
|
||||||
|
{
|
||||||
|
_options.Scenarios.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockServer.WithMapping(MappingModel[])" />
|
||||||
|
[PublicAPI]
|
||||||
|
public IWireMockServer WithMapping(params MappingModel[] mappings)
|
||||||
|
{
|
||||||
|
foreach (var mapping in mappings)
|
||||||
|
{
|
||||||
|
ConvertMappingAndRegisterAsRespondProvider(mapping, mapping.Guid ?? Guid.NewGuid());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.SetBasicAuthentication(string, string)" />
|
return this;
|
||||||
[PublicAPI]
|
}
|
||||||
public void SetBasicAuthentication([NotNull] string username, [NotNull] string password)
|
|
||||||
{
|
|
||||||
Guard.NotNull(username, nameof(username));
|
|
||||||
Guard.NotNull(password, nameof(password));
|
|
||||||
|
|
||||||
_options.AuthenticationMatcher = new BasicAuthenticationMatcher(username, password);
|
/// <inheritdoc cref="IWireMockServer.WithMapping(string)" />
|
||||||
|
[PublicAPI]
|
||||||
|
public IWireMockServer WithMapping(string mappings)
|
||||||
|
{
|
||||||
|
var mappingModels = DeserializeJsonToArray<MappingModel>(mappings);
|
||||||
|
foreach (var mappingModel in mappingModels)
|
||||||
|
{
|
||||||
|
ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.RemoveAuthentication" />
|
return this;
|
||||||
[PublicAPI]
|
}
|
||||||
public void RemoveAuthentication()
|
|
||||||
|
/// <summary>
|
||||||
|
/// The given.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="requestMatcher">The request matcher.</param>
|
||||||
|
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
|
||||||
|
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
|
||||||
|
{
|
||||||
|
return new RespondWithAProvider(RegisterMapping, requestMatcher, _settings, saveToFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RegisterMapping(IMapping mapping, bool saveToFile)
|
||||||
|
{
|
||||||
|
// Check a mapping exists with the same Guid, if so, replace it.
|
||||||
|
if (_options.Mappings.ContainsKey(mapping.Guid))
|
||||||
{
|
{
|
||||||
_options.AuthenticationMatcher = null;
|
_options.Mappings[mapping.Guid] = mapping;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_options.Mappings.TryAdd(mapping.Guid, mapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.SetMaxRequestLogCount" />
|
if (saveToFile)
|
||||||
[PublicAPI]
|
|
||||||
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
|
|
||||||
{
|
{
|
||||||
_options.MaxRequestLogCount = maxRequestLogCount;
|
_mappingToFileSaver.SaveMappingToFile(mapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitSettings(WireMockServerSettings settings)
|
||||||
|
{
|
||||||
|
if (settings.AllowBodyForAllHttpMethods == true)
|
||||||
|
{
|
||||||
|
_options.AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods;
|
||||||
|
_settings.Logger.Info("AllowBodyForAllHttpMethods is set to True");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.SetRequestLogExpirationDuration" />
|
if (settings.AllowOnlyDefinedHttpStatusCodeInResponse == true)
|
||||||
[PublicAPI]
|
|
||||||
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
|
|
||||||
{
|
{
|
||||||
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
|
_options.AllowOnlyDefinedHttpStatusCodeInResponse = _settings.AllowOnlyDefinedHttpStatusCodeInResponse;
|
||||||
|
_settings.Logger.Info("AllowOnlyDefinedHttpStatusCodeInResponse is set to True");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.ResetScenarios" />
|
if (settings.AllowPartialMapping == true)
|
||||||
[PublicAPI]
|
|
||||||
public void ResetScenarios()
|
|
||||||
{
|
{
|
||||||
_options.Scenarios.Clear();
|
AllowPartialMapping();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.WithMapping(MappingModel[])" />
|
if (settings.StartAdminInterface == true)
|
||||||
[PublicAPI]
|
|
||||||
public IWireMockServer WithMapping(params MappingModel[] mappings)
|
|
||||||
{
|
{
|
||||||
foreach (var mapping in mappings)
|
if (!string.IsNullOrEmpty(settings.AdminUsername) && !string.IsNullOrEmpty(settings.AdminPassword))
|
||||||
{
|
{
|
||||||
ConvertMappingAndRegisterAsRespondProvider(mapping, mapping.Guid ?? Guid.NewGuid());
|
SetBasicAuthentication(settings.AdminUsername, settings.AdminPassword);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
if (!string.IsNullOrEmpty(settings.AdminAzureADTenant) && !string.IsNullOrEmpty(settings.AdminAzureADAudience))
|
||||||
|
{
|
||||||
|
SetAzureADAuthentication(settings.AdminAzureADTenant, settings.AdminAzureADAudience);
|
||||||
|
}
|
||||||
|
|
||||||
|
InitAdmin();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IWireMockServer.WithMapping(string)" />
|
if (settings.ReadStaticMappings == true)
|
||||||
[PublicAPI]
|
|
||||||
public IWireMockServer WithMapping(string mappings)
|
|
||||||
{
|
{
|
||||||
var mappingModels = DeserializeJsonToArray<MappingModel>(mappings);
|
ReadStaticMappings();
|
||||||
foreach (var mappingModel in mappingModels)
|
|
||||||
{
|
|
||||||
ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid());
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
if (settings.WatchStaticMappings == true)
|
||||||
/// The given.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="requestMatcher">The request matcher.</param>
|
|
||||||
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
|
|
||||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
|
||||||
[PublicAPI]
|
|
||||||
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
|
|
||||||
{
|
{
|
||||||
return new RespondWithAProvider(RegisterMapping, requestMatcher, _settings, saveToFile);
|
WatchStaticMappings();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RegisterMapping(IMapping mapping, bool saveToFile)
|
InitProxyAndRecord(settings);
|
||||||
{
|
|
||||||
// Check a mapping exists with the same Guid, if so, replace it.
|
|
||||||
if (_options.Mappings.ContainsKey(mapping.Guid))
|
|
||||||
{
|
|
||||||
_options.Mappings[mapping.Guid] = mapping;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_options.Mappings.TryAdd(mapping.Guid, mapping);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (saveToFile)
|
if (settings.RequestLogExpirationDuration != null)
|
||||||
{
|
{
|
||||||
_mappingToFileSaver.SaveMappingToFile(mapping);
|
SetRequestLogExpirationDuration(settings.RequestLogExpirationDuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (settings.MaxRequestLogCount != null)
|
||||||
|
{
|
||||||
|
SetMaxRequestLogCount(settings.MaxRequestLogCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,5 +43,11 @@ namespace WireMock.Settings
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public string[] ExcludedCookies { get; set; }
|
public string[] ExcludedCookies { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
|
||||||
|
/// </summary>
|
||||||
|
//[PublicAPI]
|
||||||
|
//public bool PreferProxyMapping { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
@@ -35,34 +34,33 @@ namespace WireMock.Settings
|
|||||||
|
|
||||||
settings = new WireMockServerSettings
|
settings = new WireMockServerSettings
|
||||||
{
|
{
|
||||||
StartAdminInterface = parser.GetBoolValue("StartAdminInterface", true),
|
|
||||||
ReadStaticMappings = parser.GetBoolValue("ReadStaticMappings"),
|
|
||||||
WatchStaticMappings = parser.GetBoolValue("WatchStaticMappings"),
|
|
||||||
AllowPartialMapping = parser.GetBoolValue("AllowPartialMapping"),
|
|
||||||
WatchStaticMappingsInSubdirectories = parser.GetBoolValue("WatchStaticMappingsInSubdirectories"),
|
|
||||||
AdminUsername = parser.GetStringValue("AdminUsername"),
|
|
||||||
AdminPassword = parser.GetStringValue("AdminPassword"),
|
|
||||||
AdminAzureADTenant = parser.GetStringValue(nameof(WireMockServerSettings.AdminAzureADTenant)),
|
|
||||||
AdminAzureADAudience = parser.GetStringValue(nameof(WireMockServerSettings.AdminAzureADAudience)),
|
AdminAzureADAudience = parser.GetStringValue(nameof(WireMockServerSettings.AdminAzureADAudience)),
|
||||||
MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"),
|
AdminAzureADTenant = parser.GetStringValue(nameof(WireMockServerSettings.AdminAzureADTenant)),
|
||||||
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
AdminPassword = parser.GetStringValue("AdminPassword"),
|
||||||
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
|
AdminUsername = parser.GetStringValue("AdminUsername"),
|
||||||
AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods"),
|
AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods"),
|
||||||
|
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
|
||||||
AllowOnlyDefinedHttpStatusCodeInResponse = parser.GetBoolValue("AllowOnlyDefinedHttpStatusCodeInResponse"),
|
AllowOnlyDefinedHttpStatusCodeInResponse = parser.GetBoolValue("AllowOnlyDefinedHttpStatusCodeInResponse"),
|
||||||
|
AllowPartialMapping = parser.GetBoolValue("AllowPartialMapping"),
|
||||||
DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing"),
|
DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing"),
|
||||||
HandleRequestsSynchronously = parser.GetBoolValue("HandleRequestsSynchronously"),
|
HandleRequestsSynchronously = parser.GetBoolValue("HandleRequestsSynchronously"),
|
||||||
|
MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"),
|
||||||
|
ReadStaticMappings = parser.GetBoolValue("ReadStaticMappings"),
|
||||||
|
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
||||||
|
SaveUnmatchedRequests = parser.GetBoolValue(nameof(WireMockServerSettings.SaveUnmatchedRequests)),
|
||||||
|
StartAdminInterface = parser.GetBoolValue("StartAdminInterface", true),
|
||||||
ThrowExceptionWhenMatcherFails = parser.GetBoolValue("ThrowExceptionWhenMatcherFails"),
|
ThrowExceptionWhenMatcherFails = parser.GetBoolValue("ThrowExceptionWhenMatcherFails"),
|
||||||
UseRegexExtended = parser.GetBoolValue(nameof(WireMockServerSettings.UseRegexExtended), true),
|
UseRegexExtended = parser.GetBoolValue(nameof(WireMockServerSettings.UseRegexExtended), true),
|
||||||
SaveUnmatchedRequests = parser.GetBoolValue(nameof(WireMockServerSettings.SaveUnmatchedRequests)),
|
WatchStaticMappings = parser.GetBoolValue("WatchStaticMappings"),
|
||||||
|
WatchStaticMappingsInSubdirectories = parser.GetBoolValue("WatchStaticMappingsInSubdirectories"),
|
||||||
};
|
};
|
||||||
|
|
||||||
#if USE_ASPNETCORE
|
#if USE_ASPNETCORE
|
||||||
settings.CorsPolicyOptions = parser.GetValue(
|
settings.CorsPolicyOptions = parser.GetValue(nameof(WireMockServerSettings.CorsPolicyOptions), values =>
|
||||||
nameof(WireMockServerSettings.CorsPolicyOptions), values =>
|
{
|
||||||
{
|
var value = string.Join(string.Empty, values);
|
||||||
var value = string.Join(string.Empty, values);
|
return Enum.TryParse<CorsPolicyOptions>(value, true, out var corsPolicyOptions) ? corsPolicyOptions : CorsPolicyOptions.None;
|
||||||
return Enum.TryParse<CorsPolicyOptions>(value, true, out var corsPolicyOptions) ? corsPolicyOptions : CorsPolicyOptions.None;
|
});
|
||||||
});
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (logger != null)
|
if (logger != null)
|
||||||
@@ -75,9 +73,9 @@ namespace WireMock.Settings
|
|||||||
settings.Logger = new WireMockConsoleLogger();
|
settings.Logger = new WireMockConsoleLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parser.Contains("Port"))
|
if (parser.Contains(nameof(WireMockServerSettings.Port)))
|
||||||
{
|
{
|
||||||
settings.Port = parser.GetIntValue("Port");
|
settings.Port = parser.GetIntValue(nameof(WireMockServerSettings.Port));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -89,14 +87,15 @@ namespace WireMock.Settings
|
|||||||
{
|
{
|
||||||
settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
{
|
{
|
||||||
Url = proxyUrl,
|
AllowAutoRedirect = parser.GetBoolValue("AllowAutoRedirect"),
|
||||||
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
|
||||||
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
|
||||||
SaveMappingForStatusCodePattern = parser.GetStringValue("SaveMappingForStatusCodePattern"),
|
|
||||||
ClientX509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("ClientX509Certificate2ThumbprintOrSubjectName"),
|
ClientX509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("ClientX509Certificate2ThumbprintOrSubjectName"),
|
||||||
ExcludedHeaders = parser.GetValues("ExcludedHeaders"),
|
|
||||||
ExcludedCookies = parser.GetValues("ExcludedCookies"),
|
ExcludedCookies = parser.GetValues("ExcludedCookies"),
|
||||||
AllowAutoRedirect = parser.GetBoolValue("AllowAutoRedirect")
|
ExcludedHeaders = parser.GetValues("ExcludedHeaders"),
|
||||||
|
// PreferProxyMapping = parser.GetBoolValue(nameof(ProxyAndRecordSettings.PreferProxyMapping)),
|
||||||
|
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
||||||
|
SaveMappingForStatusCodePattern = parser.GetStringValue("SaveMappingForStatusCodePattern"),
|
||||||
|
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
||||||
|
Url = proxyUrl
|
||||||
};
|
};
|
||||||
|
|
||||||
string proxyAddress = parser.GetStringValue("WebProxyAddress");
|
string proxyAddress = parser.GetStringValue("WebProxyAddress");
|
||||||
|
|||||||
30
src/WireMock.Net/Util/TinyMapperUtils.cs
Normal file
30
src/WireMock.Net/Util/TinyMapperUtils.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
using Nelibur.ObjectMapper;
|
||||||
|
using WireMock.Admin.Settings;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
|
namespace WireMock.Util
|
||||||
|
{
|
||||||
|
internal sealed class TinyMapperUtils
|
||||||
|
{
|
||||||
|
public static TinyMapperUtils Instance { get; } = new TinyMapperUtils();
|
||||||
|
|
||||||
|
private TinyMapperUtils()
|
||||||
|
{
|
||||||
|
TinyMapper.Bind<ProxyAndRecordSettings, ProxyAndRecordSettingsModel>();
|
||||||
|
TinyMapper.Bind<WebProxySettings, WebProxySettingsModel>();
|
||||||
|
|
||||||
|
TinyMapper.Bind<ProxyAndRecordSettingsModel, ProxyAndRecordSettings>();
|
||||||
|
TinyMapper.Bind<WebProxySettingsModel, WebProxySettings>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProxyAndRecordSettingsModel Map(ProxyAndRecordSettings instance)
|
||||||
|
{
|
||||||
|
return instance == null ? null : TinyMapper.Map<ProxyAndRecordSettingsModel>(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProxyAndRecordSettings Map(ProxyAndRecordSettingsModel model)
|
||||||
|
{
|
||||||
|
return model == null ? null : TinyMapper.Map<ProxyAndRecordSettings>(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -51,7 +51,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Remove="Util\FileSystemWatcherExtensions.cs" />
|
<Compile Remove="Util\FileSystemWatcherExtensions.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -64,7 +64,7 @@
|
|||||||
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.14" />
|
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.14" />
|
||||||
<PackageReference Include="JmesPath.Net" Version="1.0.125" />
|
<PackageReference Include="JmesPath.Net" Version="1.0.125" />
|
||||||
<PackageReference Include="AnyOf" Version="0.3.0" />
|
<PackageReference Include="AnyOf" Version="0.3.0" />
|
||||||
<!--<PackageReference Include="TinyMapper" Version="3.0.3" />-->
|
<PackageReference Include="TinyMapper" Version="3.0.3" />
|
||||||
<!--<PackageReference Include="Mapster" Version="7.2.0" />-->
|
<!--<PackageReference Include="Mapster" Version="7.2.0" />-->
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using FluentAssertions;
|
|||||||
using Moq;
|
using Moq;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Authentication;
|
using WireMock.Authentication;
|
||||||
|
using WireMock.Constants;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Owin;
|
using WireMock.Owin;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
@@ -79,13 +80,13 @@ namespace WireMock.Net.Tests
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var mappings = server.Mappings;
|
server.Mappings.Should().NotBeNull();
|
||||||
Check.That(mappings.Count()).IsEqualTo(25);
|
server.Mappings.Should().HaveCount(25);
|
||||||
Check.That(mappings.All(m => m.Priority == int.MinValue)).IsTrue();
|
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WireMockServer_WireMockServerSettings_ProxyAndRecordSettings_ProxyPriority_Is1000_When_StartAdminInterface_IsTrue()
|
public void WireMockServer_WireMockServerSettings_ProxyAndRecordSettings_ProxyPriority_IsMinus2000000_When_StartAdminInterface_IsTrue()
|
||||||
{
|
{
|
||||||
// Assign and Act
|
// Assign and Act
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
@@ -98,10 +99,11 @@ namespace WireMock.Net.Tests
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var mappings = server.Mappings;
|
server.Mappings.Should().NotBeNull();
|
||||||
Check.That(mappings.Count()).IsEqualTo(26);
|
server.Mappings.Should().HaveCount(26);
|
||||||
Check.That(mappings.Count(m => m.Priority == int.MinValue)).IsEqualTo(25);
|
|
||||||
Check.That(mappings.Count(m => m.Priority == 1000)).IsEqualTo(1);
|
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(25);
|
||||||
|
server.Mappings.Count(m => m.Priority == WireMockConstants.ProxyPriority).Should().Be(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
Reference in New Issue
Block a user