Repository: h2non/pook

Issue:
Issues using with urllib
When using with urllib an exception is throw as response doesn't have a code attribute. Uncovered this while trying to mock a prometheus push gateway endpoint with prometheus client library. Simplified code snippet below:

Exception raised:
```
self = <urllib.request.HTTPErrorProcessor object at 0x7f79100e3c18>
request = <urllib.request.Request object at 0x7f79100e3748>
response = <http.client.HTTPResponse object at 0x7f7910608048>

    def http_response(self, request, response):
>       code, msg, hdrs = response.code, response.msg, response.info()
E       AttributeError: 'HTTPResponse' object has no attribute 'code'
```

Test code snippet:
```
    @pook.on
    def test_pook(self):
        mock = pook.put('http://prometheus_test', reply=201, response_json={'test': 'test'})

        # resp = requests.put('http://prometheus_test')
        do_request(
            url='http://prometheus_test', method='PUT', timeout=30, data=b'',
        )()

        # assert resp.status_code == 200
        assert mock.calls == 1

def do_request(url, method, timeout, data):
        request = Request(url, data=data)
        request.get_method = lambda: method
        resp = build_opener(HTTPHandler).open(request, timeout=timeout)
        if resp.code >= 400:
            raise IOError("error talking to pushgateway: {0} {1}".format(
                resp.code, resp.msg))
```

Discussion:

Patch:
diff --git a/pook/api.py b/pook/api.py
index a05c9b4..b03fbe4 100644
--- a/pook/api.py
+++ b/pook/api.py
@@ -205,7 +205,7 @@ def use(network=False):
     _engine = Engine(network=network)
     _engine.activate()
 
-    # Yield enfine to be used by the context manager
+    # Yield engine to be used by the context manager
     yield _engine
 
     # Restore engine state
@@ -492,7 +492,7 @@ def isactive():
     Otherwise ``False``.
 
     Returns:
-        bool: True is all the registered mocks are gone, otherwise False.
+        bool: True if pook is active, otherwise False.
     """
     return _engine.isactive()
 
@@ -502,7 +502,7 @@ def isdone():
     Returns True if all the registered mocks has been triggered.
 
     Returns:
-        bool: True is all the registered mocks are gone, otherwise False.
+        bool: True if all the registered mocks are gone, otherwise False.
     """
     return _engine.isdone()
 
diff --git a/pook/interceptors/http.py b/pook/interceptors/http.py
index 56bce11..1d973fc 100644
--- a/pook/interceptors/http.py
+++ b/pook/interceptors/http.py
@@ -4,7 +4,11 @@ from .base import BaseInterceptor
 
 from unittest import mock
 
-from http.client import responses as http_reasons, _CS_REQ_SENT
+from http.client import (
+    responses as http_reasons,
+    _CS_REQ_SENT,
+    HTTPSConnection,
+)
 
 PATCHES = (
     'http.client.HTTPConnection.request',
@@ -48,8 +52,13 @@ class HTTPClientInterceptor(BaseInterceptor):
         req.headers = headers or {}
         req.body = body
 
+        if isinstance(conn, HTTPSConnection):
+            schema = "https"
+        else:
+            schema = "http"
+
         # Compose URL
-        req.url = 'http://{}:{}{}'.format(conn.host, conn.port, url)
+        req.url = '{}://{}:{}{}'.format(schema, conn.host, conn.port, url)
 
         # Match the request against the registered mocks in pook
         mock = self.engine.match(req)
@@ -72,6 +81,8 @@ class HTTPClientInterceptor(BaseInterceptor):
         mockres = HTTPResponse(SocketMock(), method=method, url=url)
         mockres.version = (1, 1)
         mockres.status = res._status
+        # urllib requires `code` to be set, rather than `status`
+        mockres.code = res._status
         mockres.reason = http_reasons.get(res._status)
         mockres.headers = res._headers.to_dict()
 


Test patch:
diff --git a/tests/unit/interceptors/urllib_test.py b/tests/unit/interceptors/urllib_test.py
new file mode 100644
index 0000000..bd04955
--- /dev/null
+++ b/tests/unit/interceptors/urllib_test.py
@@ -0,0 +1,16 @@
+import pook
+from urllib.request import urlopen
+
+
+def test_urllib_ssl(pook_on):
+    pook.get("https://example.com").reply(200).body("Hello from pook")
+    res = urlopen("https://example.com")
+
+    assert res.read() == "Hello from pook"
+
+
+def test_urllib_clear(pook_on):
+    pook.get("http://example.com").reply(200).body("Hello from pook")
+    res = urlopen("http://example.com")
+
+    assert res.read() == "Hello from pook"