Skip to content

Commit

Permalink
Merge pull request #374 from aviskase/auth-honor-perrequest-options
Browse files Browse the repository at this point in the history
Support per-request API key header overwrite by _request_options
  • Loading branch information
sjaensch authored Jun 26, 2018
2 parents f8f98c0 + a77fab5 commit e92da6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG-MASTER.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Changelog-Master
*This file will contain the Changelog of the master branch.*

*The content will be used to build the Changelog of the new bravado release.*

Support per-request API key header overwrite by `_request_options`
2 changes: 1 addition & 1 deletion bravado/requests_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, host, api_key, param_name=u'api_key', param_in=u'query'):

def apply(self, request):
if self.param_in == 'header':
request.headers[self.param_name] = self.api_key
request.headers.setdefault(self.param_name, self.api_key)
else:
request.params[self.param_name] = self.api_key
return request
Expand Down
21 changes: 21 additions & 0 deletions tests/http_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ def test_api_key_header(self):
httpretty.last_request().querystring)
self.assertEqual('abc123', httpretty.last_request().headers['Key'])

@httpretty.activate
def test_api_key_header_overwrite(self):
httpretty.register_uri(
httpretty.GET, "http://swagger.py/client-test",
body='expected')

client = RequestsClient()
client.set_api_key("swagger.py", 'abc123', param_name='Key',
param_in='header')
params = self._default_params()
params['params'] = {'foo': 'bar'}
params['headers'] = {'Key': 'def456'}

resp = client.request(params).result()

self.assertEqual(200, resp.status_code)
self.assertEqual('expected', resp.text)
self.assertEqual({'foo': ['bar']},
httpretty.last_request().querystring)
self.assertEqual('def456', httpretty.last_request().headers['Key'])

@httpretty.activate
def test_auth_leak(self):
httpretty.register_uri(
Expand Down

0 comments on commit e92da6c

Please sign in to comment.