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

Add support for regex in response_strings #327

Merged
merged 3 commits into from Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions gabbi/handlers/core.py
Expand Up @@ -22,8 +22,20 @@ class StringResponseHandler(base.ResponseHandler):
test_key_value = []

def action(self, test, expected, value=None):
expected = test.replace_template(expected)
test.assert_in_or_print_output(expected, test.output)
is_regex = (expected.startswith('/') and
expected.endswith('/') and
len(expected) > 1)
cdent marked this conversation as resolved.
Show resolved Hide resolved
expected = test.replace_template(expected, escape_regex=is_regex)

if is_regex:
# Trim off /
expected = expected[1:-1]
test.assertRegex(
test.output, expected,
'Expect resonse body %s to match /%s/' %
(test.output, expected))
else:
test.assert_in_or_print_output(expected, test.output)


class ForbiddenHeadersResponseHandler(base.ResponseHandler):
Expand Down
29 changes: 28 additions & 1 deletion gabbi/tests/gabbits_intercept/regex.yaml
@@ -1,4 +1,4 @@
# Confirm regex handling in response and json path headers
# Confirm regex handling in response headers, strings and json path handlers
tests:
- name: regex header test
url: /cow?alpha=1
Expand All @@ -19,3 +19,30 @@ tests:
$.alpha: /ow$/
$.beta: /(?!cow).*/
$.gamma: /\d+/

- name: regex string test json
PUT: /cow
request_headers:
content-type: application/json
data:
alpha: cow
beta: pig
gamma: 1
response_strings:
- '/"alpha": "cow",/'

- name: regex string test multiline
GET: /presenter
response_strings:
- '/Hello World/'
- '/dolor sit/'

- name: regex string test splat
GET: /presenter
response_strings:
- '/dolor.*amet/'

- name: regex string test mix
GET: /presenter
response_strings:
- '/[Hh]el{2}o [Ww]orld/'