From 2c3c4cdd29a18a9b205c795101e9b1d9e2f827be Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Fri, 2 Oct 2026 09:15:03 +0530 Subject: [PATCH] fix: keep JSON content type local to each request --- .../src/vonage_http_client/http_client.py | 4 +-- http_client/tests/test_http_client.py | 36 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/http_client/src/vonage_http_client/http_client.py b/http_client/src/vonage_http_client/http_client.py index 97c3e8f2..4f02eedf 100644 --- a/http_client/src/vonage_http_client/http_client.py +++ b/http_client/src/vonage_http_client/http_client.py @@ -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 diff --git a/http_client/tests/test_http_client.py b/http_client/tests/test_http_client.py index 687a9a1f..6f7567de 100644 --- a/http_client/tests/test_http_client.py +++ b/http_client/tests/test_http_client.py @@ -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 @@ -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 = {