Skip to content
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
2 changes: 1 addition & 1 deletion midtransclient/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def request(self, method, server_key, request_url, parameters=dict(),

# only merge if custom headers exist
if custom_headers:
headers = {**default_headers, **headers}
headers = {**default_headers, **custom_headers}

response_object = self.http_client.request(
method,
Expand Down
30 changes: 30 additions & 0 deletions tests/test_http_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from unittest.mock import patch
from .helpers import is_str
from .context import HttpClient
import datetime
Expand Down Expand Up @@ -47,6 +48,35 @@ def test_response_not_json_exception():
except Exception as e:
assert 'JSONDecodeError' in repr(e)

def test_is_custom_headers_applied():
http_client = HttpClient()

custom_headers = {
'X-Override-Notification':'https://example.org'
}

# Mock requests
with patch('requests.request') as mock_request:
# Set status code to 200 to prevent MidtransAPIError
mock_request.return_value.status_code = 200

# Trigger request
http_client.request(method='post',
server_key='SB-Mid-server-GwUP_WGbJPXsDzsNEBRs8IYA',
request_url='https://app.sandbox.midtrans.com/snap/v1/transactions',
parameters=generate_param_min(),
custom_headers=custom_headers)

# Fetch the headers from requests.request arguments
headers = mock_request.call_args.kwargs['headers']

# Make sure default header still exist
assert headers.get('content-type') == 'application/json'

# Assert custom headers
assert 'X-Override-Notification' in headers
assert headers.get('X-Override-Notification') == 'https://example.org'

# TODO test GET request

# ======== HELPER FUNCTIONS BELOW ======== #
Expand Down