2
2
import unittest
3
3
4
4
try :
5
- from unittest .mock import patch
5
+ from unittest .mock import patch , MagicMock
6
6
except ImportError :
7
- from mock import patch
7
+ from mock import patch , MagicMock
8
8
9
9
from datadog_lambda .patch import _patch_httplib , _ensure_patch_requests
10
10
from datadog_lambda .constants import TraceHeader
@@ -24,11 +24,51 @@ def setUp(self):
24
24
def test_patch_httplib (self ):
25
25
_patch_httplib ()
26
26
if sys .version_info >= (3 , 0 , 0 ):
27
- import urllib . request as urllib
27
+ from http . client import HTTPSConnection
28
28
else :
29
- import urllib2 as urllib
30
- urllib .urlopen ("https://www.datadoghq.com/" )
29
+ from httplib import HTTPSConnection
30
+
31
+ conn = HTTPSConnection ("www.datadoghq.com" )
32
+ conn .request ("GET" , "/" )
33
+ conn .getresponse ()
34
+
35
+ self .mock_get_dd_trace_context .assert_called ()
36
+
37
+ def test_patch_httplib_dict_headers (self ):
38
+ _patch_httplib ()
39
+ if sys .version_info >= (3 , 0 , 0 ):
40
+ from http .client import HTTPSConnection
41
+ else :
42
+ from httplib import HTTPSConnection
43
+
44
+ headers = MagicMock (spec = dict )
45
+ headers ["fake-header" ] = "fake-value"
46
+
47
+ conn = HTTPSConnection ("www.datadoghq.com" )
48
+ conn .request ("GET" , "/" , headers = headers )
49
+ conn .getresponse ()
50
+
51
+ self .mock_get_dd_trace_context .assert_called ()
52
+ headers .update .assert_called ()
53
+
54
+ def test_patch_httplib_dict_like_headers (self ):
55
+ _patch_httplib ()
56
+ if sys .version_info >= (3 , 0 , 0 ):
57
+ from http .client import HTTPSConnection
58
+ from collections .abc import MutableMapping
59
+ else :
60
+ from httplib import HTTPSConnection
61
+ from collections import MutableMapping
62
+
63
+ headers = MagicMock (spec = MutableMapping )
64
+ headers ["fake-header" ] = "fake-value"
65
+
66
+ conn = HTTPSConnection ("www.datadoghq.com" )
67
+ conn .request ("GET" , "/" , headers = headers )
68
+ conn .getresponse ()
69
+
31
70
self .mock_get_dd_trace_context .assert_called ()
71
+ headers .update .assert_called ()
32
72
33
73
def test_patch_requests (self ):
34
74
_ensure_patch_requests ()
0 commit comments