summaryrefslogtreecommitdiff
path: root/httpretty/core.py
blob: b4ebfe13cfb6e23b664e42c0e8bb55899ebd3c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
# <HTTPretty - HTTP client mock for Python>
# Copyright (C) <2011-2020> Gabriel Falcão <gabriel@nacaolivre.org>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import io
import time
import codecs
import contextlib
import functools
import hashlib
import inspect
import logging
import itertools
import json
import types
import re
import socket
import tempfile
import threading
import traceback
import warnings

from functools import partial
from typing import Callable

from .compat import (
    BaseClass,
    BaseHTTPRequestHandler,
    quote,
    quote_plus,
    urlencode,
    encode_obj,
    urlunsplit,
    urlsplit,
    parse_qs,
    unquote_utf8,
)
from .http import (
    STATUSES,
    HttpBaseClass,
    parse_requestline,
    last_requestline,
)

from .utils import (
    utf8,
    decode_utf8,
)

from .errors import HTTPrettyError, UnmockedError

from datetime import datetime
from datetime import timedelta
from errno import EAGAIN

old_socket = socket.socket
old_socketpair = getattr(socket, 'socketpair', None)
old_SocketType = socket.SocketType
old_create_connection = socket.create_connection
old_gethostbyname = socket.gethostbyname
old_gethostname = socket.gethostname
old_getaddrinfo = socket.getaddrinfo
old_socksocket = None
old_ssl_wrap_socket = None
old_sslwrap_simple = None
old_sslsocket = None
old_sslcontext_wrap_socket = None
old_sslcontext = None

MULTILINE_ANY_REGEX = re.compile(r'.*', re.M)
hostname_re = re.compile(r'\^?(?:https?://)?[^:/]*[:/]?')


logger = logging.getLogger(__name__)

try:  # pragma: no cover
    import socks
    old_socksocket = socks.socksocket
except ImportError:
    socks = None

try:  # pragma: no cover
    import ssl
    old_sslcontext_class = ssl.SSLContext
    old_sslcontext = ssl.create_default_context()
    old_ssl_wrap_socket = old_sslcontext.wrap_socket
    try:
        old_sslcontext_wrap_socket = ssl.SSLContext.wrap_socket
    except AttributeError:
        pass
    old_sslsocket = ssl.SSLSocket
except ImportError:  # pragma: no cover
    ssl = None

try:
    import _ssl
except ImportError:
    _ssl = None
# used to handle error caused by ndg-httpsclient
pyopenssl_overrides_inject = []
pyopenssl_overrides_extract = []
try:
    from requests.packages.urllib3.contrib.pyopenssl import inject_into_urllib3, extract_from_urllib3
    pyopenssl_overrides_extract.append(extract_from_urllib)
    pyopenssl_overrides_inject.append(inject_from_urllib)
except Exception:
    pass



try:
    from urllib3.contrib.pyopenssl import extract_from_urllib3, inject_into_urllib3
    pyopenssl_overrides_extract.append(extract_from_urllib)
    pyopenssl_overrides_inject.append(inject_from_urllib)
except Exception:
    pass


try:
    import requests.packages.urllib3.connection as requests_urllib3_connection
    old_requests_ssl_wrap_socket = requests_urllib3_connection.ssl_wrap_socket
except ImportError:
    requests_urllib3_connection = None
    old_requests_ssl_wrap_socket = None

try:
    import eventlet
    import eventlet.green
except ImportError:
    eventlet = None

DEFAULT_HTTP_PORTS = frozenset([80])
POTENTIAL_HTTP_PORTS = set(DEFAULT_HTTP_PORTS)
DEFAULT_HTTPS_PORTS = frozenset([443])
POTENTIAL_HTTPS_PORTS = set(DEFAULT_HTTPS_PORTS)


def FALLBACK_FUNCTION(x):
    return x


class HTTPrettyRequest(BaseHTTPRequestHandler, BaseClass):
    r"""Represents a HTTP request. It takes a valid multi-line,
    ``\r\n`` separated string with HTTP headers and parse them out using
    the internal `parse_request` method.

    It also replaces the `rfile` and `wfile` attributes with :py:class:`io.BytesIO`
    instances so that we guarantee that it won't make any I/O, neither
    for writing nor reading.

    It has some convenience attributes:

    ``headers`` -> a mimetype object that can be cast into a dictionary,
    contains all the request headers

    ``protocol`` -> the protocol of this host, inferred from the port
    of the underlying fake TCP socket.

    ``host`` -> the hostname of this request.

    ``url`` -> the full url of this request.

    ``path`` -> the path of the request.

    ``method`` -> the HTTP method used in this request.

    ``querystring`` -> a dictionary containing lists with the
    attributes. Please notice that if you need a single value from a
    query string you will need to get it manually like:

    ``body`` -> the request body as a string.

    ``parsed_body`` -> the request body parsed by ``parse_request_body``.

    .. testcode::

      >>> request.querystring
      {'name': ['Gabriel Falcao']}
      >>> print request.querystring['name'][0]

    """
    def __init__(self, headers, body='', sock=None, path_encoding = 'iso-8859-1'):
        # first of all, lets make sure that if headers or body are
        # unicode strings, it must be converted into a utf-8 encoded
        # byte string
        self.created_at = time.time()
        self.raw_headers = utf8(headers.strip())
        self._body = utf8(body)
        self.connection = sock
        # Now let's concatenate the headers with the body, and create
        # `rfile` based on it
        self.rfile = io.BytesIO(b'\r\n\r\n'.join([self.raw_headers, self.body]))

        # Creating `wfile` as an empty BytesIO, just to avoid any
        # real I/O calls
        self.wfile = io.BytesIO()

        # parsing the request line preemptively
        self.raw_requestline = self.rfile.readline()

        # initiating the error attributes with None
        self.error_code = None
        self.error_message = None

        # Parse the request based on the attributes above
        if not self.parse_request():
            return

        # Now 2 convenient attributes for the HTTPretty API:

        # - `path`
        # - `querystring` holds a dictionary with the parsed query string
        # - `parsed_body` a string
        try:
            self.path = self.path.encode(path_encoding)
        except UnicodeDecodeError:
            pass

        self.path = decode_utf8(self.path)

        qstring = self.path.split("?", 1)[-1]
        self.querystring = self.parse_querystring(qstring)

        # And the body will be attempted to be parsed as
        # `application/json` or `application/x-www-form-urlencoded`
        """a dictionary containing parsed request body or None if
        HTTPrettyRequest doesn't know how to parse it.  It currently
        supports parsing body data that was sent under the
        ``content`-type` headers values: ``application/json`` or
        ``application/x-www-form-urlencoded``
        """
        self.parsed_body = self.parse_request_body(self._body)

    @property
    def method(self):
        """the HTTP method used in this request"""
        return self.command

    @property
    def protocol(self):
        """the protocol used in this request"""
        proto = ''
        if not self.connection:
            return ''
        elif self.connection.is_http:
            proto = 'http'

        if self.connection.is_secure:
            proto = 'https'

        return proto

    @property
    def body(self):
        return self._body

    @body.setter
    def body(self, value):
        self._body = utf8(value)

        # And the body will be attempted to be parsed as
        # `application/json` or `application/x-www-form-urlencoded`
        self.parsed_body = self.parse_request_body(self._body)

    def __nonzero__(self):
        return bool(self.body) or bool(self.raw_headers)

    @property
    def url(self):
        """the full url of this recorded request"""
        return "{}://{}{}".format(self.protocol, self.host, self.path)

    @property
    def host(self):
        return self.headers.get('Host') or '<unknown>'

    def __str__(self):
        tmpl = '<HTTPrettyRequest("{}", "{}", headers={}, body={})>'
        return tmpl.format(
            self.method,
            self.url,
            dict(self.headers),
            len(self.body),
        )

    def parse_querystring(self, qs):
        """parses an UTF-8 encoded query string into a dict of string lists

        :param qs: a querystring
        :returns: a dict of lists

        """
        expanded = unquote_utf8(qs)
        parsed = parse_qs(expanded)
        result = {}
        for k in parsed:
            result[k] = list(map(decode_utf8, parsed[k]))

        return result

    def parse_request_body(self, body):
        """Attempt to parse the post based on the content-type passed.
        Return the regular body if not

        :param body: string
        :returns: a python object such as dict or list in case the deserialization suceeded. Else returns the given param ``body``
        """

        PARSING_FUNCTIONS = {
            'application/json': json.loads,
            'text/json': json.loads,
            'application/x-www-form-urlencoded': self.parse_querystring,
        }

        content_type = self.headers.get('content-type', '')

        do_parse = PARSING_FUNCTIONS.get(content_type, FALLBACK_FUNCTION)
        try:
            body = decode_utf8(body)
            return do_parse(body)
        except Exception:
            return body


class EmptyRequestHeaders(dict):
    """A dict subclass used as internal representation of empty request
    headers
    """


class HTTPrettyRequestEmpty(object):
    """Represents an empty :py:class:`~httpretty.core.HTTPrettyRequest`
    where all its properties are somehow empty or ``None``
    """

    method = None
    url = None
    body = ''
    headers = EmptyRequestHeaders()


class FakeSockFile(object):
    """Fake socket file descriptor. Under the hood all data is written in
    a temporary file, giving it a real file descriptor number.

    """
    def __init__(self):
        self.file = tempfile.TemporaryFile()
        self._fileno = self.file.fileno()

    def getvalue(self):
        if hasattr(self.file, 'getvalue'):
            return self.file.getvalue()
        else:
            return self.file.read()

    def close(self):
        self.socket.close()

    def fileno(self):
        return self._fileno

    def __getattr__(self, name):
        return getattr(self.file, name)

    def __del__(self):
        try:
            self.close()
        except (ValueError, AttributeError):
            pass


class FakeSSLSocket(object):
    """Shorthand for :py:class:`~httpretty.core.fakesock`
    """
    def __init__(self, sock, *args, **kw):
        self._httpretty_sock = sock

    def __getattr__(self, attr):
        return getattr(self._httpretty_sock, attr)


class FakeAddressTuple(object):
    def __init__(self, fakesocket):
        self.fakesocket = fakesocket

    def __getitem__(self, *args, **kw):
        raise AssertionError('socket {} is not connected'.format(self.fakesocket.truesock))


def fake_socketpair(*args, **kw):
    with restored_libs():
        return old_socketpair(*args, **kw)

class fakesock(object):
    """
    fake :py:mod:`socket`
    """
    class socket(object):
        """drop-in replacement for :py:class:`socket.socket`
        """
        _entry = None
        _read_buf = None

        debuglevel = 0
        _sent_data = []
        is_secure = False
        def __init__(
            self,
            family=socket.AF_INET,
            type=socket.SOCK_STREAM,
            proto=0,
            fileno=None
        ):
            self.socket_family = family
            self.socket_type = type
            self.socket_proto = proto
            if httpretty.allow_net_connect:
                self.truesock = self.create_socket()
            else:
                self.truesock = None

            self._address = FakeAddressTuple(self)
            self.__truesock_is_connected__ = False
            self.fd = FakeSockFile()
            self.fd.socket = fileno or self
            self.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
            self._sock = fileno or self
            self.is_http = False
            self._bufsize = 32 * 1024

        def __repr__(self):
            return '{self.__class__.__module__}.{self.__class__.__name__}("{self.host}")'.format(**locals())

        @property
        def host(self):
            return ":".join(map(str, self._address))

        def create_socket(self, address=None):
            return old_socket(self.socket_family, self.socket_type, self.socket_proto)

        def getpeercert(self, *a, **kw):
            now = datetime.now()
            shift = now + timedelta(days=30 * 12)
            return {
                'notAfter': shift.strftime('%b %d %H:%M:%S GMT'),
                'subjectAltName': (
                    ('DNS', '*.%s' % self._host),
                    ('DNS', self._host),
                    ('DNS', '*'),
                ),
                'subject': (
                    (
                        ('organizationName', '*.%s' % self._host),
                    ),
                    (
                        ('organizationalUnitName',
                         'Domain Control Validated'),
                    ),
                    (
                        ('commonName', '*.%s' % self._host),
                    ),
                ),
            }

        def ssl(self, sock, *args, **kw):
            return sock

        def setsockopt(self, level, optname, value):
            if httpretty.allow_net_connect and not self.truesock:
                self.truesock = self.create_socket()
            elif not self.truesock:
                logger.debug('setsockopt(%s, %s, %s) failed', level, optname, value)
                return

            return self.truesock.setsockopt(level, optname, value)

        def connect(self, address):
            try:
                self._address = (self._host, self._port) = address
            except ValueError:
                # We get here when the address is just a string pointing to a
                # unix socket path/file
                #
                # See issue #206
                self.is_http = False
            else:
                ports_to_check = (
                    POTENTIAL_HTTP_PORTS.union(POTENTIAL_HTTPS_PORTS))
                self.is_http = self._port in ports_to_check
                self.is_secure = self._port in POTENTIAL_HTTPS_PORTS

            if not self.is_http:
                self.connect_truesock(address=address)
            elif self.truesock and not self.real_socket_is_connected():
                # TODO: remove nested if
                matcher = httpretty.match_http_address(self._host, self._port)
                if matcher is None:
                    self.connect_truesock(address=address)

        def bind(self, address):
            self._address = (self._host, self._port) = address
            if self.truesock:
                self.bind_truesock(address)

        def bind_truesock(self, address):
            if httpretty.allow_net_connect and not self.truesock:
                self.truesock = self.create_socket()
            elif not self.truesock:
                raise UnmockedError('Failed to socket.bind() because because a real socket was never created.', address=address)

            return self.truesock.bind(address)

        def connect_truesock(self, request=None, address=None):
            address = address or self._address

            if self.__truesock_is_connected__:
                return self.truesock

            if request:
                logger.warning('real call to socket.connect() for {request}'.format(**locals()))
            elif address:
                logger.warning('real call to socket.connect() for {address}'.format(**locals()))
            else:
                logger.warning('real call to socket.connect()')

            if httpretty.allow_net_connect and not self.truesock:
                self.truesock = self.create_socket(address)
            elif not self.truesock:
                raise UnmockedError('Failed to socket.connect() because because a real socket was never created.', request=request, address=address)

            undo_patch_socket()
            try:
                hostname = self._address[0]
                port = 80
                if len(self._address) == 2:
                    port = self._address[1]
                if port ==  443 and old_sslsocket:
                    self.truesock = old_ssl_wrap_socket(self.truesock, server_hostname=hostname)

                sock = self.truesock

                sock.connect(self._address)
                self.__truesock_is_connected__ = True
                self.truesock = sock
            finally:
                apply_patch_socket()

            return self.truesock

        def real_socket_is_connected(self):
            return self.__truesock_is_connected__

        def fileno(self):
            if self.truesock:
                return self.truesock.fileno()
            return self.fd.fileno()

        def close(self):
            if self.truesock:
                self.truesock.close()
                self.truesock = None
                self.__truesock_is_connected__ = False

        def makefile(self, mode='r', bufsize=-1):
            """Returns this fake socket's own tempfile buffer.

            If there is an entry associated with the socket, the file
            descriptor gets filled in with the entry data before being
            returned.
            """
            self._mode = mode
            self._bufsize = bufsize

            if self._entry:
                t = threading.Thread(
                    target=self._entry.fill_filekind, args=(self.fd,)
                )
                t.start()
                if self.timeout == socket._GLOBAL_DEFAULT_TIMEOUT:
                    timeout = None
                else:
                    timeout = self.timeout
                t.join(timeout)
                if t.is_alive():
                    raise socket.timeout

            return self.fd

        def real_sendall(self, data, *args, **kw):
            """Sends data to the remote server. This method is called
            when HTTPretty identifies that someone is trying to send
            non-http data.

            The received bytes are written in this socket's tempfile
            buffer so that HTTPretty can return it accordingly when
            necessary.
            """
            request = kw.pop('request', None)
            if request:
                bytecount = len(data)
                logger.warning('{self}.real_sendall({bytecount} bytes) to {request.url} via {request.method} at {request.created_at}'.format(**locals()))

            if httpretty.allow_net_connect and not self.truesock:

                self.connect_truesock(request=request)
            elif not self.truesock:
                raise UnmockedError(request=request)

            if not self.is_http:
                self.truesock.setblocking(1)
                return self.truesock.sendall(data, *args, **kw)

            sock = self.connect_truesock(request=request)

            sock.setblocking(1)
            sock.sendall(data, *args, **kw)

            should_continue = True
            while should_continue:
                try:
                    received = sock.recv(self._bufsize)
                    self.fd.write(received)
                    should_continue = bool(received.strip())

                except socket.error as e:
                    if e.errno == EAGAIN:
                        continue
                    break

            self.fd.seek(0)

        def sendall(self, data, *args, **kw):
            # if self.__truesock_is_connected__:
            #     return self.truesock.sendall(data, *args, **kw)

            self._sent_data.append(data)
            self.fd = FakeSockFile()
            self.fd.socket = self
            if isinstance(data, str):
                data = data.encode('utf-8')
            elif not isinstance(data, bytes):
                logger.debug('cannot sendall({data!r})')
                data = bytes(data)

            try:
                requestline, _ = data.split(b'\r\n', 1)
                method, path, version = parse_requestline(
                    decode_utf8(requestline))
                is_parsing_headers = True
            except ValueError:
                path = ''
                is_parsing_headers = False

                if self._entry is None:
                    # If the previous request wasn't mocked, don't
                    # mock the subsequent sending of data
                    return self.real_sendall(data, *args, **kw)
                else:
                    method = self._entry.method
                    path = self._entry.info.path

            self.fd.seek(0)

            if not is_parsing_headers:
                if len(self._sent_data) > 1:
                    headers = utf8(last_requestline(self._sent_data))
                    meta = self._entry.request.headers
                    body = utf8(self._sent_data[-1])
                    if meta.get('transfer-encoding', '') == 'chunked':
                        if not body.isdigit() and (body != b'\r\n') and (body != b'0\r\n\r\n'):
                            self._entry.request.body += body
                    else:
                        self._entry.request.body += body

                    httpretty.historify_request(headers, body, sock=self)
                    return

            if path[:2] == '//':
                path = '//' + path
            # path might come with
            s = urlsplit(path)
            POTENTIAL_HTTP_PORTS.add(int(s.port or 80))
            parts = list(map(utf8, data.split(b'\r\n\r\n', 1)))
            if len(parts) == 2:
                headers, body = parts
            else:
                headers = ''
                body = data

            request = httpretty.historify_request(headers, body, sock=self)

            info = URIInfo(
                hostname=self._host,
                port=self._port,
                path=s.path,
                query=s.query,
                last_request=request
            )

            matcher, entries = httpretty.match_uriinfo(info)

            if not entries:
                logger.debug('no entries matching {}'.format(request))
                self._entry = None
                self._read_buf = None
                self.real_sendall(data, request=request)
                return

            self._entry = matcher.get_next_entry(method, info, request)

        def forward_and_trace(self, function_name, *a, **kw):
            if not self.truesock:
                raise UnmockedError('Failed to socket.{}() because because a real socket was never created.'.format(function_name))

            callback = getattr(self.truesock, function_name)
            return callback(*a, **kw)

        def settimeout(self, new_timeout):
            self.timeout = new_timeout
            if not self.is_http:
                if self.truesock:
                    self.truesock.settimeout(new_timeout)

        def send(self, data, *args, **kwargs):
            self.sendall(data, *args, **kwargs)
            return len(data)

        def sendto(self, *args, **kwargs):
            return self.forward_and_trace('sendto', *args, **kwargs)

        def recvfrom_into(self, *args, **kwargs):
            return self.forward_and_trace('recvfrom_into', *args, **kwargs)

        def recv_into(self, *args, **kwargs):
            return self.forward_and_trace('recv_into', *args, **kwargs)

        def recvfrom(self, *args, **kwargs):
            return self.forward_and_trace('recvfrom', *args, **kwargs)

        def recv(self, buffersize=0, *args, **kwargs):
            if not self._read_buf:
                self._read_buf = io.BytesIO()

            if self._entry:
                self._entry.fill_filekind(self._read_buf)

            if not self._read_buf:
                raise UnmockedError('socket cannot recv(): {!r}'.format(self))

            return self._read_buf.read(buffersize)

        def __getattr__(self, name):
            if name in ('getsockopt', 'selected_alpn_protocol') and not self.truesock:
                self.truesock = self.create_socket()
            elif httpretty.allow_net_connect and not self.truesock:
                # can't call self.connect_truesock() here because we
                # don't know if user wants to execute server of client
                # calls (or can they?)
                self.truesock = self.create_socket()
            elif not self.truesock:
                # Special case for
                # `hasattr(sock, "version")` call added in urllib3>=1.26.
                if name == 'version':
                    raise AttributeError(
                        "HTTPretty synthesized this error to fix urllib3 compatibility "
                        "(see issue https://github.com/gabrielfalcao/HTTPretty/issues/409). "
                        "Please open an issue if this error causes further unexpected issues."
                    )

                raise UnmockedError('Failed to socket.{} because because a real socket does not exist'.format(name))

            return getattr(self.truesock, name)

def with_socket_is_secure(sock, kw):
    sock.is_secure = True
    sock.kwargs = kw
    for k, v in kw.items():
        setattr(sock, k, v)
    return sock

def fake_wrap_socket(orig_wrap_socket_fn, *args, **kw):
    """drop-in replacement for py:func:`ssl.wrap_socket`
    """
    if 'sock' in kw:
        sock = kw['sock']
    else:
        sock = args[0]

    server_hostname = kw.get('server_hostname')
    if server_hostname is not None:
        matcher = httpretty.match_https_hostname(server_hostname)
        if matcher is None:
            logger.debug('no requests registered for hostname: "{}"'.format(server_hostname))
            return with_socket_is_secure(sock, kw)

    return with_socket_is_secure(sock, kw)


def create_fake_connection(
        address,
        timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
        source_address=None):
    """drop-in replacement for :py:func:`socket.create_connection`"""
    s = fakesock.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
        s.settimeout(timeout)

    if isinstance(source_address, tuple) and len(source_address) == 2:
        source_address[1] = int(source_address[1])

    if source_address:
        s.bind(source_address)
    s.connect(address)
    return s


def fake_gethostbyname(host):
    """drop-in replacement for :py:func:`socket.gethostbyname`"""
    return '127.0.0.1'


def fake_gethostname():
    """drop-in replacement for :py:func:`socket.gethostname`"""
    return 'localhost'


def fake_getaddrinfo(
        host, port, family=None, socktype=None, proto=None, flags=None):
    """drop-in replacement for :py:func:`socket.getaddrinfo`"""
    return [(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP,
             '', (host, port)),
            (socket.AF_INET6, socket.SOCK_STREAM, socket.IPPROTO_TCP,
             '', (host, port))]


class Entry(BaseClass):
    """Created by :py:meth:`~httpretty.core.httpretty.register_uri` and
    stored in memory as internal representation of a HTTP
    request/response definition.

    Args:
        method (str): One of ``httpretty.GET``, ``httpretty.PUT``, ``httpretty.POST``, ``httpretty.DELETE``, ``httpretty.HEAD``, ``httpretty.PATCH``, ``httpretty.OPTIONS``, ``httpretty.CONNECT``.
        uri (str|re.Pattern): The URL to match
        adding_headers (dict): Extra headers to be added to the response
        forcing_headers (dict): Overwrite response headers.
        status (int): The status code for the response, defaults to ``200``.
        streaming (bool): Whether should stream the response into chunks via generator.
        headers: Headers to inject in the faked response.

    Returns:
        httpretty.Entry: containing the request-matching metadata.


    .. warning:: When using the ``forcing_headers`` option make sure to add the header ``Content-Length`` to match at most the total body length, otherwise some HTTP clients can hang indefinitely.
    """
    def __init__(self, method, uri, body,
                 adding_headers=None,
                 forcing_headers=None,
                 status=200,
                 streaming=False,
                 **headers):

        self.method = method
        self.uri = uri
        self.info = None
        self.request = None

        self.body_is_callable = False
        if hasattr(body, "__call__"):
            self.callable_body = body
            self.body = None
            self.body_is_callable = True
        elif isinstance(body, str):
            self.body = utf8(body)
        else:
            self.body = body

        self.streaming = streaming
        if not streaming and not self.body_is_callable:
            self.body_length = len(self.body or '')
        else:
            self.body_length = 0

        self.adding_headers = adding_headers or {}
        self.forcing_headers = forcing_headers or {}
        self.status = int(status)

        for k, v in headers.items():
            name = "-".join(k.split("_")).title()
            self.adding_headers[name] = v

        self.validate()

    def validate(self):
        """validates the body size with the value of the ``Content-Length``
        header
        """
        content_length_keys = 'Content-Length', 'content-length'
        for key in content_length_keys:
            got = self.adding_headers.get(
                key, self.forcing_headers.get(key, None))

            if got is None:
                continue

            igot = None
            try:
                igot = int(got)
            except (ValueError, TypeError):
                warnings.warn(
                    'HTTPretty got to register the Content-Length header '
                    'with "%r" which is not a number' % got)
                return

            if igot and igot > self.body_length:
                raise HTTPrettyError(
                    'HTTPretty got inconsistent parameters. The header '
                    'Content-Length you registered expects size "%d" but '
                    'the body you registered for that has actually length '
                    '"%d".' % (
                        igot, self.body_length,
                    )
                )

    def __str__(self):
        return r'<Entry {} {} getting {}>'.format(
            self.method,
            self.uri,
            self.status
        )

    def normalize_headers(self, headers):
        """Normalize keys in header names so that ``COntent-tyPe`` becomes ``content-type``

        :param headers: dict

        :returns: dict
        """
        new = {}
        for k in headers:
            new_k = '-'.join([s.lower() for s in k.split('-')])
            new[new_k] = headers[k]

        return new

    def fill_filekind(self, fk):
        """writes HTTP Response data to a file descriptor

        :parm fk: a file-like object

        .. warning:: **side-effect:** this method moves the cursor of the given file object to zero
        """
        now = datetime.utcnow()

        headers = {
            'status': self.status,
            'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
            'server': 'Python/HTTPretty',
            'connection': 'close',
        }

        if self.forcing_headers:
            headers = self.forcing_headers

        if self.adding_headers:
            headers.update(
                self.normalize_headers(
                    self.adding_headers))

        headers = self.normalize_headers(headers)
        status = headers.get('status', self.status)
        if self.body_is_callable:
            status, headers, self.body = self.callable_body(self.request, self.info.full_url(), headers)
            headers = self.normalize_headers(headers)
            # TODO: document this behavior:
            if 'content-length' not in headers:
                headers.update({
                    'content-length': len(self.body)
                })

        string_list = [
            'HTTP/1.1 %d %s' % (status, STATUSES[status]),
        ]

        if 'date' in headers:
            string_list.append('date: %s' % headers.pop('date'))

        if not self.forcing_headers:
            content_type = headers.pop('content-type',
                                       'text/plain; charset=utf-8')

            content_length = headers.pop('content-length',
                                         self.body_length)

            string_list.append('content-type: %s' % content_type)
            if not self.streaming:
                string_list.append('content-length: %s' % content_length)

            server = headers.pop('server', None)
            if server:
                string_list.append('server: %s' % server)

        for k, v in headers.items():
            string_list.append(
                '{}: {}'.format(k, v),
            )

        for item in string_list:
            fk.write(utf8(item) + b'\n')

        fk.write(b'\r\n')

        if self.streaming:
            self.body, body = itertools.tee(self.body)
            for chunk in body:
                fk.write(utf8(chunk))
        else:
            fk.write(utf8(self.body))

        fk.seek(0)


def url_fix(s, charset=None):
    """escapes special characters
    """
    if charset:
        warnings.warn("{}.url_fix() charset argument is deprecated".format(__name__), DeprecationWarning)

    scheme, netloc, path, querystring, fragment = urlsplit(s)
    path = quote(path, b'/%')
    querystring = quote_plus(querystring, b':&=')
    return urlunsplit((scheme, netloc, path, querystring, fragment))


class URIInfo(BaseClass):
    """Internal representation of `URIs <https://en.wikipedia.org/wiki/Uniform_Resource_Identifier>`_

    .. tip:: all arguments are optional

    :param username:
    :param password:
    :param hostname:
    :param port:
    :param path:
    :param query:
    :param fragment:
    :param scheme:
    :param last_request:
    """
    default_str_attrs = (
        'username',
        'password',
        'hostname',
        'port',
        'path',
    )

    def __init__(self,
                 username='',
                 password='',
                 hostname='',
                 port=80,
                 path='/',
                 query='',
                 fragment='',
                 scheme='',
                 last_request=None):

        self.username = username or ''
        self.password = password or ''
        self.hostname = hostname or ''

        if port:
            port = int(port)

        elif scheme == 'https':
            port = 443

        self.port = port or 80
        self.path = path or ''
        if query:
            query_items = sorted(parse_qs(query).items())
            self.query = urlencode(
                encode_obj(query_items),
                doseq=True,
            )
        else:
            self.query = ''
        if scheme:
            self.scheme = scheme
        elif self.port in POTENTIAL_HTTPS_PORTS:
            self.scheme = 'https'
        else:
            self.scheme = 'http'
        self.fragment = fragment or ''
        self.last_request = last_request

    def to_str(self, attrs):
        fmt = ", ".join(['%s="%s"' % (k, getattr(self, k, '')) for k in attrs])
        return r'<httpretty.URIInfo(%s)>' % fmt

    def __str__(self):
        return self.to_str(self.default_str_attrs)

    def str_with_query(self):
        attrs = self.default_str_attrs + ('query',)
        return self.to_str(attrs)

    def __hash__(self):
        return int(hashlib.sha1(bytes(self, 'ascii')).hexdigest(), 16)

    def __eq__(self, other):
        self_tuple = (
            self.port,
            decode_utf8(self.hostname.lower()),
            url_fix(decode_utf8(self.path)),
        )
        other_tuple = (
            other.port,
            decode_utf8(other.hostname.lower()),
            url_fix(decode_utf8(other.path)),
        )
        return self_tuple == other_tuple

    def full_url(self, use_querystring=True):
        """
        :param use_querystring: bool
        :returns: a string with the full url with the format ``{scheme}://{credentials}{domain}{path}{query}``
        """
        credentials = ""
        if self.password:
            credentials = "{}:{}@".format(
                self.username, self.password)

        query = ""
        if use_querystring and self.query:
            query = "?{}".format(decode_utf8(self.query))

        result = "{scheme}://{credentials}{domain}{path}{query}".format(
            scheme=self.scheme,
            credentials=credentials,
            domain=self.get_full_domain(),
            path=decode_utf8(self.path),
            query=query
        )
        return result

    def get_full_domain(self):
        """
        :returns: a string in the form ``{domain}:{port}`` or just the domain if the port is 80 or 443
        """
        hostname = decode_utf8(self.hostname)
        # Port 80/443 should not be appended to the url
        if self.port not in DEFAULT_HTTP_PORTS | DEFAULT_HTTPS_PORTS:
            return ":".join([hostname, str(self.port)])

        return hostname

    @classmethod
    def from_uri(cls, uri, entry):
        """
        :param uri: string
        :param entry: an instance of :py:class:`~httpretty.core.Entry`
        """
        result = urlsplit(uri)
        if result.scheme == 'https':
            POTENTIAL_HTTPS_PORTS.add(int(result.port or 443))
        else:
            POTENTIAL_HTTP_PORTS.add(int(result.port or 80))
        return cls(result.username,
                   result.password,
                   result.hostname,
                   result.port,
                   result.path,
                   result.query,
                   result.fragment,
                   result.scheme,
                   entry)


class URIMatcher(object):
    regex = None
    info = None

    def __init__(self, uri, entries, match_querystring=False, priority=0):
        self._match_querystring = match_querystring
        # CPython, Jython
        regex_types = ('SRE_Pattern', 'org.python.modules.sre.PatternObject',
                       'Pattern')
        is_regex = type(uri).__name__ in regex_types
        if is_regex:
            self.regex = uri
            result = urlsplit(uri.pattern)
            if result.scheme == 'https':
                POTENTIAL_HTTPS_PORTS.add(int(result.port or 443))
            else:
                POTENTIAL_HTTP_PORTS.add(int(result.port or 80))
        else:
            self.info = URIInfo.from_uri(uri, entries)

        self.entries = entries
        self.priority = priority
        self.uri = uri
        # hash of current_entry pointers, per method.
        self.current_entries = {}

    def matches(self, info):
        if self.info:
            # Query string is not considered when comparing info objects, compare separately
            return self.info == info and (not self._match_querystring or self.info.query == info.query)
        else:
            return self.regex.search(info.full_url(
                use_querystring=self._match_querystring))

    def __str__(self):
        wrap = 'URLMatcher({})'
        if self.info:
            if self._match_querystring:
                return wrap.format(str(self.info.str_with_query()))
            else:
                return wrap.format(str(self.info))
        else:
            return wrap.format(self.regex.pattern)

    def get_next_entry(self, method, info, request):
        """Cycle through available responses, but only once.
        Any subsequent requests will receive the last response"""

        if method not in self.current_entries:
            self.current_entries[method] = 0

        # restrict selection to entries that match the requested
        # method
        entries_for_method = [e for e in self.entries if e.method == method]

        if self.current_entries[method] >= len(entries_for_method):
            self.current_entries[method] = -1

        if not self.entries or not entries_for_method:
            raise ValueError('I have no entries for method %s: %s'
                             % (method, self))

        entry = entries_for_method[self.current_entries[method]]
        if self.current_entries[method] != -1:
            self.current_entries[method] += 1

        # Create a copy of the original entry to make it thread-safe
        body = entry.callable_body if entry.body_is_callable else entry.body
        new_entry = Entry(entry.method, entry.uri, body,
                          status=entry.status,
                          streaming=entry.streaming,
                          adding_headers=entry.adding_headers,
                          forcing_headers=entry.forcing_headers)

        # Attach more info to the entry
        # So the callback can be more clever about what to do
        # This does also fix the case where the callback
        # would be handed a compiled regex as uri instead of the
        # real uri
        new_entry.info = info
        new_entry.request = request
        return new_entry

    def __hash__(self):
        return hash(str(self))

    def __eq__(self, other):
        return str(self) == str(other)


class httpretty(HttpBaseClass):
    """manages HTTPretty's internal request/response registry and request matching.
    """
    _entries = {}
    latest_requests = []

    last_request = HTTPrettyRequestEmpty()
    _is_enabled = False
    allow_net_connect = True

    @classmethod
    def match_uriinfo(cls, info):
        """
        :param info: an :py:class:`~httpretty.core.URIInfo`
        :returns: a 2-item tuple: (:py:class:`~httpretty.core.URLMatcher`, :py:class:`~httpretty.core.URIInfo`) or ``(None, [])``
        """
        items = sorted(
            cls._entries.items(),
            key=lambda matcher_entries: matcher_entries[0].priority,
            reverse=True,
        )
        for matcher, value in items:
            if matcher.matches(info):
                return (matcher, info)

        return (None, [])

    @classmethod
    def match_https_hostname(cls, hostname):
        """
        :param hostname: a string
        :returns: an :py:class:`~httpretty.core.URLMatcher` or ``None``
        """
        items = sorted(
            cls._entries.items(),
            key=lambda matcher_entries: matcher_entries[0].priority,
            reverse=True,
        )
        for matcher, value in items:
            if matcher.info is None:
                pattern_with_port = "https://{0}:".format(hostname)
                pattern_without_port = "https://{0}/".format(hostname)
                hostname_pattern = (
                    hostname_re
                    .match(matcher.regex.pattern)
                    .group(0)
                )
                for pattern in [pattern_with_port, pattern_without_port]:
                    if re.match(hostname_pattern, pattern):
                        return matcher

            elif matcher.info.hostname == hostname:
                return matcher
        return None

    @classmethod
    def match_http_address(cls, hostname, port):
        """
        :param hostname: a string
        :param port: an integer
        :returns: an :py:class:`~httpretty.core.URLMatcher` or ``None``
        """
        items = sorted(
            cls._entries.items(),
            key=lambda matcher_entries: matcher_entries[0].priority,
            reverse=True,
        )
        for matcher, value in items:
            if matcher.info is None:
                if port in POTENTIAL_HTTPS_PORTS:
                    scheme = 'https://'
                else:
                    scheme = 'http://'

                pattern_without_port = "{0}{1}/".format(scheme, hostname)
                pattern_with_port = "{0}{1}:{2}/".format(scheme, hostname, port)
                hostname_pattern = (
                    hostname_re
                    .match(matcher.regex.pattern)
                    .group(0)
                )
                for pattern in [pattern_with_port, pattern_without_port]:
                    if re.match(hostname_pattern, pattern):
                        return matcher

            elif matcher.info.hostname == hostname \
                    and matcher.info.port == port:
                return matcher

        return None

    @classmethod
    @contextlib.contextmanager
    def record(cls, filename, indentation=4, encoding='utf-8', verbose=False, allow_net_connect=True, pool_manager_params=None):
        """
        .. testcode::

           import io
           import json
           import requests
           import httpretty

           with httpretty.record('/tmp/ip.json'):
               data = requests.get('https://httpbin.org/ip').json()

           with io.open('/tmp/ip.json') as fd:
               assert data == json.load(fd)

        :param filename: a string
        :param indentation: an integer, defaults to **4**
        :param encoding: a string, defaults to **"utf-8"**

        :returns: a `context-manager <https://docs.python.org/3/reference/datamodel.html#context-managers>`_
        """
        try:
            import urllib3
        except ImportError:
            msg = (
                'HTTPretty requires urllib3 installed '
                'for recording actual requests.'
            )
            raise RuntimeError(msg)

        http = urllib3.PoolManager(**pool_manager_params or {})

        cls.enable(allow_net_connect, verbose=verbose)
        calls = []

        def record_request(request, uri, headers):
            cls.disable()

            kw = {}
            kw.setdefault('body', request.body)
            kw.setdefault('headers', dict(request.headers))
            response = http.request(request.method, uri, **kw)
            calls.append({
                'request': {
                    'uri': uri,
                    'method': request.method,
                    'headers': dict(request.headers),
                    'body': decode_utf8(request.body),
                    'querystring': request.querystring
                },
                'response': {
                    'status': response.status,
                    'body': decode_utf8(response.data),
                    # urllib3 1.10 had a bug if you just did:
                    # dict(response.headers)
                    # which would cause all the values to become lists
                    # with the header name as the first item and the
                    # true value as the second item. Workaround that
                    'headers': dict(response.headers.items())
                }
            })
            cls.enable(allow_net_connect, verbose=verbose)
            return response.status, response.headers, response.data

        for method in cls.METHODS:
            cls.register_uri(method, MULTILINE_ANY_REGEX, body=record_request)

        yield
        cls.disable()
        with codecs.open(filename, 'w', encoding) as f:
            f.write(json.dumps(calls, indent=indentation))

    @classmethod
    @contextlib.contextmanager
    def playback(cls, filename, allow_net_connect=True, verbose=False):
        """
        .. testcode::

           import io
           import json
           import requests
           import httpretty

           with httpretty.record('/tmp/ip.json'):
               data = requests.get('https://httpbin.org/ip').json()

           with io.open('/tmp/ip.json') as fd:
               assert data == json.load(fd)

        :param filename: a string
        :returns: a `context-manager <https://docs.python.org/3/reference/datamodel.html#context-managers>`_
        """
        cls.enable(allow_net_connect, verbose=verbose)

        data = json.loads(open(filename).read())
        for item in data:
            uri = item['request']['uri']
            method = item['request']['method']
            body = item['response']['body']
            headers = item['response']['headers']
            cls.register_uri(method, uri, body=body, forcing_headers=headers)

        yield
        cls.disable()

    @classmethod
    def reset(cls):
        """resets the internal state of HTTPretty, unregistering all URLs
        """
        POTENTIAL_HTTP_PORTS.intersection_update(DEFAULT_HTTP_PORTS)
        POTENTIAL_HTTPS_PORTS.intersection_update(DEFAULT_HTTPS_PORTS)
        cls._entries.clear()
        cls.latest_requests = []
        cls.last_request = HTTPrettyRequestEmpty()

    @classmethod
    def historify_request(cls, headers, body='', sock=None):
        """appends request to a list for later retrieval

        .. testcode::

           import httpretty

           httpretty.register_uri(httpretty.GET, 'https://httpbin.org/ip', body='')
           with httpretty.enabled():
               requests.get('https://httpbin.org/ip')

           assert httpretty.latest_requests[-1].url == 'https://httpbin.org/ip'
        """
        request = HTTPrettyRequest(headers, body, sock=sock)
        cls.last_request = request

        if request not in cls.latest_requests:
            cls.latest_requests.append(request)
        else:
            pos = cls.latest_requests.index(request)
            cls.latest_requests[pos] = request

        logger.info("captured: {}".format(request))
        return request

    @classmethod
    def register_uri(cls, method, uri, body='{"message": "HTTPretty :)"}',
                     adding_headers=None,
                     forcing_headers=None,
                     status=200,
                     responses=None,
                     match_querystring=False,
                     priority=0,
                     **headers):
        """
        .. testcode::

           import httpretty


           def request_callback(request, uri, response_headers):
               content_type = request.headers.get('Content-Type')
               assert request.body == '{"nothing": "here"}', 'unexpected body: {}'.format(request.body)
               assert content_type == 'application/json', 'expected application/json but received Content-Type: {}'.format(content_type)
               return [200, response_headers, json.dumps({"hello": "world"})]

           httpretty.register_uri(
               HTTPretty.POST, "https://httpretty.example.com/api",
               body=request_callback)


           with httpretty.enabled():
               requests.post('https://httpretty.example.com/api', data='{"nothing": "here"}', headers={'Content-Type': 'application/json'})

           assert httpretty.latest_requests[-1].url == 'https://httpbin.org/ip'

        :param method: one of ``httpretty.GET``, ``httpretty.PUT``, ``httpretty.POST``, ``httpretty.DELETE``, ``httpretty.HEAD``, ``httpretty.PATCH``, ``httpretty.OPTIONS``, ``httpretty.CONNECT``
        :param uri: a string or regex pattern (e.g.: **"https://httpbin.org/ip"**)
        :param body: a string, defaults to ``{"message": "HTTPretty :)"}``
        :param adding_headers: dict - headers to be added to the response
        :param forcing_headers: dict - headers to be forcefully set in the response
        :param status: an integer, defaults to **200**
        :param responses: a list of entries, ideally each created with :py:meth:`~httpretty.core.httpretty.Response`
        :param priority: an integer, useful for setting higher priority over previously registered urls. defaults to zero
        :param match_querystring: bool - whether to take the querystring into account when matching an URL
        :param headers: headers to be added to the response

        .. warning:: When using a port in the request, add a trailing slash if no path is provided otherwise Httpretty will not catch the request.  Ex: ``httpretty.register_uri(httpretty.GET, 'http://fakeuri.com:8080/', body='{"hello":"world"}')``
        """
        uri_is_string = isinstance(uri, str)

        if uri_is_string and re.search(r'^\w+://[^/]+[.]\w{2,}$', uri):
            uri += '/'

        if isinstance(responses, list) and len(responses) > 0:
            for response in responses:
                response.uri = uri
                response.method = method
            entries_for_this_uri = responses
        else:
            headers['body'] = body
            headers['adding_headers'] = adding_headers
            headers['forcing_headers'] = forcing_headers
            headers['status'] = status

            entries_for_this_uri = [
                cls.Response(method=method, uri=uri, **headers),
            ]

        matcher = URIMatcher(uri, entries_for_this_uri,
                             match_querystring, priority)
        if matcher in cls._entries:
            matcher.entries.extend(cls._entries[matcher])
            del cls._entries[matcher]

        cls._entries[matcher] = entries_for_this_uri

    def __str__(self):
        return '<HTTPretty with %d URI entries>' % len(self._entries)

    @classmethod
    def Response(
            cls, body,
            method=None,
            uri=None,
            adding_headers=None,
            forcing_headers=None,
            status=200,
            streaming=False,
            **kw):
        """Shortcut to create an :py:class:`~httpretty.core.Entry` that takes
        the body as first positional argument.

        .. seealso:: the parameters of this function match those of
                     the :py:class:`~httpretty.core.Entry` constructor.

        Args:
            body (str): The body to return as response..
            method (str): One of ``httpretty.GET``, ``httpretty.PUT``, ``httpretty.POST``, ``httpretty.DELETE``, ``httpretty.HEAD``, ``httpretty.PATCH``, ``httpretty.OPTIONS``, ``httpretty.CONNECT``.
            uri (str|re.Pattern): The URL to match
            adding_headers (dict): Extra headers to be added to the response
            forcing_headers (dict): Overwrite **any** response headers, even "Content-Length".
            status (int): The status code for the response, defaults to ``200``.
            streaming (bool): Whether should stream the response into chunks via generator.
            kwargs: Keyword-arguments are forwarded to :py:class:`~httpretty.core.Entry`

        Returns:
            httpretty.Entry: containing the request-matching metadata.
        """
        kw['body'] = body
        kw['adding_headers'] = adding_headers
        kw['forcing_headers'] = forcing_headers
        kw['status'] = int(status)
        kw['streaming'] = streaming
        return Entry(method, uri, **kw)

    @classmethod
    def disable(cls):
        """Disables HTTPretty entirely, putting the original :py:mod:`socket`
        module back in its place.


        .. code::

           import re, json
           import httpretty

           httpretty.enable()
           # request passes through fake socket
           response = requests.get('https://httpbin.org')

           httpretty.disable()
           # request uses real python socket module
           response = requests.get('https://httpbin.org')

        .. note:: This method does not call :py:meth:`httpretty.core.reset` automatically.
        """
        undo_patch_socket()
        cls._is_enabled = False


    @classmethod
    def is_enabled(cls):
        """Check if HTTPretty is enabled

        :returns: bool

        .. testcode::

           import httpretty

           httpretty.enable()
           assert httpretty.is_enabled() == True

           httpretty.disable()
           assert httpretty.is_enabled() == False
        """
        return cls._is_enabled

    @classmethod
    def enable(cls, allow_net_connect=True, verbose=False):
        """Enables HTTPretty.

        :param allow_net_connect: boolean to determine if unmatched requests are forwarded to a real network connection OR throw :py:class:`httpretty.errors.UnmockedError`.
        :param verbose: boolean to set HTTPretty's logging level to DEBUG

        .. testcode::

           import re, json
           import httpretty

           httpretty.enable(allow_net_connect=True, verbose=True)

           httpretty.register_uri(
               httpretty.GET,
               re.compile(r'http://.*'),
               body=json.dumps({'man': 'in', 'the': 'middle'})
           )

           response = requests.get('https://foo.bar/foo/bar')

           response.json().should.equal({
               "man": "in",
               "the": "middle",
           })

        .. warning:: after calling this method the original :py:mod:`socket` is replaced with :py:class:`httpretty.core.fakesock`. Make sure to call :py:meth:`~httpretty.disable` after done with your tests or use the :py:class:`httpretty.enabled` as decorator or `context-manager <https://docs.python.org/3/reference/datamodel.html#context-managers>`_
        """
        httpretty.allow_net_connect = allow_net_connect
        apply_patch_socket()
        cls._is_enabled = True
        if verbose:
            logger.setLevel(logging.DEBUG)
        else:
            logger.setLevel(logging.getLogger().level)


def apply_patch_socket():
    # Some versions of python internally shadowed the
    # SocketType variable incorrectly https://bugs.python.org/issue20386
    bad_socket_shadow = (socket.socket != socket.SocketType)

    new_wrap = None
    socket.socket = fakesock.socket
    socket.socketpair = fake_socketpair
    socket._socketobject = fakesock.socket
    if not bad_socket_shadow:
        socket.SocketType = fakesock.socket

    socket.create_connection = create_fake_connection
    socket.gethostname = fake_gethostname
    socket.gethostbyname = fake_gethostbyname
    socket.getaddrinfo = fake_getaddrinfo

    socket.__dict__['socket'] = fakesock.socket
    socket.__dict__['socketpair'] = fake_socketpair
    socket.__dict__['_socketobject'] = fakesock.socket
    if not bad_socket_shadow:
        socket.__dict__['SocketType'] = fakesock.socket

    socket.__dict__['create_connection'] = create_fake_connection
    socket.__dict__['gethostname'] = fake_gethostname
    socket.__dict__['gethostbyname'] = fake_gethostbyname
    socket.__dict__['getaddrinfo'] = fake_getaddrinfo


    # Take out the pyopenssl version - use the default implementation
    for extract_from_urllib3 in pyopenssl_overrides_extract:
        extract_into_urllib3()

    if requests_urllib3_connection is not None:
        urllib3_wrap = partial(fake_wrap_socket, old_requests_ssl_wrap_socket)
        requests_urllib3_connection.ssl_wrap_socket = urllib3_wrap
        requests_urllib3_connection.__dict__['ssl_wrap_socket'] = urllib3_wrap

    if eventlet:
        eventlet.green.ssl.GreenSSLContext = old_sslcontext_class
        eventlet.green.ssl.__dict__['GreenSSLContext'] = old_sslcontext_class
        eventlet.green.ssl.SSLContext = old_sslcontext_class
        eventlet.green.ssl.__dict__['SSLContext'] = old_sslcontext_class

    if socks:
        socks.socksocket = fakesock.socket
        socks.__dict__['socksocket'] = fakesock.socket

    if ssl:
        new_wrap = partial(fake_wrap_socket, old_ssl_wrap_socket)
        ssl.wrap_socket = new_wrap
        ssl.SSLSocket = FakeSSLSocket
        ssl.SSLContext = old_sslcontext_class
        try:
            ssl.SSLContext.wrap_socket = partial(fake_wrap_socket, old_ssl_wrap_socket)
        except AttributeError:
            pass

        ssl.__dict__['wrap_socket'] = new_wrap
        ssl.__dict__['SSLSocket'] = FakeSSLSocket
        ssl.__dict__['SSLContext'] = old_sslcontext_class


def undo_patch_socket():
    socket.socket = old_socket
    socket.socketpair = old_socketpair
    socket.SocketType = old_SocketType
    socket._socketobject = old_socket

    socket.create_connection = old_create_connection
    socket.gethostname = old_gethostname
    socket.gethostbyname = old_gethostbyname
    socket.getaddrinfo = old_getaddrinfo

    socket.__dict__['socket'] = old_socket
    socket.__dict__['socketpair'] = old_socketpair
    socket.__dict__['_socketobject'] = old_socket
    socket.__dict__['SocketType'] = old_SocketType

    socket.__dict__['create_connection'] = old_create_connection
    socket.__dict__['gethostname'] = old_gethostname
    socket.__dict__['gethostbyname'] = old_gethostbyname
    socket.__dict__['getaddrinfo'] = old_getaddrinfo

    if socks:
        socks.socksocket = old_socksocket
        socks.__dict__['socksocket'] = old_socksocket

    if ssl:
        ssl.wrap_socket = old_ssl_wrap_socket
        ssl.SSLSocket = old_sslsocket
        try:
            ssl.SSLContext.wrap_socket = old_sslcontext_wrap_socket
        except AttributeError:
            pass
        ssl.__dict__['wrap_socket'] = old_ssl_wrap_socket
        ssl.__dict__['SSLSocket'] = old_sslsocket

    if requests_urllib3_connection is not None:
        requests_urllib3_connection.ssl_wrap_socket = \
            old_requests_ssl_wrap_socket
        requests_urllib3_connection.__dict__['ssl_wrap_socket'] = \
            old_requests_ssl_wrap_socket


    # Put the pyopenssl version back in place
    for inject_from_urllib3 in pyopenssl_overrides_inject:
        inject_into_urllib3()


@contextlib.contextmanager
def restored_libs():
    undo_patch_socket()
    yield
    apply_patch_socket()


class httprettized(object):
    """`context-manager <https://docs.python.org/3/reference/datamodel.html#context-managers>`_ for enabling HTTPretty.

    .. tip:: Also available under the alias :py:func:`httpretty.enabled`

    .. testcode::

       import json
       import httpretty

       httpretty.register_uri(httpretty.GET, 'https://httpbin.org/ip', body=json.dumps({'origin': '42.42.42.42'}))
       with httpretty.enabled():
           response = requests.get('https://httpbin.org/ip')

       assert httpretty.latest_requests[-1].url == 'https://httpbin.org/ip'
       assert response.json() == {'origin': '42.42.42.42'}
    """
    def __init__(self, allow_net_connect=True, verbose=False):
        self.allow_net_connect = allow_net_connect
        self.verbose = verbose

    def __enter__(self):
        httpretty.reset()
        httpretty.enable(allow_net_connect=self.allow_net_connect, verbose=self.verbose)

    def __exit__(self, exc_type, exc_value, db):
        httpretty.disable()
        httpretty.reset()


def httprettified(test=None, allow_net_connect=True, verbose=False):
    """decorator for test functions

    .. tip:: Also available under the alias :py:func:`httpretty.activate`

    :param test: a callable


    example usage with `nosetests <https://nose.readthedocs.io/en/latest/>`_

    .. testcode::

       import sure
       from httpretty import httprettified

       @httprettified
       def test_using_nosetests():
           httpretty.register_uri(
               httpretty.GET,
               'https://httpbin.org/ip'
           )

           response = requests.get('https://httpbin.org/ip')

           response.json().should.equal({
               "message": "HTTPretty :)"
           })

    example usage with `unittest module <https://docs.python.org/3/library/unittest.html>`_

    .. testcode::

       import unittest
       from sure import expect
       from httpretty import httprettified

       @httprettified
       class TestWithPyUnit(unittest.TestCase):
           def test_httpbin(self):
               httpretty.register_uri(httpretty.GET, 'https://httpbin.org/ip')
               response = requests.get('https://httpbin.org/ip')
               expect(response.json()).to.equal({
                   "message": "HTTPretty :)"
               })

    """
    def decorate_unittest_TestCase_setUp(klass):

        # Prefer addCleanup (added in python 2.7), but fall back
        # to using tearDown if it isn't available
        use_addCleanup = hasattr(klass, 'addCleanup')

        original_setUp = (klass.setUp
                          if hasattr(klass, 'setUp')
                          else None)

        def new_setUp(self):
            httpretty.reset()
            httpretty.enable(allow_net_connect, verbose=verbose)
            if use_addCleanup:
                self.addCleanup(httpretty.disable)
            if original_setUp:
                original_setUp(self)
        klass.setUp = new_setUp

        if not use_addCleanup:
            original_tearDown = (klass.setUp
                                 if hasattr(klass, 'tearDown')
                                 else None)

            def new_tearDown(self):
                httpretty.disable()
                httpretty.reset()
                if original_tearDown:
                    original_tearDown(self)
            klass.tearDown = new_tearDown

        return klass

    def decorate_test_methods(klass):
        for attr in dir(klass):
            if not attr.startswith('test_'):
                continue

            attr_value = getattr(klass, attr)
            if not hasattr(attr_value, "__call__"):
                continue

            setattr(klass, attr, decorate_callable(attr_value))
        return klass

    def is_unittest_TestCase(klass):
        try:
            import unittest
            return issubclass(klass, unittest.TestCase)
        except ImportError:
            return False

    def decorate_class(klass):
        if is_unittest_TestCase(klass):
            return decorate_unittest_TestCase_setUp(klass)
        return decorate_test_methods(klass)

    def decorate_callable(test):
        @functools.wraps(test)
        def wrapper(*args, **kw):
            with httprettized(allow_net_connect):
                return test(*args, **kw)
        return wrapper

    if isinstance(test, type):
        return decorate_class(test)
    elif callable(test):
        return decorate_callable(test)
    return decorate_callable