mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-05 22:53:22 +02:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4bb378bdce | ||
|
|
f9df0d0ee9 | ||
|
|
f8d3b51fbc | ||
|
|
be9864461d | ||
|
|
4bb7e8af45 | ||
|
|
8bf42904ab | ||
|
|
0a48b40021 | ||
|
|
1962437dcd | ||
|
|
85d61a1877 | ||
|
|
885911203b | ||
|
|
1e591d5f8a | ||
|
|
02b7e3744e | ||
|
|
6e2a4d7e04 | ||
|
|
479bb0b8ec | ||
|
|
a453e00fdb |
4
.github/copilot-instructions.md
vendored
4
.github/copilot-instructions.md
vendored
@@ -1,4 +0,0 @@
|
|||||||
# Copilot Instructions
|
|
||||||
|
|
||||||
## Project Guidelines
|
|
||||||
- All new byte[xx] calls should use using var data = ArrayPool<byte>.Shared.Lease(xx); instead of directly allocating byte arrays
|
|
||||||
15
.github/instructions/instructions.md
vendored
Normal file
15
.github/instructions/instructions.md
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
---
|
||||||
|
description: 'Guidelines for this solution'
|
||||||
|
applyTo: '**/*.csproj, **/*.cs'
|
||||||
|
---
|
||||||
|
|
||||||
|
# Multi .NET Framework Targeting
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
- The main project "WireMock.Net.Minimal" targets `netstandard2.0` and `net8.0`. Ensure that any new code or dependencies are compatible with these frameworks.
|
||||||
|
|
||||||
|
|
||||||
|
# C# Guidelines
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
- When a new ByteArray is needed, do not use `var data = new byte[bufferSize];`. Always use `var data = ArrayPool<byte>.Shared.Lease(bufferSize);`.
|
||||||
22
CHANGELOG.md
22
CHANGELOG.md
@@ -1,3 +1,25 @@
|
|||||||
|
# 2.5.0 (04 May 2026)
|
||||||
|
- [#1451](https://github.com/wiremock/WireMock.Net/pull/1451) - Feature/early mismatch [feature] contributed by [Stepami](https://github.com/Stepami)
|
||||||
|
- [#1452](https://github.com/wiremock/WireMock.Net/pull/1452) - Bump log4net from 2.0.15 to 3.3.0 in example console app [dependencies, .NET] contributed by [dependabot[bot]](https://github.com/apps/dependabot)
|
||||||
|
- [#1453](https://github.com/wiremock/WireMock.Net/pull/1453) - Fix CVE-2026-40021: upgrade log4net to 3.3.0 in examples/WireMock.Net.Service/packages.config [dependencies] contributed by [Copilot](https://github.com/apps/copilot-swe-agent)
|
||||||
|
- [#1442](https://github.com/wiremock/WireMock.Net/issues/1442) - Bug: [grpc] WithBodyAsProtoBuf exception on match [bug]
|
||||||
|
|
||||||
|
# 2.4.0 (24 April 2026)
|
||||||
|
- [#1437](https://github.com/wiremock/WireMock.Net/pull/1437) - Added feature to enable and disable mappings [feature] contributed by [jayaraman-venkatesan](https://github.com/jayaraman-venkatesan)
|
||||||
|
- [#1450](https://github.com/wiremock/WireMock.Net/pull/1450) - Bump OpenTelemetry.Exporter.OpenTelemetryProtocol from 1.14.0 to 1.15.x [dependencies, .NET] contributed by [dependabot[bot]](https://github.com/apps/dependabot)
|
||||||
|
- [#1421](https://github.com/wiremock/WireMock.Net/issues/1421) - Deactivate mapping without deleting it [feature]
|
||||||
|
|
||||||
|
# 2.3.0 (20 April 2026)
|
||||||
|
- [#1436](https://github.com/wiremock/WireMock.Net/pull/1436) - Moving Scenario state change before global response delay is set [feature] contributed by [jayaraman-venkatesan](https://github.com/jayaraman-venkatesan)
|
||||||
|
- [#1440](https://github.com/wiremock/WireMock.Net/pull/1440) - Bump log4net from 2.0.15 to 3.3.0 [dependencies] contributed by [dependabot[bot]](https://github.com/apps/dependabot)
|
||||||
|
- [#1443](https://github.com/wiremock/WireMock.Net/pull/1443) - Fix ExactMatcher and JsonMatcher not working for ISO dates as string [bug] contributed by [StefH](https://github.com/StefH)
|
||||||
|
- [#1444](https://github.com/wiremock/WireMock.Net/pull/1444) - Update instructions.md [refactor] contributed by [StefH](https://github.com/StefH)
|
||||||
|
- [#1448](https://github.com/wiremock/WireMock.Net/pull/1448) - Use DefaultJsonSerializer for BodyAsJson-Response [bug] contributed by [StefH](https://github.com/StefH)
|
||||||
|
- [#1205](https://github.com/wiremock/WireMock.Net/issues/1205) - System.Private.Uri 4.3.0 Blackduck security High vulnerability [bug]
|
||||||
|
- [#1260](https://github.com/wiremock/WireMock.Net/issues/1260) - Scenario state change before a response delay timeout ends [bug]
|
||||||
|
- [#1441](https://github.com/wiremock/WireMock.Net/issues/1441) - ExactMatcher and JsonMatcher not working for ISO dates in v2.2.0 [bug]
|
||||||
|
- [#1446](https://github.com/wiremock/WireMock.Net/issues/1446) - WithBodyAsJson does not return correctly formatted json when using SystemTextJsonConverter [bug]
|
||||||
|
|
||||||
# 2.2.0 (30 March 2026)
|
# 2.2.0 (30 March 2026)
|
||||||
- [#1433](https://github.com/wiremock/WireMock.Net/pull/1433) - Add comments for ScenarioStateStore related code [refactor] contributed by [StefH](https://github.com/StefH)
|
- [#1433](https://github.com/wiremock/WireMock.Net/pull/1433) - Add comments for ScenarioStateStore related code [refactor] contributed by [StefH](https://github.com/StefH)
|
||||||
- [#1434](https://github.com/wiremock/WireMock.Net/pull/1434) - Upgrade Scriban.Signed [security] contributed by [StefH](https://github.com/StefH)
|
- [#1434](https://github.com/wiremock/WireMock.Net/pull/1434) - Upgrade Scriban.Signed [security] contributed by [StefH](https://github.com/StefH)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>2.2.0</VersionPrefix>
|
<VersionPrefix>2.5.0</VersionPrefix>
|
||||||
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
||||||
<PackageProjectUrl>https://github.com/wiremock/WireMock.Net</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/wiremock/WireMock.Net</PackageProjectUrl>
|
||||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
rem https://github.com/StefH/GitHubReleaseNotes
|
rem https://github.com/StefH/GitHubReleaseNotes
|
||||||
|
|
||||||
SET version=2.2.0
|
SET version=2.5.0
|
||||||
|
|
||||||
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels wontfix test question invalid doc duplicate example environment --version %version% --token %GH_TOKEN%
|
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels wontfix test question invalid doc duplicate example environment --version %version% --token %GH_TOKEN%
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
# 2.2.0 (30 March 2026)
|
# 2.5.0 (04 May 2026)
|
||||||
- #1433 Add comments for ScenarioStateStore related code [refactor]
|
- #1451 Feature/early mismatch [feature]
|
||||||
- #1434 Upgrade Scriban.Signed [security]
|
- #1452 Bump log4net from 2.0.15 to 3.3.0 in example console app [dependencies, .NET]
|
||||||
|
- #1453 Fix CVE-2026-40021: upgrade log4net to 3.3.0 in examples/WireMock.Net.Service/packages.config [dependencies]
|
||||||
|
- #1442 Bug: [grpc] WithBodyAsProtoBuf exception on match [bug]
|
||||||
|
|
||||||
The full release notes can be found here: https://github.com/wiremock/WireMock.Net/blob/master/CHANGELOG.md
|
The full release notes can be found here: https://github.com/wiremock/WireMock.Net/blob/master/CHANGELOG.md
|
||||||
@@ -12,11 +12,11 @@
|
|||||||
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.4.0" />
|
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.4.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="8.0.0" />
|
||||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
|
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.15.3" />
|
||||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
|
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.15.3" />
|
||||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
|
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.2" />
|
||||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
|
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.15.1" />
|
||||||
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
|
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.15.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
||||||
<PackageReference Include="log4net" Version="2.0.15" />
|
<PackageReference Include="log4net" Version="3.3.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
||||||
<PackageReference Include="log4net" Version="2.0.15" />
|
<PackageReference Include="log4net" Version="3.3.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.14.0" />
|
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.15.3" />
|
||||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.14.0" />
|
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.15.1" />
|
||||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0" />
|
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.15.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<packages>
|
<packages>
|
||||||
<package id="log4net" version="2.0.15" targetFramework="net48" />
|
<package id="log4net" version="3.3.0" targetFramework="net48" />
|
||||||
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
|
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
|
||||||
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
|
<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.Owin" version="5.2.3" targetFramework="net452" />
|
||||||
|
|||||||
@@ -1,29 +1,18 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace WireMock.Net.WebApplication;
|
namespace WireMock.Net.WebApplication;
|
||||||
|
|
||||||
public class App : IHostedService
|
public class App(IWireMockService service) : IHostedService
|
||||||
{
|
{
|
||||||
private readonly IWireMockService _service;
|
|
||||||
|
|
||||||
public App(IWireMockService 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,5 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.WebApplication;
|
namespace WireMock.Net.WebApplication;
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\src\WireMock.Net.Abstractions\WireMock.Net.Abstractions.csproj" />
|
|
||||||
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
|
||||||
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;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
"WireMockServerSettings": {
|
"WireMockServerSettings": {
|
||||||
"StartAdminInterface": true,
|
"StartAdminInterface": true,
|
||||||
"Urls": [
|
"Urls": [
|
||||||
"http://localhost"
|
"http://localhost:0"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<!--
|
<!--
|
||||||
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
|
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
|
||||||
-->
|
-->
|
||||||
<system.webServer>
|
<system.webServer>
|
||||||
<handlers>
|
<handlers>
|
||||||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
|
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
|
||||||
</handlers>
|
</handlers>
|
||||||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
|
<httpProtocol>
|
||||||
</system.webServer>
|
<customHeaders>
|
||||||
|
<add name="X-Frame-Options" value="SAMEORIGIN" />
|
||||||
|
</customHeaders>
|
||||||
|
</httpProtocol>
|
||||||
|
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
|
||||||
|
</system.webServer>
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -61,6 +61,11 @@ public class MappingModel
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int? TimesInSameState { get; set; }
|
public int? TimesInSameState { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Value to determine if the mapping is disabled. Defaults to <c>null</c> (not disabled).
|
||||||
|
/// </summary>
|
||||||
|
public bool? IsDisabled { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The request model.
|
/// The request model.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
|
|
||||||
namespace WireMock.Admin.Mappings;
|
namespace WireMock.Admin.Mappings;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -66,4 +68,10 @@ public class RequestModel
|
|||||||
/// Gets or sets the body.
|
/// Gets or sets the body.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public BodyModel? Body { get; set; }
|
public BodyModel? Body { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Type of the request matcher to return an immediate mismatch during mapping processing.
|
||||||
|
/// Optional.
|
||||||
|
/// </summary>
|
||||||
|
public RequestMatcherType? EarlyMatcherType { get; set; }
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,11 @@ namespace WireMock.Matchers.Request;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IRequestMatcher
|
public interface IRequestMatcher
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the request matcher's type.
|
||||||
|
/// </summary>
|
||||||
|
public RequestMatcherType Type { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the specified RequestMessage is match.
|
/// Determines whether the specified RequestMessage is match.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
namespace WireMock.Matchers.Request;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of predefined request matcher types.
|
||||||
|
/// </summary>
|
||||||
|
public enum RequestMatcherType
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageBodyMatcher
|
||||||
|
/// </summary>
|
||||||
|
Body = 0,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageBodyMatcher{T}
|
||||||
|
/// </summary>
|
||||||
|
BodyOfT = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageClientIPMatcher
|
||||||
|
/// </summary>
|
||||||
|
ClientIP = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageCookieMatcher
|
||||||
|
/// </summary>
|
||||||
|
Cookie = 3,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageGraphQLMatcher
|
||||||
|
/// </summary>
|
||||||
|
GraphQL = 4,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageHeaderMatcher
|
||||||
|
/// </summary>
|
||||||
|
Header = 5,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageHttpVersionMatcher
|
||||||
|
/// </summary>
|
||||||
|
HttpVersion = 6,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageMethodMatcher
|
||||||
|
/// </summary>
|
||||||
|
Method = 7,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageMultiPartMatcher
|
||||||
|
/// </summary>
|
||||||
|
MultiPart = 8,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageParamMatcher
|
||||||
|
/// </summary>
|
||||||
|
Param = 9,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessagePathMatcher
|
||||||
|
/// </summary>
|
||||||
|
Path = 10,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageProtoBufMatcher
|
||||||
|
/// </summary>
|
||||||
|
ProtoBuf = 11,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageScenarioAndStateMatcher
|
||||||
|
/// </summary>
|
||||||
|
ScenarioAndState = 12,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageUrlMatcher
|
||||||
|
/// </summary>
|
||||||
|
Url = 13,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RequestMessageCompositeMatcher
|
||||||
|
/// </summary>
|
||||||
|
Composite = 14
|
||||||
|
}
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JsonConverter.Abstractions" Version="0.8.0" />
|
<PackageReference Include="JsonConverter.Abstractions" Version="0.9.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -62,6 +62,9 @@ public class Mapping : IMapping
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsProxy => Provider is ProxyAsyncResponseProvider;
|
public bool IsProxy => Provider is ProxyAsyncResponseProvider;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool IsDisabled { get; set; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool LogMapping => Provider is not (DynamicResponseProvider or DynamicAsyncResponseProvider);
|
public bool LogMapping => Provider is not (DynamicResponseProvider or DynamicAsyncResponseProvider);
|
||||||
|
|
||||||
|
|||||||
@@ -142,6 +142,9 @@ public class RequestMessageBodyMatcher : IRequestMatcher
|
|||||||
MatchOperator = matchOperator;
|
MatchOperator = matchOperator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Body;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ public class RequestMessageBodyMatcher<T> : IRequestMatcher
|
|||||||
Func = Guard.NotNull(func);
|
Func = Guard.NotNull(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.BodyOfT;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -74,6 +74,9 @@ public class RequestMessageClientIPMatcher : IRequestMatcher
|
|||||||
Funcs = Guard.NotNull(funcs);
|
Funcs = Guard.NotNull(funcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.ClientIP;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -20,6 +20,11 @@ public abstract class RequestMessageCompositeMatcher : IRequestMatcher
|
|||||||
/// </value>
|
/// </value>
|
||||||
private IEnumerable<IRequestMatcher> RequestMatchers { get; }
|
private IEnumerable<IRequestMatcher> RequestMatchers { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Selected type to choose the matcher from <see cref="RequestMatchers"/> which will immediately return a mismatch.
|
||||||
|
/// </summary>
|
||||||
|
internal RequestMatcherType? EarlyMatcherType { get; private protected set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessageCompositeMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessageCompositeMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -31,6 +36,9 @@ public abstract class RequestMessageCompositeMatcher : IRequestMatcher
|
|||||||
_type = type;
|
_type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Composite;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
@@ -39,6 +47,13 @@ public abstract class RequestMessageCompositeMatcher : IRequestMatcher
|
|||||||
return MatchScores.Mismatch;
|
return MatchScores.Mismatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var earlyMatcher = new RequestMessageEarlyMatcher(EarlyMatcherType, RequestMatchers);
|
||||||
|
var earlyMatchResult = earlyMatcher.GetMatchingScore(requestMessage, requestMatchResult);
|
||||||
|
if (!MatchScores.IsPerfect(earlyMatchResult))
|
||||||
|
{
|
||||||
|
return MatchScores.Mismatch;
|
||||||
|
}
|
||||||
|
|
||||||
if (_type == CompositeMatcherType.And)
|
if (_type == CompositeMatcherType.And)
|
||||||
{
|
{
|
||||||
return RequestMatchers.Average(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult));
|
return RequestMatchers.Average(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult));
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace WireMock.Matchers.Request;
|
namespace WireMock.Matchers.Request;
|
||||||
|
|
||||||
@@ -93,6 +92,9 @@ public class RequestMessageCookieMatcher : IRequestMatcher
|
|||||||
Name = string.Empty; // Not used when Func, but set to a non-null valid value.
|
Name = string.Empty; // Not used when Func, but set to a non-null valid value.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Cookie;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
namespace WireMock.Matchers.Request;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return the mismatch if the matching score of matchers is not perfect.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class RequestMessageEarlyMatcher(
|
||||||
|
RequestMatcherType? earlyMatcherType,
|
||||||
|
IEnumerable<IRequestMatcher> requestMatchers) : IRequestMatcher
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Composite;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
|
{
|
||||||
|
if (earlyMatcherType is null)
|
||||||
|
{
|
||||||
|
return MatchScores.Perfect;
|
||||||
|
}
|
||||||
|
|
||||||
|
var earlyMatchers = requestMatchers
|
||||||
|
.Where(m => m.Type == earlyMatcherType)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (earlyMatchers.Count is 0)
|
||||||
|
{
|
||||||
|
return MatchScores.Perfect;
|
||||||
|
}
|
||||||
|
|
||||||
|
var compositeMatcher = new RequestBuilders.Request(earlyMatchers);
|
||||||
|
return compositeMatcher.GetMatchingScore(requestMessage, requestMatchResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System.Linq;
|
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
|
|
||||||
@@ -12,7 +11,7 @@ namespace WireMock.Matchers.Request;
|
|||||||
/// <inheritdoc cref="IRequestMatcher"/>
|
/// <inheritdoc cref="IRequestMatcher"/>
|
||||||
public class RequestMessageHeaderMatcher : IRequestMatcher
|
public class RequestMessageHeaderMatcher : IRequestMatcher
|
||||||
{
|
{
|
||||||
private const string _name = nameof(RequestMessageCookieMatcher);
|
private const string _name = nameof(RequestMessageHeaderMatcher);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MatchBehaviour
|
/// MatchBehaviour
|
||||||
@@ -106,6 +105,9 @@ public class RequestMessageHeaderMatcher : IRequestMatcher
|
|||||||
Name = string.Empty; // Not used when Func, but set to a non-null valid value.
|
Name = string.Empty; // Not used when Func, but set to a non-null valid value.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Header;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -65,6 +65,9 @@ public class RequestMessageHttpVersionMatcher : IRequestMatcher
|
|||||||
MatcherOnStringFunc = Guard.NotNull(func);
|
MatcherOnStringFunc = Guard.NotNull(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.HttpVersion;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ internal class RequestMessageMethodMatcher : IRequestMatcher
|
|||||||
MatchOperator = matchOperator;
|
MatchOperator = matchOperator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Method;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -55,6 +55,9 @@ public class RequestMessageMultiPartMatcher : IRequestMatcher
|
|||||||
MatchOperator = matchOperator;
|
MatchOperator = matchOperator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.MultiPart;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ public class RequestMessageParamMatcher : IRequestMatcher
|
|||||||
Funcs = Guard.NotNull(funcs);
|
Funcs = Guard.NotNull(funcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Param;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ public class RequestMessagePathMatcher : IRequestMatcher
|
|||||||
Funcs = Guard.NotNull(funcs);
|
Funcs = Guard.NotNull(funcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Path;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ internal class RequestMessageScenarioAndStateMatcher : IRequestMatcher
|
|||||||
_executionConditionState = executionConditionState;
|
_executionConditionState = executionConditionState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.ScenarioAndState;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ public class RequestMessageUrlMatcher : IRequestMatcher
|
|||||||
Funcs = Guard.NotNull(funcs);
|
Funcs = Guard.NotNull(funcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.Url;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
@@ -99,4 +100,12 @@ internal interface IWireMockMiddlewareOptions
|
|||||||
/// WebSocket settings.
|
/// WebSocket settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
WebSocketSettings? WebSocketSettings { get; set; }
|
WebSocketSettings? WebSocketSettings { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the default JSON converter used for serialization.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Set this property to customize how objects are serialized to and deserialized from JSON during mapping.
|
||||||
|
/// </remarks>
|
||||||
|
IJsonConverter DefaultJsonSerializer { get; set; }
|
||||||
}
|
}
|
||||||
@@ -3,249 +3,242 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using RandomDataGenerator.FieldOptions;
|
using RandomDataGenerator.FieldOptions;
|
||||||
using RandomDataGenerator.Randomizers;
|
using RandomDataGenerator.Randomizers;
|
||||||
using Stef.Validation;
|
|
||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.ResponseProviders;
|
using WireMock.ResponseProviders;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.Owin.Mappers
|
namespace WireMock.Owin.Mappers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OwinResponseMapper
|
||||||
|
/// </summary>
|
||||||
|
internal class OwinResponseMapper(IWireMockMiddlewareOptions options) : IOwinResponseMapper
|
||||||
{
|
{
|
||||||
/// <summary>
|
private readonly IRandomizerNumber<double> _randomizerDouble = RandomizerFactory.GetRandomizer(new FieldOptionsDouble { Min = 0, Max = 1 });
|
||||||
/// OwinResponseMapper
|
private readonly IRandomizerBytes _randomizerBytes = RandomizerFactory.GetRandomizer(new FieldOptionsBytes { Min = 100, Max = 200 });
|
||||||
/// </summary>
|
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
||||||
internal class OwinResponseMapper : IOwinResponseMapper
|
|
||||||
|
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
|
||||||
|
private static readonly IDictionary<string, Action<HttpResponse, bool, WireMockList<string>>> ResponseHeadersToFix =
|
||||||
|
new Dictionary<string, Action<HttpResponse, bool, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase)
|
||||||
|
{
|
||||||
|
{ HttpKnownHeaderNames.ContentType, (r, _, v) => r.ContentType = v.FirstOrDefault() },
|
||||||
|
{ HttpKnownHeaderNames.ContentLength, (r, hasBody, v) =>
|
||||||
|
{
|
||||||
|
// Only set the Content-Length header if the response does not have a body
|
||||||
|
if (!hasBody && long.TryParse(v.FirstOrDefault(), out var contentLength))
|
||||||
|
{
|
||||||
|
r.ContentLength = contentLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task MapAsync(IResponseMessage? responseMessage, HttpResponse response)
|
||||||
{
|
{
|
||||||
private readonly IRandomizerNumber<double> _randomizerDouble = RandomizerFactory.GetRandomizer(new FieldOptionsDouble { Min = 0, Max = 1 });
|
if (responseMessage == null || responseMessage is WebSocketHandledResponse)
|
||||||
private readonly IRandomizerBytes _randomizerBytes = RandomizerFactory.GetRandomizer(new FieldOptionsBytes { Min = 100, Max = 200 });
|
|
||||||
private readonly IWireMockMiddlewareOptions _options;
|
|
||||||
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
|
||||||
|
|
||||||
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
|
|
||||||
private static readonly IDictionary<string, Action<HttpResponse, bool, WireMockList<string>>> ResponseHeadersToFix =
|
|
||||||
new Dictionary<string, Action<HttpResponse, bool, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase)
|
|
||||||
{
|
|
||||||
{ HttpKnownHeaderNames.ContentType, (r, _, v) => r.ContentType = v.FirstOrDefault() },
|
|
||||||
{ HttpKnownHeaderNames.ContentLength, (r, hasBody, v) =>
|
|
||||||
{
|
|
||||||
// Only set the Content-Length header if the response does not have a body
|
|
||||||
if (!hasBody && long.TryParse(v.FirstOrDefault(), out var contentLength))
|
|
||||||
{
|
|
||||||
r.ContentLength = contentLength;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Constructor
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="options">The IWireMockMiddlewareOptions.</param>
|
|
||||||
public OwinResponseMapper(IWireMockMiddlewareOptions options)
|
|
||||||
{
|
{
|
||||||
_options = Guard.NotNull(options);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
var bodyData = responseMessage.BodyData;
|
||||||
public async Task MapAsync(IResponseMessage? responseMessage, HttpResponse response)
|
if (bodyData?.GetDetectedBodyType() == BodyType.SseString)
|
||||||
{
|
{
|
||||||
if (responseMessage == null || responseMessage is WebSocketHandledResponse)
|
await HandleSseStringAsync(responseMessage, response, bodyData);
|
||||||
{
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var bodyData = responseMessage.BodyData;
|
byte[]? bytes;
|
||||||
if (bodyData?.GetDetectedBodyType() == BodyType.SseString)
|
switch (responseMessage.FaultType)
|
||||||
{
|
{
|
||||||
await HandleSseStringAsync(responseMessage, response, bodyData);
|
case FaultType.EMPTY_RESPONSE:
|
||||||
return;
|
bytes = IsFault(responseMessage) ? [] : await GetNormalBodyAsync(responseMessage).ConfigureAwait(false);
|
||||||
}
|
break;
|
||||||
|
|
||||||
byte[]? bytes;
|
case FaultType.MALFORMED_RESPONSE_CHUNK:
|
||||||
switch (responseMessage.FaultType)
|
bytes = await GetNormalBodyAsync(responseMessage).ConfigureAwait(false) ?? [];
|
||||||
{
|
if (IsFault(responseMessage))
|
||||||
case FaultType.EMPTY_RESPONSE:
|
|
||||||
bytes = IsFault(responseMessage) ? [] : await GetNormalBodyAsync(responseMessage).ConfigureAwait(false);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FaultType.MALFORMED_RESPONSE_CHUNK:
|
|
||||||
bytes = await GetNormalBodyAsync(responseMessage).ConfigureAwait(false) ?? [];
|
|
||||||
if (IsFault(responseMessage))
|
|
||||||
{
|
|
||||||
bytes = bytes.Take(bytes.Length / 2).Union(_randomizerBytes.Generate()).ToArray();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
bytes = await GetNormalBodyAsync(responseMessage).ConfigureAwait(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (responseMessage.StatusCode is HttpStatusCode or int)
|
|
||||||
{
|
|
||||||
response.StatusCode = MapStatusCode((int) responseMessage.StatusCode);
|
|
||||||
}
|
|
||||||
else if (responseMessage.StatusCode is string statusCodeAsString)
|
|
||||||
{
|
|
||||||
// Note: this case will also match on null
|
|
||||||
_ = int.TryParse(statusCodeAsString, out var statusCodeTypeAsInt);
|
|
||||||
response.StatusCode = MapStatusCode(statusCodeTypeAsInt);
|
|
||||||
}
|
|
||||||
|
|
||||||
SetResponseHeaders(responseMessage, bytes != null, response);
|
|
||||||
|
|
||||||
if (bytes != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
await response.Body.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
bytes = bytes.Take(bytes.Length / 2).Union(_randomizerBytes.Generate()).ToArray();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
bytes = await GetNormalBodyAsync(responseMessage).ConfigureAwait(false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (responseMessage.StatusCode is HttpStatusCode or int)
|
||||||
|
{
|
||||||
|
response.StatusCode = MapStatusCode((int)responseMessage.StatusCode);
|
||||||
|
}
|
||||||
|
else if (responseMessage.StatusCode is string statusCodeAsString)
|
||||||
|
{
|
||||||
|
// Note: this case will also match on null
|
||||||
|
_ = int.TryParse(statusCodeAsString, out var statusCodeTypeAsInt);
|
||||||
|
response.StatusCode = MapStatusCode(statusCodeTypeAsInt);
|
||||||
|
}
|
||||||
|
|
||||||
|
SetResponseHeaders(responseMessage, bytes != null, response);
|
||||||
|
|
||||||
|
if (bytes != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await response.Body.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
options.Logger.Warn("Error writing response body. Exception : {0}", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SetResponseTrailingHeaders(responseMessage, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task HandleSseStringAsync(IResponseMessage responseMessage, HttpResponse response, IBodyData bodyData)
|
||||||
|
{
|
||||||
|
if (bodyData.SseStringQueue == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetResponseHeaders(responseMessage, true, response);
|
||||||
|
|
||||||
|
string? text;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (bodyData.SseStringQueue.TryRead(out text))
|
||||||
|
{
|
||||||
|
await response.WriteAsync(text);
|
||||||
|
await response.Body.FlushAsync();
|
||||||
|
}
|
||||||
|
} while (text != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int MapStatusCode(int code)
|
||||||
|
{
|
||||||
|
if (options.AllowOnlyDefinedHttpStatusCodeInResponse == true && !Enum.IsDefined(typeof(HttpStatusCode), code))
|
||||||
|
{
|
||||||
|
return (int)HttpStatusCode.OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsFault(IResponseMessage responseMessage)
|
||||||
|
{
|
||||||
|
return responseMessage.FaultPercentage == null || _randomizerDouble.Generate() <= responseMessage.FaultPercentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<byte[]?> GetNormalBodyAsync(IResponseMessage responseMessage)
|
||||||
|
{
|
||||||
|
var bodyData = responseMessage.BodyData;
|
||||||
|
switch (bodyData?.GetDetectedBodyType())
|
||||||
|
{
|
||||||
|
case BodyType.String:
|
||||||
|
case BodyType.FormUrlEncoded:
|
||||||
|
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(bodyData.BodyAsString!);
|
||||||
|
|
||||||
|
case BodyType.Json:
|
||||||
|
var jsonConverterOptions = new JsonConverterOptions
|
||||||
{
|
{
|
||||||
_options.Logger.Warn("Error writing response body. Exception : {0}", ex);
|
WriteIndented = bodyData.BodyAsJsonIndented == true,
|
||||||
}
|
IgnoreNullValues = true
|
||||||
}
|
};
|
||||||
|
|
||||||
SetResponseTrailingHeaders(responseMessage, response);
|
var jsonBody = options.DefaultJsonSerializer.Serialize(bodyData.BodyAsJson!, jsonConverterOptions);
|
||||||
}
|
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody);
|
||||||
|
|
||||||
private static async Task HandleSseStringAsync(IResponseMessage responseMessage, HttpResponse response, IBodyData bodyData)
|
case BodyType.ProtoBuf:
|
||||||
{
|
if (TypeLoader.TryLoadStaticInstance<IProtoBufUtils>(out var protoBufUtils))
|
||||||
if (bodyData.SseStringQueue == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SetResponseHeaders(responseMessage, true, response);
|
|
||||||
|
|
||||||
string? text;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
if (bodyData.SseStringQueue.TryRead(out text))
|
|
||||||
{
|
{
|
||||||
await response.WriteAsync(text);
|
var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts;
|
||||||
await response.Body.FlushAsync();
|
return await protoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
} while (text != null);
|
break;
|
||||||
|
|
||||||
|
case BodyType.Bytes:
|
||||||
|
return bodyData.BodyAsBytes;
|
||||||
|
|
||||||
|
case BodyType.File:
|
||||||
|
return options.FileSystemHandler?.ReadResponseBodyAsFile(bodyData.BodyAsFile!);
|
||||||
|
|
||||||
|
case BodyType.MultiPart:
|
||||||
|
options.Logger.Warn("MultiPart body type is not handled!");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BodyType.None:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int MapStatusCode(int code)
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SetResponseHeaders(IResponseMessage responseMessage, bool hasBody, HttpResponse response)
|
||||||
|
{
|
||||||
|
// Force setting the Date header (#577)
|
||||||
|
AppendResponseHeader(
|
||||||
|
response,
|
||||||
|
HttpKnownHeaderNames.Date,
|
||||||
|
[DateTime.UtcNow.ToString(CultureInfo.InvariantCulture.DateTimeFormat.RFC1123Pattern, CultureInfo.InvariantCulture)]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set other headers
|
||||||
|
foreach (var item in responseMessage.Headers!)
|
||||||
{
|
{
|
||||||
if (_options.AllowOnlyDefinedHttpStatusCodeInResponse == true && !Enum.IsDefined(typeof(HttpStatusCode), code))
|
var headerName = item.Key;
|
||||||
|
var value = item.Value;
|
||||||
|
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
|
||||||
{
|
{
|
||||||
return (int)HttpStatusCode.OK;
|
action.Invoke(response, hasBody, value);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsFault(IResponseMessage responseMessage)
|
|
||||||
{
|
|
||||||
return responseMessage.FaultPercentage == null || _randomizerDouble.Generate() <= responseMessage.FaultPercentage;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<byte[]?> GetNormalBodyAsync(IResponseMessage responseMessage)
|
|
||||||
{
|
|
||||||
var bodyData = responseMessage.BodyData;
|
|
||||||
switch (bodyData?.GetDetectedBodyType())
|
|
||||||
{
|
{
|
||||||
case BodyType.String:
|
// Check if this response header can be added (#148, #227 and #720)
|
||||||
case BodyType.FormUrlEncoded:
|
if (!HttpKnownHeaderNames.IsRestrictedResponseHeader(headerName))
|
||||||
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(bodyData.BodyAsString!);
|
|
||||||
|
|
||||||
case BodyType.Json:
|
|
||||||
var formatting = bodyData.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None;
|
|
||||||
var jsonBody = JsonConvert.SerializeObject(bodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore });
|
|
||||||
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody);
|
|
||||||
|
|
||||||
case BodyType.ProtoBuf:
|
|
||||||
if (TypeLoader.TryLoadStaticInstance<IProtoBufUtils>(out var protoBufUtils))
|
|
||||||
{
|
|
||||||
var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts;
|
|
||||||
return await protoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BodyType.Bytes:
|
|
||||||
return bodyData.BodyAsBytes;
|
|
||||||
|
|
||||||
case BodyType.File:
|
|
||||||
return _options.FileSystemHandler?.ReadResponseBodyAsFile(bodyData.BodyAsFile!);
|
|
||||||
|
|
||||||
case BodyType.MultiPart:
|
|
||||||
_options.Logger.Warn("MultiPart body type is not handled!");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BodyType.None:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void SetResponseHeaders(IResponseMessage responseMessage, bool hasBody, HttpResponse response)
|
|
||||||
{
|
|
||||||
// Force setting the Date header (#577)
|
|
||||||
AppendResponseHeader(
|
|
||||||
response,
|
|
||||||
HttpKnownHeaderNames.Date,
|
|
||||||
[DateTime.UtcNow.ToString(CultureInfo.InvariantCulture.DateTimeFormat.RFC1123Pattern, CultureInfo.InvariantCulture)]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Set other headers
|
|
||||||
foreach (var item in responseMessage.Headers!)
|
|
||||||
{
|
|
||||||
var headerName = item.Key;
|
|
||||||
var value = item.Value;
|
|
||||||
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
|
|
||||||
{
|
{
|
||||||
action.Invoke(response, hasBody, value);
|
AppendResponseHeader(response, headerName, value.ToArray());
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Check if this response header can be added (#148, #227 and #720)
|
|
||||||
if (!HttpKnownHeaderNames.IsRestrictedResponseHeader(headerName))
|
|
||||||
{
|
|
||||||
AppendResponseHeader(response, headerName, value.ToArray());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private static void SetResponseTrailingHeaders(IResponseMessage responseMessage, HttpResponse response)
|
|
||||||
{
|
private static void SetResponseTrailingHeaders(IResponseMessage responseMessage, HttpResponse response)
|
||||||
if (responseMessage.TrailingHeaders == null)
|
{
|
||||||
{
|
if (responseMessage.TrailingHeaders == null)
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
#if TRAILINGHEADERS
|
|
||||||
foreach (var (headerName, value) in responseMessage.TrailingHeaders)
|
#if TRAILINGHEADERS
|
||||||
{
|
foreach (var (headerName, value) in responseMessage.TrailingHeaders)
|
||||||
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
|
{
|
||||||
{
|
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
|
||||||
action.Invoke(response, false, value);
|
{
|
||||||
}
|
action.Invoke(response, false, value);
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
// Check if this trailing header can be added to the response
|
{
|
||||||
if (response.SupportsTrailers() && !HttpKnownHeaderNames.IsRestrictedResponseHeader(headerName))
|
// Check if this trailing header can be added to the response
|
||||||
{
|
if (response.SupportsTrailers() && !HttpKnownHeaderNames.IsRestrictedResponseHeader(headerName))
|
||||||
response.AppendTrailer(headerName, new Microsoft.Extensions.Primitives.StringValues(value.ToArray()));
|
{
|
||||||
}
|
response.AppendTrailer(headerName, new Microsoft.Extensions.Primitives.StringValues(value.ToArray()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
}
|
||||||
}
|
#endif
|
||||||
|
}
|
||||||
private static void AppendResponseHeader(HttpResponse response, string headerName, string[] values)
|
|
||||||
{
|
private static void AppendResponseHeader(HttpResponse response, string headerName, string[] values)
|
||||||
response.Headers.Append(headerName, values);
|
{
|
||||||
}
|
response.Headers.Append(headerName, values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,7 @@ internal class MappingMatcher(IWireMockMiddlewareOptions options, IRandomizerDou
|
|||||||
var possibleMappings = new List<MappingMatcherResult>();
|
var possibleMappings = new List<MappingMatcherResult>();
|
||||||
|
|
||||||
var mappings = _options.Mappings.Values
|
var mappings = _options.Mappings.Values
|
||||||
|
.Where(m => !m.IsDisabled)
|
||||||
.Where(m => m.TimeSettings.IsValid())
|
.Where(m => m.TimeSettings.IsValid())
|
||||||
.Where(m => m.Probability is null || _randomizerDoubleBetween0And1.Generate() <= m.Probability)
|
.Where(m => m.Probability is null || _randomizerDoubleBetween0And1.Generate() <= m.Probability)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|||||||
@@ -122,6 +122,14 @@ internal class WireMockMiddleware(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Transition scenario state immediately after matching, before any delay (global or
|
||||||
|
// per-mapping) so that concurrent retries arriving during a delay period see the
|
||||||
|
// updated state and match the correct next mapping instead of re-matching this one.
|
||||||
|
if (targetMapping.Scenario != null)
|
||||||
|
{
|
||||||
|
UpdateScenarioState(targetMapping);
|
||||||
|
}
|
||||||
|
|
||||||
if (!targetMapping.IsAdminInterface && options.RequestProcessingDelay > TimeSpan.Zero)
|
if (!targetMapping.IsAdminInterface && options.RequestProcessingDelay > TimeSpan.Zero)
|
||||||
{
|
{
|
||||||
await Task.Delay(options.RequestProcessingDelay.Value).ConfigureAwait(false);
|
await Task.Delay(options.RequestProcessingDelay.Value).ConfigureAwait(false);
|
||||||
@@ -147,11 +155,6 @@ internal class WireMockMiddleware(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetMapping.Scenario != null)
|
|
||||||
{
|
|
||||||
UpdateScenarioState(targetMapping);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!targetMapping.IsAdminInterface && targetMapping.Webhooks?.Length > 0)
|
if (!targetMapping.IsAdminInterface && targetMapping.Webhooks?.Length > 0)
|
||||||
{
|
{
|
||||||
await SendToWebhooksAsync(targetMapping, request, response).ConfigureAwait(false);
|
await SendToWebhooksAsync(targetMapping, request, response).ConfigureAwait(false);
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
@@ -108,5 +110,9 @@ internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public ConcurrentDictionary<Guid, WebSocketConnectionRegistry> WebSocketRegistries { get; } = new();
|
public ConcurrentDictionary<Guid, WebSocketConnectionRegistry> WebSocketRegistries { get; } = new();
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public WebSocketSettings? WebSocketSettings { get; set; }
|
public WebSocketSettings? WebSocketSettings { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IJsonConverter DefaultJsonSerializer { get; set; } = new NewtonsoftJsonConverter();
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ public partial class Request : RequestMessageCompositeMatcher, IRequestBuilder
|
|||||||
/// Initializes a new instance of the <see cref="Request"/> class.
|
/// Initializes a new instance of the <see cref="Request"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="requestMatchers">The request matchers.</param>
|
/// <param name="requestMatchers">The request matchers.</param>
|
||||||
private Request(IList<IRequestMatcher> requestMatchers) : base(requestMatchers)
|
internal Request(IList<IRequestMatcher> requestMatchers) : base(requestMatchers)
|
||||||
{
|
{
|
||||||
_requestMatchers = Guard.NotNull(requestMatchers);
|
_requestMatchers = Guard.NotNull(requestMatchers);
|
||||||
}
|
}
|
||||||
@@ -81,6 +81,13 @@ public partial class Request : RequestMessageCompositeMatcher, IRequestBuilder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IRequestBuilder WithEarlyMismatch(RequestMatcherType? earlyMatcherType)
|
||||||
|
{
|
||||||
|
EarlyMatcherType = earlyMatcherType;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
internal bool TryGetProtoBufMatcher([NotNullWhen(true)] out IProtoBufMatcher? protoBufMatcher)
|
internal bool TryGetProtoBufMatcher([NotNullWhen(true)] out IProtoBufMatcher? protoBufMatcher)
|
||||||
{
|
{
|
||||||
protoBufMatcher = GetRequestMessageMatcher<RequestMessageProtoBufMatcher>()?.Matcher;
|
protoBufMatcher = GetRequestMessageMatcher<RequestMessageProtoBufMatcher>()?.Matcher;
|
||||||
|
|||||||
@@ -66,6 +66,12 @@ internal class MappingConverter(MatcherMapper mapper)
|
|||||||
|
|
||||||
// Request
|
// Request
|
||||||
sb.AppendLine(" .Given(Request.Create()");
|
sb.AppendLine(" .Given(Request.Create()");
|
||||||
|
|
||||||
|
if (request.EarlyMatcherType != null)
|
||||||
|
{
|
||||||
|
sb.AppendLine($" .WithEarlyMismatch({request.EarlyMatcherType.Value.GetFullyQualifiedEnumValue()})");
|
||||||
|
}
|
||||||
|
|
||||||
sb.AppendLine($" .UsingMethod({To1Or2Or3Arguments(methodMatcher?.MatchBehaviour, methodMatcher?.MatchOperator, methodMatcher?.Methods, HttpRequestMethod.GET)})");
|
sb.AppendLine($" .UsingMethod({To1Or2Or3Arguments(methodMatcher?.MatchBehaviour, methodMatcher?.MatchOperator, methodMatcher?.Methods, HttpRequestMethod.GET)})");
|
||||||
|
|
||||||
if (pathMatcher?.Matchers != null)
|
if (pathMatcher?.Matchers != null)
|
||||||
@@ -275,6 +281,7 @@ internal class MappingConverter(MatcherMapper mapper)
|
|||||||
TimesInSameState = !string.IsNullOrWhiteSpace(mapping.NextState) ? mapping.TimesInSameState : null,
|
TimesInSameState = !string.IsNullOrWhiteSpace(mapping.NextState) ? mapping.TimesInSameState : null,
|
||||||
Data = mapping.Data,
|
Data = mapping.Data,
|
||||||
Probability = mapping.Probability,
|
Probability = mapping.Probability,
|
||||||
|
IsDisabled = mapping.IsDisabled ? true : null,
|
||||||
Request = new RequestModel
|
Request = new RequestModel
|
||||||
{
|
{
|
||||||
Headers = headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
|
Headers = headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
|
||||||
@@ -299,7 +306,9 @@ internal class MappingConverter(MatcherMapper mapper)
|
|||||||
IgnoreCase = pm.IgnoreCase ? true : null,
|
IgnoreCase = pm.IgnoreCase ? true : null,
|
||||||
RejectOnMatch = pm.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : null,
|
RejectOnMatch = pm.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : null,
|
||||||
Matchers = _mapper.Map(pm.Matchers)
|
Matchers = _mapper.Map(pm.Matchers)
|
||||||
}).ToList() : null
|
}).ToList() : null,
|
||||||
|
|
||||||
|
EarlyMatcherType = request.EarlyMatcherType
|
||||||
},
|
},
|
||||||
Response = new ResponseModel()
|
Response = new ResponseModel()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using JsonConverter.Abstractions;
|
using JsonConverter.Abstractions;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
|
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
@@ -10,9 +11,15 @@ namespace WireMock.Serialization;
|
|||||||
|
|
||||||
internal class MappingSerializer(IJsonConverter jsonConverter)
|
internal class MappingSerializer(IJsonConverter jsonConverter)
|
||||||
{
|
{
|
||||||
|
private static readonly JsonConverterOptions JsonConverterOptions = new JsonConverterOptions
|
||||||
|
{
|
||||||
|
DateParseHandling = (int) DateParseHandling.None
|
||||||
|
};
|
||||||
|
|
||||||
internal T[] DeserializeJsonToArray<T>(string value)
|
internal T[] DeserializeJsonToArray<T>(string value)
|
||||||
{
|
{
|
||||||
return DeserializeObjectToArray<T>(jsonConverter.Deserialize<object>(value)!);
|
// DeserializeObject
|
||||||
|
return DeserializeObjectToArray<T>(jsonConverter.Deserialize<object>(value, JsonConverterOptions)!);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static T[] DeserializeObjectToArray<T>(object value)
|
internal static T[] DeserializeObjectToArray<T>(object value)
|
||||||
|
|||||||
@@ -234,6 +234,13 @@ public interface IRespondWithAProvider
|
|||||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||||
IRespondWithAProvider WithProbability(double probability);
|
IRespondWithAProvider WithProbability(double probability);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Define whether this mapping is disabled. Defaults to <c>false</c>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="isDisabled">Whether this mapping is disabled.</param>
|
||||||
|
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||||
|
IRespondWithAProvider WithIsDisabled(bool isDisabled);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Define a Grpc ProtoDefinition which is used for the request and the response.
|
/// Define a Grpc ProtoDefinition which is used for the request and the response.
|
||||||
/// This can be a ProtoDefinition as a string, or an id when the ProtoDefinitions are defined at the WireMockServer.
|
/// This can be a ProtoDefinition as a string, or an id when the ProtoDefinitions are defined at the WireMockServer.
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
|||||||
private int _timesInSameState = 1;
|
private int _timesInSameState = 1;
|
||||||
private bool? _useWebhookFireAndForget;
|
private bool? _useWebhookFireAndForget;
|
||||||
private double? _probability;
|
private double? _probability;
|
||||||
|
private bool _isDisabled = false;
|
||||||
private GraphQLSchemaDetails? _graphQLSchemaDetails; // Future Use.
|
private GraphQLSchemaDetails? _graphQLSchemaDetails; // Future Use.
|
||||||
|
|
||||||
public Guid Guid { get; private set; }
|
public Guid Guid { get; private set; }
|
||||||
@@ -108,6 +109,11 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
|||||||
mapping.WithProbability(_probability.Value);
|
mapping.WithProbability(_probability.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_isDisabled)
|
||||||
|
{
|
||||||
|
mapping.IsDisabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (ProtoDefinition != null)
|
if (ProtoDefinition != null)
|
||||||
{
|
{
|
||||||
mapping.WithProtoDefinition(ProtoDefinition.Value);
|
mapping.WithProtoDefinition(ProtoDefinition.Value);
|
||||||
@@ -354,6 +360,13 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IRespondWithAProvider WithIsDisabled(bool isDisabled)
|
||||||
|
{
|
||||||
|
_isDisabled = isDisabled;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IRespondWithAProvider WithProtoDefinition(params string[] protoDefinitionOrId)
|
public IRespondWithAProvider WithProtoDefinition(params string[] protoDefinitionOrId)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
@@ -26,9 +25,6 @@ using WireMock.Util;
|
|||||||
|
|
||||||
namespace WireMock.Server;
|
namespace WireMock.Server;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The fluent mock server.
|
|
||||||
/// </summary>
|
|
||||||
public partial class WireMockServer
|
public partial class WireMockServer
|
||||||
{
|
{
|
||||||
private const int EnhancedFileSystemWatcherTimeoutMs = 1000;
|
private const int EnhancedFileSystemWatcherTimeoutMs = 1000;
|
||||||
@@ -61,6 +57,8 @@ public partial class WireMockServer
|
|||||||
public string OpenApi => $"{_prefix}/openapi";
|
public string OpenApi => $"{_prefix}/openapi";
|
||||||
|
|
||||||
public RegexMatcher MappingsGuidPathMatcher => new($"^{_prefixEscaped}\\/mappings\\/([0-9A-Fa-f]{{8}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{12}})$");
|
public RegexMatcher MappingsGuidPathMatcher => new($"^{_prefixEscaped}\\/mappings\\/([0-9A-Fa-f]{{8}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{12}})$");
|
||||||
|
public RegexMatcher MappingsGuidEnablePathMatcher => new($"^{_prefixEscaped}\\/mappings\\/([0-9A-Fa-f]{{8}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{12}})\\/enable$");
|
||||||
|
public RegexMatcher MappingsGuidDisablePathMatcher => new($"^{_prefixEscaped}\\/mappings\\/([0-9A-Fa-f]{{8}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{12}})\\/disable$");
|
||||||
public RegexMatcher MappingsCodeGuidPathMatcher => new($"^{_prefixEscaped}\\/mappings\\/code\\/([0-9A-Fa-f]{{8}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{12}})$");
|
public RegexMatcher MappingsCodeGuidPathMatcher => new($"^{_prefixEscaped}\\/mappings\\/code\\/([0-9A-Fa-f]{{8}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{12}})$");
|
||||||
public RegexMatcher RequestsGuidPathMatcher => new($"^{_prefixEscaped}\\/requests\\/([0-9A-Fa-f]{{8}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{12}})$");
|
public RegexMatcher RequestsGuidPathMatcher => new($"^{_prefixEscaped}\\/requests\\/([0-9A-Fa-f]{{8}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{4}}[-][0-9A-Fa-f]{{12}})$");
|
||||||
public RegexMatcher ScenariosNameMatcher => new($"^{_prefixEscaped}\\/scenarios\\/.+$");
|
public RegexMatcher ScenariosNameMatcher => new($"^{_prefixEscaped}\\/scenarios\\/.+$");
|
||||||
@@ -104,6 +102,12 @@ public partial class WireMockServer
|
|||||||
Given(Request.Create().WithPath(_adminPaths.MappingsGuidPathMatcher).UsingPut().WithHeader(HttpKnownHeaderNames.ContentType, AdminRequestContentTypeJson)).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingPut));
|
Given(Request.Create().WithPath(_adminPaths.MappingsGuidPathMatcher).UsingPut().WithHeader(HttpKnownHeaderNames.ContentType, AdminRequestContentTypeJson)).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingPut));
|
||||||
Given(Request.Create().WithPath(_adminPaths.MappingsGuidPathMatcher).UsingDelete()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingDelete));
|
Given(Request.Create().WithPath(_adminPaths.MappingsGuidPathMatcher).UsingDelete()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingDelete));
|
||||||
|
|
||||||
|
// __admin/mappings/{guid}/enable
|
||||||
|
Given(Request.Create().WithPath(_adminPaths.MappingsGuidEnablePathMatcher).UsingPut()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingEnable));
|
||||||
|
|
||||||
|
// __admin/mappings/{guid}/disable
|
||||||
|
Given(Request.Create().WithPath(_adminPaths.MappingsGuidDisablePathMatcher).UsingPut()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingDisable));
|
||||||
|
|
||||||
// __admin/mappings/code/{guid}
|
// __admin/mappings/code/{guid}
|
||||||
Given(Request.Create().WithPath(_adminPaths.MappingsCodeGuidPathMatcher).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingCodeGet));
|
Given(Request.Create().WithPath(_adminPaths.MappingsCodeGuidPathMatcher).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingCodeGet));
|
||||||
|
|
||||||
@@ -430,6 +434,47 @@ public partial class WireMockServer
|
|||||||
var lastPart = requestMessage.Path.Split('/').LastOrDefault();
|
var lastPart = requestMessage.Path.Split('/').LastOrDefault();
|
||||||
return Guid.TryParse(lastPart, out guid);
|
return Guid.TryParse(lastPart, out guid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool TryParseGuidFromSecondToLastSegment(IRequestMessage requestMessage, out Guid guid)
|
||||||
|
{
|
||||||
|
var parts = requestMessage.Path.Split('/');
|
||||||
|
if (parts.Length >= 2 && Guid.TryParse(parts[parts.Length - 2], out guid))
|
||||||
|
return true;
|
||||||
|
guid = Guid.Empty;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IResponseMessage MappingEnable(HttpContext _, IRequestMessage requestMessage)
|
||||||
|
{
|
||||||
|
if (TryParseGuidFromSecondToLastSegment(requestMessage, out var guid))
|
||||||
|
{
|
||||||
|
var mapping = Mappings.FirstOrDefault(m => !m.IsAdminInterface && m.Guid == guid);
|
||||||
|
if (mapping != null)
|
||||||
|
{
|
||||||
|
mapping.IsDisabled = false;
|
||||||
|
return ResponseMessageBuilder.Create(HttpStatusCode.OK, "Mapping enabled", guid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_settings.Logger.Warn("HttpStatusCode set to 404 : Mapping not found");
|
||||||
|
return ResponseMessageBuilder.Create(HttpStatusCode.NotFound, "Mapping not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
private IResponseMessage MappingDisable(HttpContext _, IRequestMessage requestMessage)
|
||||||
|
{
|
||||||
|
if (TryParseGuidFromSecondToLastSegment(requestMessage, out var guid))
|
||||||
|
{
|
||||||
|
var mapping = Mappings.FirstOrDefault(m => !m.IsAdminInterface && m.Guid == guid);
|
||||||
|
if (mapping != null)
|
||||||
|
{
|
||||||
|
mapping.IsDisabled = true;
|
||||||
|
return ResponseMessageBuilder.Create(HttpStatusCode.OK, "Mapping disabled", guid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_settings.Logger.Warn("HttpStatusCode set to 404 : Mapping not found");
|
||||||
|
return ResponseMessageBuilder.Create(HttpStatusCode.NotFound, "Mapping not found");
|
||||||
|
}
|
||||||
#endregion Mapping/{guid}
|
#endregion Mapping/{guid}
|
||||||
|
|
||||||
#region Mappings
|
#region Mappings
|
||||||
|
|||||||
@@ -120,6 +120,11 @@ public partial class WireMockServer
|
|||||||
respondProvider.WithProbability(mappingModel.Probability.Value);
|
respondProvider.WithProbability(mappingModel.Probability.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mappingModel.IsDisabled == true)
|
||||||
|
{
|
||||||
|
respondProvider.WithIsDisabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
// ProtoDefinition is defined at Mapping level
|
// ProtoDefinition is defined at Mapping level
|
||||||
if (mappingModel.ProtoDefinition != null)
|
if (mappingModel.ProtoDefinition != null)
|
||||||
{
|
{
|
||||||
@@ -263,6 +268,8 @@ public partial class WireMockServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
requestBuilder = requestBuilder.WithEarlyMismatch(requestModel.EarlyMatcherType);
|
||||||
|
|
||||||
return requestBuilder;
|
return requestBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -414,6 +414,7 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
||||||
_options.ClientCertificateMode = (Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode)_settings.ClientCertificateMode;
|
_options.ClientCertificateMode = (Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode)_settings.ClientCertificateMode;
|
||||||
_options.AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate;
|
_options.AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate;
|
||||||
|
_options.DefaultJsonSerializer = _settings.DefaultJsonSerializer;
|
||||||
|
|
||||||
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
||||||
var startTask = _httpServer.StartAsync();
|
var startTask = _httpServer.StartAsync();
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
|
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
|
||||||
<PackageReference Include="NUnit" Version="4.4.0" />
|
<PackageReference Include="NUnit" Version="4.4.0" />
|
||||||
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -25,9 +25,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0" />
|
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.15.3" />
|
||||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.14.0" />
|
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.2" />
|
||||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0" />
|
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.15.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -163,6 +163,22 @@ public interface IWireMockAdminApi
|
|||||||
[Header("Content-Type", "application/json")]
|
[Header("Content-Type", "application/json")]
|
||||||
Task<StatusModel> PutMappingAsync([Path] Guid guid, [Body] MappingModel mapping, CancellationToken cancellationToken = default);
|
Task<StatusModel> PutMappingAsync([Path] Guid guid, [Body] MappingModel mapping, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enable a mapping based on the guid.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="guid">The Guid.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||||
|
[Put("mappings/{guid}/enable")]
|
||||||
|
Task<StatusModel> EnableMappingAsync([Path] Guid guid, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Disable a mapping based on the guid.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="guid">The Guid.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||||
|
[Put("mappings/{guid}/disable")]
|
||||||
|
Task<StatusModel> DisableMappingAsync([Path] Guid guid, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete a mapping based on the guid
|
/// Delete a mapping based on the guid
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
|
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
|
||||||
<PackageReference Include="RestEase" Version="1.6.4" />
|
<PackageReference Include="RestEase" Version="1.6.4" />
|
||||||
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -108,6 +108,14 @@ public interface IMapping
|
|||||||
/// </value>
|
/// </value>
|
||||||
bool IsProxy { get; }
|
bool IsProxy { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether this mapping is disabled.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// <c>true</c> if this mapping is disabled; otherwise, <c>false</c>.
|
||||||
|
/// </value>
|
||||||
|
bool IsDisabled { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether this mapping to be logged.
|
/// Gets a value indicating whether this mapping to be logged.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -69,6 +69,9 @@ public class RequestMessageGraphQLMatcher : IRequestMatcher
|
|||||||
MatchOperator = matchOperator;
|
MatchOperator = matchOperator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.GraphQL;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ public class RequestMessageProtoBufMatcher : IRequestMatcher
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public RequestMatcherType Type => RequestMatcherType.ProtoBuf;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ namespace WireMock.RequestBuilders;
|
|||||||
public interface IRequestBuilder : IClientIPRequestBuilder
|
public interface IRequestBuilder : IClientIPRequestBuilder
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a request matcher to the builder.
|
/// Adds a request matcher to the builder.<br/>
|
||||||
|
/// If the request matcher is already present, it will be replaced.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of the request matcher.</typeparam>
|
/// <typeparam name="T">The type of the request matcher.</typeparam>
|
||||||
/// <param name="requestMatcher">The request matcher to add.</param>
|
/// <param name="requestMatcher">The request matcher to add.</param>
|
||||||
@@ -21,4 +22,11 @@ public interface IRequestBuilder : IClientIPRequestBuilder
|
|||||||
/// The link back to the Mapping.
|
/// The link back to the Mapping.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IMapping Mapping { get; set; }
|
IMapping Mapping { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Chooses the <see cref="IRequestMatcher"/> which immediately returns a mismatch during mappings enumeration.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="earlyMatcherType">Selected type to choose the matcher from available list.</param>
|
||||||
|
/// <returns>The current <see cref="IRequestBuilder"/> instance.</returns>
|
||||||
|
IRequestBuilder WithEarlyMismatch(RequestMatcherType? earlyMatcherType);
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
||||||
<PackageReference Include="AnyOf" Version="0.5.0.1" />
|
<PackageReference Include="AnyOf" Version="0.5.0.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
|
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -0,0 +1,134 @@
|
|||||||
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
using RestEase;
|
||||||
|
using WireMock.Admin.Mappings;
|
||||||
|
using WireMock.Client;
|
||||||
|
using WireMock.Server;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests.AdminApi;
|
||||||
|
|
||||||
|
public partial class WireMockAdminApiTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task IWireMockAdminApi_PostMappingAsync_WithIsDisabledTrue_DoesNotMatchRequests()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var ct = TestContext.Current.CancellationToken;
|
||||||
|
using var server = WireMockServer.StartWithAdminInterface();
|
||||||
|
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||||
|
var httpClient = server.CreateClient();
|
||||||
|
|
||||||
|
var model = new MappingModel
|
||||||
|
{
|
||||||
|
Request = new RequestModel { Path = "/foo", Methods = ["GET"] },
|
||||||
|
Response = new ResponseModel { Body = "hello", StatusCode = 200 },
|
||||||
|
IsDisabled = true
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act — POST the disabled mapping
|
||||||
|
var postResult = await api.PostMappingAsync(model, ct);
|
||||||
|
postResult.Should().NotBeNull();
|
||||||
|
|
||||||
|
// Assert — request should not be matched (404)
|
||||||
|
var response = await httpClient.GetAsync("/foo", ct);
|
||||||
|
((int)response.StatusCode).Should().Be(404);
|
||||||
|
|
||||||
|
// Assert — mapping exists but IsDisabled is true
|
||||||
|
server.Mappings.Where(m => !m.IsAdminInterface).Should().ContainSingle(m => m.IsDisabled == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task IWireMockAdminApi_DisableMappingAsync_PreventsMatching()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var ct = TestContext.Current.CancellationToken;
|
||||||
|
using var server = WireMockServer.StartWithAdminInterface();
|
||||||
|
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||||
|
var httpClient = server.CreateClient();
|
||||||
|
|
||||||
|
var model = new MappingModel
|
||||||
|
{
|
||||||
|
Request = new RequestModel { Path = "/bar", Methods = ["GET"] },
|
||||||
|
Response = new ResponseModel { Body = "world", StatusCode = 200 }
|
||||||
|
};
|
||||||
|
var postResult = await api.PostMappingAsync(model, ct);
|
||||||
|
var guid = postResult.Guid!.Value;
|
||||||
|
|
||||||
|
// Assert — mapping matches before disable
|
||||||
|
var before = await httpClient.GetAsync("/bar", ct);
|
||||||
|
((int)before.StatusCode).Should().Be(200);
|
||||||
|
|
||||||
|
// Act — disable
|
||||||
|
var disableResult = await api.DisableMappingAsync(guid, ct);
|
||||||
|
disableResult.Status.Should().Be("Mapping disabled");
|
||||||
|
|
||||||
|
// Assert — no match after disable
|
||||||
|
var after = await httpClient.GetAsync("/bar", ct);
|
||||||
|
((int)after.StatusCode).Should().Be(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task IWireMockAdminApi_EnableMappingAsync_ResumesMatching()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var ct = TestContext.Current.CancellationToken;
|
||||||
|
using var server = WireMockServer.StartWithAdminInterface();
|
||||||
|
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||||
|
var httpClient = server.CreateClient();
|
||||||
|
|
||||||
|
var model = new MappingModel
|
||||||
|
{
|
||||||
|
Request = new RequestModel { Path = "/baz", Methods = ["GET"] },
|
||||||
|
Response = new ResponseModel { Body = "re-enabled", StatusCode = 200 },
|
||||||
|
IsDisabled = true
|
||||||
|
};
|
||||||
|
var postResult = await api.PostMappingAsync(model, ct);
|
||||||
|
var guid = postResult.Guid!.Value;
|
||||||
|
|
||||||
|
// Assert — no match while disabled
|
||||||
|
var before = await httpClient.GetAsync("/baz", ct);
|
||||||
|
((int)before.StatusCode).Should().Be(404);
|
||||||
|
|
||||||
|
// Act — enable
|
||||||
|
var enableResult = await api.EnableMappingAsync(guid, ct);
|
||||||
|
enableResult.Status.Should().Be("Mapping enabled");
|
||||||
|
|
||||||
|
// Assert — mapping matches after enable
|
||||||
|
var after = await httpClient.GetAsync("/baz", ct);
|
||||||
|
((int)after.StatusCode).Should().Be(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task IWireMockAdminApi_GetMappingAsync_ReturnsIsDisabledTrue_WhenDisabled()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var ct = TestContext.Current.CancellationToken;
|
||||||
|
using var server = WireMockServer.StartWithAdminInterface();
|
||||||
|
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||||
|
|
||||||
|
var disabledModel = new MappingModel
|
||||||
|
{
|
||||||
|
Request = new RequestModel { Path = "/check-disabled" },
|
||||||
|
Response = new ResponseModel { Body = "x", StatusCode = 200 },
|
||||||
|
IsDisabled = true
|
||||||
|
};
|
||||||
|
var enabledModel = new MappingModel
|
||||||
|
{
|
||||||
|
Request = new RequestModel { Path = "/check-enabled" },
|
||||||
|
Response = new ResponseModel { Body = "y", StatusCode = 200 }
|
||||||
|
};
|
||||||
|
|
||||||
|
var disabledPost = await api.PostMappingAsync(disabledModel, ct);
|
||||||
|
var enabledPost = await api.PostMappingAsync(enabledModel, ct);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var disabledGot = await api.GetMappingAsync(disabledPost.Guid!.Value, ct);
|
||||||
|
var enabledGot = await api.GetMappingAsync(enabledPost.Guid!.Value, ct);
|
||||||
|
|
||||||
|
// Assert — disabled mapping serializes IsDisabled = true
|
||||||
|
disabledGot.IsDisabled.Should().BeTrue();
|
||||||
|
|
||||||
|
// Assert — enabled mapping omits IsDisabled (null = default not disabled)
|
||||||
|
enabledGot.IsDisabled.Should().BeNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,5 +6,5 @@ internal static class Constants
|
|||||||
{
|
{
|
||||||
internal const int NumStaticMappings = 10;
|
internal const int NumStaticMappings = 10;
|
||||||
|
|
||||||
internal const int NumAdminMappings = 37;
|
internal const int NumAdminMappings = 39;
|
||||||
}
|
}
|
||||||
@@ -8,8 +8,11 @@ using ExampleIntegrationTest.Lookup;
|
|||||||
using Google.Protobuf.WellKnownTypes;
|
using Google.Protobuf.WellKnownTypes;
|
||||||
using Greet;
|
using Greet;
|
||||||
using Grpc.Net.Client;
|
using Grpc.Net.Client;
|
||||||
|
using Moq;
|
||||||
using WireMock.Constants;
|
using WireMock.Constants;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.Net.Xunit;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
@@ -731,6 +734,93 @@ message Other {
|
|||||||
Then_ReplyMessage_Should_BeCorrect(reply);
|
Then_ReplyMessage_Should_BeCorrect(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(true)]
|
||||||
|
[InlineData(false)]
|
||||||
|
public async Task WireMockServer_WithBodyAsProtoBuf_WithEarlyMismatch_ErrorLogs_Issue1442(
|
||||||
|
bool withEarlyMismatch)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var greeterId = $"test-greeter-{Guid.NewGuid()}";
|
||||||
|
var policyId = $"test-policy-{Guid.NewGuid()}";
|
||||||
|
var ct = TestContext.Current.CancellationToken;
|
||||||
|
|
||||||
|
var greeterProtoDefinition = ProtoDefinition;
|
||||||
|
var policyProtoDefinition = File.ReadAllText("./Grpc/policy.proto");
|
||||||
|
|
||||||
|
var mockTestOutputHelper = new Mock<ITestOutputHelper>();
|
||||||
|
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
|
{
|
||||||
|
UseHttp2 = true,
|
||||||
|
Logger = new TestOutputHelperWireMockLogger(mockTestOutputHelper.Object)
|
||||||
|
});
|
||||||
|
|
||||||
|
server
|
||||||
|
.AddProtoDefinition(greeterId, greeterProtoDefinition)
|
||||||
|
.Given(Request.Create()
|
||||||
|
.UsingPost()
|
||||||
|
.WithHttpVersion("2")
|
||||||
|
.WithPath("/greet.Greeter/SayHello")
|
||||||
|
.WithEarlyMismatch(withEarlyMismatch
|
||||||
|
? RequestMatcherType.Path
|
||||||
|
: null)
|
||||||
|
.WithBodyAsProtoBuf("greet.HelloRequest", new JsonMatcher(new { name = "stef" })))
|
||||||
|
.WithProtoDefinition(greeterId)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/grpc")
|
||||||
|
.WithTrailingHeader("grpc-status", "0")
|
||||||
|
.WithBodyAsProtoBuf("greet.HelloReply",
|
||||||
|
new
|
||||||
|
{
|
||||||
|
message = "hello {{request.BodyAsJson.name}} {{request.method}}"
|
||||||
|
})
|
||||||
|
.WithTransformer());
|
||||||
|
|
||||||
|
server
|
||||||
|
.AddProtoDefinition(policyId, policyProtoDefinition)
|
||||||
|
.Given(Request.Create()
|
||||||
|
.UsingPost()
|
||||||
|
.WithHttpVersion("2")
|
||||||
|
.WithPath("/Policy.PolicyService/GetVersion")
|
||||||
|
.WithEarlyMismatch(withEarlyMismatch
|
||||||
|
? RequestMatcherType.Path
|
||||||
|
: null)
|
||||||
|
.WithBodyAsProtoBuf("ExampleIntegrationTest.Lookup.GetVersionRequest", new NotNullOrEmptyMatcher()))
|
||||||
|
.WithProtoDefinition(policyId)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/grpc")
|
||||||
|
.WithTrailingHeader("grpc-status", "0")
|
||||||
|
.WithBodyAsProtoBuf("ExampleIntegrationTest.Lookup.GetVersionResponse",
|
||||||
|
new GetVersionResponse
|
||||||
|
{
|
||||||
|
Version = "test",
|
||||||
|
DateHired = new Timestamp
|
||||||
|
{
|
||||||
|
Seconds = 1722301323,
|
||||||
|
Nanos = 12300
|
||||||
|
},
|
||||||
|
Client = new ExampleIntegrationTest.Lookup.Client
|
||||||
|
{
|
||||||
|
ClientName = ExampleIntegrationTest.Lookup.Client.Types.Clients.Test,
|
||||||
|
CorrelationId = "correlation"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.WithTransformer());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var channel = GrpcChannel.ForAddress(server.Url!);
|
||||||
|
var policyServiceClient = new PolicyService.PolicyServiceClient(channel);
|
||||||
|
var greeterClient = new Greeter.GreeterClient(channel);
|
||||||
|
|
||||||
|
_ = await policyServiceClient.GetVersionAsync(new GetVersionRequest(), cancellationToken: ct);
|
||||||
|
_ = await greeterClient.SayHelloAsync(new HelloRequest { Name = "stef" }, cancellationToken: ct);
|
||||||
|
|
||||||
|
mockTestOutputHelper.Verify(
|
||||||
|
x => x.WriteLine(
|
||||||
|
It.Is<string>(log => log.Contains("[Error]") && log.Contains("Exception"))),
|
||||||
|
withEarlyMismatch ? Times.Never : Times.AtLeastOnce);
|
||||||
|
}
|
||||||
|
|
||||||
private static WireMockServer Given_When_ServerStarted_And_RunningOnHttpAndGrpc()
|
private static WireMockServer Given_When_ServerStarted_And_RunningOnHttpAndGrpc()
|
||||||
{
|
{
|
||||||
var settings = new WireMockServerSettings
|
var settings = new WireMockServerSettings
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ using WireMock.Util;
|
|||||||
using WireMock.Owin;
|
using WireMock.Owin;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
|
using JsonConverter.System.Text.Json;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.Owin.Mappers;
|
namespace WireMock.Net.Tests.Owin.Mappers;
|
||||||
|
|
||||||
@@ -34,6 +36,7 @@ public class OwinResponseMapperTests
|
|||||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||||
_optionsMock.SetupAllProperties();
|
_optionsMock.SetupAllProperties();
|
||||||
_optionsMock.SetupGet(o => o.FileSystemHandler).Returns(_fileSystemHandlerMock.Object);
|
_optionsMock.SetupGet(o => o.FileSystemHandler).Returns(_fileSystemHandlerMock.Object);
|
||||||
|
_optionsMock.SetupGet(o => o.DefaultJsonSerializer).Returns(new NewtonsoftJsonConverter());
|
||||||
|
|
||||||
_headers = new Mock<IHeaderDictionary>();
|
_headers = new Mock<IHeaderDictionary>();
|
||||||
_headers.SetupAllProperties();
|
_headers.SetupAllProperties();
|
||||||
@@ -186,7 +189,7 @@ public class OwinResponseMapperTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_BodyAsJson()
|
public async Task OwinResponseMapper_MapAsync_BodyAsJson_UsingNewtonsoftJson()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var json = new { t = "x", i = (string?)null };
|
var json = new { t = "x", i = (string?)null };
|
||||||
@@ -203,6 +206,25 @@ public class OwinResponseMapperTests
|
|||||||
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task OwinResponseMapper_MapAsync_BodyAsJson_UsingSystemTextJson()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var json = new { t = "x", i = (string?)null };
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
|
{
|
||||||
|
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||||
|
BodyData = new BodyData { DetectedBodyType = BodyType.Json, BodyAsJson = json, BodyAsJsonIndented = false }
|
||||||
|
};
|
||||||
|
_optionsMock.SetupGet(o => o.DefaultJsonSerializer).Returns(new SystemTextJsonConverter());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
|
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ public class MappingMatcherTests
|
|||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var mappingMock = new Mock<IMapping>();
|
var mappingMock = new Mock<IMapping>();
|
||||||
|
mappingMock.SetupGet(m => m.IsDisabled).Returns(false);
|
||||||
mappingMock.Setup(m => m.GetRequestMatchResult(It.IsAny<RequestMessage>(), It.IsAny<string>())).Throws<Exception>();
|
mappingMock.Setup(m => m.GetRequestMatchResult(It.IsAny<RequestMessage>(), It.IsAny<string>())).Throws<Exception>();
|
||||||
|
|
||||||
var mappings = new ConcurrentDictionary<Guid, IMapping>();
|
var mappings = new ConcurrentDictionary<Guid, IMapping>();
|
||||||
@@ -229,6 +230,35 @@ public class MappingMatcherTests
|
|||||||
result.Match!.Mapping.Guid.Should().Be(withProbability);
|
result.Match!.Mapping.Guid.Should().Be(withProbability);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingMatcher_FindBestMatch_WhenMappingIsDisabled_ShouldReturnNull()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var guid = Guid.Parse("00000000-0000-0000-0000-000000000001");
|
||||||
|
var mappingMock = new Mock<IMapping>();
|
||||||
|
mappingMock.SetupGet(m => m.Guid).Returns(guid);
|
||||||
|
mappingMock.SetupGet(m => m.IsDisabled).Returns(true);
|
||||||
|
mappingMock.SetupGet(m => m.Probability).Returns((double?)null);
|
||||||
|
|
||||||
|
var matchResult = new RequestMatchResult();
|
||||||
|
matchResult.AddScore(typeof(object), 1.0, null);
|
||||||
|
mappingMock.Setup(m => m.GetRequestMatchResult(It.IsAny<RequestMessage>(), It.IsAny<string>())).Returns(matchResult);
|
||||||
|
|
||||||
|
var mappings = new ConcurrentDictionary<Guid, IMapping>();
|
||||||
|
mappings.TryAdd(guid, mappingMock.Object);
|
||||||
|
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
|
||||||
|
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _sut.FindBestMatch(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Match.Should().BeNull();
|
||||||
|
result.Partial.Should().BeNull();
|
||||||
|
mappingMock.Verify(m => m.GetRequestMatchResult(It.IsAny<RequestMessage>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
private static ConcurrentDictionary<Guid, IMapping> InitMappings(params (Guid guid, double[] scores, double? probability)[] matches)
|
private static ConcurrentDictionary<Guid, IMapping> InitMappings(params (Guid guid, double[] scores, double? probability)[] matches)
|
||||||
{
|
{
|
||||||
var mappings = new ConcurrentDictionary<Guid, IMapping>();
|
var mappings = new ConcurrentDictionary<Guid, IMapping>();
|
||||||
@@ -237,6 +267,7 @@ public class MappingMatcherTests
|
|||||||
{
|
{
|
||||||
var mappingMock = new Mock<IMapping>();
|
var mappingMock = new Mock<IMapping>();
|
||||||
mappingMock.SetupGet(m => m.Guid).Returns(match.guid);
|
mappingMock.SetupGet(m => m.Guid).Returns(match.guid);
|
||||||
|
mappingMock.SetupGet(m => m.IsDisabled).Returns(false);
|
||||||
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
foreach (var score in match.scores)
|
foreach (var score in match.scores)
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using Moq;
|
using Moq;
|
||||||
|
using WireMock.Constants;
|
||||||
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.Models;
|
using WireMock.Models;
|
||||||
|
using WireMock.RequestBuilders;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.RequestMatchers;
|
namespace WireMock.Net.Tests.RequestMatchers;
|
||||||
|
|
||||||
@@ -10,8 +13,12 @@ public class RequestMessageCompositeMatcherTests
|
|||||||
{
|
{
|
||||||
private class Helper : RequestMessageCompositeMatcher
|
private class Helper : RequestMessageCompositeMatcher
|
||||||
{
|
{
|
||||||
public Helper(IEnumerable<IRequestMatcher> requestMatchers, CompositeMatcherType type = CompositeMatcherType.And) : base(requestMatchers, type)
|
public Helper(
|
||||||
|
IEnumerable<IRequestMatcher> requestMatchers,
|
||||||
|
CompositeMatcherType type = CompositeMatcherType.And,
|
||||||
|
RequestMatcherType? earlyMatcherType = null) : base(requestMatchers, type)
|
||||||
{
|
{
|
||||||
|
EarlyMatcherType = earlyMatcherType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,4 +84,74 @@ public class RequestMessageCompositeMatcherTests
|
|||||||
requestMatcher1Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
requestMatcher1Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
||||||
requestMatcher2Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
requestMatcher2Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageCompositeMatcher_GetMatchingScore_EarlyMismatch()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var requestMatcher1Mock = new Mock<IRequestMatcher>();
|
||||||
|
requestMatcher1Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(1.0d);
|
||||||
|
var requestMatcher2Mock = new Mock<IRequestMatcher>();
|
||||||
|
requestMatcher2Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(0.8d);
|
||||||
|
var postMatcher = new RequestMessageMethodMatcher(HttpRequestMethod.POST);
|
||||||
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), HttpRequestMethod.GET, "127.0.0.1");
|
||||||
|
var matcher = new Helper(
|
||||||
|
[requestMatcher1Mock.Object, requestMatcher2Mock.Object, postMatcher],
|
||||||
|
earlyMatcherType: RequestMatcherType.Method);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
score.Should().Be(0.0d);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
requestMatcher1Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Never);
|
||||||
|
requestMatcher2Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageCompositeMatcher_GetMatchingScore_SeveralHeadersEarlyMismatch()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var headers = new Dictionary<string, string[]>
|
||||||
|
{
|
||||||
|
{ "teST", new[] { "x" } },
|
||||||
|
{ "teST2", new[] { "z" } }
|
||||||
|
};
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
|
||||||
|
var request = Request.Create()
|
||||||
|
.WithEarlyMismatch(RequestMatcherType.Header)
|
||||||
|
.UsingAnyMethod()
|
||||||
|
.WithHeader("teST", "x")
|
||||||
|
.WithHeader("teST1", ["xx", "yy"])
|
||||||
|
.WithHeader("teST2", ["y", "z"], matchOperator: MatchOperator.And);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var score = request.GetMatchingScore(requestMessage, new RequestMatchResult());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
score.Should().Be(0.0d);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageCompositeMatcher_GetMatchingScore_SeveralParamEarlyMismatchSuccess()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var uriWithParams = new Uri("http://localhost?test1=1&test2=2");
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails(uriWithParams), "GET", "127.0.0.1");
|
||||||
|
var request = Request.Create()
|
||||||
|
.WithEarlyMismatch(RequestMatcherType.Param)
|
||||||
|
.UsingAnyMethod()
|
||||||
|
.WithParam("test1", "1")
|
||||||
|
.WithParam("test2", "2");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var score = request.GetMatchingScore(requestMessage, new RequestMatchResult());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
score.Should().Be(1.0d);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.Net.Tests.VerifyExtensions;
|
using WireMock.Net.Tests.VerifyExtensions;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
@@ -101,6 +102,7 @@ public partial class MappingConverterTests
|
|||||||
var guid = new Guid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc");
|
var guid = new Guid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc");
|
||||||
var request = Request.Create()
|
var request = Request.Create()
|
||||||
.UsingGet()
|
.UsingGet()
|
||||||
|
.WithEarlyMismatch(RequestMatcherType.Method)
|
||||||
.WithPath("/test_path")
|
.WithPath("/test_path")
|
||||||
.WithParam("q", "42")
|
.WithParam("q", "42")
|
||||||
.WithClientIP("112.123.100.99")
|
.WithClientIP("112.123.100.99")
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
builder
|
builder
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
|
.WithEarlyMismatch(WireMock.Matchers.Request.RequestMatcherType.Method)
|
||||||
.UsingMethod("GET")
|
.UsingMethod("GET")
|
||||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||||
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
var builder = new MappingBuilder();
|
var builder = new MappingBuilder();
|
||||||
builder
|
builder
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
|
.WithEarlyMismatch(WireMock.Matchers.Request.RequestMatcherType.Method)
|
||||||
.UsingMethod("GET")
|
.UsingMethod("GET")
|
||||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||||
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
|
.WithEarlyMismatch(WireMock.Matchers.Request.RequestMatcherType.Method)
|
||||||
.UsingMethod("GET")
|
.UsingMethod("GET")
|
||||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||||
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
var server = WireMockServer.Start();
|
var server = WireMockServer.Start();
|
||||||
server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
|
.WithEarlyMismatch(WireMock.Matchers.Request.RequestMatcherType.Method)
|
||||||
.UsingMethod("GET")
|
.UsingMethod("GET")
|
||||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||||
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
Guid: Guid_1,
|
||||||
|
UpdatedAt: DateTime_1,
|
||||||
|
Title: ,
|
||||||
|
Description: ,
|
||||||
|
Priority: 42,
|
||||||
|
Request: {
|
||||||
|
Path: {
|
||||||
|
Matchers: [
|
||||||
|
{
|
||||||
|
Name: WildcardMatcher,
|
||||||
|
Pattern: 1.2.3.4,
|
||||||
|
IgnoreCase: false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
Headers: [
|
||||||
|
{
|
||||||
|
Name: x1,
|
||||||
|
Matchers: [
|
||||||
|
{
|
||||||
|
Name: WildcardMatcher,
|
||||||
|
Pattern: y,
|
||||||
|
IgnoreCase: true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
IgnoreCase: true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
EarlyMatcherType: ClientIP
|
||||||
|
},
|
||||||
|
Response: {},
|
||||||
|
UseWebhooksFireAndForget: false
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.Models;
|
using WireMock.Models;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
@@ -640,4 +641,25 @@ message HelloReply {
|
|||||||
// Verify
|
// Verify
|
||||||
return Verify(model);
|
return Verify(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task ToMappingModel_Request_WithEarlyMismatch_ReturnsCorrectModel()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var request = Request.Create().WithEarlyMismatch(RequestMatcherType.ClientIP)
|
||||||
|
.WithHeader("x1", "y")
|
||||||
|
.WithClientIP("1.2.3.4");
|
||||||
|
var response = Response.Create();
|
||||||
|
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, null);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var model = _sut.ToMappingModel(mapping);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
model.Should().NotBeNull();
|
||||||
|
model.Request.EarlyMatcherType.Should().Be(RequestMatcherType.ClientIP);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
return Verify(model);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
using JsonConverter.Newtonsoft.Json;
|
using JsonConverter.Newtonsoft.Json;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Serialization;
|
using WireMock.Serialization;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
#if NET8_0_OR_GREATER
|
#if NET8_0_OR_GREATER
|
||||||
using JsonConverter.System.Text.Json;
|
using JsonConverter.System.Text.Json;
|
||||||
#endif
|
#endif
|
||||||
@@ -319,5 +321,92 @@ public class MappingSerializerTests
|
|||||||
act.Should().Throw<InvalidOperationException>()
|
act.Should().Throw<InvalidOperationException>()
|
||||||
.WithMessage("Cannot deserialize the provided value to an array or object.");
|
.WithMessage("Cannot deserialize the provided value to an array or object.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_DateTimeStringInQueryParamExactMatcherPattern_ShouldPreservePatternAsString()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var mappingJson =
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"Guid": "12345678-1234-1234-1234-aaaaaaaaaaaa",
|
||||||
|
"Request": {
|
||||||
|
"Path": "/api/report",
|
||||||
|
"Methods": ["GET"],
|
||||||
|
"Params": [
|
||||||
|
{
|
||||||
|
"Name": "asOfDate",
|
||||||
|
"Matchers": [
|
||||||
|
{
|
||||||
|
"Name": "ExactMatcher",
|
||||||
|
"Pattern": "2021-11-10T13:39:13.705"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"StatusCode": 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(mappingJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
var matcher = result[0].Request!.Params![0].Matchers![0];
|
||||||
|
matcher.Name.Should().Be("ExactMatcher");
|
||||||
|
matcher.Pattern.Should().BeOfType<string>()
|
||||||
|
.Which.Should().Be("2021-11-10T13:39:13.705",
|
||||||
|
"datetime-format strings in ExactMatcher Pattern fields must survive deserialization as strings, " +
|
||||||
|
"not be auto-converted to DateTime by Newtonsoft.Json's DateParseHandling.DateTime");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_DateTimeStringInJsonMatcherBodyPattern_ShouldPreservePatternAsString()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
// Pattern is an INLINE JSON object (not a string) - this is how WireMock mapping files store
|
||||||
|
// JsonMatcher patterns when recorded. Newtonsoft with DateParseHandling.DateTime will convert
|
||||||
|
// the datetime value inside the JObject to JTokenType.Date during deserialization.
|
||||||
|
var mappingJson =
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"Guid": "12345678-1234-1234-1234-bbbbbbbbbbbb",
|
||||||
|
"Request": {
|
||||||
|
"Path": "/api/report",
|
||||||
|
"Methods": ["POST"],
|
||||||
|
"Body": {
|
||||||
|
"Matcher": {
|
||||||
|
"Name": "JsonMatcher",
|
||||||
|
"Pattern": {"Date": "2021-09-30T00:00:00Z", "Names": ["Cash"]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"StatusCode": 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(mappingJson);
|
||||||
|
|
||||||
|
// Assert - datetime values inside the JObject pattern must remain JTokenType.String.
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
var matcher = result[0].Request!.Body!.Matcher!;
|
||||||
|
matcher.Name.Should().Be("JsonMatcher");
|
||||||
|
var patternJObject = matcher.Pattern.Should().BeOfType<JObject>().Subject;
|
||||||
|
patternJObject["Date"]!.Type.Should().Be(JTokenType.String,
|
||||||
|
"datetime-format strings inside an inline JsonMatcher body pattern must retain JTokenType.String " +
|
||||||
|
"after deserialization; if DateParseHandling.DateTime auto-converts them to JTokenType.Date, " +
|
||||||
|
"JToken.DeepEquals will fail against incoming request bodies parsed with DateParseHandling.None");
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -413,6 +413,120 @@ public class StatefulBehaviorTests
|
|||||||
action.Should().ThrowAsync<HttpRequestException>();
|
action.Should().ThrowAsync<HttpRequestException>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Scenarios_FirstRequestWithDelay_StateTransitions_BeforeDelayCompletes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var cancellationToken = TestContext.Current.CancellationToken;
|
||||||
|
var path = $"/foo_{Guid.NewGuid()}";
|
||||||
|
using var server = WireMockServer.Start();
|
||||||
|
|
||||||
|
// Mapping 1: start state, has a 500 ms delay
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath(path).UsingGet())
|
||||||
|
.InScenario("1260")
|
||||||
|
.WillSetStateTo("State1")
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody("delayed response")
|
||||||
|
.WithDelay(TimeSpan.FromMilliseconds(500)));
|
||||||
|
|
||||||
|
// Mapping 2: only matches after state has transitioned to "State1"
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath(path).UsingGet())
|
||||||
|
.InScenario("1260")
|
||||||
|
.WhenStateIs("State1")
|
||||||
|
.RespondWith(Response.Create().WithBody("immediate response"));
|
||||||
|
|
||||||
|
var client = new HttpClient();
|
||||||
|
|
||||||
|
// Act: fire request 1 but don't await it yet — it will sit in a 500 ms delay
|
||||||
|
var request1Task = client.GetStringAsync(server.Url + path, cancellationToken);
|
||||||
|
|
||||||
|
// Give the server a moment to match & transition state before the delay completes
|
||||||
|
await Task.Delay(100, cancellationToken);
|
||||||
|
|
||||||
|
// Request 2 is sent while request 1 is still being delayed.
|
||||||
|
// After the fix the state has already transitioned, so request 2 matches Mapping 2.
|
||||||
|
var response2 = await client.GetStringAsync(server.Url + path, cancellationToken);
|
||||||
|
|
||||||
|
var response1 = await request1Task;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response1.Should().Be("delayed response");
|
||||||
|
response2.Should().Be("immediate response");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Scenarios_WithGlobalRequestProcessingDelay_StateTransitions_BeforeDelayCompletes()
|
||||||
|
{
|
||||||
|
// Arrange: use the global RequestProcessingDelay instead of a per-mapping delay
|
||||||
|
var cancellationToken = TestContext.Current.CancellationToken;
|
||||||
|
var path = $"/foo_{Guid.NewGuid()}";
|
||||||
|
using var server = WireMockServer.Start();
|
||||||
|
server.AddGlobalProcessingDelay(TimeSpan.FromMilliseconds(500));
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath(path).UsingGet())
|
||||||
|
.InScenario("s")
|
||||||
|
.WillSetStateTo("State1")
|
||||||
|
.RespondWith(Response.Create().WithBody("delayed response"));
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath(path).UsingGet())
|
||||||
|
.InScenario("s")
|
||||||
|
.WhenStateIs("State1")
|
||||||
|
.RespondWith(Response.Create().WithBody("immediate response"));
|
||||||
|
|
||||||
|
var client = new HttpClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var request1Task = client.GetStringAsync(server.Url + path, cancellationToken);
|
||||||
|
await Task.Delay(100, cancellationToken);
|
||||||
|
var response2 = await client.GetStringAsync(server.Url + path, cancellationToken);
|
||||||
|
var response1 = await request1Task;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response1.Should().Be("delayed response");
|
||||||
|
response2.Should().Be("immediate response");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Scenarios_WithDelay_And_TimesInSameState_Should_Transition_After_Required_Hits()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var cancellationToken = TestContext.Current.CancellationToken;
|
||||||
|
var path = $"/foo_{Guid.NewGuid()}";
|
||||||
|
using var server = WireMockServer.Start();
|
||||||
|
|
||||||
|
// Mapping 1: requires 2 hits before transitioning; has a short delay
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath(path).UsingGet())
|
||||||
|
.InScenario("s")
|
||||||
|
.WillSetStateTo("State1", 2)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody("first")
|
||||||
|
.WithDelay(TimeSpan.FromMilliseconds(50)));
|
||||||
|
|
||||||
|
// Mapping 2: matches after state is "State1"
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath(path).UsingGet())
|
||||||
|
.InScenario("s")
|
||||||
|
.WhenStateIs("State1")
|
||||||
|
.RespondWith(Response.Create().WithBody("second"));
|
||||||
|
|
||||||
|
var client = new HttpClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response1 = await client.GetStringAsync(server.Url + path, cancellationToken);
|
||||||
|
var response2 = await client.GetStringAsync(server.Url + path, cancellationToken);
|
||||||
|
var response3 = await client.GetStringAsync(server.Url + path, cancellationToken);
|
||||||
|
|
||||||
|
// Assert: state only transitions after 2 hits, so request 3 is the first to match Mapping 2
|
||||||
|
response1.Should().Be("first");
|
||||||
|
response2.Should().Be("first");
|
||||||
|
response3.Should().Be("second");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Scenarios_Should_process_request_if_equals_state_and_multiple_state_defined()
|
public async Task Scenarios_Should_process_request_if_equals_state_and_multiple_state_defined()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,10 +4,13 @@ using System.Net;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using JsonConverter.System.Text.Json;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests;
|
namespace WireMock.Net.Tests;
|
||||||
|
|
||||||
@@ -18,11 +21,41 @@ public partial class WireMockServerTests
|
|||||||
public string? Hi { get; set; }
|
public string? Hi { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class Person
|
||||||
|
{
|
||||||
|
[JsonPropertyName("first_name")]
|
||||||
|
public string FirstName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("last_name")]
|
||||||
|
public string LastName { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WireMockServer_WithBodyAsJson_UsingWireMockServerSettings_SystemTextJsonConverter_ShouldReturnCorrectResponse()
|
||||||
|
{
|
||||||
|
// Arange
|
||||||
|
var person = new Person { FirstName = "John", LastName = "Smith" };
|
||||||
|
|
||||||
|
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
|
{
|
||||||
|
DefaultJsonSerializer = new SystemTextJsonConverter()
|
||||||
|
});
|
||||||
|
|
||||||
|
// Act
|
||||||
|
server
|
||||||
|
.Given(Request.Create().UsingAnyMethod())
|
||||||
|
.RespondWith(Response.Create().WithBodyAsJson(person));
|
||||||
|
|
||||||
|
var response = await server.CreateClient().GetStringAsync("/", _ct);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response.Should().BeEquivalentTo("{\"first_name\":\"John\",\"last_name\":\"Smith\"}");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
@@ -56,7 +89,7 @@ public partial class WireMockServerTests
|
|||||||
var requestUri = new Uri($"http://localhost:{server.Port}/a");
|
var requestUri = new Uri($"http://localhost:{server.Port}/a");
|
||||||
|
|
||||||
var json = new { requestId = "1", value = "A" };
|
var json = new { requestId = "1", value = "A" };
|
||||||
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, cancellationToken);
|
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -68,7 +101,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch_BestMatching()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch_BestMatching()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
@@ -105,7 +137,7 @@ public partial class WireMockServerTests
|
|||||||
var requestUri = new Uri($"http://localhost:{server.Port}/a");
|
var requestUri = new Uri($"http://localhost:{server.Port}/a");
|
||||||
|
|
||||||
var json = new { extra = "X", requestId = "1", value = "A" };
|
var json = new { extra = "X", requestId = "1", value = "A" };
|
||||||
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, cancellationToken);
|
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -117,7 +149,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||||
@@ -132,7 +163,7 @@ public partial class WireMockServerTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject, cancellationToken);
|
var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject, _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -144,7 +175,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||||
@@ -154,7 +184,7 @@ public partial class WireMockServerTests
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }"), cancellationToken);
|
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }"), _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -166,7 +196,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
|
|
||||||
var matcher = new JsonPartialWildcardMatcher(new { method = "initialize", id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
var matcher = new JsonPartialWildcardMatcher(new { method = "initialize", id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||||
@@ -188,13 +217,13 @@ public partial class WireMockServerTests
|
|||||||
// Act
|
// Act
|
||||||
var content = "{\"jsonrpc\":\"2.0\",\"id\":\"ec475f56d4694b48bc737500ba575b35-1\",\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2024-11-05\",\"capabilities\":{},\"clientInfo\":{\"name\":\"GitHub Test\",\"version\":\"1.0.0\"}}}";
|
var content = "{\"jsonrpc\":\"2.0\",\"id\":\"ec475f56d4694b48bc737500ba575b35-1\",\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2024-11-05\",\"capabilities\":{},\"clientInfo\":{\"name\":\"GitHub Test\",\"version\":\"1.0.0\"}}}";
|
||||||
var response = await new HttpClient()
|
var response = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/foo", new StringContent(content, Encoding.UTF8, "application/json"), cancellationToken)
|
.PostAsync($"{server.Url}/foo", new StringContent(content, Encoding.UTF8, "application/json"), _ct)
|
||||||
;
|
;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
|
var responseText = await response.Content.ReadAsStringAsync(_ct);
|
||||||
responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
|
responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,8 +232,7 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
using var server = WireMockServer.Start(x => x.DefaultJsonSerializer = new SystemTextJsonConverter());
|
||||||
using var server = WireMockServer.Start(x => x.DefaultJsonSerializer = new JsonConverter.System.Text.Json.SystemTextJsonConverter() );
|
|
||||||
|
|
||||||
var matcher = new JsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
var matcher = new JsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||||
server.Given(Request.Create()
|
server.Given(Request.Create()
|
||||||
@@ -220,12 +248,12 @@ public partial class WireMockServerTests
|
|||||||
// Act
|
// Act
|
||||||
var content = """{"id":"ec475f56d4694b48bc737500ba575b35-1"}""";
|
var content = """{"id":"ec475f56d4694b48bc737500ba575b35-1"}""";
|
||||||
using var httpClient = new HttpClient();
|
using var httpClient = new HttpClient();
|
||||||
var response = await httpClient.PostAsync($"{server.Url}/system-text-json", new StringContent(content, Encoding.UTF8, "application/json"), cancellationToken);
|
var response = await httpClient.PostAsync($"{server.Url}/system-text-json", new StringContent(content, Encoding.UTF8, "application/json"), _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
|
var responseText = await response.Content.ReadAsStringAsync(_ct);
|
||||||
responseText.Should().Contain("OK");
|
responseText.Should().Contain("OK");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -234,7 +262,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
|
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancelationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
@@ -249,7 +276,7 @@ public partial class WireMockServerTests
|
|||||||
// Act
|
// Act
|
||||||
var content = new FormUrlEncodedContent([new KeyValuePair<string, string>("key1", "value1")]);
|
var content = new FormUrlEncodedContent([new KeyValuePair<string, string>("key1", "value1")]);
|
||||||
var response = await new HttpClient()
|
var response = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/foo", content, cancelationToken);
|
.PostAsync($"{server.Url}/foo", content, _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -261,7 +288,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithExactMatcher()
|
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithExactMatcher()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
@@ -282,7 +308,7 @@ public partial class WireMockServerTests
|
|||||||
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
||||||
]);
|
]);
|
||||||
using var httpClient = new HttpClient();
|
using var httpClient = new HttpClient();
|
||||||
var response = await httpClient.PostAsync($"{server.Url}/foo", content, cancellationToken)
|
var response = await httpClient.PostAsync($"{server.Url}/foo", content, _ct)
|
||||||
;
|
;
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -294,7 +320,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFormUrlEncodedMatcher()
|
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFormUrlEncodedMatcher()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancelationToken = TestContext.Current.CancellationToken;
|
|
||||||
var matcher = new FormUrlEncodedMatcher(["email=johndoe@example.com", "name=John Doe"]);
|
var matcher = new FormUrlEncodedMatcher(["email=johndoe@example.com", "name=John Doe"]);
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
@@ -326,7 +351,7 @@ public partial class WireMockServerTests
|
|||||||
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
||||||
]);
|
]);
|
||||||
var responseOrdered = await new HttpClient()
|
var responseOrdered = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/foo", contentOrdered, cancelationToken)
|
.PostAsync($"{server.Url}/foo", contentOrdered, _ct)
|
||||||
;
|
;
|
||||||
|
|
||||||
// Assert 1
|
// Assert 1
|
||||||
@@ -340,7 +365,7 @@ public partial class WireMockServerTests
|
|||||||
new KeyValuePair<string, string>("name", "John Doe"),
|
new KeyValuePair<string, string>("name", "John Doe"),
|
||||||
]);
|
]);
|
||||||
var responseUnordered = await new HttpClient()
|
var responseUnordered = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/bar", contentUnordered, cancelationToken)
|
.PostAsync($"{server.Url}/bar", contentUnordered, _ct)
|
||||||
;
|
;
|
||||||
|
|
||||||
// Assert 2
|
// Assert 2
|
||||||
@@ -353,7 +378,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithSseBody()
|
public async Task WireMockServer_WithSseBody()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server
|
server
|
||||||
.WhenRequest(r => r
|
.WhenRequest(r => r
|
||||||
@@ -387,9 +411,8 @@ public partial class WireMockServerTests
|
|||||||
using var client = new HttpClient();
|
using var client = new HttpClient();
|
||||||
|
|
||||||
// Act 1
|
// Act 1
|
||||||
var normal = await client.GetAsync(server.Url, cancellationToken)
|
var normal = await client.GetAsync(server.Url, _ct);
|
||||||
;
|
(await normal.Content.ReadAsStringAsync(_ct)).Should().Be("normal");
|
||||||
(await normal.Content.ReadAsStringAsync(cancellationToken)).Should().Be("normal");
|
|
||||||
|
|
||||||
// Act 2
|
// Act 2
|
||||||
using var response = await client.GetStreamAsync($"{server.Url}/sse", _ct);
|
using var response = await client.GetStreamAsync($"{server.Url}/sse", _ct);
|
||||||
|
|||||||
Reference in New Issue
Block a user