-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_http_wrapper.py
171 lines (138 loc) · 5.74 KB
/
test_http_wrapper.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import unittest
from unittest.mock import patch
import requests
from http_wrapper import endpoint_call
class TestEndpointCall(unittest.TestCase):
@patch("http_wrapper.requests.get")
def test_get_success(self, mock_get):
# Mock the response for a successful GET request
mock_response = mock_get.return_value
mock_response.json.return_value = {"key": "value"}
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
result = endpoint_call(
url, "get", headers=headers, body=None, json_data=None
)
self.assertEqual(result.json(), {"key": "value"})
mock_get.assert_called_once_with(url, headers=headers)
@patch("http_wrapper.requests.post")
def test_post_success(self, mock_post):
# Mock the response for a successful POST request
mock_response = mock_post.return_value
mock_response.json.return_value = {"status": "success"}
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
body = {"data": "value"}
result = endpoint_call(
url, "post", headers=headers, body=body, json_data=None
)
self.assertEqual(result.json(), {"status": "success"})
mock_post.assert_called_once_with(
url, headers=headers, data=body, json=None
)
@patch("http_wrapper.requests.delete")
def test_delete_success(self, mock_post):
# Mock the response for a successful DELETE request
mock_response = mock_post.return_value
mock_response.json.return_value = {"status": "success"}
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
result = endpoint_call(
url, "delete", headers=headers, body=None, json_data=None
)
self.assertEqual(result.json(), {"status": "success"})
mock_post.assert_called_once_with(url, headers=headers)
@patch("http_wrapper.requests.get")
def test_unsupported_request_type(self, mock_get):
# Simulate a unsupported request type
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
with self.assertRaises(Exception) as context:
endpoint_call(
url, "posr", headers=headers, body=None, json_data=None
)
self.assertEqual(
str(context.exception),
"Unexpected Error: request type posr not supported",
)
mock_get.assert_not_called()
@patch("http_wrapper.requests.get")
def test_get_http_code_error(self, mock_get):
# Simulate a connection error
mock_get.side_effect = requests.exceptions.HTTPError("405")
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
with self.assertRaises(Exception) as context:
endpoint_call(
url, "get", headers=headers, body=None, json_data=None
)
self.assertEqual(
str(context.exception),
"Http Error: 405",
)
mock_get.assert_called_once_with(url, headers=headers)
@patch("http_wrapper.requests.get")
def test_get_connection_error(self, mock_get):
# Simulate a connection error
mock_get.side_effect = requests.exceptions.ConnectionError(
"Failed to connect"
)
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
with self.assertRaises(Exception) as context:
endpoint_call(
url, "get", headers=headers, body=None, json_data=None
)
self.assertEqual(
str(context.exception),
"Error Connecting: Failed to connect",
)
mock_get.assert_called_once_with(url, headers=headers)
@patch("http_wrapper.requests.get")
def test_get_timeout_error(self, mock_get):
# Simulate a timeout error
mock_get.side_effect = requests.exceptions.Timeout(
"Connection timed out"
)
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
with self.assertRaises(Exception) as context:
endpoint_call(
url, "get", headers=headers, body=None, json_data=None
)
self.assertEqual(
str(context.exception),
"Timeout Error: Connection timed out",
)
mock_get.assert_called_once_with(url, headers=headers)
@patch("http_wrapper.requests.get")
def test_other_request_exception(self, mock_get):
# Simulate an else HTTP exception
mock_get.side_effect = requests.exceptions.RequestException(
"Testing other request exception!"
)
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
with self.assertRaises(Exception) as context:
endpoint_call(
url, "get", headers=headers, body=None, json_data=None
)
self.assertEqual(
str(context.exception),
"Oops: Something Else: Testing other request exception!",
)
mock_get.assert_called_once_with(url, headers=headers)
@patch("http_wrapper.requests.get")
def test_unexpected_exception(self, mock_get):
# Simulate a not known error
mock_get.side_effect = Exception("Not request error")
url = "http://example.com/api"
headers = {"Authorization": "Bearer token"}
with self.assertRaises(Exception) as context:
endpoint_call(
url, "get", headers=headers, body=None, json_data=None
)
self.assertEqual(
str(context.exception), "Unexpected Error: Not request error"
)
mock_get.assert_called_once_with(url, headers=headers)