forked from ask/python-github2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
99 lines (67 loc) · 2.45 KB
/
utils.py
File metadata and controls
99 lines (67 loc) · 2.45 KB
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
import _setup
import os
import sys
import unittest
from email import message_from_file
import httplib2
from github2.client import Github
from github2.request import charset_from_headers
if sys.version_info[0] == 2:
bytes = lambda x, enc: x
HTTP_DATA_DIR = "tests/data/"
ORIG_HTTP_OBJECT = httplib2.Http
class HttpMock(object):
"""Simple Http mock that returns saved entries
Implementation tests should never span network boundaries
"""
def __init__(self, cache=None, timeout=None, proxy_info=None, ca_certs=None):
"""Create a mock httplib.Http object
.. attribute: called_with
``locals()`` during ``__init__``, for testing call spec
"""
self.called_with = locals()
def request(self, uri, method='GET', body=None, headers=None,
redirections=5, connection_type=None):
file = os.path.join(HTTP_DATA_DIR, httplib2.safename(uri))
if os.path.exists(file):
response = message_from_file(open(file))
headers = httplib2.Response(response)
body = bytes(response.get_payload(), charset_from_headers(headers))
return (headers, body)
else:
return (httplib2.Response({"status": "404"}),
"Resource %r unavailable from test data store" % file)
class HttpMockTestCase(unittest.TestCase):
def setUp(self):
"""Prepare test fixtures
`httplib2.Http` is patched to return cached entries via
:class:`HttpMock`.
:attr:`client` is an unauthenticated :obj:`Github` object for easy use
in tests.
"""
httplib2.Http = HttpMock
self.client = Github()
def tearDown(self):
"""Remove test fixtures
`httplib2.Http` is returned to its original state.
"""
httplib2.Http = ORIG_HTTP_OBJECT
class HttpMockAuthenticatedTestCase(HttpMockTestCase):
def setUp(self):
"""Prepare test fixtures
:see: ``HttpMockTestCase``
:attr:`client` is an authenticated :obj:`Github` object for easy use
in tests.
"""
httplib2.Http = HttpMock
self.client = Github(access_token='xxx')
def set_http_mock():
"""Function to enable ``Http`` mock
This is useful in simple `nose`-compliant test functions
"""
httplib2.Http = HttpMock
def unset_http_mock():
"""Function to disable ``Http`` mock
:see: :func:`set_http_mock`
"""
httplib2.Http = ORIG_HTTP_OBJECT