-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_github.py
More file actions
88 lines (63 loc) · 3.07 KB
/
Copy pathtest_github.py
File metadata and controls
88 lines (63 loc) · 3.07 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
from requests import HTTPError
from watchlist import github
from watchlist.tests.base import GithubMockTestCase
from watchlist.tests.base import RequestsResponseStub
from watchlist.tests.base import StubConfig
import json
def generate_links(path, **pages):
header = []
template = '<https://api.github.com/%s?page=%i>; rel="%s"'
for rel, page in pages.items():
header.append(template % (path, page, rel))
return ', '.join(header)
class TestGithubRequests(GithubMockTestCase):
def test_get_with_pagination(self):
page_one = ['foo']
page_two = ['bar']
page_three = ['baz']
self.mock_github_request(
'foo', json.dumps(page_one), response_headers={
'link': generate_links('foo', next=2, last=3)})
self.mock_github_request(
'foo?page=2', json.dumps(page_two), response_headers={
'link': generate_links('foo', first=1, prev=1, next=3, last=3)})
self.mock_github_request(
'foo?page=3', json.dumps(page_three), response_headers={
'link': generate_links('foo', first=1, prev=2)})
self.mocker.replay()
self.assertEquals(['foo', 'bar', 'baz'], github.get('foo', StubConfig()))
def test_get_raises_httperrors(self):
self.mock_github_request('foo', '', response_status_code=401)
self.mocker.replay()
with self.assertRaises(HTTPError) as cm:
github.get('foo', StubConfig())
self.assertEquals('401 Client Error: Unauthorized', str(cm.exception))
def test_put_request(self):
self.mock_github_request('foo', '["foo"]', method='put', payload='fuhuu')
self.mocker.replay()
github.put('foo', StubConfig(), 'fuhuu')
def test_put_raises_httperrors(self):
self.mock_github_request('foo', '["foo"]', method='put', payload='fuhuu',
response_status_code=401)
self.mocker.replay()
with self.assertRaises(HTTPError) as cm:
github.put('foo', StubConfig(), 'fuhuu')
self.assertEquals('401 Client Error: Unauthorized', str(cm.exception))
def test_delete_request(self):
self.mock_github_request('foo', '', method='delete')
self.mocker.replay()
github.delete('foo', StubConfig())
def test_delete_raises_httperrors(self):
self.mock_github_request('foo', '', method='delete', response_status_code=401)
self.mocker.replay()
with self.assertRaises(HTTPError) as cm:
github.delete('foo', StubConfig())
self.assertEquals('401 Client Error: Unauthorized', str(cm.exception))
def test_extracting_link_headers(self):
self.mocker.replay()
response = RequestsResponseStub(headers={
'link': '<https://api.github.com/foo?page=2>; rel="next"'
', <https://api.github.com/foo?page=6>; rel="last"'})
self.assertEquals({'next': 'https://api.github.com/foo?page=2',
'last': 'https://api.github.com/foo?page=6'},
github._extract_link_header(response))