Skip to content

Commit 6035cc3

Browse files
committed
Http mocking switched to use mock.Mock object.
1 parent e1b7047 commit 6035cc3

2 files changed

Lines changed: 21 additions & 38 deletions

File tree

extra/requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
-r requirements.txt
22
coverage>=3.5
3+
mock>=0.7.1
34
nose>=1.1.2

tests/utils.py

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import httplib2
1313

14+
from mock import Mock
15+
1416
from github2.client import Github
1517
from github2.request import charset_from_headers
1618

@@ -19,32 +21,26 @@
1921
bytes = lambda x, enc: x
2022

2123

22-
HTTP_DATA_DIR = "tests/data/"
23-
24-
ORIG_HTTP_OBJECT = httplib2.Http
24+
ORIG_REQUEST_METHOD = httplib2.Http.request
2525

2626

27-
class HttpMock(object):
28-
"""Simple Http mock that returns saved entries.
27+
def request_mock(uri, method='GET', body=None, headers=None,
28+
redirections=5, connection_type=None):
29+
"""Http mock side effect that returns saved entries.
2930
3031
Implementation tests should never span network boundaries.
3132
3233
"""
3334

34-
def __init__(self, *args, **kwargs):
35-
pass
36-
37-
def request(self, uri, method='GET', body=None, headers=None,
38-
redirections=5, connection_type=None):
39-
file = os.path.join(HTTP_DATA_DIR, httplib2.safename(uri))
40-
if os.path.exists(file):
41-
response = message_from_file(open(file))
42-
headers = httplib2.Response(response)
43-
body = bytes(response.get_payload(), charset_from_headers(headers))
44-
return (headers, body)
45-
else:
46-
return (httplib2.Response({"status": "404"}),
47-
"Resource %r unavailable from test data store" % file)
35+
file = os.path.join("tests/data", httplib2.safename(uri))
36+
if os.path.exists(file):
37+
response = message_from_file(open(file))
38+
headers = httplib2.Response(response)
39+
body = bytes(response.get_payload(), charset_from_headers(headers))
40+
return (headers, body)
41+
else:
42+
return (httplib2.Response({"status": "404"}),
43+
"Resource %r unavailable from test data store" % file)
4844

4945

5046
class HttpMockTestCase(unittest.TestCase):
@@ -58,7 +54,8 @@ def setUp(self):
5854
in tests.
5955
6056
"""
61-
httplib2.Http = HttpMock
57+
httplib2.Http.request = Mock(spec_set=httplib2.Http.request,
58+
side_effect=request_mock)
6259
self.client = Github()
6360

6461
def tearDown(self):
@@ -67,7 +64,7 @@ def tearDown(self):
6764
`httplib2.Http` is returned to its original state.
6865
6966
"""
70-
httplib2.Http = ORIG_HTTP_OBJECT
67+
httplib2.Http.request = ORIG_REQUEST_METHOD
7168

7269

7370
class HttpMockAuthenticatedTestCase(HttpMockTestCase):
@@ -80,21 +77,6 @@ def setUp(self):
8077
in tests.
8178
8279
"""
83-
httplib2.Http = HttpMock
80+
httplib2.Http.request = Mock(spec_set=httplib2.Http.request,
81+
side_effect=request_mock)
8482
self.client = Github(access_token='xxx')
85-
86-
87-
def set_http_mock():
88-
"""Function to enable ``Http`` mock
89-
90-
This is useful in simple `nose`-compliant test functions
91-
"""
92-
httplib2.Http = HttpMock
93-
94-
95-
def unset_http_mock():
96-
"""Function to disable ``Http`` mock
97-
98-
:see: :func:`set_http_mock`
99-
"""
100-
httplib2.Http = ORIG_HTTP_OBJECT

0 commit comments

Comments
 (0)