Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injected headers are casted to strings #288

Merged
merged 1 commit into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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