-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
67 lines (49 loc) · 2.08 KB
/
tests.py
File metadata and controls
67 lines (49 loc) · 2.08 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
# -*- coding: UTF-8 -*-
import unittest
from unittest.mock import patch
import requests
from apirequests import Client
class TestClient(unittest.TestCase):
def test_init(self):
c = Client('localhost')
self.assertEqual('localhost', c.host)
self.assertFalse(c.silent)
self.assertFalse(c.slash)
def test_url(self):
localhost = 'http://localhost'
c = Client(localhost)
self.assertEqual(localhost + '/path', c._url('path'))
self.assertEqual(localhost + '/path/23/', c._url('/path/23/'))
c = Client(localhost + '/api/')
self.assertEqual(localhost + '/api/path', c._url('path'))
self.assertEqual(localhost + '/api/path/', c._url('path/'))
self.assertEqual(localhost + '/abspath/', c._url('/abspath/'))
c = Client(localhost)
self.assertEqual(localhost + '/', c._url('/'))
self.assertEqual(localhost, c._url(''))
self.assertEqual(localhost, c._url(None))
c = Client(localhost)
c.slash = True
self.assertEqual(localhost + '/', c._url(None))
self.assertEqual(localhost + '/path/', c._url('path'))
self.assertEqual(localhost + '/path/', c._url('/path/'))
@patch('requests.Session.request')
def test_request(self, request_mock):
c = Client('http://localhost')
c.request('GET', 'path', data=42)
request_mock.assert_called_once_with('GET', 'http://localhost/path', data=42)
response = requests.Response()
request_mock.return_value = response
c.silent = True
response.status_code = 200
self.assertTrue(c.request('GET', '/').ok)
response.status_code = 500
self.assertFalse(c.request('GET', '/').ok)
c.silent = False
response.status_code = 404
with self.assertRaisesRegex(requests.exceptions.HTTPError, '404'):
c.request('GET', '/')
response.status_code = 200
self.assertTrue(c.request('GET', '/').ok)
if __name__ == "__main__":
unittest.main()