Skip to content
Open
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: 2 additions & 2 deletions http_client/src/vonage_http_client/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ def make_request(
request_params = {
'method': request_type,
'url': url,
'headers': self._headers,
'headers': self._headers.copy(),
'timeout': self._timeout,
}

if sent_data_type == 'json':
self._headers['Content-Type'] = 'application/json'
request_params['headers']['Content-Type'] = 'application/json'
request_params['json'] = params
elif sent_data_type == 'query_params':
request_params['params'] = params
Expand Down
36 changes: 35 additions & 1 deletion http_client/tests/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from unittest.mock import patch

import responses
from pytest import raises
from pytest import mark, raises
from requests import PreparedRequest, Response, Session
from requests.exceptions import ConnectionError
from responses import matchers
Expand Down Expand Up @@ -115,6 +115,40 @@ def test_make_post_request():
assert loads(responses.calls[0].request.body) == params


@mark.parametrize('sent_data_type', ['form', 'query_params'])
@responses.activate
def test_json_content_type_does_not_leak_to_later_requests(sent_data_type):
responses.add(responses.POST, 'https://example.com/json', json={})
responses.add(responses.POST, 'https://example.com/next', json={})
client = HttpClient(Auth('test-key', 'test-secret'))

client.post(
host='example.com',
request_path='/json',
params={'value': 'first'},
auth_type='basic',
)
client.post(
host='example.com',
request_path='/next',
params={'value': 'second'},
auth_type='basic',
sent_data_type=sent_data_type,
)

assert responses.calls[0].request.headers['Content-Type'] == 'application/json'
second = responses.calls[1].request
if sent_data_type == 'form':
assert second.headers['Content-Type'] == 'application/x-www-form-urlencoded'
assert second.body == 'value=second'
else:
assert 'Content-Type' not in second.headers
assert second.url == 'https://example.com/next?value=second'
assert second.headers['User-Agent'] == client.user_agent
assert second.headers['Accept'] == 'application/json'
assert second.headers['Authorization'] == client.auth.create_basic_auth_string()


@responses.activate
def test_make_post_request_with_signature():
params = {
Expand Down