Compare commits

...
46 Commits
Author SHA1 Message Date
Stef Heyenrath f6dc299ecb 2.20.0 2026-10-23 17:54:30 +02:00
Mattias SundströmandMattias dc14136492 Add near-miss diagnostics and request count/unmatched endpoints to the admin API (#1521)
* Add near-miss diagnostics and request count/unmatched admin endpoints (#171)

Add IWireMockServer.FindUnmatchedLogEntries, FindNearMisses and
FindNearMissesForUnmatchedRequests, which rank the mappings that
partially match a request by score.

Expose them through the admin API (POST requests/count,
GET requests/unmatched, GET requests/unmatched/near-misses and
GET requests/{guid}/near-misses), the RestClient and the OpenAPI document.

* Make scenario delay tests independent of server warm-up time

The two *_StateTransitions_BeforeDelayCompletes tests waited a fixed
100 ms for the scenario state to change, which fails on a cold server.
Poll server.Scenarios until the expected state is reached instead.

---------

Co-authored-by: Mattias <13809209+Quattordici@users.noreply.github.com>
2026-10-23 17:48:54 +02:00
Stef Heyenrath c94824bd64 Update Generate-ReleaseNotes.cmd 2026-10-19 19:30:12 +02:00
Stef HeyenrathandCopilot Autofix powered by AI 54e16fc045 Potential fix for code scanning alert no. 8: Workflow does not contain permissions (#1523)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
2026-10-19 19:25:01 +02:00
Stef Heyenrath 7d0de75ab1 Downgrade JsonPath.Net from 3.0.2 to 2.2.0 to avoid Open Source Maintenance Fee issues (#1522) 2026-10-19 18:42:43 +02:00
Stef Heyenrath afb01a0fa6 Bump MessagePack from 2.5.302 to 2.5.303 (#1519) 2026-10-18 10:51:38 +02:00
Mattias SundströmandMattias 0132ec873a Add ScribanRegistrationCallback to register custom Scriban functions (#1515) (#1516)
- Add WireMockServerSettings.ScribanRegistrationCallback
  (Action<object, IFileSystemHandler>?) so code-configured servers can add
  functions, objects or variables to Scriban and Scriban-DotLiquid templates.
- Pass the settings to ScribanContextFactory, which invokes the callback with
  a new ScriptObject per transformer context.
- Push the custom globals beneath the template model in ScribanContext so
  request, response, mapping and data take precedence; keep the original
  Template.Render call when no callback is configured.
- Add the strongly typed WithScribanRegistration extension method.
- Add a Scriban example to the .NET 8 console app.
- Make the proxy tests use a local target server instead of www.google.com,
  and skip the IPv6 loopback test when the OS has no IPv6 support.

Co-authored-by: Mattias <13809209+Quattordici@users.noreply.github.com>
2026-10-18 09:29:11 +02:00
Stef Heyenrath 64734c4f92 <VersionPrefix>2.19.0</VersionPrefix> 2026-10-07 18:25:30 +02:00
Stef Heyenrath a5589e1e2f 2.19.0 2026-10-07 18:22:51 +02:00
Stef HeyenrathandStef Heyenrath cb4503b1a6 Fix DateTime in Response (#1514)
* Fix DateTime in Response

* // Set the DateTime from the Response to the current DateTime

---------

Co-authored-by: Stef Heyenrath <stef.heyenrath@apg.nl>
2026-10-07 17:35:58 +02:00
Stef Heyenrath dd762f218b Use IDateTimeUtils in WireMockWebSocketContext (#1512) 2026-10-06 22:58:33 +02:00
Stef Heyenrath b0f9924933 Add example /test (#1513) 2026-10-06 22:51:17 +02:00
Stef Heyenrath dc4cdfae50 2.18.0 2026-09-22 22:21:50 +02:00
Stef Heyenrath f19d94acae 2.18.0 2026-09-22 22:18:47 +02:00
Stef Heyenrath 90819f031e WireMock.Net.OpenApiParser: fix depencency on RamlToOpenApiConverter.SourceOnly (#1510) 2026-09-22 22:16:13 +02:00
Stef Heyenrath 471745f704 2.17.0 2026-09-21 20:53:22 +02:00
Stef Heyenrath 7db5b562d8 Revert to RamlToOpenApiConverter.SourceOnly (#1509) 2026-09-21 20:50:49 +02:00
Stef Heyenrath e4389333a1 2.16.0 2026-09-19 07:54:10 +02:00
Stef HeyenrathandCopilot Autofix powered by AI f0969e9618 Upgrade dependency RamlToOpenApiConverter to version 0.40.0 (#1505)
* Upgrade dependency RamlToOpenApiConverter to version 0.40.0

* fix code comment

* .

* Potential fix for pull request finding 'Constant condition'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>

* Potential fix for pull request finding 'Constant condition'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>

* fix

---------

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
2026-09-10 20:28:43 +02:00
John Call 84f4109a7c Update SharpYaml package version to 2.1.5 (#1504)
Fix https://github.com/xoofx/SharpYaml/issues/130
2026-09-05 08:54:06 +02:00
ee0f890795 Fix FormUrlEncodedMatcher (+ refactor values / matchers logic) (#1503)
* Fix FormUrlEncodedMatcher

* tests

* Apply suggestions from code review

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix comments

---------

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-08-20 17:31:16 +02:00
Dmytro Nikitin 2b165eb6a1 Fix #1501: Generate WhenStateIs/WillSetStateTo correctly in ToCSharpCode (#1502)
* Fix #1501: Generate WhenStateIs/WillSetStateTo correctly in ToCSharpCode

* Fix matcher to code converters. Refactor changes.

* Adjust test artifacts.

* Return fully qualified class names for matchers
2026-08-19 19:58:47 +02:00
Stef Heyenrath 42353f035e 2.15.0 2026-08-15 09:51:50 +02:00
Stef Heyenrath 7e9718eab7 Update Testcontainers (#1499) 2026-08-14 16:55:57 +02:00
Dmytro Nikitin a0df0916f9 Fix #1496: WithParam RejectOnMatch inverting the match result (#1497) 2026-08-13 20:42:06 +02:00
Dmytro Nikitin 7a8582fef5 Fix #1494: WithClientIP(MatchOperator, IStringMatcher[]) ignores the operator (#1495) 2026-08-09 18:49:13 +02:00
Stef Heyenrath 05929bdea6 remove copilot-setup-steps.yml 2026-08-06 20:15:30 +02:00
Stef Heyenrath 84c32d5b2d 2.14.0 2026-08-06 19:32:33 +02:00
Dmytro Nikitin d39f7c771f Fix #1492: Serialize WithClientIP matcher to Request.ClientIP (#1493) 2026-08-06 19:28:40 +02:00
Stef Heyenrath 97c8e4a08a 2.13.0 2026-07-19 17:04:12 +02:00
Stef Heyenrath f5eb01c580 2.13.0 2026-07-19 09:59:27 +02:00
Stef Heyenrath 8260dba641 Add some more tests for WireMockList + move AdminPaths.cs file (#1491) 2026-07-18 09:48:41 +02:00
Stef Heyenrath 5e1c126e34 Pipeline parameter : runWindowsReleaseToMyGet 2026-07-18 09:48:23 +02:00
ca4eea808e Fix: Fully respect configured custom admin path (merge PR #1483) (#1490)
* Fix: Fully respect configured custom admin path

* Add tests for custom admin path behaviour

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Peter Benko <peter.benko@gmail.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Stef Heyenrath <Stef.Heyenrath@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-07-18 09:01:04 +02:00
Stef Heyenrath 5692498ea3 Swagger (#1489)
* Add openapi swagger yml

* fix

* version

* reloadStaticMappings
2026-07-17 18:59:19 +02:00
СтепанandМинин Степан Александрович d270ad7f4f feat(xunit logging): TestOutputHelperWireMockLogger (#1488)
* feat(xunit logging): TestOutputHelperWireMockLogger

allow to provide current test output helper

* fix(ctor): TestOutputHelperWireMockLogger

made factory return nullable

---------

Co-authored-by: Минин Степан Александрович <minin_s@goldapple.ru>
2026-07-17 18:25:25 +02:00
Stef Heyenrath 345efe8cfc 2.12.0 2026-07-07 21:38:12 +02:00
Stef Heyenrath a74468e780 When Response.Create() is called without a pre-built ResponseMessage, use the DateTime.UtcNow (#1487) 2026-07-07 21:36:11 +02:00
John Call 041160d824 Update Scriban.Signed package version to 7.2.5 (#1484)
* Update Scriban.Signed package version to 7.2.6

* Downgrade Scriban.Signed package version to 7.2.5
2026-07-07 20:54:11 +02:00
Stef Heyenrath 4666757df9 Upgrade RamlToOpenApiConverter.SourceOnly to 0.21.0 (#1480) 2026-07-03 21:04:58 +02:00
Stef Heyenrath 49dcbda647 Upgrade Microsoft.OpenApi to 3.7.0 and YamlDotNet to 18.1.0 (#1479)
* Upgrade Microsoft.OpenApi to 3.7.0 and YamlDotNet to 18.1.0

* fix
2026-07-01 22:44:34 +02:00
Stef Heyenrath cbc28c235c 2.11.0 2026-06-12 20:09:27 +02:00
Stef Heyenrath dd1aa5139e Fix BodyParser (#1476) 2026-06-12 20:04:23 +02:00
Stef Heyenrath 89b3149732 Update NuGet packages (Aspire, MessagePack and more) (#1475) 2026-06-12 12:01:09 +02:00
Stef Heyenrath d2b794ec5a 2.10.0 2026-06-07 22:52:32 +02:00
Stef Heyenrath 921a3ebc63 Update JsonConverter to 0.13.0 (#1472) 2026-06-07 22:37:55 +02:00
180 changed files with 5653 additions and 715 deletions

No files matched your search

+3
View File
@@ -8,6 +8,9 @@ on:
branches:
- 'main'
permissions:
contents: read
jobs:
windows-build-and-run:
name: Run Tests on Windows
-36
View File
@@ -1,36 +0,0 @@
name: "Copilot Setup Steps"
# Automatically run the setup steps when they are changed to allow for easy validation, and
# allow manual testing through the repository's "Actions" tab
on:
workflow_dispatch:
push:
paths:
- .github/workflows/copilot-setup-steps.yml
pull_request:
paths:
- .github/workflows/copilot-setup-steps.yml
jobs:
# The job MUST be called `copilot-setup-steps` or it will not be picked up by Copilot.
copilot-setup-steps:
runs-on: ubuntu-latest
# Set the permissions to the lowest permissions possible needed for your steps.
# Copilot will be given its own token for its operations.
permissions:
# If you want to clone the repository as part of your setup steps, for example to install dependencies, you'll need the `contents: read` permission. If you don't clone the repository in your setup steps, Copilot will do this for you automatically after the steps complete.
contents: read
# You can define any steps you want, and they will run before the agent starts.
# If you do not check out your code, Copilot will do this for you.
steps:
- name: Install .NET 10.x
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
10.x
dotnet-quality: preview
- name: dotnet --info
run: dotnet --info
+59
View File
@@ -1,3 +1,62 @@
# 2.20.0 (23 October 2026)
- [#1516](https://github.com/wiremock/WireMock.Net/pull/1516) - Add ScribanRegistrationCallback to register custom Scriban functions [feature] contributed by [Quattordici](https://github.com/Quattordici)
- [#1519](https://github.com/wiremock/WireMock.Net/pull/1519) - Bump MessagePack from 2.5.302 to 2.5.303 [dependencies] contributed by [StefH](https://github.com/StefH)
- [#1521](https://github.com/wiremock/WireMock.Net/pull/1521) - Add near-miss diagnostics and request count/unmatched endpoints to the admin API [feature] contributed by [Quattordici](https://github.com/Quattordici)
- [#1522](https://github.com/wiremock/WireMock.Net/pull/1522) - Downgrade JsonPath.Net from 3.0.2 to 2.2.0 to avoid Open Source Maintenance Fee issues [dependencies] contributed by [StefH](https://github.com/StefH)
- [#1515](https://github.com/wiremock/WireMock.Net/issues/1515) - Allow registering custom Scriban functions (ScribanRegistrationCallback), like HandlebarsRegistrationCallback [feature]
- [#1520](https://github.com/wiremock/WireMock.Net/issues/1520) - json-everything sub-dependency now has Open Source Maintenance Fee [dependencies]
# 2.19.0 (07 October 2026)
- [#1512](https://github.com/wiremock/WireMock.Net/pull/1512) - Use IDateTimeUtils in WireMockWebSocketContext [bug] contributed by [StefH](https://github.com/StefH)
- [#1514](https://github.com/wiremock/WireMock.Net/pull/1514) - Fix DateTime in Response [bug] contributed by [StefH](https://github.com/StefH)
- [#1511](https://github.com/wiremock/WireMock.Net/issues/1511) - Incorrect response datetime for mapped response [bug]
# 2.18.0 (22 September 2026)
- [#1510](https://github.com/wiremock/WireMock.Net/pull/1510) - WireMock.Net.OpenApiParser: fix depencency on RamlToOpenApiConverter.SourceOnly [bug] contributed by [StefH](https://github.com/StefH)
- [#1508](https://github.com/wiremock/WireMock.Net/issues/1508) - Transitive Dependency Conflict with Microsoft.OpenApi in 2.16.0 [bug]
# 2.17.0 (21 September 2026)
- [#1509](https://github.com/wiremock/WireMock.Net/pull/1509) - Revert to RamlToOpenApiConverter.SourceOnly [bug] contributed by [StefH](https://github.com/StefH)
# 2.16.0 (19 September 2026)
- [#1502](https://github.com/wiremock/WireMock.Net/pull/1502) - Fix #1501: Generate WhenStateIs/WillSetStateTo correctly in ToCSharpCode contributed by [Bafyn](https://github.com/Bafyn)
- [#1503](https://github.com/wiremock/WireMock.Net/pull/1503) - Fix FormUrlEncodedMatcher (+ refactor values / matchers logic) contributed by [StefH](https://github.com/StefH)
- [#1504](https://github.com/wiremock/WireMock.Net/pull/1504) - Update SharpYaml package to version 2.5.1 [dependencies] contributed by [johnthcall](https://github.com/johnthcall)
- [#1505](https://github.com/wiremock/WireMock.Net/pull/1505) - Upgrade dependency RamlToOpenApiConverter to version 0.40.0 [dependencies] contributed by [StefH](https://github.com/StefH)
- [#1500](https://github.com/wiremock/WireMock.Net/issues/1500) - Sending a form-urlencoded body with a repeated field name returns HTTP 500 [bug]
- [#1501](https://github.com/wiremock/WireMock.Net/issues/1501) - Generated C# code for stateful (Scenario) mappings does not compile and drops the WhenStateIs condition [bug]
# 2.15.0 (15 August 2026)
- [#1495](https://github.com/wiremock/WireMock.Net/pull/1495) - Fix #1494: `WithClientIP(MatchOperator, params IStringMatcher[])` ignores the MatchOperator argument [bug] contributed by [Bafyn](https://github.com/Bafyn)
- [#1497](https://github.com/wiremock/WireMock.Net/pull/1497) - Fix WithParam RejectOnMatch inverting the match result #1496 [bug] contributed by [Bafyn](https://github.com/Bafyn)
- [#1499](https://github.com/wiremock/WireMock.Net/pull/1499) - Update Testcontainers [bug] contributed by [StefH](https://github.com/StefH)
- [#1494](https://github.com/wiremock/WireMock.Net/issues/1494) - `WithClientIP(MatchOperator, params IStringMatcher[])` ignores the `MatchOperator` argument [bug]
- [#1496](https://github.com/wiremock/WireMock.Net/issues/1496) - `WithParam(key, MatchBehaviour.RejectOnMatch, values)` inverts the match result [bug]
# 2.14.0 (06 August 2026)
- [#1493](https://github.com/wiremock/WireMock.Net/pull/1493) - Fix #1492: Serialize WithClientIP matcher to Request.ClientIP [bug] contributed by [Bafyn](https://github.com/Bafyn)
- [#1492](https://github.com/wiremock/WireMock.Net/issues/1492) - `WithClientIP` matcher is lost / corrupted when a mapping is serialized [bug]
# 2.13.0 (19 July 2026)
- [#1488](https://github.com/wiremock/WireMock.Net/pull/1488) - feat(xunit logging): TestOutputHelperWireMockLogger [feature] contributed by [Stepami](https://github.com/Stepami)
- [#1490](https://github.com/wiremock/WireMock.Net/pull/1490) - Fix: Fully respect configured custom admin path (merge PR #1483) [feature] contributed by [Copilot](https://github.com/apps/copilot-swe-agent)
- [#1482](https://github.com/wiremock/WireMock.Net/issues/1482) - WireMock.Minimal.Owin does not respect custom pathing for determining admin paths [bug]
# 2.12.0 (07 July 2026)
- [#1479](https://github.com/wiremock/WireMock.Net/pull/1479) - Upgrade Microsoft.OpenApi to 3.7.0 and YamlDotNet to 18.1.0 [security] contributed by [StefH](https://github.com/StefH)
- [#1480](https://github.com/wiremock/WireMock.Net/pull/1480) - Upgrade RamlToOpenApiConverter.SourceOnly to 0.21.0 [feature] contributed by [StefH](https://github.com/StefH)
- [#1484](https://github.com/wiremock/WireMock.Net/pull/1484) - Update Scriban.Signed package version to 7.2.5 [security] contributed by [johnthcall](https://github.com/johnthcall)
- [#1487](https://github.com/wiremock/WireMock.Net/pull/1487) - Response.Create() sets the DateTime to UtcNow [bug] contributed by [StefH](https://github.com/StefH)
- [#1486](https://github.com/wiremock/WireMock.Net/issues/1486) - ResponseMessage.DateTime defaults to DateTime.MinValue, causing premature log entry purging for *matched* requests [bug]
# 2.11.0 (12 June 2026)
- [#1475](https://github.com/wiremock/WireMock.Net/pull/1475) - Update NuGet packages (Aspire, MessagePack and more) [security] contributed by [StefH](https://github.com/StefH)
- [#1476](https://github.com/wiremock/WireMock.Net/pull/1476) - Fix BodyParser [bug] contributed by [StefH](https://github.com/StefH)
- [#1473](https://github.com/wiremock/WireMock.Net/issues/1473) - JsonPartialWildCardMatcher With DateTime Regex Stopped Matching (Regression?) [bug]
# 2.10.0 (07 June 2026)
- [#1472](https://github.com/wiremock/WireMock.Net/pull/1472) - Update JsonConverter to 0.13.0 [bug] contributed by [StefH](https://github.com/StefH)
# 2.9.0 (02 June 2026)
- [#1470](https://github.com/wiremock/WireMock.Net/pull/1470) - Fix WireMock.Net.Aspire project when using Aspire 13.4.0 [bug] contributed by [StefH](https://github.com/StefH)
- [#1469](https://github.com/wiremock/WireMock.Net/issues/1469) - WireMock.Net doesn't play well with Aspire 13.4.0 [bug]
+3 -3
View File
@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>2.9.0</VersionPrefix>
<VersionPrefix>2.20.0</VersionPrefix>
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/wiremock/WireMock.Net</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
@@ -51,8 +51,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2025.2.4" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.300" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2026.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.401" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
+12 -2
View File
@@ -1,7 +1,17 @@
rem https://github.com/StefH/GitHubReleaseNotes
SET version=2.9.0
@echo off
setlocal
for /f "usebackq delims=" %%v in (`powershell -NoProfile -Command "[xml]$props = Get-Content 'Directory.Build.props'; $props.Project.PropertyGroup | ForEach-Object { $_.VersionPrefix } | Where-Object { $_ } | Select-Object -First 1"`) do set version=%%v
if "%version%"=="" (
echo Unable to determine VersionPrefix from Directory.Build.props.
exit /b 1
)
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels wontfix test question invalid doc duplicate example environment --version %version% --token %GH_TOKEN%
if errorlevel 1 exit /b %errorlevel%
GitHubReleaseNotes --output PackageReleaseNotes.txt --skip-empty-releases --exclude-labels wontfix test question invalid doc duplicate example environment --template PackageReleaseNotes.template --version %version% --token %GH_TOKEN%
GitHubReleaseNotes --output PackageReleaseNotes.txt --skip-empty-releases --exclude-labels wontfix test question invalid doc duplicate example environment --template PackageReleaseNotes.template --version %version% --token %GH_TOKEN%
if errorlevel 1 exit /b %errorlevel%
+7 -3
View File
@@ -1,5 +1,9 @@
# 2.9.0 (02 June 2026)
- #1470 Fix WireMock.Net.Aspire project when using Aspire 13.4.0 [bug]
- #1469 WireMock.Net doesn't play well with Aspire 13.4.0 [bug]
# 2.20.0 (23 October 2026)
- #1516 Add ScribanRegistrationCallback to register custom Scriban functions [feature]
- #1519 Bump MessagePack from 2.5.302 to 2.5.303 [dependencies]
- #1521 Add near-miss diagnostics and request count/unmatched endpoints to the admin API [feature]
- #1522 Downgrade JsonPath.Net from 3.0.2 to 2.2.0 to avoid Open Source Maintenance Fee issues [dependencies]
- #1515 Allow registering custom Scriban functions (ScribanRegistrationCallback), like HandlebarsRegistrationCallback [feature]
- #1520 json-everything sub-dependency now has Open Source Maintenance Fee [dependencies]
The full release notes can be found here: https://github.com/wiremock/WireMock.Net/blob/master/CHANGELOG.md
+7
View File
@@ -1,3 +1,9 @@
parameters:
- name: runWindowsReleaseToMyGet
displayName: Run Windows Release to MyGet
type: boolean
default: false
variables:
Prerelease: 'ci'
buildId: "1$(Build.BuildId)"
@@ -131,6 +137,7 @@ jobs:
- job: Windows_Release_to_MyGet
dependsOn: Windows_Build_Test
condition: and(succeeded(), ${{ parameters.runWindowsReleaseToMyGet }})
pool:
vmImage: 'windows-2025'
@@ -18,7 +18,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="13.4.0" />
<PackageReference Include="Aspire.Hosting.AppHost" Version="13.4.3" />
<!-- CVE-2026-48109 / CVE-2026-92707 -->
<PackageReference Include="MessagePack" Version="2.5.303" />
</ItemGroup>
<ItemGroup>
@@ -15,7 +15,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="13.4.0" />
<PackageReference Include="Aspire.Hosting.AppHost" Version="13.4.3" />
<!-- CVE-2026-48109 / CVE-2026-92707 -->
<PackageReference Include="MessagePack" Version="2.5.303" />
</ItemGroup>
</Project>
-2
View File
@@ -1,9 +1,7 @@
// Copyright © WireMock.Net
using System;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestEase;
using WireMock.Admin.Settings;
@@ -9,9 +9,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.25.1" />
<PackageReference Include="Grpc.Net.Client" Version="2.60.0" />
<PackageReference Include="Grpc.Tools" Version="2.60.0">
<PackageReference Include="Google.Protobuf" Version="3.33.5" />
<PackageReference Include="Grpc.Net.Client" Version="2.76.0" />
<PackageReference Include="Grpc.Tools" Version="2.78.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
@@ -9,7 +9,7 @@ namespace WireMock.Net.Console.MimePart;
static class Program
{
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()!);
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
static async Task Main(params string[] args)
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System.IO;
using WireMock.Logging;
using WireMock.Server;
using WireMock.Settings;
+27 -8
View File
@@ -1,14 +1,9 @@
// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SharpYaml.Model;
using Scriban.Runtime;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Models;
@@ -291,11 +286,21 @@ namespace WireMock.Net.ConsoleApplication
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = true,
Logger = new WireMockConsoleLogger(),
Port = 9091
});
server
.WhenRequest(r => r
.WithPath("/test")
.UsingAnyMethod()
)
.ThenRespondWith(r => r
.WithStatusCode(HttpStatusCode.OK)
.WithBody("OK")
);
server
.WhenRequest(r => r
.WithPath("/Content-Length")
@@ -405,12 +410,26 @@ namespace WireMock.Net.ConsoleApplication
// handlebarsContext.RegisterHelper(transformer.Name, transformer.Render); TODO
},
ScribanRegistrationCallback = (scriptObject, fileSystemHandler) =>
{
var globals = (ScriptObject)scriptObject;
globals.Import("greet", new Func<string, string>(name => $"Hello {name}"));
},
// Uncomment below if you want to use the CustomFileSystemFileHandler
// FileSystemHandler = new CustomFileSystemFileHandler()
});
System.Console.WriteLine("WireMockServer listening at {0}", string.Join(",", server.Urls));
server.SetBasicAuthentication("a", "b");
server
.Given(Request.Create().WithPath("/scriban/greet").UsingGet())
.RespondWith(Response.Create()
.WithBody("{{ greet 'WireMock.Net' }}")
.WithTransformer(TransformerType.Scriban)
);
//server.SetAzureADAuthentication(Environment.GetEnvironmentVariable("WIREMOCK_AAD_TENANT")!, "api://e083d51a-01a6-446c-8ad5-0c5c7f002208");
//var http = new HttpClient();
@@ -1115,7 +1134,7 @@ namespace WireMock.Net.ConsoleApplication
{
BodyData = new BodyData
{
BodyAsString = "random200or505:" + code + ", HeadersFromRequest = " + string.Join(",", request.Headers),
BodyAsString = "random200or505:" + code + ", HeadersFromRequest = " + request.Headers != null ? string.Join(",", request.Headers!.Select(h => $"{h.Key}:{h.Value}")) : "none",
DetectedBodyType = BodyType.String,
},
StatusCode = code
@@ -1,8 +1,6 @@
// Copyright © WireMock.Net
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
using log4net.Repository;
@@ -12,8 +10,7 @@ namespace WireMock.Net.Console.NET8;
static class Program
{
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()!);
static async Task Main(params string[] args)
{
@@ -1,9 +1,5 @@
// Copyright © WireMock.Net
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using WireMock.Logging;
using WireMock.Net.StandAlone;
using WireMock.RequestBuilders;
@@ -16,7 +16,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="WireMock.Net" Version="2.6.0" />
<PackageReference Include="WireMock.Net" Version="2.15.0" />
</ItemGroup>
</Project>
@@ -1,7 +1,5 @@
// Copyright © WireMock.Net
using System;
using System.IO;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
@@ -6,18 +6,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.OpenApi.YamlReader" Version="2.3.0" />
<ProjectReference Include="..\..\src\WireMock.Net.Abstractions\WireMock.Net.Abstractions.csproj" />
<ProjectReference Include="..\..\src\WireMock.Net.OpenApiParser\WireMock.Net.OpenApiParser.csproj" />
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<PackageReference Include="Microsoft.OpenApi" Version="2.3.0" PrivateAssets="All" />
<!--<PackageReference Include="Microsoft.OpenApi.YamlReader" Version="2.3.0" PrivateAssets="All" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="SharpYaml" Version="2.1.3" />-->
<PackageReference Include="Microsoft.OpenApi" Version="3.10.2" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
@@ -36,12 +36,6 @@
<StartupObject>Wiremock.Net.Service.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="SimMetrics.Net, Version=1.0.5.0, Culture=neutral, PublicKeyToken=c58dc06d59f3391b, processorArchitecture=MSIL">
<HintPath>..\..\packages\SimMetrics.Net.1.0.5\lib\net45\SimMetrics.Net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" />
@@ -91,13 +85,13 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="log4net">
<Version>3.3.0</Version>
<Version>3.3.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core">
<Version>2.3.9</Version>
<Version>2.3.11</Version>
</PackageReference>
<PackageReference Include="WireMock.Net">
<Version>1.25.0</Version>
<Version>2.12.0</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
@@ -11,7 +11,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
@@ -0,0 +1,34 @@
// Copyright © WireMock.Net
namespace WireMock.Admin.Requests;
/// <summary>
/// NearMiss Model: a mapping which did not (fully) match a request.
/// </summary>
public class NearMissModel
{
/// <summary>
/// The unique identifier of the log entry for the request. [Optional]
/// </summary>
public Guid? LogEntryGuid { get; set; }
/// <summary>
/// The request.
/// </summary>
public LogRequestModel? Request { get; set; }
/// <summary>
/// The unique identifier of the mapping.
/// </summary>
public Guid MappingGuid { get; set; }
/// <summary>
/// The title of the mapping.
/// </summary>
public string? MappingTitle { get; set; }
/// <summary>
/// The request match result, containing the score per matcher.
/// </summary>
public LogRequestMatchModel? RequestMatchResult { get; set; }
}
@@ -0,0 +1,14 @@
// Copyright © WireMock.Net
namespace WireMock.Admin.Requests;
/// <summary>
/// RequestCount Model
/// </summary>
public class RequestCountModel
{
/// <summary>
/// The number of requests which match the criteria.
/// </summary>
public int Count { get; set; }
}
@@ -32,7 +32,7 @@ public interface IRequestMessage
string? ProxyUrl { get; set; }
/// <summary>
/// Gets the DateTime.
/// The DateTime when this request was done.
/// </summary>
DateTime DateTime { get; }
@@ -52,7 +52,7 @@ public interface IResponseMessage
object? StatusCode { get; set; }
/// <summary>
/// Gets the DateTime.
/// The DateTime when this response was sent.
/// </summary>
DateTime DateTime { get; }
@@ -0,0 +1,34 @@
// Copyright © WireMock.Net
namespace WireMock.Matchers.Request;
/// <summary>
/// A mapping which did not (fully) match a request, together with the match result explaining why.
/// </summary>
public class NearMiss
{
/// <summary>
/// The request which was evaluated against the mapping.
/// </summary>
public required IRequestMessage RequestMessage { get; init; }
/// <summary>
/// The unique identifier of the log entry for the request. [Optional]
/// </summary>
public Guid? LogEntryGuid { get; init; }
/// <summary>
/// The unique identifier of the mapping.
/// </summary>
public required Guid MappingGuid { get; init; }
/// <summary>
/// The title of the mapping. [Optional]
/// </summary>
public string? MappingTitle { get; init; }
/// <summary>
/// The match result, containing the score per matcher.
/// </summary>
public required IRequestMatchResult RequestMatchResult { get; init; }
}
@@ -46,7 +46,7 @@ public interface IBodyData
/// <summary>
/// The body as Form UrlEncoded dictionary.
/// </summary>
IDictionary<string, string>? BodyAsFormUrlEncoded { get; set; }
IDictionary<string, WireMockList<string>>? BodyAsFormUrlEncoded { get; set; }
/// <summary>
/// The detected body type (detection based on body content).
@@ -107,6 +107,39 @@ public interface IWireMockServer : IDisposable
/// <returns>The <see cref="IReadOnlyList{ILogEntry}"/>.</returns>
IReadOnlyList<ILogEntry> FindLogEntries(params IRequestMatcher[] matchers);
/// <summary>
/// Get the log-entries for the requests which did not fully match any mapping.
/// This includes the requests which were served by a partial match when AllowPartialMapping is enabled.
/// </summary>
/// <returns>The <see cref="IReadOnlyList{ILogEntry}"/>.</returns>
IReadOnlyList<ILogEntry> FindUnmatchedLogEntries();
/// <summary>
/// Find the mappings which partially match the request (the near misses), ordered by best match first.
/// Perfect matches and mappings which do not match at all are excluded.
/// Use this to find out why a request did not match the expected mapping.
/// </summary>
/// <param name="requestMessage">The request to evaluate.</param>
/// <param name="maxResults">The maximum number of near misses to return. Default is 3.</param>
/// <returns>The <see cref="IReadOnlyList{NearMiss}"/>.</returns>
IReadOnlyList<NearMiss> FindNearMisses(IRequestMessage requestMessage, int maxResults = 3);
/// <summary>
/// Find the mappings which partially match the request of a log-entry (the near misses), ordered by best match first.
/// Perfect matches and mappings which do not match at all are excluded.
/// </summary>
/// <param name="logEntry">The log-entry for which to evaluate the request.</param>
/// <param name="maxResults">The maximum number of near misses to return. Default is 3.</param>
/// <returns>The <see cref="IReadOnlyList{NearMiss}"/>.</returns>
IReadOnlyList<NearMiss> FindNearMisses(ILogEntry logEntry, int maxResults = 3);
/// <summary>
/// Find the near misses for all requests which did not fully match any mapping.
/// </summary>
/// <param name="maxResultsPerRequest">The maximum number of near misses to return per request. Default is 3.</param>
/// <returns>The <see cref="IReadOnlyList{NearMiss}"/>.</returns>
IReadOnlyList<NearMiss> FindNearMissesForUnmatchedRequests(int maxResultsPerRequest = 3);
/// <summary>
/// Reads a static mapping file and adds or updates a single mapping.
///
@@ -1,11 +1,11 @@
// Copyright © WireMock.Net
using System.Linq;
namespace WireMock.Types;
/// <summary>
/// A special List which overrides the ToString() to return first value.
/// A special List which overrides the ToString() to return first value in case of a single element.
/// Else it will return a comma separated list of all values.
/// If null or empty, it will return an empty string.
/// </summary>
/// <typeparam name="T">The generic type</typeparam>
/// <seealso cref="List{T}" />
@@ -46,6 +46,98 @@ public class WireMockList<T> : List<T>
/// <param name="values">The values to set.</param>
public static implicit operator WireMockList<T>(T[] values) => new(values);
/// <summary>
/// Operator for equality comparison from WireMockList to T
/// </summary>
public static bool operator ==(WireMockList<T>? left, T? right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if (left?.Count == 1 && Equals(left[0], right))
{
return true;
}
return false;
}
/// <summary>
/// Operator for equality comparison from T to WireMockList
/// </summary>
public static bool operator ==(T? left, WireMockList<T>? right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if (right?.Count == 1 && Equals(left, right[0]))
{
return true;
}
return false;
}
/// <summary>
/// Operator for inequality comparison from WireMockList to T
/// </summary>
public static bool operator !=(WireMockList<T>? left, T? right) => !(left == right);
/// <summary>
/// Operator for inequality comparison from T to WireMockList
/// </summary>
public static bool operator !=(T? left, WireMockList<T>? right) => !(left == right);
/// <summary>
/// Determines whether the specified object is equal to the current instance.
/// Two <see cref="WireMockList{T}"/> instances are equal if they contain the same elements in the same order.
/// </summary>
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj is WireMockList<T> other)
{
if (Count != other.Count)
{
return false;
}
for (var i = 0; i < Count; i++)
{
if (!Equals(this[i], other[i]))
{
return false;
}
}
return true;
}
return false;
}
/// <summary>
/// Returns a hash code for this instance based on its elements.
/// </summary>
public override int GetHashCode()
{
var hashCode = 17;
foreach (var item in this)
{
hashCode = hashCode * 31 + (item?.GetHashCode() ?? 0);
}
return hashCode;
}
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
@@ -62,10 +154,10 @@ public class WireMockList<T> : List<T>
return strValue;
}
return this[0]?.ToString();
return this[0]?.ToString() ?? string.Empty;
default:
var strings = this.Select(x => x as string ?? x?.ToString());
var strings = this.Select(x => x as string ?? x?.ToString() ?? string.Empty);
return string.Join(", ", strings);
}
}
@@ -39,7 +39,7 @@
</ItemGroup>-->
<ItemGroup>
<PackageReference Include="FluentBuilder" Version="0.14.0">
<PackageReference Include="FluentBuilder" Version="0.15.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
@@ -45,7 +45,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="13.4.0" />
<PackageReference Include="Aspire.Hosting" Version="13.4.3" />
<!-- CVE-2026-48109 / CVE-2026-92707 -->
<PackageReference Include="MessagePack" Version="2.5.303" />
</ItemGroup>
<ItemGroup>
@@ -25,7 +25,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JsonConverter.Abstractions" Version="0.12.0" />
<PackageReference Include="JsonConverter.Abstractions" Version="0.13.0" />
</ItemGroup>
<ItemGroup>
@@ -70,7 +70,7 @@ public class CSharpCodeMatcher : ICSharpCodeMatcher
/// <inheritdoc />
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(CSharpCodeMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}, " +
@@ -126,7 +126,7 @@ public class SystemTextJsonPathMatcher : ISystemTextJsonPathMatcher
/// <inheritdoc />
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(SystemTextJsonPathMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}, " +
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>A SystemTextJsonPathMatcher which can be used to match WireMock.Net Requests using JsonPath.Net.</Description>
@@ -35,7 +35,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="JsonPath.Net" Version="3.0.2" />
<!-- Downgraded from 3.0.2 to 2.2.0 to avoid Open Source Maintenance Fee issues -->
<PackageReference Include="JsonPath.Net" Version="2.2.0" />
</ItemGroup>
</Project>
</Project>
@@ -36,7 +36,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Stef.Validation" Version="0.2.0" />
<PackageReference Include="Stef.Validation" Version="0.3.0" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
@@ -0,0 +1,14 @@
// Copyright © WireMock.Net
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http;
namespace WireMock.Extensions;
internal static class HttpContextExtensions
{
internal static bool TryGet<T>(this HttpContext httpContext, [NotNullWhen(true)] out T? value)
{
return httpContext.Items.TryGetValue(nameof(T), out value);
}
}
@@ -80,10 +80,10 @@ public class ContentTypeMatcher : WildcardMatcher
/// <inheritdoc />
public override string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(ContentTypeMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MappingConverterUtils.ToCSharpCodeArguments(_patterns)}, " +
$"new AnyOfTypes.AnyOf<string, WireMock.Models.StringPattern>[] {{ {MappingConverterUtils.ToCSharpCodeArguments(_patterns)} }}, " +
$"{CSharpFormatter.ToCSharpBooleanLiteral(IgnoreCase)}" +
$")";
}
@@ -95,7 +95,7 @@ public class ExactMatcher : IStringMatcher, IIgnoreCaseMatcher
/// <inheritdoc />
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(ExactMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ToCSharpBooleanLiteral(IgnoreCase)}, " +
@@ -1,10 +1,10 @@
// Copyright © WireMock.Net
using System.Linq;
using AnyOfTypes;
using Stef.Validation;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Types;
using WireMock.Util;
namespace WireMock.Matchers;
@@ -21,7 +21,7 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
/// <inheritdoc />
public MatchBehaviour MatchBehaviour { get; }
private readonly List<(WildcardMatcher Key, WildcardMatcher? Value)> _pairs = [];
private readonly List<(WildcardMatcher Key, WildcardMatcher[]? Values)> KeyValueMatchers = [];
/// <summary>
/// Initializes a new instance of the <see cref="FormUrlEncodedMatcher"/> class.
@@ -91,9 +91,10 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
{
foreach (var nameValue in nameValueCollection)
{
var keyMatcher = new WildcardMatcher(MatchBehaviour.AcceptOnMatch, [nameValue.Key], ignoreCase, MatchOperator);
var valueMatcher = new WildcardMatcher(MatchBehaviour.AcceptOnMatch, [nameValue.Value], ignoreCase, MatchOperator);
_pairs.Add((keyMatcher, valueMatcher));
var keyMatcher = new WildcardMatcher(MatchBehaviour.AcceptOnMatch, nameValue.Key, ignoreCase);
var valueMatchers = nameValue.Value.Select(value => new WildcardMatcher(MatchBehaviour.AcceptOnMatch, value, ignoreCase)).ToArray();
KeyValueMatchers.Add((keyMatcher, valueMatchers));
}
}
}
@@ -119,34 +120,48 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score));
}
private bool[] GetMatches(IDictionary<string, string> inputNameValueCollection)
private List<double> GetMatches(IDictionary<string, WireMockList<string>> inputNameValueCollection)
{
var matches = new List<bool>();
if (_pairs.Count > inputNameValueCollection.Count)
var inputPairs = inputNameValueCollection.ToArray();
var rowCount = inputPairs.Length;
var columnCount = KeyValueMatchers.Count;
if (rowCount == 0 && columnCount == 0)
{
matches.AddRange(Enumerable.Repeat(false, _pairs.Count - inputNameValueCollection.Count));
return [];
}
foreach (var inputKeyValuePair in inputNameValueCollection)
var matrix = new double[rowCount][];
for (var row = 0; row < rowCount; row++)
{
var match = false;
foreach (var pair in _pairs)
matrix[row] = new double[columnCount];
var inputKeyValuePair = inputPairs[row];
var inputKey = inputKeyValuePair.Key;
var inputValues = inputKeyValuePair.Value;
for (var column = 0; column < columnCount; column++)
{
var keyMatchResult = pair.Key.IsMatch(inputKeyValuePair.Key).IsPerfect();
if (keyMatchResult)
{
match = pair.Value?.IsMatch(inputKeyValuePair.Value).IsPerfect() ?? false;
if (match)
{
break;
}
}
}
var (keyMatcher, valuesMatchers) = KeyValueMatchers[column];
matches.Add(match);
var keyScore = keyMatcher.IsMatch(inputKey).Score;
var valueScore = valuesMatchers != null ? MatchScores.ToScore(inputValues, valuesMatchers) : MatchScores.Perfect;
matrix[row][column] = Math.Min(keyScore, valueScore);
}
}
return matches.ToArray();
var rowScores = rowCount == 0 ? [] : matrix.Select(row => row.Length == 0 ? MatchScores.Mismatch : row.Max()).ToList();
var columnScores = new List<double>();
for (var column = 0; column < columnCount; column++)
{
columnScores.Add(rowCount == 0 ? MatchScores.Mismatch : matrix.Max(row => row[column]));
}
rowScores.AddRange(columnScores);
return rowScores;
}
/// <inheritdoc />
@@ -167,10 +182,10 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
/// <inheritdoc />
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(FormUrlEncodedMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MappingConverterUtils.ToCSharpCodeArguments(_patterns)}, " +
$"new AnyOfTypes.AnyOf<string, WireMock.Models.StringPattern>[] {{ {MappingConverterUtils.ToCSharpCodeArguments(_patterns)} }}, " +
$"{CSharpFormatter.ToCSharpBooleanLiteral(IgnoreCase)}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}" +
$")";
@@ -76,7 +76,7 @@ public class FuncMatcher : IFuncMatcher
public string GetCSharpCodeArguments()
{
var funcType = _stringFunc != null ? "Func<string?, bool>" : "Func<byte[]?, bool>";
return $"new {Name}" +
return $"new {typeof(FuncMatcher).FullName}" +
$"(" +
$"/* {funcType} function */, " +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}" +
@@ -119,7 +119,7 @@ public class JsonPathMatcher : IJsonPathMatcher
/// <inheritdoc />
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(JsonPathMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}, " +
@@ -119,7 +119,7 @@ public class JmesPathMatcher : IStringMatcher, IObjectMatcher
/// <inheritdoc />
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(JmesPathMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}, " +
@@ -110,7 +110,7 @@ public class JsonMatcher : IJsonMatcher
/// <inheritdoc />
public virtual string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(JsonMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ConvertToAnonymousObjectDefinition(Value, 3)}, " +
@@ -41,7 +41,7 @@ public class JsonPartialMatcher : AbstractJsonPartialMatcher
/// <inheritdoc />
public override string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(JsonPartialMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ConvertToAnonymousObjectDefinition(Value, 3)}, " +
@@ -41,7 +41,7 @@ public class JsonPartialWildcardMatcher : AbstractJsonPartialMatcher
/// <inheritdoc />
public override string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(JsonPartialWildcardMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ConvertToAnonymousObjectDefinition(Value, 3)}, " +
@@ -141,7 +141,7 @@ public class LinqMatcher : IObjectMatcher, IStringMatcher
/// <inheritdoc />
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(LinqMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}, " +
@@ -2,6 +2,7 @@
using Stef.Validation;
using WireMock.Matchers.Helpers;
using WireMock.Types;
using WireMock.Util;
namespace WireMock.Matchers.Request;
@@ -34,7 +35,7 @@ public class RequestMessageBodyMatcher : IRequestMatcher
/// <summary>
/// The body data function for FormUrlEncoded
/// </summary>
public Func<IDictionary<string, string>?, bool>? MatchOnBodyAsFormUrlEncodedFunc { get; }
public Func<IDictionary<string, WireMockList<string>>?, bool>? MatchOnBodyAsFormUrlEncodedFunc { get; }
/// <summary>
/// The matchers.
@@ -116,7 +117,7 @@ public class RequestMessageBodyMatcher : IRequestMatcher
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
/// <param name="func">The function.</param>
public RequestMessageBodyMatcher(Func<IDictionary<string, string>?, bool> func)
public RequestMessageBodyMatcher(Func<IDictionary<string, WireMockList<string>>?, bool> func)
{
MatchOnBodyAsFormUrlEncodedFunc = Guard.NotNull(func);
}
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System.Linq;
using Stef.Validation;
using WireMock.Types;
@@ -54,7 +53,10 @@ public class RequestMessageParamMatcher : IRequestMatcher
/// <param name="ignoreCase">Defines if the key should be matched using case-ignore.</param>
/// <param name="values">The values.</param>
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, string key, bool ignoreCase, params string[]? values) :
this(matchBehaviour, key, ignoreCase, values?.Select(value => new ExactMatcher(matchBehaviour, ignoreCase, MatchOperator.And, value)).Cast<IStringMatcher>().ToArray())
// Note: the inner ExactMatcher must use AcceptOnMatch.
// The MatchBehaviour (e.g. RejectOnMatch) is applied once by this matcher's GetMatchingScore.
// Passing matchBehaviour here as well would apply the conversion twice and invert RejectOnMatch.
this(matchBehaviour, key, ignoreCase, values?.Select(value => new ExactMatcher(MatchBehaviour.AcceptOnMatch, ignoreCase, MatchOperator.And, value)).Cast<IStringMatcher>().ToArray())
{
}
@@ -113,36 +115,6 @@ public class RequestMessageParamMatcher : IRequestMatcher
}
// Return the score based on Matchers and valuesPresentInRequestMessage
return CalculateScore(Matchers, valuesPresentInRequestMessage);
}
private static double CalculateScore(IReadOnlyList<IStringMatcher> matchers, WireMockList<string> valuesPresentInRequestMessage)
{
var total = new List<double>();
// If the total patterns in all matchers > values in message, use the matcher as base
if (matchers.Sum(m => m.GetPatterns().Length) > valuesPresentInRequestMessage.Count)
{
foreach (var matcher in matchers)
{
double score = 0d;
foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage)
{
score += matcher.IsMatch(valuePresentInRequestMessage).Score / matcher.GetPatterns().Length;
}
total.Add(score);
}
}
else
{
foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage)
{
var score = matchers.Max(m => m.IsMatch(valuePresentInRequestMessage).Score);
total.Add(score);
}
}
return total.Any() ? MatchScores.ToScore(total, MatchOperator.Average) : 0;
return MatchScores.ToScore(valuesPresentInRequestMessage, Matchers.ToArray());
}
}
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System.Linq;
using AnyOfTypes;
using SimMetrics.Net;
using SimMetrics.Net.API;
@@ -92,10 +91,10 @@ public class SimMetricsMatcher : IStringMatcher
/// <inheritdoc />
public virtual string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(SimMetricsMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MappingConverterUtils.ToCSharpCodeArguments(_patterns)}, " +
$"new AnyOfTypes.AnyOf<string, WireMock.Models.StringPattern>[] {{ {MappingConverterUtils.ToCSharpCodeArguments(_patterns)} }}, " +
$"{_simMetricType.GetFullyQualifiedEnumValue()}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}" +
$")";
@@ -115,7 +115,7 @@ public class SystemTextJsonMatcher : IJsonMatcher
/// <inheritdoc />
public virtual string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(SystemTextJsonMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ConvertToAnonymousObjectDefinition(Value, 3)}, " +
@@ -41,7 +41,7 @@ public class SystemTextJsonPartialMatcher : AbstractSystemTextJsonPartialMatcher
/// <inheritdoc />
public override string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(SystemTextJsonPartialMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ConvertToAnonymousObjectDefinition(Value, 3)}, " +
@@ -41,7 +41,7 @@ public class SystemTextJsonPartialWildcardMatcher : AbstractSystemTextJsonPartia
/// <inheritdoc />
public override string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(SystemTextJsonPartialWildcardMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ConvertToAnonymousObjectDefinition(Value, 3)}, " +
@@ -101,7 +101,7 @@ public class XPathMatcher : IStringMatcher
/// <inheritdoc />
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
return $"new {typeof(XPathMatcher).FullName}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}, " +
@@ -0,0 +1,61 @@
// Copyright © WireMock.Net
using System.Text.RegularExpressions;
using WireMock.Matchers;
namespace WireMock.Owin;
internal sealed class AdminPaths(string? adminPath) : IAdminPaths
{
private const string DefaultAdminPathPrefix = "/__admin";
private readonly string _prefix = adminPath ?? DefaultAdminPathPrefix;
public string Files => $"{_prefix}/files";
public string Health => $"{_prefix}/health";
public string Mappings => $"{_prefix}/mappings";
public string MappingsCode => $"{_prefix}/mappings/code";
public string MappingsWireMockOrg => $"{_prefix}/mappings/wiremock.org";
public string Requests => $"{_prefix}/requests";
public string Settings => $"{_prefix}/settings";
public string Scenarios => $"{_prefix}/scenarios";
public string OpenApi => $"{_prefix}/openapi";
public string ProtoDefinitions => $"{_prefix}/protodefinitions";
private string PrefixRegexEscaped => Regex.Escape(_prefix);
public RegexMatcher MappingsGuidPathMatcher => new($@"^{PrefixRegexEscaped}\/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($@"^{PrefixRegexEscaped}\/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($@"^{PrefixRegexEscaped}\/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($@"^{PrefixRegexEscaped}\/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($@"^{PrefixRegexEscaped}\/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 RequestsGuidNearMissesPathMatcher => new($@"^{PrefixRegexEscaped}\/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}})\/near-misses$");
public RegexMatcher ScenariosNameMatcher => new($@"^{PrefixRegexEscaped}\/scenarios\/.+$");
public RegexMatcher ScenariosNameWithStateMatcher => new($@"^{PrefixRegexEscaped}\/scenarios\/.+\/state$");
public RegexMatcher ScenariosNameWithResetMatcher => new($@"^{PrefixRegexEscaped}\/scenarios\/.+\/reset$");
public RegexMatcher FilesFilenamePathMatcher => new($@"^{PrefixRegexEscaped}\/files\/.+$");
public RegexMatcher ProtoDefinitionsIdPathMatcher => new($@"^{PrefixRegexEscaped}\/protodefinitions\/.+$");
public bool Includes(string? path) => path?.StartsWith($"{_prefix}/") ?? false;
public override string ToString() => _prefix;
}
@@ -66,6 +66,7 @@ internal partial class AspNetCoreSelfHost
.ConfigureServices(services =>
{
services.AddSingleton(_wireMockMiddlewareOptions);
services.AddSingleton(_wireMockMiddlewareOptions.AdminPaths);
services.AddSingleton<IMappingMatcher, MappingMatcher>();
services.AddSingleton<IRandomizerDoubleBetween0And1, RandomizerDoubleBetween0And1>();
services.AddSingleton<IOwinRequestMapper, OwinRequestMapper>();
@@ -0,0 +1,50 @@
using WireMock.Matchers;
namespace WireMock.Owin;
internal interface IAdminPaths
{
string Files { get; }
string Health { get; }
string Mappings { get; }
string MappingsCode { get; }
string MappingsWireMockOrg { get; }
RegexMatcher MappingsGuidPathMatcher { get; }
RegexMatcher MappingsGuidEnablePathMatcher { get; }
RegexMatcher MappingsGuidDisablePathMatcher { get; }
RegexMatcher MappingsCodeGuidPathMatcher { get; }
RegexMatcher RequestsGuidPathMatcher { get; }
RegexMatcher RequestsGuidNearMissesPathMatcher { get; }
RegexMatcher ScenariosNameMatcher { get; }
RegexMatcher ScenariosNameWithStateMatcher { get; }
RegexMatcher ScenariosNameWithResetMatcher { get; }
RegexMatcher FilesFilenamePathMatcher { get; }
RegexMatcher ProtoDefinitionsIdPathMatcher { get; }
string Requests { get; }
string Settings { get; }
string Scenarios { get; }
string OpenApi { get; }
string ProtoDefinitions { get; }
bool Includes(string? path);
}
@@ -5,4 +5,6 @@ namespace WireMock.Owin;
internal interface IMappingMatcher
{
(MappingMatcherResult? Match, MappingMatcherResult? Partial) FindBestMatch(RequestMessage request);
IReadOnlyList<MappingMatcherResult> FindNearMisses(IRequestMessage request, int maxResults);
}
@@ -108,4 +108,6 @@ internal interface IWireMockMiddlewareOptions
/// Set this property to customize how objects are serialized to and deserialized from JSON during mapping.
/// </remarks>
IJsonConverter DefaultJsonSerializer { get; set; }
IAdminPaths AdminPaths { get; set; }
}
@@ -12,7 +12,7 @@ namespace WireMock.Owin.Mappers;
/// <summary>
/// OwinRequestMapper
/// </summary>
internal class OwinRequestMapper : IOwinRequestMapper
internal class OwinRequestMapper(IDateTimeUtils dateTimeUtils) : IOwinRequestMapper
{
/// <inheritdoc />
public async Task<RequestMessage> MapAsync(HttpContext context, IWireMockMiddlewareOptions options)
@@ -72,7 +72,7 @@ internal class OwinRequestMapper : IOwinRequestMapper
await request.HttpContext.Connection.GetClientCertificateAsync()
)
{
DateTime = DateTime.UtcNow
DateTime = dateTimeUtils.UtcNow
};
}
@@ -83,6 +83,39 @@ internal class MappingMatcher(IWireMockMiddlewareOptions options, IRandomizerDou
return (match, partialMatch);
}
public IReadOnlyList<MappingMatcherResult> FindNearMisses(IRequestMessage request, int maxResults)
{
Guard.NotNull(request);
var nearMisses = new List<MappingMatcherResult>();
// Probability is not taken into account here: a mapping which was skipped based on probability can still be a near miss.
foreach (var mapping in _options.Mappings.Values.Where(m => !m.IsAdminInterface && !m.IsDisabled && m.TimeSettings.IsValid()))
{
try
{
var requestMatchResult = mapping.GetRequestMatchResult(request, GetNextState(mapping));
if (requestMatchResult.MatchDetails.All(md => md.Exception == null))
{
nearMisses.Add(new MappingMatcherResult(mapping, requestMatchResult));
}
}
catch (Exception ex)
{
LogException(mapping, ex);
}
}
// A near miss is a mapping which matches the request partially: perfect matches and complete mismatches are excluded.
return nearMisses
.Where(nm => !nm.RequestMatchResult.IsPerfectMatch && nm.RequestMatchResult.AverageTotalScore > 0.0)
.OrderBy(nm => nm.RequestMatchResult)
.ThenBy(nm => nm.Mapping.Priority)
.ThenByDescending(nm => nm.Mapping.UpdatedAt)
.Take(maxResults)
.ToArray();
}
private void LogException(IMapping mapping, Exception ex)
{
_options.Logger.Error($"Getting a Request MatchResult for Mapping '{mapping.Guid}' failed. This mapping will not be evaluated. Exception: {ex}");
@@ -21,6 +21,7 @@ internal class WireMockMiddleware(
RequestDelegate next,
#pragma warning restore CS9113 // Parameter is unread.
IWireMockMiddlewareOptions options,
IAdminPaths adminPaths,
IOwinRequestMapper requestMapper,
IOwinResponseMapper responseMapper,
IMappingMatcher mappingMatcher,
@@ -64,7 +65,7 @@ internal class WireMockMiddleware(
Activity? activity = null;
// Check if we should trace this request (optionally exclude admin requests)
var shouldTrace = tracingEnabled && !(excludeAdmin && request.Path.StartsWith("/__admin/"));
var shouldTrace = tracingEnabled && !(excludeAdmin && adminPaths.Includes(request.Path));
if (shouldTrace)
{
@@ -170,6 +171,12 @@ internal class WireMockMiddleware(
}
finally
{
// Set the DateTime from the Response to the current DateTime
if (response is ResponseMessage responseMessage)
{
responseMessage.DateTime = dateTimeUtils.UtcNow;
}
logger.LogRequestAndResponse(logRequest, request, response, result.Match, result.Partial, activity);
try
@@ -254,4 +261,4 @@ internal class WireMockMiddleware(
scenario.Finished = mapping.NextState == null;
});
}
}
}
@@ -9,39 +9,44 @@ using WireMock.Util;
namespace WireMock.Owin;
internal class WireMockMiddlewareLogger(
IWireMockMiddlewareOptions _options,
LogEntryMapper _logEntryMapper,
IGuidUtils _guidUtils
IWireMockMiddlewareOptions options,
LogEntryMapper logEntryMapper,
IGuidUtils guidUtils,
IAdminPaths adminPaths,
IDateTimeUtils dateTimeUtils
) : IWireMockMiddlewareLogger
{
public void LogRequestAndResponse(bool logRequest, RequestMessage request, IResponseMessage? response, MappingMatcherResult? match, MappingMatcherResult? partialMatch, Activity? activity)
{
var mapping = match?.Mapping;
var partialMapping = partialMatch?.Mapping;
var logEntry = new LogEntry
{
Guid = _guidUtils.NewGuid(),
Guid = guidUtils.NewGuid(),
RequestMessage = request,
ResponseMessage = response,
MappingGuid = match?.Mapping?.Guid,
MappingTitle = match?.Mapping?.Title,
MappingGuid = mapping?.Guid,
MappingTitle = mapping?.Title,
RequestMatchResult = match?.RequestMatchResult,
PartialMappingGuid = partialMatch?.Mapping?.Guid,
PartialMappingTitle = partialMatch?.Mapping?.Title,
PartialMappingGuid = partialMapping?.Guid,
PartialMappingTitle = partialMapping?.Title,
PartialMatchResult = partialMatch?.RequestMatchResult
};
WireMockActivitySource.EnrichWithLogEntry(activity, logEntry, _options.ActivityTracingOptions);
WireMockActivitySource.EnrichWithLogEntry(activity, logEntry, options.ActivityTracingOptions);
activity?.Dispose();
LogLogEntry(logEntry, logRequest);
try
{
if (_options.SaveUnmatchedRequests == true && match?.RequestMatchResult is not { IsPerfectMatch: true })
if (options.SaveUnmatchedRequests == true && match?.RequestMatchResult is not { IsPerfectMatch: true })
{
var filename = $"{logEntry.Guid}.LogEntry.json";
_options.FileSystemHandler?.WriteUnmatchedRequest(filename, _options.DefaultJsonSerializer.Serialize(logEntry));
options.FileSystemHandler?.WriteUnmatchedRequest(filename, options.DefaultJsonSerializer.Serialize(logEntry));
}
}
catch
@@ -52,35 +57,36 @@ internal class WireMockMiddlewareLogger(
public void LogLogEntry(LogEntry entry, bool addRequest)
{
_options.Logger.DebugRequestResponse(_logEntryMapper.Map(entry), entry.RequestMessage?.Path.StartsWith("/__admin/") == true);
var isAdminRequest = adminPaths.Includes(entry.RequestMessage?.Path);
options.Logger.DebugRequestResponse(logEntryMapper.Map(entry), isAdminRequest);
// If addRequest is set to true and MaxRequestLogCount is null or does have a value greater than 0, try to add a new request log.
if (addRequest && _options.MaxRequestLogCount is null or > 0)
if (addRequest && options.MaxRequestLogCount is null or > 0)
{
TryAddLogEntry(entry);
}
// In case MaxRequestLogCount has a value greater than 0, try to delete existing request logs based on the count.
if (_options.MaxRequestLogCount is > 0)
if (options.MaxRequestLogCount is > 0)
{
var logEntries = _options.LogEntries.ToList();
var logEntries = options.LogEntries.ToList();
foreach (var logEntry in logEntries
.OrderBy(le => le.RequestMessage?.DateTime ?? le.ResponseMessage?.DateTime)
.Take(logEntries.Count - _options.MaxRequestLogCount.Value))
.OrderBy(le => le.RequestMessage?.DateTime)
.Take(logEntries.Count - options.MaxRequestLogCount.Value))
{
TryRemoveLogEntry(logEntry);
}
}
// In case RequestLogExpirationDuration has a value greater than 0, try to delete existing request logs based on the date.
if (_options.RequestLogExpirationDuration is > 0)
// In case RequestLogExpirationDuration has a value greater than 0, try to delete existing request logs based on the RequestMessage date.
if (options.RequestLogExpirationDuration is > 0)
{
var logEntries = _options.LogEntries.ToList();
var logEntries = options.LogEntries.ToList();
var checkTime = DateTime.UtcNow.AddHours(-_options.RequestLogExpirationDuration.Value);
foreach (var logEntry in logEntries.Where(le => le.RequestMessage?.DateTime < checkTime || le.ResponseMessage?.DateTime < checkTime))
var checkTime = dateTimeUtils.UtcNow.AddHours(-options.RequestLogExpirationDuration.Value);
foreach (var logEntry in logEntries.Where(le => le.RequestMessage?.DateTime < checkTime))
{
TryRemoveLogEntry(logEntry);
}
@@ -91,7 +97,7 @@ internal class WireMockMiddlewareLogger(
{
try
{
_options.LogEntries.Add(logEntry);
options.LogEntries.Add(logEntry);
}
catch
{
@@ -103,7 +109,7 @@ internal class WireMockMiddlewareLogger(
{
try
{
_options.LogEntries.Remove(logEntry);
options.LogEntries.Remove(logEntry);
}
catch
{
@@ -115,4 +115,7 @@ internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions
/// <inheritdoc />
public IJsonConverter DefaultJsonSerializer { get; set; } = new NewtonsoftJsonConverter();
/// <inheritdoc />
public IAdminPaths AdminPaths { get; set; } = new AdminPaths(null);
}
@@ -34,6 +34,7 @@ internal static class WireMockMiddlewareOptionsHelper
options.QueryParameterMultipleValueSupport = settings.QueryParameterMultipleValueSupport;
options.RequestLogExpirationDuration = settings.RequestLogExpirationDuration;
options.SaveUnmatchedRequests = settings.SaveUnmatchedRequests;
options.AdminPaths = new AdminPaths(settings.AdminPath);
#if USE_ASPNETCORE
options.AdditionalServiceRegistration = settings.AdditionalServiceRegistration;
@@ -19,7 +19,7 @@ public partial class Request
{
Guard.NotNullOrEmpty(matchers);
_requestMatchers.Add(new RequestMessageClientIPMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, matchers));
_requestMatchers.Add(new RequestMessageClientIPMatcher(MatchBehaviour.AcceptOnMatch, matchOperator, matchers));
return this;
}
@@ -5,6 +5,7 @@
using Stef.Validation;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Types;
using WireMock.Util;
namespace WireMock.RequestBuilders;
@@ -84,7 +85,7 @@ public partial class Request
}
/// <inheritdoc />
public IRequestBuilder WithBody(Func<IDictionary<string, string>?, bool> func)
public IRequestBuilder WithBody(Func<IDictionary<string, WireMockList<string>>?, bool> func)
{
_requestMatchers.Add(new RequestMessageBodyMatcher(Guard.NotNull(func)));
return this;
@@ -11,7 +11,6 @@ using WireMock.RequestBuilders;
using WireMock.Settings;
using WireMock.Transformers;
using WireMock.Types;
using WireMock.Util;
namespace WireMock.ResponseBuilders;
@@ -86,7 +85,11 @@ public partial class Response : IResponseBuilder
[PublicAPI]
public static IResponseBuilder Create(ResponseMessage? responseMessage = null)
{
var message = responseMessage ?? new ResponseMessage();
var message = responseMessage ?? new ResponseMessage
{
DateTime = DateTime.UtcNow // Initially, this is set to the current time, but will be updated to the real time once the response has been sent.
};
return new Response(message);
}
@@ -2,7 +2,6 @@
// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System.Linq;
using Stef.Validation;
using WireMock.ResponseBuilders;
using WireMock.Types;
@@ -67,7 +67,8 @@ internal class WebSocketResponseProvider(WebSocketBuilder builder, IGuidUtils gu
builder,
options,
logger,
guidUtils
guidUtils,
dateTimeUtils
);
// Add to registry
@@ -14,60 +14,7 @@ internal class LogEntryMapper(IWireMockMiddlewareOptions options)
{
public LogEntryModel Map(ILogEntry logEntry)
{
LogRequestModel? logRequestModel = null;
if (logEntry.RequestMessage != null)
{
logRequestModel = new LogRequestModel
{
DateTime = logEntry.RequestMessage.DateTime,
ClientIP = logEntry.RequestMessage.ClientIP,
Path = logEntry.RequestMessage.Path,
AbsolutePath = logEntry.RequestMessage.AbsolutePath,
Url = logEntry.RequestMessage.Url,
AbsoluteUrl = logEntry.RequestMessage.AbsoluteUrl,
ProxyUrl = logEntry.RequestMessage.ProxyUrl,
Query = logEntry.RequestMessage.Query,
Method = logEntry.RequestMessage.Method,
HttpVersion = logEntry.RequestMessage.HttpVersion,
Headers = logEntry.RequestMessage.Headers,
Cookies = logEntry.RequestMessage.Cookies
};
if (logEntry.RequestMessage.BodyData != null)
{
logRequestModel.DetectedBodyType = logEntry.RequestMessage.BodyData.DetectedBodyType?.ToString();
logRequestModel.DetectedBodyTypeFromContentType = logEntry.RequestMessage.BodyData.DetectedBodyTypeFromContentType?.ToString();
switch (logEntry.RequestMessage.BodyData.DetectedBodyType)
{
case BodyType.String:
case BodyType.FormUrlEncoded:
case BodyType.WebSocketText:
case BodyType.WebSocketClose:
logRequestModel.Body = logEntry.RequestMessage.BodyData.BodyAsString;
break;
case BodyType.Json:
logRequestModel.Body = logEntry.RequestMessage.BodyData.BodyAsString; // In case of Json, do also save the Body as string (backwards compatible)
logRequestModel.BodyAsJson = logEntry.RequestMessage.BodyData.BodyAsJson;
break;
case BodyType.Bytes:
case BodyType.WebSocketBinary:
logRequestModel.BodyAsBytes = logEntry.RequestMessage.BodyData.BodyAsBytes;
break;
}
logRequestModel.BodyEncoding = logEntry.RequestMessage.BodyData.Encoding != null
? new EncodingModel
{
EncodingName = logEntry.RequestMessage.BodyData.Encoding.EncodingName,
CodePage = logEntry.RequestMessage.BodyData.Encoding.CodePage,
WebName = logEntry.RequestMessage.BodyData.Encoding.WebName
}
: null;
}
}
var logRequestModel = logEntry.RequestMessage != null ? MapRequest(logEntry.RequestMessage) : null;
LogResponseModel? logResponseModel = null;
if (logEntry.ResponseMessage != null)
@@ -123,6 +70,74 @@ internal class LogEntryMapper(IWireMockMiddlewareOptions options)
};
}
public NearMissModel Map(NearMiss nearMiss)
{
return new NearMissModel
{
LogEntryGuid = nearMiss.LogEntryGuid,
Request = MapRequest(nearMiss.RequestMessage),
MappingGuid = nearMiss.MappingGuid,
MappingTitle = nearMiss.MappingTitle,
RequestMatchResult = Map(nearMiss.RequestMatchResult)
};
}
private static LogRequestModel MapRequest(IRequestMessage requestMessage)
{
var logRequestModel = new LogRequestModel
{
DateTime = requestMessage.DateTime,
ClientIP = requestMessage.ClientIP,
Path = requestMessage.Path,
AbsolutePath = requestMessage.AbsolutePath,
Url = requestMessage.Url,
AbsoluteUrl = requestMessage.AbsoluteUrl,
ProxyUrl = requestMessage.ProxyUrl,
Query = requestMessage.Query,
Method = requestMessage.Method,
HttpVersion = requestMessage.HttpVersion,
Headers = requestMessage.Headers,
Cookies = requestMessage.Cookies
};
if (requestMessage.BodyData != null)
{
logRequestModel.DetectedBodyType = requestMessage.BodyData.DetectedBodyType?.ToString();
logRequestModel.DetectedBodyTypeFromContentType = requestMessage.BodyData.DetectedBodyTypeFromContentType?.ToString();
switch (requestMessage.BodyData.DetectedBodyType)
{
case BodyType.String:
case BodyType.FormUrlEncoded:
case BodyType.WebSocketText:
case BodyType.WebSocketClose:
logRequestModel.Body = requestMessage.BodyData.BodyAsString;
break;
case BodyType.Json:
logRequestModel.Body = requestMessage.BodyData.BodyAsString; // In case of Json, do also save the Body as string (backwards compatible)
logRequestModel.BodyAsJson = requestMessage.BodyData.BodyAsJson;
break;
case BodyType.Bytes:
case BodyType.WebSocketBinary:
logRequestModel.BodyAsBytes = requestMessage.BodyData.BodyAsBytes;
break;
}
logRequestModel.BodyEncoding = requestMessage.BodyData.Encoding != null
? new EncodingModel
{
EncodingName = requestMessage.BodyData.Encoding.EncodingName,
CodePage = requestMessage.BodyData.Encoding.CodePage,
WebName = requestMessage.BodyData.Encoding.WebName
}
: null;
}
return logRequestModel;
}
private void MapBody(ILogEntry logEntry, LogResponseModel logResponseModel)
{
switch (logEntry.ResponseMessage?.BodyData?.DetectedBodyType)
@@ -146,7 +146,7 @@ internal class MappingConverter(MatcherMapper mapper)
case JsonMatcher jsonMatcher:
{
var matcherType = jsonMatcher.GetType().Name;
var matcherType = jsonMatcher.GetType().FullName;
sb.AppendLine($" .WithBody(new {matcherType}(");
sb.AppendLine($" value: {ConvertToAnonymousObjectDefinition(jsonMatcher.Value, 3)},");
sb.AppendLine($" ignoreCase: {ToCSharpBooleanLiteral(jsonMatcher.IgnoreCase)},");
@@ -162,14 +162,18 @@ internal class MappingConverter(MatcherMapper mapper)
// Guid
sb.AppendLine($" .WithGuid(\"{mapping.Guid}\")");
// Scenario + State
// Scenario, ExecutionConditionState, NextState and StateTimes
if (!string.IsNullOrEmpty(mapping.Scenario))
{
sb.AppendLine($" .InScenario({ToCSharpStringLiteral(mapping.Scenario)})");
}
if (!string.IsNullOrEmpty(mapping.ExecutionConditionState))
{
sb.AppendLine($" .WhenStateIs({ToCSharpStringLiteral(mapping.ExecutionConditionState)})");
}
if (!string.IsNullOrEmpty(mapping.NextState))
{
sb.AppendLine($" .WhenStateIs({ToCSharpStringLiteral(mapping.NextState)}, {ToCSharpIntLiteral(mapping.TimesInSameState)})");
sb.AppendLine($" .WillSetStateTo({ToCSharpStringLiteral(mapping.NextState)}, {ToCSharpIntLiteral(mapping.TimesInSameState)})");
}
if (mapping.Probability != null)
@@ -342,7 +346,7 @@ internal class MappingConverter(MatcherMapper mapper)
if (clientIPMatcher?.Matchers != null)
{
var clientIPMatchers = _mapper.Map(clientIPMatcher.Matchers);
mappingModel.Request.Path = new ClientIPModel
mappingModel.Request.ClientIP = new ClientIPModel
{
Matchers = clientIPMatchers,
MatchOperator = clientIPMatchers?.Length > 1 ? clientIPMatcher.MatchOperator.ToString() : null
@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using Stef.Validation;
using WireMock.Admin.Mappings;
using WireMock.Admin.Requests;
using WireMock.Admin.Scenarios;
using WireMock.Admin.Settings;
using WireMock.Constants;
@@ -19,7 +20,6 @@ using WireMock.Owin;
using WireMock.RequestBuilders;
using WireMock.ResponseProviders;
using WireMock.Serialization;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
@@ -28,50 +28,16 @@ namespace WireMock.Server;
public partial class WireMockServer
{
private const int EnhancedFileSystemWatcherTimeoutMs = 1000;
private const string DefaultAdminPathPrefix = "/__admin";
private const string QueryParamReloadStaticMappings = "reloadStaticMappings";
private static readonly Guid ProxyMappingGuid = new("e59914fd-782e-428e-91c1-4810ffb86567");
private static readonly RegexMatcher AdminRequestContentTypeJson = new ContentTypeMatcher(WireMockConstants.ContentTypeJson, true);
private EnhancedFileSystemWatcher? _enhancedFileSystemWatcher;
private AdminPaths? _adminPaths;
private sealed class AdminPaths
{
private readonly string _prefix;
private readonly string _prefixEscaped;
public AdminPaths(WireMockServerSettings settings)
{
_prefix = settings.AdminPath ?? DefaultAdminPathPrefix;
_prefixEscaped = _prefix.Replace("/", "\\/");
}
public string Files => $"{_prefix}/files";
public string Health => $"{_prefix}/health";
public string Mappings => $"{_prefix}/mappings";
public string MappingsCode => $"{_prefix}/mappings/code";
public string MappingsWireMockOrg => $"{_prefix}mappings/wiremock.org";
public string Requests => $"{_prefix}/requests";
public string Settings => $"{_prefix}/settings";
public string Scenarios => $"{_prefix}/scenarios";
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 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 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 ScenariosNameWithStateMatcher => new($"^{_prefixEscaped}\\/scenarios\\/.+\\/state$");
public RegexMatcher ScenariosNameWithResetMatcher => new($"^{_prefixEscaped}\\/scenarios\\/.+\\/reset$");
public RegexMatcher FilesFilenamePathMatcher => new($"^{_prefixEscaped}\\/files\\/.+$");
public RegexMatcher ProtoDefinitionsIdPathMatcher => new($"^{_prefixEscaped}\\/protodefinitions\\/.+$");
}
private IAdminPaths? _adminPaths;
#region InitAdmin
private void InitAdmin()
{
_adminPaths = new AdminPaths(_settings);
_adminPaths = _options.AdminPaths;
// __admin/health
Given(Request.Create().WithPath(_adminPaths.Health).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(HealthGet));
@@ -128,6 +94,18 @@ public partial class WireMockServer
Given(Request.Create().WithPath(_adminPaths.RequestsGuidPathMatcher).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestGet));
Given(Request.Create().WithPath(_adminPaths.RequestsGuidPathMatcher).UsingDelete()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestDelete));
// __admin/requests/{guid}/near-misses
Given(Request.Create().WithPath(_adminPaths.RequestsGuidNearMissesPathMatcher).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestNearMissesGet));
// __admin/requests/count
Given(Request.Create().WithPath(_adminPaths.Requests + "/count").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsCount));
// __admin/requests/unmatched
Given(Request.Create().WithPath(_adminPaths.Requests + "/unmatched").UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsUnmatchedGet));
// __admin/requests/unmatched/near-misses
Given(Request.Create().WithPath(_adminPaths.Requests + "/unmatched/near-misses").UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsUnmatchedNearMissesGet));
// __admin/requests/find
Given(Request.Create().WithPath(_adminPaths.Requests + "/find").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsFind));
Given(Request.Create().WithPath(_adminPaths.Requests + "/find").UsingGet().WithParam("mappingGuid", new NotNullOrEmptyMatcher())).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsFindByMappingGuid));
@@ -631,7 +609,7 @@ public partial class WireMockServer
{
if (TryParseGuidFromRequestMessage(requestMessage, out var guid))
{
var entry = LogEntries.SingleOrDefault(r => r.RequestMessage != null && !r.RequestMessage.Path.StartsWith("/__admin/") && r.Guid == guid);
var entry = LogEntries.SingleOrDefault(r => r.RequestMessage != null && !_adminPaths!.Includes(r.RequestMessage.Path) && r.Guid == guid);
if (entry is { })
{
var model = new LogEntryMapper(_options).Map(entry);
@@ -660,7 +638,7 @@ public partial class WireMockServer
{
var logEntryMapper = new LogEntryMapper(_options);
var result = LogEntries
.Where(r => r.RequestMessage != null && !r.RequestMessage.Path.StartsWith("/__admin/"))
.Where(r => r.RequestMessage != null && !_adminPaths!.Includes(r.RequestMessage.Path))
.Select(logEntryMapper.Map);
return ToJson(result);
@@ -676,13 +654,29 @@ public partial class WireMockServer
#region Requests/find
private IResponseMessage RequestsFind(HttpContext _, IRequestMessage requestMessage)
{
var logEntryMapper = new LogEntryMapper(_options);
var result = FindLogEntriesByRequestModel(requestMessage)
.OrderBy(x => x.Value.AverageTotalScore)
.Select(x => x.Key)
.Select(logEntryMapper.Map);
return ToJson(result);
}
private IResponseMessage RequestsCount(HttpContext _, IRequestMessage requestMessage)
{
return ToJson(new RequestCountModel { Count = FindLogEntriesByRequestModel(requestMessage).Count });
}
private Dictionary<ILogEntry, RequestMatchResult> FindLogEntriesByRequestModel(IRequestMessage requestMessage)
{
var requestModel = DeserializeObject<RequestModel>(requestMessage);
var request = (Request)InitRequestBuilder(requestModel);
var dict = new Dictionary<ILogEntry, RequestMatchResult>();
foreach (var logEntry in LogEntries.Where(le => le.RequestMessage != null && !le.RequestMessage.Path.StartsWith("/__admin/")))
foreach (var logEntry in LogEntries.Where(le => le.RequestMessage != null && !_adminPaths!.Includes(le.RequestMessage.Path)))
{
var requestMatchResult = new RequestMatchResult();
if (request.GetMatchingScore(logEntry.RequestMessage!, requestMatchResult) > MatchScores.AlmostPerfect)
@@ -691,10 +685,7 @@ public partial class WireMockServer
}
}
var logEntryMapper = new LogEntryMapper(_options);
var result = dict.OrderBy(x => x.Value.AverageTotalScore).Select(x => x.Key).Select(logEntryMapper.Map);
return ToJson(result);
return dict;
}
private IResponseMessage RequestsFindByMappingGuid(HttpContext _, IRequestMessage requestMessage)
@@ -704,7 +695,7 @@ public partial class WireMockServer
Guid.TryParse(value.ToString(), out var mappingGuid)
)
{
var logEntries = LogEntries.Where(le => le.RequestMessage != null && !le.RequestMessage.Path.StartsWith("/__admin/") && le.MappingGuid == mappingGuid);
var logEntries = LogEntries.Where(le => le.RequestMessage != null && !_adminPaths!.Includes(le.RequestMessage.Path) && le.MappingGuid == mappingGuid);
var logEntryMapper = new LogEntryMapper(_options);
var result = logEntries.Select(logEntryMapper.Map);
return ToJson(result);
@@ -714,6 +705,65 @@ public partial class WireMockServer
}
#endregion Requests/find
#region Requests/unmatched
private IResponseMessage RequestsUnmatchedGet(HttpContext _, IRequestMessage requestMessage)
{
var logEntryMapper = new LogEntryMapper(_options);
var result = FindUnmatchedLogEntries().Select(logEntryMapper.Map);
return ToJson(result);
}
private IResponseMessage RequestsUnmatchedNearMissesGet(HttpContext _, IRequestMessage requestMessage)
{
if (!TryGetMaxResultsFromQuery(requestMessage, out var maxResults))
{
return _responseMessageBuilder.Create(HttpStatusCode.BadRequest, "Query parameter 'maxResults' must be a positive integer");
}
var logEntryMapper = new LogEntryMapper(_options);
var result = FindNearMissesForUnmatchedRequests(maxResults).Select(logEntryMapper.Map);
return ToJson(result);
}
#endregion Requests/unmatched
#region Requests/{guid}/near-misses
private IResponseMessage RequestNearMissesGet(HttpContext _, IRequestMessage requestMessage)
{
if (!TryGetMaxResultsFromQuery(requestMessage, out var maxResults))
{
return _responseMessageBuilder.Create(HttpStatusCode.BadRequest, "Query parameter 'maxResults' must be a positive integer");
}
if (TryParseGuidFromSecondToLastSegment(requestMessage, out var guid))
{
var entry = LogEntries.SingleOrDefault(r => !_adminPaths!.Includes(r.RequestMessage?.Path) && r.Guid == guid);
if (entry is not null)
{
var logEntryMapper = new LogEntryMapper(_options);
var result = FindNearMisses(entry, maxResults).Select(logEntryMapper.Map);
return ToJson(result);
}
}
_settings.Logger.Warn("HttpStatusCode set to 404 : Request not found");
return _responseMessageBuilder.Create(HttpStatusCode.NotFound, "Request not found");
}
private static bool TryGetMaxResultsFromQuery(IRequestMessage requestMessage, out int maxResults)
{
maxResults = DefaultMaxNearMisses;
if (requestMessage.Query == null || !requestMessage.Query.TryGetValue("maxResults", out var value))
{
return true;
}
return int.TryParse(value.ToString(), out maxResults) && maxResults > 0;
}
#endregion Requests/{guid}/near-misses
#region Scenarios
private IResponseMessage ScenariosGet(HttpContext _, IRequestMessage requestMessage)
{
@@ -155,7 +155,8 @@ public partial class WireMockServer
var clientIPModel = _settings.DefaultJsonSerializer.ParseJsonToken<ClientIPModel>(requestModel.ClientIP);
if (clientIPModel.Matchers != null)
{
requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
var matchOperator = StringUtils.ParseMatchOperator(clientIPModel.MatchOperator);
requestBuilder = requestBuilder.WithClientIP(matchOperator, clientIPModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
}
}
}
@@ -7,6 +7,8 @@ using Stef.Validation;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Owin;
using WireMock.Services;
namespace WireMock.Server;
@@ -57,6 +59,68 @@ public partial class WireMockServer
.ToArray();
}
/// <inheritdoc />
[PublicAPI]
public IReadOnlyList<ILogEntry> FindUnmatchedLogEntries()
{
return LogEntries
.Where(le => le.RequestMessage != null && !_options.AdminPaths.Includes(le.RequestMessage.Path))
.Where(le => le.RequestMatchResult is not { IsPerfectMatch: true })
.ToArray();
}
/// <inheritdoc />
[PublicAPI]
public IReadOnlyList<NearMiss> FindNearMisses(IRequestMessage requestMessage, int maxResults = DefaultMaxNearMisses)
{
Guard.NotNull(requestMessage);
Guard.Condition(maxResults, m => m > 0);
return FindNearMisses(requestMessage, null, maxResults);
}
/// <inheritdoc />
[PublicAPI]
public IReadOnlyList<NearMiss> FindNearMisses(ILogEntry logEntry, int maxResults = DefaultMaxNearMisses)
{
Guard.NotNull(logEntry);
Guard.Condition(maxResults, m => m > 0);
return logEntry.RequestMessage is null ? [] : FindNearMisses(logEntry.RequestMessage, logEntry.Guid, maxResults);
}
/// <inheritdoc />
[PublicAPI]
public IReadOnlyList<NearMiss> FindNearMissesForUnmatchedRequests(int maxResultsPerRequest = DefaultMaxNearMisses)
{
Guard.Condition(maxResultsPerRequest, m => m > 0);
return FindUnmatchedLogEntries()
.SelectMany(le => FindNearMisses(le, maxResultsPerRequest))
.ToArray();
}
private const int DefaultMaxNearMisses = 3;
private IMappingMatcher? _nearMissMappingMatcher;
private IReadOnlyList<NearMiss> FindNearMisses(IRequestMessage requestMessage, Guid? logEntryGuid, int maxResults)
{
_nearMissMappingMatcher ??= new MappingMatcher(_options, new RandomizerDoubleBetween0And1());
return _nearMissMappingMatcher
.FindNearMisses(requestMessage, maxResults)
.Select(nm => new NearMiss
{
RequestMessage = requestMessage,
LogEntryGuid = logEntryGuid,
MappingGuid = nm.Mapping.Guid,
MappingTitle = nm.Mapping.Title,
RequestMatchResult = nm.RequestMatchResult
})
.ToArray();
}
/// <inheritdoc cref="IWireMockServer.ResetLogEntries" />
[PublicAPI]
public void ResetLogEntries()
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,40 @@
// Copyright © WireMock.Net
using JetBrains.Annotations;
using Scriban.Runtime;
using Stef.Validation;
using WireMock.Handlers;
namespace WireMock.Settings;
/// <summary>
/// Extension methods for <see cref="WireMockServerSettings"/>.
/// </summary>
public static class WireMockServerSettingsExtensions
{
/// <summary>
/// Adds a strongly typed Scriban registration to <see cref="WireMockServerSettings.ScribanRegistrationCallback"/>.
/// </summary>
/// <remarks>
/// Members added to the <see cref="ScriptObject"/> are available as globals in Scriban and Scriban-DotLiquid templates.
/// When a callback is already configured, it is kept and invoked before <paramref name="registrationCallback"/>.
/// </remarks>
/// <param name="settings">The settings.</param>
/// <param name="registrationCallback">The action which registers functions, objects or variables on the <see cref="ScriptObject"/>.</param>
/// <returns>The same <see cref="WireMockServerSettings"/> instance.</returns>
[PublicAPI]
public static WireMockServerSettings WithScribanRegistration(this WireMockServerSettings settings, Action<ScriptObject, IFileSystemHandler> registrationCallback)
{
Guard.NotNull(settings);
Guard.NotNull(registrationCallback);
var existingCallback = settings.ScribanRegistrationCallback;
settings.ScribanRegistrationCallback = (scriptObject, fileSystemHandler) =>
{
existingCallback?.Invoke(scriptObject, fileSystemHandler);
registrationCallback((ScriptObject)scriptObject, fileSystemHandler);
};
return settings;
}
}
@@ -1,6 +1,7 @@
// Copyright © WireMock.Net
using Scriban;
using Scriban.Runtime;
using Stef.Validation;
using WireMock.Handlers;
using WireMock.Types;
@@ -9,21 +10,40 @@ namespace WireMock.Transformers.Scriban;
internal class ScribanContext : ITransformerContext
{
private static readonly MemberRenamerDelegate MemberRenamer = member => member.Name;
private readonly TransformerType _transformerType;
private readonly ScriptObject? _customGlobals;
public IFileSystemHandler FileSystemHandler { get; }
public ScribanContext(IFileSystemHandler fileSystemHandler, TransformerType transformerType)
public ScribanContext(IFileSystemHandler fileSystemHandler, TransformerType transformerType, ScriptObject? customGlobals = null)
{
FileSystemHandler = Guard.NotNull(fileSystemHandler);
_transformerType = transformerType;
_customGlobals = customGlobals;
}
public string ParseAndRender(string text, object model)
{
var template = _transformerType == TransformerType.ScribanDotLiquid ? Template.ParseLiquid(text) : Template.Parse(text);
var isLiquid = _transformerType == TransformerType.ScribanDotLiquid;
var template = isLiquid ? Template.ParseLiquid(text) : Template.Parse(text);
return template.Render(model, member => member.Name);
if (_customGlobals is null)
{
return template.Render(model, MemberRenamer);
}
// Same as Template.Render(model, MemberRenamer), but with the custom globals pushed beneath the model so that the model takes precedence.
var context = isLiquid ? new LiquidTemplateContext() : new TemplateContext();
context.MemberRenamer = MemberRenamer;
context.PushGlobal(_customGlobals);
var modelGlobals = new ScriptObject();
modelGlobals.Import(model, renamer: MemberRenamer);
context.PushGlobal(modelGlobals);
return template.Render(context);
}
public object? ParseAndEvaluate(string text, object model)
@@ -1,24 +1,32 @@
// Copyright © WireMock.Net
using WireMock.Handlers;
using WireMock.Types;
using Scriban.Runtime;
using Stef.Validation;
using WireMock.Settings;
using WireMock.Types;
namespace WireMock.Transformers.Scriban;
internal class ScribanContextFactory : ITransformerContextFactory
{
private readonly IFileSystemHandler _fileSystemHandler;
private readonly WireMockServerSettings _settings;
private readonly TransformerType _transformerType;
public ScribanContextFactory(IFileSystemHandler fileSystemHandler, TransformerType transformerType)
public ScribanContextFactory(WireMockServerSettings settings, TransformerType transformerType)
{
_fileSystemHandler = Guard.NotNull(fileSystemHandler);
_settings = Guard.NotNull(settings);
_transformerType = Guard.Condition(transformerType, t => t is TransformerType.Scriban or TransformerType.ScribanDotLiquid);
}
public ITransformerContext Create()
{
return new ScribanContext(_fileSystemHandler, _transformerType);
ScriptObject? customGlobals = null;
if (_settings.ScribanRegistrationCallback is { } registrationCallback)
{
customGlobals = new ScriptObject();
registrationCallback(customGlobals, _settings.FileSystemHandler);
}
return new ScribanContext(_settings.FileSystemHandler, _transformerType, customGlobals);
}
}
@@ -19,8 +19,8 @@ internal static class TransformerFactory
case TransformerType.Scriban:
case TransformerType.ScribanDotLiquid:
var factoryDotLiquid = new ScribanContextFactory(settings.FileSystemHandler, transformerType);
return new Transformer(settings, factoryDotLiquid);
var factoryScriban = new ScribanContextFactory(settings, transformerType);
return new Transformer(settings, factoryScriban);
default:
throw new NotSupportedException($"{nameof(TransformerType)} '{transformerType}' is not supported.");
@@ -18,6 +18,8 @@ namespace WireMock.WebSockets;
/// </summary>
public class WireMockWebSocketContext : IWebSocketContext
{
private readonly IDateTimeUtils _dateTimeUtils;
/// <inheritdoc />
public Guid ConnectionId { get; }
@@ -50,7 +52,8 @@ public class WireMockWebSocketContext : IWebSocketContext
WebSocketBuilder builder,
IWireMockMiddlewareOptions options,
IWireMockMiddlewareLogger logger,
IGuidUtils guidUtils
IGuidUtils guidUtils,
IDateTimeUtils dateTimeUtils
)
{
HttpContext = httpContext;
@@ -61,6 +64,7 @@ public class WireMockWebSocketContext : IWebSocketContext
Builder = builder;
Options = options;
Logger = logger;
_dateTimeUtils = dateTimeUtils;
ConnectionId = guidUtils.NewGuid();
}
@@ -169,7 +173,7 @@ public class WireMockWebSocketContext : IWebSocketContext
null
)
{
DateTime = DateTime.UtcNow
DateTime = _dateTimeUtils.UtcNow
};
}
else
@@ -179,7 +183,7 @@ public class WireMockWebSocketContext : IWebSocketContext
{
Method = method,
BodyData = bodyData,
DateTime = DateTime.UtcNow
DateTime = _dateTimeUtils.UtcNow
};
}
@@ -43,7 +43,7 @@
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="TinyMapper.Signed" Version="4.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.34.0" />
<PackageReference Include="Scriban.Signed" Version="7.2.0" />
<PackageReference Include="Scriban.Signed" Version="7.2.5" />
</ItemGroup>
<ItemGroup>
@@ -85,4 +85,4 @@
<ProjectReference Include="..\WireMock.Net.OpenApiParser\WireMock.Net.OpenApiParser.csproj" />
</ItemGroup>
</Project>
</Project>
@@ -25,9 +25,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.12.0" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.13.0" />
<PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="Stef.Validation" Version="0.2.0" />
<PackageReference Include="Stef.Validation" Version="0.3.0" />
</ItemGroup>
<ItemGroup>
@@ -1,10 +1,10 @@
// Copyright © WireMock.Net
#if NETSTANDARD2_0
namespace System.Collections.Generic;
internal static class DictionaryExtensions
{
#if NETSTANDARD2_0
public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue>? dictionary, TKey key, TValue value)
{
if (dictionary is null || dictionary.ContainsKey(key))
@@ -16,5 +16,29 @@ internal static class DictionaryExtensions
return true;
}
}
#endif
#endif
public static bool TryGetFirstValue<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary, out TValue? value)
{
if (dictionary != null && dictionary.Count > 0)
{
value = dictionary.First().Value;
return true;
}
value = default;
return false;
}
public static bool TryGetFirstOrDefault<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary, out KeyValuePair<TKey, TValue> value)
{
if (dictionary != null && dictionary.Count > 0)
{
value = dictionary.First();
return true;
}
value = default;
return false;
}
}
@@ -1,7 +1,5 @@
// Copyright © WireMock.Net
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
@@ -56,37 +54,25 @@ internal static class OpenApiSchemaExtensions
public static SchemaFormat GetSchemaFormat(this IOpenApiSchema? schema)
{
switch (schema?.Format)
return (schema?.Format) switch
{
case "float":
return SchemaFormat.Float;
"float" => SchemaFormat.Float,
"double" => SchemaFormat.Double,
"int32" => SchemaFormat.Int32,
"int64" => SchemaFormat.Int64,
"date" => SchemaFormat.Date,
"date-time" => SchemaFormat.DateTime,
"password" => SchemaFormat.Password,
"byte" => SchemaFormat.Byte,
"binary" => SchemaFormat.Binary,
_ => SchemaFormat.Undefined,
};
}
case "double":
return SchemaFormat.Double;
case "int32":
return SchemaFormat.Int32;
case "int64":
return SchemaFormat.Int64;
case "date":
return SchemaFormat.Date;
case "date-time":
return SchemaFormat.DateTime;
case "password":
return SchemaFormat.Password;
case "byte":
return SchemaFormat.Byte;
case "binary":
return SchemaFormat.Binary;
default:
return SchemaFormat.Undefined;
}
internal static JsonNode? FindFirstExample(this IOpenApiSchema? schema)
{
#pragma warning disable CS0618 // Type or member is obsolete
return schema?.Examples?.FirstOrDefault() ?? schema?.Example;
#pragma warning restore CS0618 // Type or member is obsolete
}
}
@@ -1,7 +1,5 @@
// Copyright © WireMock.Net
using System.IO;
using System.Linq;
using JetBrains.Annotations;
using Stef.Validation;
using WireMock.Net.OpenApiParser.Models;
@@ -16,7 +14,8 @@ namespace WireMock.Net.OpenApiParser.Extensions;
public static class WireMockServerExtensions
{
/// <summary>
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1 file.
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1/V3.2 file.
/// In case of an error, no mappings are registered and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="server">The WireMockServer instance</param>
/// <param name="path">Path containing OpenAPI file to parse and use the mappings.</param>
@@ -28,7 +27,8 @@ public static class WireMockServerExtensions
}
/// <summary>
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1 file.
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1/V3.2 file.
/// In case of an error, no mappings are registered and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="server">The WireMockServer instance</param>
/// <param name="path">Path containing OpenAPI file to parse and use the mappings.</param>
@@ -46,7 +46,8 @@ public static class WireMockServerExtensions
}
/// <summary>
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1 stream.
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1/V3.2 stream.
/// In case of an error, no mappings are registered and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="server">The WireMockServer instance</param>
/// <param name="stream">Stream containing OpenAPI description to parse and use the mappings.</param>
@@ -58,7 +59,8 @@ public static class WireMockServerExtensions
}
/// <summary>
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1 stream.
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1/V3.2 stream.
/// In case of an error, no mappings are registered and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="server">The WireMockServer instance</param>
/// <param name="stream">Stream containing OpenAPI description to parse and use the mappings.</param>
@@ -77,7 +79,8 @@ public static class WireMockServerExtensions
}
/// <summary>
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1 document.
/// Register the mappings via an OpenAPI (swagger) V2/V3/V3.1/V3.2 document.
/// In case of an error, no mappings are registered and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="server">The WireMockServer instance</param>
/// <param name="document">The OpenAPI document to use as mappings.</param>
@@ -9,11 +9,11 @@
<InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.OpenApi'" />
<LibraryPath Include="%(ReferencePathWithRefAssemblies.RelativeDir)" />
</ItemGroup>
<ItemGroup>
<DoNotInternalizeAssemblies Include="WireMock.Net.OpenApiParser" />
</ItemGroup>
<ILRepack
Parallel="true"
Internalize="true"
@@ -1,7 +1,5 @@
// Copyright © WireMock.Net
using System.Collections.Generic;
using System.IO;
using WireMock.Admin.Mappings;
using WireMock.Net.OpenApiParser.Models;
using WireMock.Net.OpenApiParser.Settings;
@@ -15,6 +13,7 @@ public interface IWireMockOpenApiParser
{
/// <summary>
/// Generate <see cref="IReadOnlyList{MappingModel}"/> from a file-path.
/// In case of an error, an empty list is returned and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="path">The path to read the OpenApi/Swagger/V2/V3/V31 or Raml file.</param>
/// <param name="diagnostic">OpenApiDiagnostic output</param>
@@ -23,6 +22,7 @@ public interface IWireMockOpenApiParser
/// <summary>
/// Generate <see cref="IReadOnlyList{MappingModel}"/> from a file-path.
/// In case of an error, an empty list is returned and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="path">The path to read the OpenApi/Swagger/V2/V3/V31 or Raml file.</param>
/// <param name="settings">Additional settings</param>
@@ -40,6 +40,7 @@ public interface IWireMockOpenApiParser
/// <summary>
/// Generate <see cref="IReadOnlyList{MappingModel}"/> from a <seealso cref="Stream"/>.
/// In case of an error, an empty list is returned and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="stream">The source stream</param>
/// <param name="diagnostic">OpenApiDiagnostic output</param>
@@ -48,6 +49,7 @@ public interface IWireMockOpenApiParser
/// <summary>
/// Generate <see cref="IReadOnlyList{MappingModel}"/> from a <seealso cref="Stream"/>.
/// In case of an error, an empty list is returned and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="stream">The source stream</param>
/// <param name="settings">Additional settings</param>
@@ -57,6 +59,7 @@ public interface IWireMockOpenApiParser
/// <summary>
/// Generate <see cref="IReadOnlyList{MappingModel}"/> from a <seealso cref="string"/>.
/// In case of an error, an empty list is returned and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="text">The source text</param>
/// <param name="diagnostic">OpenApiDiagnostic output</param>
@@ -65,6 +68,7 @@ public interface IWireMockOpenApiParser
/// <summary>
/// Generate <see cref="IReadOnlyList{MappingModel}"/> from a <seealso cref="string"/>.
/// In case of an error, an empty list is returned and <see cref="OpenApiDiagnostic"/> contains the error details.
/// </summary>
/// <param name="text">The source text</param>
/// <param name="settings">Additional settings</param>
@@ -53,7 +53,7 @@ internal class OpenApiPathsMapper(WireMockOpenApiParserSettings settings)
Headers = MapRequestHeaders(requestHeaders),
Body = GetRequestBodyModel(operation.RequestBody)
},
Response = GetResponseModel(operation.Responses?.FirstOrDefault())
Response = operation.Responses?.TryGetFirstOrDefault(out var firstResponse) == true ? GetResponseModel(firstResponse) : new ResponseModel { StatusCode = 200 }
};
}
@@ -70,8 +70,7 @@ internal class OpenApiPathsMapper(WireMockOpenApiParserSettings settings)
var requestExample = requestContent?.Example;
var requestExamples = requestContent?.Examples;
var requestSchemaExample = requestContent?.Schema?.Example;
var requestSchemaExamples = requestContent?.Schema?.Examples;
var requestSchemaExample = requestContent?.Schema.FindFirstExample();
JsonNode? request;
if (requestExample != null)
@@ -86,10 +85,6 @@ internal class OpenApiPathsMapper(WireMockOpenApiParserSettings settings)
{
request = requestExamples.FirstOrDefault().Value.Value;
}
else if (requestSchemaExamples != null)
{
request = requestSchemaExamples.FirstOrDefault();
}
else
{
var requestSchema = content?.FirstOrDefault().Value.Schema;
@@ -99,16 +94,15 @@ internal class OpenApiPathsMapper(WireMockOpenApiParserSettings settings)
return MapRequestBody(request) ?? new BodyModel();
}
private ResponseModel GetResponseModel(KeyValuePair<string, IOpenApiResponse>? openApiResponse)
private ResponseModel GetResponseModel(KeyValuePair<string, IOpenApiResponse> openApiResponse)
{
var content = openApiResponse?.Value.Content;
var content = openApiResponse.Value.Content;
TryGetContent(content, out var responseContent, out var contentType);
var responseExample = responseContent?.Example;
var responseExamples = responseContent?.Examples;
var responseSchemaExample = responseContent?.Schema?.Example;
var responseSchemaExamples = responseContent?.Schema?.Examples;
var responseSchemaExample = responseContent?.Schema.FindFirstExample();
JsonNode? response;
if (responseExample != null)
@@ -119,24 +113,19 @@ internal class OpenApiPathsMapper(WireMockOpenApiParserSettings settings)
{
response = responseSchemaExample;
}
else if (responseExamples != null)
else if (responseExamples.TryGetFirstValue(out var firstResponseFromExamples))
{
response = responseExamples.FirstOrDefault().Value.Value;
}
else if (responseSchemaExamples != null)
{
response = responseSchemaExamples.FirstOrDefault();
response = firstResponseFromExamples?.Value;
}
else
{
var responseSchema = content?.FirstOrDefault().Value?.Schema;
response = MapSchemaToObject(responseSchema);
response = content.TryGetFirstValue(out var firstContent) ? MapSchemaToObject(firstContent?.Schema) : null;
}
return new ResponseModel
{
StatusCode = int.TryParse(openApiResponse?.Key, out var httpStatusCode) ? httpStatusCode : 200,
Headers = MapHeaders(contentType, openApiResponse?.Value.Headers),
StatusCode = int.TryParse(openApiResponse.Key, out var httpStatusCode) ? httpStatusCode : 200,
Headers = MapHeaders(contentType, openApiResponse.Value.Headers),
BodyAsJson = response != null ? JsonConvert.DeserializeObject(SystemTextJsonSerializer.Serialize(response)) : null
};
}
@@ -159,7 +148,7 @@ internal class OpenApiPathsMapper(WireMockOpenApiParserSettings settings)
};
}
private static bool TryGetContent(IDictionary<string, OpenApiMediaType>? contents, [NotNullWhen(true)] out OpenApiMediaType? openApiMediaType, [NotNullWhen(true)] out string? contentType)
private static bool TryGetContent(IDictionary<string, IOpenApiMediaType>? contents, [NotNullWhen(true)] out IOpenApiMediaType? openApiMediaType, [NotNullWhen(true)] out string? contentType)
{
openApiMediaType = null;
contentType = null;
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using RamlToOpenApiConverter;
namespace WireMock.Net.OpenApiParser.Models;
/// <summary>
@@ -24,5 +21,5 @@ public class OpenApiDiagnostic
/// <summary>
/// Open API specification version of the document parsed.
/// </summary>
public OpenApiSpecificationVersion SpecificationVersion { get; set; }
public OpenApiSpecVersion SpecificationVersion { get; set; }
}
@@ -1,7 +1,5 @@
// Copyright © WireMock.Net
using System.Linq;
using RamlToOpenApiConverter;
using MicrosoftOpenApiDiagnostic = Microsoft.OpenApi.Reader.OpenApiDiagnostic;
namespace WireMock.Net.OpenApiParser.Models;
@@ -19,7 +17,7 @@ internal static class OpenApiMapper
{
Errors = openApiDiagnostic.Errors.Select(e => new OpenApiError(e.Pointer, e.Message)).ToList(),
Warnings = openApiDiagnostic.Warnings.Select(e => new OpenApiError(e.Pointer, e.Message)).ToList(),
SpecificationVersion = (OpenApiSpecificationVersion)openApiDiagnostic.SpecificationVersion
SpecificationVersion = (OpenApiSpecVersion)openApiDiagnostic.SpecificationVersion
};
}
}
@@ -0,0 +1,29 @@
// Copyright © WireMock.Net
namespace WireMock.Net.OpenApiParser.Models;
/// <summary>
/// Represents versions of OpenAPI specification.
/// </summary>
public enum OpenApiSpecVersion
{
/// <summary>
/// Represents OpenAPI V2.0 spec
/// </summary>
OpenApi2_0,
/// <summary>
/// Represents all patches of OpenAPI V3.0 spec (e.g. 3.0.0, 3.0.1)
/// </summary>
OpenApi3_0,
/// <summary>
/// Represents OpenAPI V3.1 spec
/// </summary>
OpenApi3_1,
/// <summary>
/// Represents OpenAPI V3.2 spec
/// </summary>
OpenApi3_2
}
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System;
using Microsoft.OpenApi;
namespace WireMock.Net.OpenApiParser.Settings;
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System;
using Microsoft.OpenApi;
using RandomDataGenerator.FieldOptions;
using RandomDataGenerator.Randomizers;
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System;
using Microsoft.OpenApi;
namespace WireMock.Net.OpenApiParser.Settings;
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System;
using System.Globalization;
namespace WireMock.Net.OpenApiParser.Utils;
@@ -1,7 +1,5 @@
// Copyright © WireMock.Net
using System;
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.OpenApi;
using Stef.Validation;
@@ -39,7 +37,7 @@ internal class ExampleValueGenerator
public JsonNode GetExampleValue(IOpenApiSchema? schema)
{
var schemaExample = schema?.Example;
var schemaExample = schema?.FindFirstExample();
var schemaEnum = schema?.Enum?.FirstOrDefault();
_exampleValues.Schema = schema;
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>An OpenApi (swagger) parser to generate MappingModel or mapping.json file.</Description>
@@ -23,7 +23,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Required" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -40,25 +40,29 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="RamlToOpenApiConverter.SourceOnly" Version="0.11.0" />
<PackageReference Include="YamlDotNet" Version="16.3.0" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.19.1" />
<PackageReference Include="Stef.Validation" Version="0.2.0" />
<PackageReference Include="Stef.Validation" Version="0.3.0" />
</ItemGroup>
<!-- https://github.com/wiremock/WireMock.Net/issues/1508 -->
<ItemGroup>
<PackageReference Include="RamlToOpenApiConverter.SourceOnly" Version="0.50.0" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<PackageReference Include="Microsoft.OpenApi.YamlReader" Version="2.3.0" />
<PackageReference Include="Microsoft.OpenApi.YamlReader" Version="3.10.2" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<!-- Keep at version 2.0.43 (https://github.com/ravibpatel/ILRepack.Lib.MSBuild.Task/issues/80) -->
<PackageReference Include="ILRepack.Lib.MSBuild.Task" Version="2.0.43" PrivateAssets="All" />
<PackageReference Include="Microsoft.OpenApi" Version="2.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.OpenApi.YamlReader" Version="2.3.0" PrivateAssets="All" />
<PackageReference Include="SharpYaml" Version="2.1.3" />
<PackageReference Include="Microsoft.OpenApi" Version="3.10.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.OpenApi.YamlReader" Version="3.10.2" PrivateAssets="All" />
<PackageReference Include="SharpYaml" Version="2.1.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\WireMock.Net.Abstractions\WireMock.Net.Abstractions.csproj" />
</ItemGroup>
</Project>
</Project>
@@ -1,8 +1,6 @@
// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using JetBrains.Annotations;
using Microsoft.OpenApi;
@@ -18,7 +16,7 @@ using OpenApiDiagnostic = WireMock.Net.OpenApiParser.Models.OpenApiDiagnostic;
namespace WireMock.Net.OpenApiParser;
/// <summary>
/// Parse a OpenApi/Swagger/V2/V3/V3.1 to WireMock.Net MappingModels.
/// Parse a OpenApi/Swagger/V2/V3/V3.1/V3.2 to WireMock.Net MappingModels.
/// </summary>
public class WireMockOpenApiParser : IWireMockOpenApiParser
{
@@ -32,8 +30,15 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
_readerSettings = new OpenApiReaderSettings();
_readerSettings.AddMicrosoftExtensionParsers();
_readerSettings.AddJsonReader();
_readerSettings.TryAddReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
_readerSettings.TryAddReader(OpenApiConstants.Yml, new OpenApiYamlReader());
var openApiYamlReaderSettings = new OpenApiYamlReaderSettings
{
MaxAliasExpansionNodeCount = 1_000_000
};
var openApiYamlReader = new OpenApiYamlReader(openApiYamlReaderSettings);
_readerSettings.TryAddReader(OpenApiConstants.Yaml, openApiYamlReader);
_readerSettings.TryAddReader(OpenApiConstants.Yml, openApiYamlReader);
}
/// <inheritdoc />
@@ -55,7 +60,12 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
}
else
{
document = Read(File.OpenRead(path), out diagnostic);
if (!TryRead(File.OpenRead(path), out var documentFromYaml, out diagnostic))
{
return [];
}
document = documentFromYaml;
}
return FromDocument(document, settings);
@@ -74,14 +84,24 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
[PublicAPI]
public IReadOnlyList<MappingModel> FromStream(Stream stream, out OpenApiDiagnostic diagnostic)
{
return FromDocument(Read(stream, out diagnostic));
if (TryRead(stream, out var openApiDocument, out diagnostic))
{
return FromDocument(openApiDocument);
}
return [];
}
/// <inheritdoc />
[PublicAPI]
public IReadOnlyList<MappingModel> FromStream(Stream stream, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic)
{
return FromDocument(Read(stream, out diagnostic), settings);
if (TryRead(stream, out var openApiDocument, out diagnostic))
{
return FromDocument(openApiDocument, settings);
}
return [];
}
/// <inheritdoc />
@@ -98,7 +118,7 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
return FromStream(new MemoryStream(Encoding.UTF8.GetBytes(text)), settings, out diagnostic);
}
private OpenApiDocument Read(Stream stream, out OpenApiDiagnostic diagnostic)
private bool TryRead(Stream stream, [NotNullWhen(true)] out OpenApiDocument? openApiDocument, out OpenApiDiagnostic diagnostic)
{
if (stream is not MemoryStream memoryStream)
{
@@ -108,7 +128,8 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
var result = OpenApiDocument.Load(memoryStream, settings: _readerSettings);
diagnostic = OpenApiMapper.Map(result.Diagnostic) ?? new OpenApiDiagnostic();
return result.Document ?? throw new InvalidOperationException("The document is null.");
openApiDocument = result.Document;
return openApiDocument != null && !diagnostic.Errors.Any();
}
private static MemoryStream ReadStreamIntoMemoryStream(Stream stream)
@@ -250,6 +250,39 @@ public interface IWireMockAdminApi
[Get("requests/find")]
Task<IList<LogEntryModel>> FindRequestsByMappingGuidAsync([Query] Guid mappingGuid, CancellationToken cancellationToken = default);
/// <summary>
/// Count the requests which match the criteria (<see cref="RequestModel"/>).
/// </summary>
/// <param name="model">The RequestModel</param>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Post("requests/count")]
[Header("Content-Type", "application/json")]
Task<RequestCountModel> CountRequestsAsync([Body] RequestModel model, CancellationToken cancellationToken = default);
/// <summary>
/// Get the requests which did not fully match any mapping.
/// </summary>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Get("requests/unmatched")]
Task<IList<LogEntryModel>> GetUnmatchedRequestsAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Get the near misses (the mappings which partially match) for all requests which did not fully match any mapping.
/// </summary>
/// <param name="maxResults">The maximum number of near misses per request. Default is 3.</param>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Get("requests/unmatched/near-misses")]
Task<IList<NearMissModel>> GetNearMissesForUnmatchedRequestsAsync([Query] int? maxResults = null, CancellationToken cancellationToken = default);
/// <summary>
/// Get the near misses (the mappings which partially match) for a request.
/// </summary>
/// <param name="guid">The Guid of the request</param>
/// <param name="maxResults">The maximum number of near misses. Default is 3.</param>
/// <param name="cancellationToken">The optional cancellationToken.</param>
[Get("requests/{guid}/near-misses")]
Task<IList<NearMissModel>> GetNearMissesForRequestAsync([Path] Guid guid, [Query] int? maxResults = null, CancellationToken cancellationToken = default);
/// <summary>
/// Get all scenarios
/// </summary>
Loaded 100 of 180 files, more files were not shown because too many files have changed in this diff. Show more