Skip to content

Commit

Permalink
Merge pull request #232 from joshleeb/header-key-substitution
Browse files Browse the repository at this point in the history
Run replace template for header keys
  • Loading branch information
cdent committed Dec 18, 2017
2 parents 222c8b7 + 86dfb5d commit 1de3dd8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/source/format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ All of these variables may be used in all of the following fields:
* ``url``
* ``query_parameters``
* ``data``
* ``request_headers``
* ``request_headers`` (in both the key and value)
* ``response_strings``
* ``response_json_paths`` (in both the key and value, see
:ref:`json path substitution <json-subs>` for more info)
* ``response_headers`` (on the value side of the key value pair)
* ``response_headers`` (in both the key and value)
* ``response_forbidden_headers``
* ``count`` and ``delay`` fields of ``poll``

Expand Down
28 changes: 22 additions & 6 deletions gabbi/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,21 @@ def _run_request(self, url, method, headers, body, redirect=False):
self.response_data = None
self.output = decoded_output

def _replace_headers_template(self, test_name, headers):
replaced_headers = {}

for name in headers:
try:
replaced_name = self.replace_template(name)
replaced_headers[replaced_name] = self.replace_template(
headers[name]
)
except TypeError as exc:
raise exception.GabbiFormatError(
'malformed headers in test %s: %s' % (test_name, exc))

return replaced_headers

def _run_test(self):
"""Make an HTTP request and compare the response with expectations."""
test = self.test_data
Expand All @@ -487,14 +502,15 @@ def _run_test(self):
self.url = base_url
full_url = self._parse_url(base_url)

# Replace variables in headers with variable values. This includes both
# in the header key and the header value.
test['request_headers'] = self._replace_headers_template(
test['name'], test['request_headers'])
test['response_headers'] = self._replace_headers_template(
test['name'], test['response_headers'])

method = test['method'].upper()
headers = test['request_headers']
for name in headers:
try:
headers[name] = self.replace_template(headers[name])
except TypeError as exc:
raise exception.GabbiFormatError(
'malformed headers in test %s: %s' % (test['name'], exc))

if test['data'] != '':
body = self._test_data_to_string(
Expand Down
8 changes: 8 additions & 0 deletions gabbi/tests/gabbits_runner/header_key.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests:
- name: expected success
GET: /header_key
request_headers:
$SCHEME: some-scheme
status: 200
response_headers:
$SCHEME: some-scheme
11 changes: 11 additions & 0 deletions gabbi/tests/simple_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ def __call__(self, environ, start_response):
return [json.dumps({
"nan": float('nan')
}).encode('utf-8')]
elif path_info == '/header_key':
scheme_header = environ.get('HTTP_HTTP', False)

if scheme_header:
headers.append(('HTTP', scheme_header))
start_response('200 OK', headers)
else:
start_response('500 SERVER ERROR', headers)

query_output = json.dumps(query_data)
return [query_output.encode('utf-8')]

start_response('200 OK', headers)

Expand Down
13 changes: 13 additions & 0 deletions gabbi/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ def test_unsafe_yaml(self):
except SystemExit as err:
self.assertSuccess(err)

def test_header_key_variable_substitution(self):
sys.argv = [
'gabbi-run', 'http://%s:%s/header_key' % (self.host, self.port)]

sys.argv.append('--')
sys.argv.append('gabbi/tests/gabbits_runner/header_key.yaml')

with self.server():
try:
runner.run()
except SystemExit as err:
self.assertSuccess(err)

def test_target_url_parsing(self):
sys.argv = ['gabbi-run', 'http://%s:%s/foo' % (self.host, self.port)]

Expand Down

0 comments on commit 1de3dd8

Please sign in to comment.