pact-foundation / pact-reference

Reference implementations for the pact specifications
https://pact.io
MIT License
91 stars 46 forks source link

Allow `StringPattern` for simple text response #291

Closed joeftiger closed 1 year ago

joeftiger commented 1 year ago

When building an expecting response, it is currently only possible to define JSON patterns (afaik).

However, some apis may return a plain text boolean as in True or False. I do not find a possibility to define a pattern for a text response, as the function ResponseBuilder::body() accepts no patterns.

I tried fiddling with body_and_matching_rules_mut(), but this would

  1. be a bit cumbersome to use
  2. does not work as necessary structs are not public.

The following does not work either as it returns the boolean encapsulated inside "True" or "False".

PactBuilder::new_v4( ... )
    .interaction( ..., |mut i| {
        ...
        i.response
             ...
             .json_body(term!("^(True|False)$", "True"))
    })

It would be great when textual patterns could be implemented.

rholshausen commented 1 year ago

1.0.2 now supports matching on text bodies:

let pact = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.body_matching(term!("^(True|False)$", "True"));
        i
      })
      .build()