Search code examples
c#.netpact

Using Regex in PactNet 4.0


I am using Pact NET but I am unable to use the Regex Matcher.

I have the following code on the consumer side.

'''

    [Fact]
    public async void GetProductById_AssuresTheCorrectProductByIdIsRetrieved_CorrectProductIsRetrieved()
    {
        // Arange
        var pact = Pact.V3("ApiClient", "ProductService", PactHelper.GetPactConfiguration());

        // initialize backend
        var pactBuilder = pact.UsingNativeBackend(9000);

        var products = new List<object>()
            {
                new { id = 9, name = "Pants", sku = "SKU9", price = 10.00M },
                new { id = 10, name = "Shirt", sku = Match.Regex("SKU10", "SKU*"), price = 100.00M }
            };

        pactBuilder
            .UponReceiving("Get product with id 10")
            .WithRequest(HttpMethod.Get, "/api/products/10")
            .WillRespond()
            .WithStatus(HttpStatusCode.OK)
            .WithHeader("Content-Type", "application/json; charset=utf-8")
            .WithJsonBody(products[1]);

        var gateway = new ProductGateway(new System.Uri(PactHelper.GetIntegrationData()));

        // Act
        await pactBuilder.VerifyAsync(async ctx =>
        {
            var response = await gateway.GetProductById(10);

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        });
    }

'''

If I remove the sku = Match.Regex("SKU10", "SKU*") and simply use sku = "SKU10" the pact is verified by the provider with sucess.

Am I missing something or is this Matcher not available for Pact 4.0?

I do not think this is a regex related problem as I tried to user other Matchers and the pact is also not verified.


Solution

  • What language are you using to verify the pact that is generated by Pact .NET? If it's another language, you may need to ensure the specification versions match up (e.g. Pact .NET is likely to be producing v3 specification pact files by default, whereas Pact JS currently only supports v2).