-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_test_request.py
95 lines (56 loc) · 2.42 KB
/
_test_request.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
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
import unittest
from unittest.mock import Mock, patch
from request import Request
RESPONSE = {
"status": "success",
"message": "Processed"
}
class Response:
def __init__(self):
self.text = RESPONSE['message']
def json(self):
return RESPONSE
mock_response = Response()
class TestRequest(unittest.TestCase):
def test_init(self):
request = Request()
self.assertNotEqual(request.headers, None, f'Should not be None')
self.assertNotEqual(request.timeout, None, f'Should not be None')
@patch('request.requests.head')
def test_head(self, mock_head):
URL = 'https://example.com/working'
mock_head.return_value.status_code = 200
request = Request()
self.assertEqual(request.head(URL).status_code, 200, f'Should return 200 status code')
@patch('request.requests.head')
def test_status_code_400(self, mock_head):
URL = 'https://example.com/not_found'
mock_head.return_value.status_code = 400
request = Request()
self.assertEqual(request.get_status_code(URL), 400, f'Should return 400 status code')
@patch('request.requests.get')
def test_get(self, mock_get):
URL = 'https://example.com/working'
mock_get.return_value = mock_response
request = Request()
response = request.get(URL)
response = response.json()
self.assertEqual(response['status'], RESPONSE['status'], f'Should return success status')
self.assertEqual(response['message'], RESPONSE['message'], f'Should return Processed message')
@patch('request.requests.get')
def test_get_text_response(self, mock_post):
URL = 'https://example.com/working'
mock_post.return_value = mock_response
request = Request()
response = request.get_text_response(URL)
self.assertEqual(response, RESPONSE['message'], f'Should {RESPONSE["message"]} message')
@patch('request.requests.get')
def test_get_json_response(self, mock_post):
URL = 'https://example.com/working'
mock_post.return_value = mock_response
request = Request()
response = request.get_json_response(URL)
self.assertEqual(response['status'], RESPONSE['status'], f'Should return success status')
self.assertEqual(response['message'], RESPONSE['message'], f'Should return Processed message')
if __name__ == '__main__':
unittest.main()