-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest_protocols.py
More file actions
99 lines (90 loc) · 3.2 KB
/
Copy pathtest_protocols.py
File metadata and controls
99 lines (90 loc) · 3.2 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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from unittest.mock import Mock
import pytest
from smithy_aws_core.aio.protocols import AWSErrorIdentifier, AWSJSONDocument
from smithy_core.exceptions import DiscriminatorError
from smithy_core.schemas import APIOperation, Schema
from smithy_core.shapes import ShapeID, ShapeType
from smithy_http import Fields, tuples_to_fields
from smithy_http.aio import HTTPResponse
from smithy_json import JSONSettings
@pytest.mark.parametrize(
"header, expected",
[
("FooError", "com.test#FooError"),
(
"FooError:http://internal.amazon.com/coral/com.amazon.coral.validate/",
"com.test#FooError",
),
(
"com.test#FooError:http://internal.amazon.com/coral/com.amazon.coral.validate",
"com.test#FooError",
),
("", None),
(":", None),
(None, None),
],
)
def test_aws_error_identifier(header: str | None, expected: ShapeID | None) -> None:
fields = Fields()
if header is not None:
fields = tuples_to_fields([("x-amzn-errortype", header)])
http_response = HTTPResponse(status=500, fields=fields)
operation = Mock(spec=APIOperation)
operation.schema = Schema(
id=ShapeID("com.test#TestOperation"), shape_type=ShapeType.OPERATION
)
error_identifier = AWSErrorIdentifier()
actual = error_identifier.identify(operation=operation, response=http_response)
assert actual == expected
@pytest.mark.parametrize(
"document, expected",
[
({"__type": "FooError"}, "com.test#FooError"),
({"__type": "com.test#FooError"}, "com.test#FooError"),
(
{
"__type": "FooError:http://internal.amazon.com/coral/com.amazon.coral.validate/"
},
"com.test#FooError",
),
(
{
"__type": "com.test#FooError:http://internal.amazon.com/coral/com.amazon.coral.validate"
},
"com.test#FooError",
),
({"code": "FooError"}, "com.test#FooError"),
({"code": "com.test#FooError"}, "com.test#FooError"),
(
{
"code": "FooError:http://internal.amazon.com/coral/com.amazon.coral.validate/"
},
"com.test#FooError",
),
(
{
"code": "com.test#FooError:http://internal.amazon.com/coral/com.amazon.coral.validate"
},
"com.test#FooError",
),
({"__type": "FooError", "code": "BarError"}, "com.test#FooError"),
("FooError", None),
({"__type": None}, None),
({"__type": ""}, None),
({"__type": ":"}, None),
],
)
def test_aws_json_document_discriminator(
document: dict[str, str], expected: ShapeID | None
) -> None:
settings = JSONSettings(
document_class=AWSJSONDocument, default_namespace="com.test"
)
if expected is None:
with pytest.raises(DiscriminatorError):
AWSJSONDocument(document, settings=settings).discriminator
else:
discriminator = AWSJSONDocument(document, settings=settings).discriminator
assert discriminator == expected