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

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

Open
miklosbagi opened this issue Jul 13, 2023 · 1 comment
Open

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

miklosbagi opened this issue Jul 13, 2023 · 1 comment

Comments

@miklosbagi
Copy link
Contributor

miklosbagi commented Jul 13, 2023

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 miklosbagi changed the title [bug] regex assertion for multiline data is broken Regex assertion for multiline data is broken (go ?m flag helps) Jul 14, 2023
@miklosbagi
Copy link
Contributor Author

miklosbagi commented Jul 14, 2023

@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.

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

1 participant