33"""
44import base64
55import json
6+ from datetime import datetime
7+ from uuid import UUID
8+
69import urllib3
710from unittest import mock
811from unittest .mock import patch
1316from botocore .exceptions import BotoCoreError
1417
1518import pytest
19+ from freezegun import freeze_time
1620
1721from pynamodb .connection import Connection
1822from pynamodb .connection .base import MetaTable
@@ -1515,11 +1519,13 @@ def test_connection__botocore_config():
15151519 assert c .client ._client_config .max_pool_connections == 20
15161520
15171521
1518- @mock . patch ( 'botocore.httpsession.URLLib3Session.send' )
1519- def test_connection_make_api_call___extra_headers (send_mock ):
1522+ @freeze_time ( )
1523+ def test_connection_make_api_call___extra_headers (mocker ):
15201524 good_response = mock .Mock (spec = AWSResponse , status_code = 200 , headers = {}, text = '{}' , content = b'{}' )
1525+ send_mock = mocker .patch ('botocore.httpsession.URLLib3Session.send' , return_value = good_response )
15211526
1522- send_mock .return_value = good_response
1527+ # return constant UUID
1528+ mocker .patch ('uuid.uuid4' , return_value = UUID ('01FC4BDB-B223-4B86-88F4-DEE79B77F275' ))
15231529
15241530 c = Connection (extra_headers = {'foo' : 'bar' }, max_retry_attempts = 0 )
15251531 c ._make_api_call (
@@ -1529,7 +1535,18 @@ def test_connection_make_api_call___extra_headers(send_mock):
15291535
15301536 assert send_mock .call_count == 1
15311537 request = send_mock .call_args [0 ][0 ]
1532- assert request .headers .get ('foo' ).decode () == 'bar'
1538+ assert request .headers ['foo' ] == 'bar'
1539+
1540+ c = Connection (extra_headers = {'foo' : 'baz' }, max_retry_attempts = 0 )
1541+ c ._make_api_call (
1542+ 'DescribeTable' ,
1543+ {'TableName' : 'MyTable' },
1544+ )
1545+
1546+ assert send_mock .call_count == 2
1547+ request2 = send_mock .call_args [0 ][0 ]
1548+ # all headers, including signatures, and except 'foo', should match
1549+ assert {** request .headers , 'foo' : '' } == {** request2 .headers , 'foo' : '' }
15331550
15341551
15351552@mock .patch ('botocore.httpsession.URLLib3Session.send' )
0 commit comments