forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.py
More file actions
104 lines (86 loc) · 3.18 KB
/
errors_test.py
File metadata and controls
104 lines (86 loc) · 3.18 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
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
from types import SimpleNamespace
from unittest import TestCase
from linode_api4.errors import ApiError, UnexpectedResponseError
class ApiErrorTest(TestCase):
def test_from_response(self):
mock_response = SimpleNamespace(
status_code=400,
json=lambda: {
"errors": [
{"reason": "foo"},
{"field": "bar", "reason": "oh no"},
]
},
text='{"errors": [{"reason": "foo"}, {"field": "bar", "reason": "oh no"}]}',
request=SimpleNamespace(
method="POST",
path_url="foo/bar",
),
)
exc = ApiError.from_response(mock_response)
assert str(exc) == "POST foo/bar: [400] foo; bar: oh no"
assert exc.status == 400
assert exc.json == {
"errors": [{"reason": "foo"}, {"field": "bar", "reason": "oh no"}]
}
assert exc.response.request.method == "POST"
assert exc.response.request.path_url == "foo/bar"
def test_from_response_non_json_body(self):
mock_response = SimpleNamespace(
status_code=500,
json=lambda: None,
text="foobar",
request=SimpleNamespace(
method="POST",
path_url="foo/bar",
),
)
exc = ApiError.from_response(mock_response)
assert str(exc) == "POST foo/bar: [500] foobar"
assert exc.status == 500
assert exc.json is None
assert exc.response.request.method == "POST"
assert exc.response.request.path_url == "foo/bar"
def test_from_response_empty_body(self):
mock_response = SimpleNamespace(
status_code=500,
json=lambda: None,
text=None,
request=SimpleNamespace(
method="POST",
path_url="foo/bar",
),
)
exc = ApiError.from_response(mock_response)
assert str(exc) == "POST foo/bar: [500] N/A"
assert exc.status == 500
assert exc.json is None
assert exc.response.request.method == "POST"
assert exc.response.request.path_url == "foo/bar"
def test_from_response_no_request(self):
mock_response = SimpleNamespace(
status_code=500, json=lambda: None, text="foobar", request=None
)
exc = ApiError.from_response(mock_response)
assert str(exc) == "[500] foobar"
assert exc.status == 500
assert exc.json is None
assert exc.response.request is None
class UnexpectedResponseErrorTest(TestCase):
def test_from_response(self):
mock_response = SimpleNamespace(
status_code=400,
json=lambda: {
"foo": "bar",
},
request=SimpleNamespace(
method="POST",
path_url="foo/bar",
),
)
exc = UnexpectedResponseError.from_response("foobar", mock_response)
assert str(exc) == "foobar"
assert exc.status == 400
assert exc.json == {"foo": "bar"}
assert exc.response.request.method == "POST"
assert exc.response.request.path_url == "foo/bar"