Hi. I have a question regarding pact C++. When I p...
# general
a
Hi. I have a question regarding pact C++. When I pass a raw string or a std::string to match a regex pattern against a URL pattern e.g.
^https:\/\/some.corp.com\/path$|^https:\/\/some.corp.com\/otherpath$
so if I call
Copy code
Matching(R"(^https:\/\/some.corp.com\/path$|^https:\/\/some.corp.com\/otherpath$)", var.toStdString())
the created json shows:
Copy code
"matchingRules": {
          "body": {
            "$.type": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "regex",
                  "regex": "^https:\\/\\/some.corp.com\\/path$|^https:\\/\\/some.corp.com\\/otherpath$"
                }
              ]
            }
          },
          "header": {}
        },
Is this correct? I would expect the regex pattern to show
Copy code
"regex":   "^https:\/\/some.corp.com\/path$|^https:\/\/some.corp.com\/otherpath$"
Or does it depend on the consumer's language whether you need the escaped forward slashes? So if the consumer is not PHP or JavaScript based this would be fine:
"regex":   "^<https://some.corp.com/path$%7C^https://some.corp.com/otherpath$>"
if the consumer was Java, Go, Rust, C++?
m
hmm I’m not sure off the top of my head if it’s a problem - is it causing issues?
It shouldn’t depend on the client languages, the pact file follows the spec, which should be interoperable
j
Oh, the joys of escaping regex patterns 😆 While what is/isn't escaped when defining the pattern in the specific language's SDK might vary, I agree with Matt in that the generated Pact file should be interoperable between all implementations. The string itself looks correctly to me, because JSON strings are not 'raw'. That is,
\n
in a JSON string translates to a newline and not the literal
\
and
n
characters. As a result,
\\/
does correspond to that value of
\
and
/
. You can check the actual value (once deserialised) as follows:
Copy code
# Having copied the JSON string, including the surrounding `"`
$ pbpaste | jq -r
^https:\/\/some.corp.com\/path$|^https:\/\/some.corp.com\/otherpath$
Note that
pbpaste
is a macOS tool to output what is in the clipboard. There will be other tools available on other platforms, and I highly recommend using such a tool again to avoid any risk of the shell interfering with backslashes in the string 🙂
😆 1
a
Thanks for the responses, especially regarding the interoperability issues, I was worried that the regex string would be treated as raw, I think it should be fine as we are mainly working in a C++ environment so the regex pattern:
"regex":   "^<https://some.corp.com/path$%7C^https://some.corp.com/otherpath$>"
should be fine.
👍 1
j
You might just want to check that
%7C
in the middle whether it is intentional or not.
a
@Joshua Ellis That's a typo, it should read:
"regex":   "^<https://some.corp.com/path$%7C^https://some.corp.com/otherpath$>"
OK. For some reason slack is converting
|
into
%7C
against my wishes, please ignore it
😆 1
j
When software thinks it knows better 😆
a
I've tried editing it 4 times and it keeps converting it, I give up!