Skip to content

Commit

Permalink
Handle $RESPONSE templates in the current response
Browse files Browse the repository at this point in the history
In some APIs it is useful to refer to the response of a previous
request in the response of this current request to, for example,
verify that an ID continues to be used.

Up to now $RESPONSE handling worked in url and data, this change
adds it to response_strings and response_json_paths.

As with all of the $RESPONSE handling, this is power that should
really only be used when testing existing APIs. If you need a lot of
this when you are developing new APIs YOU ARE DOING IT WRONG.
  • Loading branch information
cdent committed Feb 5, 2015
1 parent 8a304c4 commit 5d9d0d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
13 changes: 9 additions & 4 deletions gabbi/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _assert_response(self, response, content, status, headers=None,
# Compare strings in response body
if expected:
for expect in expected:
expect = self._replace_response_values(expect)
self.assertIn(expect, decoded_output)

# Decode body as JSON and test against json_paths
Expand All @@ -86,9 +87,9 @@ def _assert_response(self, response, content, status, headers=None,
self.json_data = response_data
for path in json_paths:
match = self._extract_json_path_value(response_data, path)
self.assertEqual(json_paths[path], match,
'Unable to match %s as %s'
% (path, json_paths[path]))
expected = self._replace_response_values(json_paths[path])
self.assertEqual(expected, match, 'Unable to match %s as %s'
% (path, expected))

def _decode_content(self, response, content):
"""Decode content to a proper string."""
Expand Down Expand Up @@ -171,7 +172,11 @@ def _replacer(self, match):

def _replace_response_values(self, template):
"""Replace a JSON Path in a template string with its value."""
return re.sub(r"\$RESPONSE\['([^']+)'\]", self._replacer, template)
try:
return re.sub(r"\$RESPONSE\['([^']+)'\]", self._replacer, template)
except TypeError:
# template is not a string
return template

def _run_test(self):
"""Make an HTTP request and compare the response with expectations."""
Expand Down
23 changes: 18 additions & 5 deletions gabbi/gabbits_intercept/backref.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,25 @@ tests:
request_headers:
content-type: application/json
method: POST
data: |
{"a": $RESPONSE['a'],
"c": "$RESPONSE['link']"}
data:
a: $RESPONSE['a']
c: $RESPONSE['link']
response_json_paths:
a: 1
a: $RESPONSE['a']
c: /v2
response_headers:
x-gabbi-url: $SCHEME://$NETLOC/v2

- name: post even more json
url: $RESPONSE['c']
request_headers:
content-type: application/json
method: POST
data: |
{"a": "$RESPONSE['a']",
"c": "$RESPONSE['c']"}
response_strings:
- '"a": "$RESPONSE[''a'']"'
- '"c": "/v2"'
response_headers:
x-gabbi-url: $SCHEME://$NETLOC/v2

0 comments on commit 5d9d0d4

Please sign in to comment.