Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I do RegEx in Message Header #1409

Open
IsaacHu opened this issue Jul 27, 2021 · 2 comments
Open

How can I do RegEx in Message Header #1409

IsaacHu opened this issue Jul 27, 2021 · 2 comments

Comments

@IsaacHu
Copy link

IsaacHu commented Jul 27, 2021

I am aim to test a field "partitionKey" as RegEx "[A-Z]{3}", like "ABC01".
below code cannot add to "matchingRules" in contract, is there anyway in Pact can do that?

    MessagePact itsVesselCapacityUpdatePack(MessagePactBuilder builder) {
        //Map<String, String> headers = Map.of(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        Map<String, Object> headers = new HashMap<>();
        headers.put(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        headers.put("partitionKey", new RegexMatcher("[A-Z]{3}", "ABC01"));

        return builder
                .given("xxxxxxxxxx")
                .expectsToReceive("yyyyyyyyyyyyyyy")
                .withMetadata(headers)
                .withContent(event)
                .toPact();
    }
@uglyog
Copy link
Member

uglyog commented Jul 31, 2021

Your regex is wrong, it also needs to match the values after the initial 3 letters. So [A-Z]{3}.* or [A-Z]{3}\\d+
Also make sure you are using the correct class, it should be au.com.dius.pact.core.model.matchingrules.RegexMatcher.

I've also added a metadata DSL class to make it easier to setup matching rules for message metadata, for instance,

.withMetadata(md -> {
    md.put(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
    md.matchRegex("partitionKey", "[A-Z]{3}\\d+", "ABC01");
})

@uglyog
Copy link
Member

uglyog commented Aug 1, 2021

Released 4.1.24 and 4.2.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants