Skip to content

Commit 11a9f26

Browse files
author
Zaki Ibrahim
authored
Merge pull request #23 from ffd114/fix-custom-headers
fix: custom_headers not applied
2 parents 7f8b615 + 649aab4 commit 11a9f26

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

midtransclient/http_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def request(self, method, server_key, request_url, parameters=dict(),
4545

4646
# only merge if custom headers exist
4747
if custom_headers:
48-
headers = {**default_headers, **headers}
48+
headers = {**default_headers, **custom_headers}
4949

5050
response_object = self.http_client.request(
5151
method,

tests/test_http_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from unittest.mock import patch
23
from .helpers import is_str
34
from .context import HttpClient
45
import datetime
@@ -47,6 +48,35 @@ def test_response_not_json_exception():
4748
except Exception as e:
4849
assert 'JSONDecodeError' in repr(e)
4950

51+
def test_is_custom_headers_applied():
52+
http_client = HttpClient()
53+
54+
custom_headers = {
55+
'X-Override-Notification':'https://example.org'
56+
}
57+
58+
# Mock requests
59+
with patch('requests.request') as mock_request:
60+
# Set status code to 200 to prevent MidtransAPIError
61+
mock_request.return_value.status_code = 200
62+
63+
# Trigger request
64+
http_client.request(method='post',
65+
server_key='SB-Mid-server-GwUP_WGbJPXsDzsNEBRs8IYA',
66+
request_url='https://app.sandbox.midtrans.com/snap/v1/transactions',
67+
parameters=generate_param_min(),
68+
custom_headers=custom_headers)
69+
70+
# Fetch the headers from requests.request arguments
71+
headers = mock_request.call_args.kwargs['headers']
72+
73+
# Make sure default header still exist
74+
assert headers.get('content-type') == 'application/json'
75+
76+
# Assert custom headers
77+
assert 'X-Override-Notification' in headers
78+
assert headers.get('X-Override-Notification') == 'https://example.org'
79+
5080
# TODO test GET request
5181

5282
# ======== HELPER FUNCTIONS BELOW ======== #

0 commit comments

Comments
 (0)