Skip to content

Commit

Permalink
Merge pull request #294 from macisamuele/fix-regression-stringify-hea…
Browse files Browse the repository at this point in the history
…ders

Fix regression introduced by PR #288
  • Loading branch information
macisamuele authored Jun 22, 2017
2 parents 43c1a1f + a9631ab commit a78f23a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
11 changes: 8 additions & 3 deletions bravado/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ def construct_request(operation, request_options, **op_kwargs):
'method': str(operation.http_method.upper()),
'url': url,
'params': {}, # filled in downstream
# Ensure that headers injected via request_options are converted to string
# This is need to workaround https://github.com/requests/requests/issues/3491
'headers': {k: str(v) for k, v in iteritems(request_options.get('headers', {}))},
'headers': request_options.get('headers', {}),
}

# Copy over optional request options
Expand All @@ -294,6 +292,13 @@ def construct_request(operation, request_options, **op_kwargs):
request[request_option] = request_options[request_option]

construct_params(operation, request, op_kwargs)

# Ensure that all the headers are converted to strings.
# This is need to workaround https://github.com/requests/requests/issues/3491
request['headers'] = {
k: str(v)
for k, v in iteritems(request['headers'])
}
return request


Expand Down
11 changes: 8 additions & 3 deletions tests/client/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def getPetById_spec(petstore_dict):


@pytest.fixture
def minimal_swagger_spec(getPetById_spec):
def minimal_swagger_dict(getPetById_spec):
spec_dict = {
'paths': {
'/pet/{petId}': {
Expand All @@ -50,6 +50,11 @@ def minimal_swagger_spec(getPetById_spec):
},
},
}
spec = Spec(spec_dict)
spec.api_url = 'http://localhost/swagger.json'
return spec_dict


@pytest.fixture
def minimal_swagger_spec(minimal_swagger_dict):
spec = Spec(minimal_swagger_dict)
spec.api_url = 'http://localhost/'
return spec
68 changes: 55 additions & 13 deletions tests/client/construct_request_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-
import pytest
from bravado_core.operation import Operation
from bravado_core.request import IncomingRequest
from bravado_core.request import unmarshal_request
from mock import mock
from mock import patch

from bravado.client import CallableOperation
from bravado.client import construct_request
from tests.client.conftest import minimal_swagger_spec as build_swagger_spec


@pytest.mark.parametrize('timeout_kv', [
Expand All @@ -25,19 +29,57 @@ def test_with_timeouts(
assert mock_marshal_param.call_count == 2


@pytest.mark.parametrize('header_name, header_value', [
('boolean', True),
('integer', 1),
('float', 2.0),
])
@patch('bravado.client.marshal_param')
@pytest.mark.parametrize(
'swagger_type, swagger_format, header_name, header_value', [
('boolean', None, 'boolean', True),
('integer', None, 'integer', 1),
('number', 'float', 'float', 2.0),
],
)
def test_with_not_string_headers(
mock_marshal_param, minimal_swagger_spec,
getPetById_spec, request_dict, header_name, header_value,
minimal_swagger_dict, getPetById_spec, request_dict,
swagger_type, swagger_format, header_name, header_value,
):
request_dict['url'] = '/pet/{petId}'
op = CallableOperation(Operation.from_spec(
minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec))
request = construct_request(op, request_options={'headers': {header_name: header_value}}, petId=34, api_key='foo')
url = '/pet/{petId}'
parameter = {
'name': header_name,
'in': 'header',
'required': False,
'type': swagger_type,
}
if swagger_format:
parameter['format'] = swagger_format
minimal_swagger_dict['paths'][url]['get']['parameters'].append(parameter)

minimal_swagger_spec = build_swagger_spec(minimal_swagger_dict)
request_dict['url'] = url

operation = Operation.from_spec(
swagger_spec=minimal_swagger_spec,
path_name='/pet/{petId}',
http_method='get',
op_spec=getPetById_spec,
)

petId = 34
api_key = 'foo'
request = construct_request(
operation=operation,
request_options={'headers': {header_name: header_value}},
petId=petId,
api_key=api_key,
)

# To unmarshall a request bravado-core needs the request to be wrapped
# by an object with a specific list of attributes
request_object = type('IncomingRequest', (IncomingRequest,), {
'path': {'petId': petId},
'query': {},
'form': {},
'headers': request['headers'],
'files': mock.Mock(),
})

assert request['headers'][header_name] == str(header_value)
assert mock_marshal_param.call_count == 2
unmarshalled_request = unmarshal_request(request_object, operation)
assert unmarshalled_request[header_name] == header_value

0 comments on commit a78f23a

Please sign in to comment.