From a77fab53a92e6b34b18798dcc211aa7321928455 Mon Sep 17 00:00:00 2001 From: Yuliya Bagriy Date: Fri, 15 Jun 2018 20:57:40 -0400 Subject: [PATCH] Support per-request API key header overwrite by _request_options --- CHANGELOG-MASTER.rst | 2 ++ bravado/requests_client.py | 2 +- tests/http_client_test.py | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-MASTER.rst b/CHANGELOG-MASTER.rst index fa9ba502..fff65858 100644 --- a/CHANGELOG-MASTER.rst +++ b/CHANGELOG-MASTER.rst @@ -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` diff --git a/bravado/requests_client.py b/bravado/requests_client.py index 9e02a8e3..4a930a71 100644 --- a/bravado/requests_client.py +++ b/bravado/requests_client.py @@ -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 diff --git a/tests/http_client_test.py b/tests/http_client_test.py index d8fcfb26..09118c5f 100644 --- a/tests/http_client_test.py +++ b/tests/http_client_test.py @@ -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(