venom icon indicating copy to clipboard operation
venom copied to clipboard

Regex assertion for multiline data is broken (go ?m flag helps)

Open miklosbagi opened this issue 1 year ago • 1 comments

Howdy,

I know this is work in progress, but figured I share this - worst case: gets closed :) Pre-requisites: some HTTP server, e.g. docker run -p 8081:80 nginx for quick test.

The data we're looking at for ref is:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Example test case:

name: Testing Regexp

testcases:
  - name: data
    steps:
      - type: http
        method: GET
        url: http://localhost:8081
        assertions:
          - result.body ShouldMatchRegex '^<\!DOC.*>'

      - type: http
        method: GET
        url: http://localhost:8081
        assertions:
          - result.body ShouldMatchRegex '^<\!DOC.*>\n'

      - type: http
        method: GET
        url: http://localhost:8081
        assertions:
          - result.body ShouldMatchRegex '^<\!DOC.*>$'

      - type: http
        method: GET
        url: http://localhost:8081
        assertions:
          - result.body ShouldMatchRegex '^<html>$'
  1. step: assumes no end of line
  2. step: assumes line break at end of line
  3. step: assumes line break at end of line
  4. step: assumed this is a standalone line with ^...$, but it's not.

It would be awesome if all of these passed. The key usecase is validating data from an endpoint with regexp, and expecting ^ and $ at our disposal to clearly mark line start/end.
As a workaround, assertions like this are possible, but not too elegant: - result.body ShouldMatchRegex '\n<html>\n'.

Also, please let me know if I'm misinterpreting the doc, happy to be corrected here.

Thanks, mb

miklosbagi avatar Jul 13 '23 16:07 miklosbagi

@theUm pointed out that this is actually how it works with standard go regexp, and so a multiline flag (?m) is needed to enable this.
The following works fine:

- type: http
        method: GET
        url: http://localhost:8081/
        vars:
          content:
            from: result.body
        assertions:
          - result.body ShouldMatchRegex '(?m)^<html>$'

So that changes this to kindly requesting above to be added to the examples.

miklosbagi avatar Jul 14 '23 08:07 miklosbagi