Skip to content

Commit

Permalink
Injected headers are casted to strings (workaround psf/requests#3491)
Browse files Browse the repository at this point in the history
  • Loading branch information
macisamuele committed Jun 11, 2017
1 parent a526c35 commit c8686cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion bravado/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ def construct_request(operation, request_options, **op_kwargs):
'method': str(operation.http_method.upper()),
'url': url,
'params': {}, # filled in downstream
'headers': request_options.get('headers', {}),
# 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', {}))},
}

# Copy over optional request options
Expand Down
24 changes: 22 additions & 2 deletions tests/client/construct_request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,32 @@
('connect_timeout', 2),
])
@patch('bravado.client.marshal_param')
def test_with_timeouts(mock_marshal_param, minimal_swagger_spec,
getPetById_spec, request_dict, timeout_kv):
def test_with_timeouts(
mock_marshal_param, minimal_swagger_spec,
getPetById_spec, request_dict, timeout_kv,
):
request_dict['url'] = '/pet/{petId}'
op = CallableOperation(Operation.from_spec(
minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec))
k, v = timeout_kv
request = construct_request(op, request_options={k: v}, petId=34, api_key='foo')
assert request[k] == v
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')
def test_with_not_string_headers(
mock_marshal_param, minimal_swagger_spec,
getPetById_spec, request_dict, 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')
assert request['headers'][header_name] == str(header_value)
assert mock_marshal_param.call_count == 2

0 comments on commit c8686cb

Please sign in to comment.