mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-05 23:44:08 +02:00
* feat(request matchers): Add support for early mismatch in mapping processing * test(request matchers): Add unit test for early mismatch functionality * test(grpc): Add test for grpc requests early mismatch and error logging (Issue #1442) * feat(request matchers): RequestMatcherType Add `RequestMatcherType` to request matchers for improved type identification Closes #1442 * refactor(request matchers): Request Replace `EarlyMatcherSelector` with `EarlyMatcherType` for improved clarity and consistency Closes #1442 * feat(request): conversion Add EarlyMatcherType support in request models and mapping conversion Closes #1442 * test(mapping): new tests add unit tests for EarlyMatcherType in mapping conversion and serialization Closes #1442 * refactor(request matchers): RequestMessageEarlyMatcher Replaced inline `EarlyMatcherType` logic with the new `RequestMessageEarlyMatcher` class to support cases when several matchers of the same type are present. For instance - Header, Cookie, Param Closes #1442 * test(request matchers): Early Mismatch add unit tests for early mismatch scenarios with several matchers of same type. Currently, headers and parameters Closes #1442 * refactor(mapping): RequestModel.EarlyMatcherType use fully qualified enum for EarlyMatcherType in serialization Closes #1442 * style(review): fixes - removed unused method - added missing curly brackets Closes #1442
112 lines
4.3 KiB
C#
112 lines
4.3 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AnyOfTypes;
|
|
using Stef.Validation;
|
|
using WireMock.Models;
|
|
using WireMock.Models.GraphQL;
|
|
using WireMock.Types;
|
|
using WireMock.Util;
|
|
|
|
namespace WireMock.Matchers.Request;
|
|
|
|
/// <summary>
|
|
/// The request body GraphQL matcher.
|
|
/// </summary>
|
|
public class RequestMessageGraphQLMatcher : IRequestMatcher
|
|
{
|
|
/// <summary>
|
|
/// The matchers.
|
|
/// </summary>
|
|
public IMatcher[]? Matchers { get; }
|
|
|
|
/// <summary>
|
|
/// The <see cref="MatchOperator"/>
|
|
/// </summary>
|
|
public MatchOperator MatchOperator { get; } = MatchOperator.Or;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageGraphQLMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
|
/// <param name="schema">The schema.</param>
|
|
/// <param name="customScalars">A dictionary defining the custom scalars used in this schema. [optional]</param>
|
|
public RequestMessageGraphQLMatcher(MatchBehaviour matchBehaviour, string schema, IDictionary<string, Type>? customScalars = null) :
|
|
this(CreateMatcherArray(matchBehaviour, schema, customScalars))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageGraphQLMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
|
/// <param name="schema">The schema.</param>
|
|
/// <param name="customScalars">A dictionary defining the custom scalars used in this schema. [optional]</param>
|
|
public RequestMessageGraphQLMatcher(MatchBehaviour matchBehaviour, ISchemaData schema, IDictionary<string, Type>? customScalars = null) :
|
|
this(CreateMatcherArray(matchBehaviour, new AnyOf<string, StringPattern, ISchemaData>(schema), customScalars))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageGraphQLMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchers">The matchers.</param>
|
|
public RequestMessageGraphQLMatcher(params IMatcher[] matchers)
|
|
{
|
|
Matchers = Guard.NotNull(matchers);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageGraphQLMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchers">The matchers.</param>
|
|
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use.</param>
|
|
public RequestMessageGraphQLMatcher(MatchOperator matchOperator, params IMatcher[] matchers)
|
|
{
|
|
Matchers = Guard.NotNull(matchers);
|
|
MatchOperator = matchOperator;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public RequestMatcherType Type => RequestMatcherType.GraphQL;
|
|
|
|
/// <inheritdoc />
|
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
|
{
|
|
var results = CalculateMatchResults(requestMessage);
|
|
var (score, exception) = MatchResult.From(nameof(RequestMessageGraphQLMatcher), results, MatchOperator).Expand();
|
|
|
|
return requestMatchResult.AddScore(GetType(), score, exception);
|
|
}
|
|
|
|
private static MatchResult CalculateMatchResult(IRequestMessage requestMessage, IMatcher matcher)
|
|
{
|
|
// In case the matcher is a IStringMatcher and the body is a Json or a String, use the BodyAsString to match on.
|
|
if (matcher is IStringMatcher stringMatcher && requestMessage.BodyData?.DetectedBodyType is BodyType.Json or BodyType.String or BodyType.FormUrlEncoded)
|
|
{
|
|
return stringMatcher.IsMatch(requestMessage.BodyData.BodyAsString);
|
|
}
|
|
|
|
return MatchResult.From(nameof(RequestMessageGraphQLMatcher));
|
|
}
|
|
|
|
private IReadOnlyList<MatchResult> CalculateMatchResults(IRequestMessage requestMessage)
|
|
{
|
|
return Matchers == null ? [MatchResult.From(nameof(RequestMessageGraphQLMatcher))] : Matchers.Select(matcher => CalculateMatchResult(requestMessage, matcher)).ToArray();
|
|
}
|
|
|
|
private static IMatcher[] CreateMatcherArray(
|
|
MatchBehaviour matchBehaviour,
|
|
AnyOf<string, StringPattern, ISchemaData> schema,
|
|
IDictionary<string, Type>? customScalars
|
|
)
|
|
{
|
|
if (TypeLoader.TryLoadNewInstance<IGraphQLMatcher>(out var graphQLMatcher, schema, customScalars, matchBehaviour, MatchOperator.Or))
|
|
{
|
|
return [graphQLMatcher];
|
|
}
|
|
return [];
|
|
}
|
|
} |