Skip to content

Commit

Permalink
Add support for regex in response_strings
Browse files Browse the repository at this point in the history
Very similar to what is used for headers.

Fixes #326
  • Loading branch information
cdent committed Mar 2, 2024
1 parent 6939357 commit 14f128b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
16 changes: 14 additions & 2 deletions gabbi/handlers/core.py
Original file line number Diff line number Diff line change
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)
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
Original file line number Diff line number Diff line change
@@ -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/'

0 comments on commit 14f128b

Please sign in to comment.