WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.39k stars 207 forks source link

Did the ThrowExceptionWhenMatcherFails option got removed from 1.5.16 to 1.5.51? #1086

Closed JasonLandbridge closed 5 months ago

JasonLandbridge commented 6 months ago

Hi there,

I just upgraded to .NET 8 and upgraded Wiremock from 1.5.16 to 1.5.51, but the ThrowExceptionWhenMatcherFails option seems to not be there anymore?

        Server = WireMockServer.Start(new WireMockServerSettings()
        {
            // Cannot resolve symbol 'ThrowExceptionWhenMatcherFails'
            ThrowExceptionWhenMatcherFails = true,
            HostingScheme = HostingScheme.HttpAndHttps,
        });

Searching the repo for any mention of ThrowExceptionWhenMatcherFails seems it has disappeared.

Did I miss a breaking change or migration?

Thanks!

StefH commented 6 months ago

@JasonLandbridge Sorry, this was indeed a small breaking change introduced in https://github.com/WireMock-Net/WireMock.Net/pull/986 in version 1.5.36 (21 September 2023).

This property was removed, and now you get logging in case the matcher fails.

JasonLandbridge commented 6 months ago

@StefH Thanks for tracking this down! I actually loved this option as it would make my tests fail if an URL changed or a new URL was called by my code not yet covered by my tests. But now, I assume, the tests can still pass due to the lack of a thrown exception if this happens.

Any chance this option can be returned or is there something similar available?

Cheers!

StefH commented 6 months ago

The functionality is like:

constructor

When creating a matcher using the constructor, and the pattern is invalid, an exception is thrown immediately. Like:

    [Fact]
    public void JsonMatcher_WithInvalidStringValue_Should_ThrowException()
    {
        // Act
        // ReSharper disable once ObjectCreationAsStatement
        Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\"");

        // Assert
        action.Should().Throw<JsonException>();
    }

    // or this
    [Fact]
    public void GraphQLMatcher_For_InvalidSchema_ThrowsGraphQLSyntaxErrorException()
    {
        // Act
        // ReSharper disable once ObjectCreationAsStatement
        Action action = () => _ = new GraphQLMatcher("in va lid");

        // Assert
        action.Should().Throw<GraphQLSyntaxErrorException>();
    }

during matching

When the input value for the matcher to match on, is invalid, the exception is remembered and returned in the result. Like:

    [Fact]
    public void GraphQLMatcher_For_ValidSchema_And_IncorrectQueryWithError_WithThrowExceptionTrue_ReturnsError()
    {
        // Arrange
        var input = "{\"query\":\"{\\r\\n studentsX {\\r\\n fullName\\r\\n X\\r\\n }\\r\\n}\"}";

        // Act
        var matcher = new GraphQLMatcher(TestSchema);
        var result = matcher.IsMatch(input);

        // Assert
        result.Score.Should().Be(MatchScores.Mismatch);
        result.Exception!.Message.Should().StartWith("Cannot query field 'studentsX' on type 'Query'");
    }

In the high level mapping matcher code, this error/exception is logged and this mapping is skipped because it contains errors.

Does this help you, you did you actually use it in a different way ? Because I do not understand this:

it would make my tests fail if an URL changed or a new URL was called by my code not yet covered by my tests

JasonLandbridge commented 5 months ago

Thank you very much for this answer! I will try it out when I have the chance. I will close this for now