Skip to content

Commit

Permalink
Merge pull request #242 from cdent/cd/data-style-replacements
Browse files Browse the repository at this point in the history
Make it possible to use non-string $RESPONSE in data
  • Loading branch information
cdent committed Apr 9, 2018
2 parents 3a5621f + 2cd486d commit ebb49f3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
27 changes: 23 additions & 4 deletions gabbi/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def wrapper(self):
return wrapper


def _is_complex_type(data):
"""If data is a list or dict return True."""
return isinstance(data, list) or isinstance(data, dict)


class HTTPTestCase(testtools.TestCase):
"""Encapsulate a single HTTP request as a TestCase.
Expand Down Expand Up @@ -558,23 +563,37 @@ def _test_data_to_string(self, data, content_type):
"""Turn the request data into a string.
If the data is not binary, replace template strings.
If the result of the template handling is not a string,
run the result through the dumper.
"""
if isinstance(data, str):
if data.startswith('<@'):
dumper_class = self.get_content_handler(content_type)
if not _is_complex_type(data):
if isinstance(data, six.string_types) and data.startswith('<@'):
info = self.load_data_file(data.replace('<@', '', 1))
if utils.not_binary(content_type):
data = six.text_type(info, 'UTF-8')
else:
# Return early we are binary content
return info
else:
dumper_class = self.get_content_handler(content_type)
# We have a complex data structure, try to dump it.
if dumper_class:
data = self.replace_template(data)
data = dumper_class.dumps(data, test=self)
else:
raise ValueError(
'unable to process data to %s' % content_type)
return self.replace_template(data)

data = self.replace_template(data)

# If the result after template handling is not a string, dump
# it if there is a suitable dumper.
if dumper_class and not isinstance(data, six.string_types):
# If there are errors dumping we want them to raise to the
# test harness.
data = dumper_class.dumps(data, test=self)
return data

def _test_status(self, expected_status, observed_status):
"""Confirm we got the expected status.
Expand Down
30 changes: 28 additions & 2 deletions gabbi/tests/gabbits_intercept/backref.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,42 @@ tests:
content-type: application/json
method: POST
data: |
{"a": "$RESPONSE["a"]",
{"a": $RESPONSE["a"],
"c": "$RESPONSE["c"]"}
response_strings:
- '"a": "$RESPONSE["a"]"'
- '"a": $RESPONSE["a"]'
- '"c": "/v2"'
response_headers:
location: $SCHEME://$NETLOC$RESPONSE['c']
x-gabbi-url: $SCHEME://$NETLOC/v2
content-type: $HEADERS['content-type']

- name: use raw json from response
POST: $LAST_URL
request_headers:
content-type: application/json
# the value of '$' here should be {"c": "/v2", "a": 1}
data: $RESPONSE['$']
response_json_paths:
$.c: /v2
$.a: 1

- name: post a raw int as json
POST: /
request_headers:
content-type: application/json
data: 1
response_json_paths:
$: 1

- name: repost that raw int
POST: /
request_headers:
content-type: application/json
data: $RESPONSE['$']
response_json_paths:
$: 1

- name: backref json fail start
url: /
method: POST
Expand Down

0 comments on commit ebb49f3

Please sign in to comment.