-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
61 lines (47 loc) · 2.09 KB
/
tests.py
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
import unittest
import requests
from patch_requests import patch_requests, PatchingError
class TestPatcher(unittest.TestCase):
def test_multiple_requests(self):
with patch_requests([
('get', (200, {1: 1})),
('post', (201, {2: 2})),
('GET', (404, '<html><p><br/>')),
('patch', (500, b'\\')),
]) as p:
response = requests.get('http://example.com')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {1: 1})
s = requests.Session()
response = s.post('http://www.example.com')
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), {2: 2})
response = s.get('http://')
self.assertEqual(response.status_code, 404)
self.assertEqual(response.text, '<html><p><br/>')
s.close()
response = requests.patch('')
self.assertEqual(response.status_code, 500)
self.assertEqual(response.content, b'\\')
self.assertEqual(
p.mocks['get'].call_args_list[0][0], ('http://example.com',))
self.assertEqual(
p.mocks['post'].call_args_list[0][0], ('http://www.example.com',))
def test_request_amount_more_than_expected(self):
with self.assertRaises(PatchingError) as cm:
with patch_requests([('get', (200, {1: 1}))]):
requests.get('example.com')
requests.get('example.com')
self.assertEqual(
str(cm.exception),
"Unexpeced amount of requests (latest was GET with args: "
"('example.com',) and kwargs: {})")
def test_unexpeced_method(self):
with self.assertRaises(PatchingError) as cm:
with patch_requests([
('get', (200, {1: 1})),
('post', (200, {1: 1})),
]):
requests.get('example.com')
requests.get('example.com')
self.assertEqual(str(cm.exception), 'Expected method POST, got GET')