]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # -*- coding: utf-8 -*- |
2 | import unittest | |
f67539c2 | 3 | |
11fdf7f2 TL |
4 | import requests.exceptions |
5 | ||
9f95a23c TL |
6 | try: |
7 | from mock import patch | |
8 | except ImportError: | |
9 | from unittest.mock import patch | |
10 | ||
11fdf7f2 | 11 | from urllib3.exceptions import MaxRetryError, ProtocolError |
f67539c2 | 12 | |
11fdf7f2 TL |
13 | from .. import mgr |
14 | from ..rest_client import RequestException, RestClient | |
15 | ||
16 | ||
f67539c2 TL |
17 | class RestClientTestClass(RestClient): |
18 | """RestClient subclass for testing purposes.""" | |
19 | @RestClient.api_get('/') | |
20 | def fake_endpoint_method_with_annotation(self, request=None) -> bool: | |
21 | pass | |
22 | ||
23 | ||
11fdf7f2 TL |
24 | class RestClientTest(unittest.TestCase): |
25 | def setUp(self): | |
26 | settings = {'REST_REQUESTS_TIMEOUT': 45} | |
27 | mgr.get_module_option.side_effect = settings.get | |
28 | ||
29 | def test_timeout_auto_set(self): | |
30 | with patch('requests.Session.request') as mock_request: | |
31 | rest_client = RestClient('localhost', 8000) | |
32 | rest_client.session.request('GET', '/test') | |
33 | mock_request.assert_called_with('GET', '/test', timeout=45) | |
34 | ||
35 | def test_timeout_auto_set_arg(self): | |
36 | with patch('requests.Session.request') as mock_request: | |
37 | rest_client = RestClient('localhost', 8000) | |
38 | rest_client.session.request( | |
39 | 'GET', '/test', None, None, None, None, | |
40 | None, None, None) | |
41 | mock_request.assert_called_with( | |
42 | 'GET', '/test', None, None, None, None, | |
43 | None, None, None, timeout=45) | |
44 | ||
45 | def test_timeout_no_auto_set_kwarg(self): | |
46 | with patch('requests.Session.request') as mock_request: | |
47 | rest_client = RestClient('localhost', 8000) | |
48 | rest_client.session.request('GET', '/test', timeout=20) | |
49 | mock_request.assert_called_with('GET', '/test', timeout=20) | |
50 | ||
51 | def test_timeout_no_auto_set_arg(self): | |
52 | with patch('requests.Session.request') as mock_request: | |
53 | rest_client = RestClient('localhost', 8000) | |
54 | rest_client.session.request( | |
55 | 'GET', '/test', None, None, None, None, | |
56 | None, None, 40) | |
57 | mock_request.assert_called_with( | |
58 | 'GET', '/test', None, None, None, None, | |
59 | None, None, 40) | |
60 | ||
61 | ||
62 | class RestClientDoRequestTest(unittest.TestCase): | |
63 | @classmethod | |
64 | def setUpClass(cls): | |
65 | cls.mock_requests = patch('requests.Session').start() | |
f67539c2 TL |
66 | cls.rest_client = RestClientTestClass('localhost', 8000, 'UnitTest') |
67 | ||
68 | def test_endpoint_method_with_annotation(self): | |
69 | self.assertEqual(self.rest_client.fake_endpoint_method_with_annotation(), None) | |
11fdf7f2 TL |
70 | |
71 | def test_do_request_exception_no_args(self): | |
72 | self.mock_requests().get.side_effect = requests.exceptions.ConnectionError() | |
73 | with self.assertRaises(RequestException) as context: | |
74 | self.rest_client.do_request('GET', '/test') | |
75 | self.assertEqual('UnitTest REST API cannot be reached. Please ' | |
76 | 'check your configuration and that the API ' | |
77 | 'endpoint is accessible', | |
78 | context.exception.message) | |
79 | ||
80 | def test_do_request_exception_args_1(self): | |
81 | self.mock_requests().post.side_effect = requests.exceptions.ConnectionError( | |
82 | MaxRetryError('Abc', 'http://xxx.yyy', 'too many redirects')) | |
83 | with self.assertRaises(RequestException) as context: | |
84 | self.rest_client.do_request('POST', '/test') | |
85 | self.assertEqual('UnitTest REST API cannot be reached. Please ' | |
86 | 'check your configuration and that the API ' | |
87 | 'endpoint is accessible', | |
88 | context.exception.message) | |
89 | ||
90 | def test_do_request_exception_args_2(self): | |
91 | self.mock_requests().put.side_effect = requests.exceptions.ConnectionError( | |
92 | ProtocolError('Connection broken: xyz')) | |
93 | with self.assertRaises(RequestException) as context: | |
94 | self.rest_client.do_request('PUT', '/test') | |
95 | self.assertEqual('UnitTest REST API cannot be reached. Please ' | |
96 | 'check your configuration and that the API ' | |
97 | 'endpoint is accessible', | |
98 | context.exception.message) | |
99 | ||
100 | def test_do_request_exception_nested_args(self): | |
101 | self.mock_requests().delete.side_effect = requests.exceptions.ConnectionError( | |
102 | MaxRetryError('Xyz', 'https://foo.bar', | |
103 | Exception('Foo: [Errno -42] bla bla bla'))) | |
104 | with self.assertRaises(RequestException) as context: | |
105 | self.rest_client.do_request('DELETE', '/test') | |
106 | self.assertEqual('UnitTest REST API cannot be reached: bla ' | |
107 | 'bla bla [errno -42]. Please check your ' | |
108 | 'configuration and that the API endpoint ' | |
109 | 'is accessible', | |
110 | context.exception.message) |