1
+ # Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
1
14
import json
15
+ from collections .abc import AsyncIterator
2
16
3
17
import pytest
4
18
5
- from smithy_python ._private .http import Response
6
- from smithy_python .interfaces . http import HeadersList
19
+ from smithy_python ._private .http import HTTPResponse , tuples_list_to_fields
20
+ from smithy_python .async_utils import async_list
7
21
from smithy_python .protocolutils import RestJsonErrorInfo , parse_rest_json_error_info
8
22
from smithy_python .types import Document
9
23
10
24
11
- class _AsyncReader :
12
- def __init__ (self , body : str ):
13
- self ._body : bytes = body .encode ("utf-8" )
14
-
15
- async def read (self , size : int = - 1 ) -> bytes :
16
- result : bytes = self ._body
17
- if size <= 0 :
18
- self ._body = b""
19
- else :
20
- result = self ._body [:size ]
21
- self ._body = self ._body [size :]
22
- return result
23
-
24
-
25
25
@pytest .mark .parametrize (
26
26
"headers, body, expected" ,
27
27
[
@@ -82,17 +82,19 @@ async def read(self, size: int = -1) -> bytes:
82
82
],
83
83
)
84
84
async def test_parse_rest_json_error_info (
85
- headers : HeadersList , body : Document , expected : RestJsonErrorInfo
85
+ headers : list [ tuple [ str , str ]] , body : Document , expected : RestJsonErrorInfo
86
86
) -> None :
87
- response = Response (
88
- status_code = 400 , headers = headers , body = _AsyncReader (json .dumps (body ))
87
+ response = HTTPResponse (
88
+ status = 400 ,
89
+ fields = tuples_list_to_fields (headers ),
90
+ body = async_list ([json .dumps (body ).encode ()]),
89
91
)
90
92
actual = await parse_rest_json_error_info (response )
91
93
assert actual == expected
92
94
93
95
94
96
class _ExceptionThrowingBody :
95
- async def read (self , size : int = - 1 ) -> bytes :
97
+ def __aiter__ (self ) -> AsyncIterator [ bytes ] :
96
98
raise Exception ("Body unexpectedly accessed" )
97
99
98
100
@@ -117,8 +119,10 @@ async def read(self, size: int = -1) -> bytes:
117
119
],
118
120
)
119
121
async def test_parse_rest_json_error_info_without_body (
120
- headers : HeadersList , expected : RestJsonErrorInfo
122
+ headers : list [ tuple [ str , str ]] , expected : RestJsonErrorInfo
121
123
) -> None :
122
- response = Response (status_code = 400 , headers = headers , body = _ExceptionThrowingBody ())
124
+ response = HTTPResponse (
125
+ status = 400 , fields = tuples_list_to_fields (headers ), body = _ExceptionThrowingBody ()
126
+ )
123
127
actual = await parse_rest_json_error_info (response , check_body = False )
124
128
assert actual == expected
0 commit comments