|
23 | 23 | from smithy_core.traits import Trait |
24 | 24 | from smithy_core.types import TypedProperties |
25 | 25 | from smithy_http import Fields, tuples_to_fields |
26 | | -from smithy_http.aio import HTTPResponse |
| 26 | +from smithy_http.aio import HTTPRequest, HTTPResponse |
27 | 27 | from smithy_json import JSONSettings |
28 | 28 |
|
29 | 29 |
|
@@ -199,60 +199,80 @@ async def test_aws_query_serializes_base_request_shape() -> None: |
199 | 199 | assert body == b"Action=TestOperation&Version=2020-01-08&name=example" |
200 | 200 |
|
201 | 201 |
|
202 | | -def test_aws_query_resolves_modeled_error_from_query_error_trait() -> None: |
| 202 | +@pytest.mark.asyncio |
| 203 | +async def test_aws_query_resolves_modeled_error_from_query_error_trait() -> None: |
203 | 204 | protocol = AwsQueryClientProtocol(_SERVICE_SCHEMA, "2020-01-08") |
204 | | - error = getattr(protocol, "_create_error")( |
205 | | - operation=_mock_operation( |
206 | | - _operation_schema("FailingOperation"), |
207 | | - error_schemas=[_INVALID_ACTION_ERROR_SCHEMA], |
208 | | - ), |
209 | | - response=HTTPResponse(status=400, fields=tuples_to_fields([])), |
210 | | - response_body=( |
211 | | - b"<ErrorResponse><Error><Code>InvalidAction</Code>" |
212 | | - b"<message>bad request</message></Error></ErrorResponse>" |
213 | | - ), |
214 | | - error_registry=TypeRegistry( |
215 | | - {ShapeID("com.test#InvalidActionError"): _ModeledQueryError} |
216 | | - ), |
217 | | - ) |
218 | | - |
219 | | - assert isinstance(error, _ModeledQueryError) |
220 | | - assert error.message == "bad request" |
| 205 | + with pytest.raises(_ModeledQueryError) as exc_info: |
| 206 | + await protocol.deserialize_response( |
| 207 | + operation=_mock_operation( |
| 208 | + _operation_schema("FailingOperation"), |
| 209 | + error_schemas=[_INVALID_ACTION_ERROR_SCHEMA], |
| 210 | + ), |
| 211 | + request=cast(HTTPRequest, Mock()), |
| 212 | + response=HTTPResponse( |
| 213 | + status=400, |
| 214 | + fields=tuples_to_fields([]), |
| 215 | + body=( |
| 216 | + b"<ErrorResponse><Error><Code>InvalidAction</Code>" |
| 217 | + b"<message>bad request</message></Error></ErrorResponse>" |
| 218 | + ), |
| 219 | + ), |
| 220 | + error_registry=TypeRegistry( |
| 221 | + {ShapeID("com.test#InvalidActionError"): _ModeledQueryError} |
| 222 | + ), |
| 223 | + context=TypedProperties(), |
| 224 | + ) |
| 225 | + |
| 226 | + assert exc_info.value.message == "bad request" |
221 | 227 |
|
222 | 228 |
|
223 | | -def test_aws_query_resolves_modeled_error_from_default_namespace_fallback() -> None: |
| 229 | +@pytest.mark.asyncio |
| 230 | +async def test_aws_query_resolves_modeled_error_from_default_namespace_fallback() -> ( |
| 231 | + None |
| 232 | +): |
224 | 233 | protocol = AwsQueryClientProtocol(_SERVICE_SCHEMA, "2020-01-08") |
225 | | - error = getattr(protocol, "_create_error")( |
226 | | - operation=_mock_operation(_operation_schema("FailingOperation")), |
227 | | - response=HTTPResponse(status=503, fields=tuples_to_fields([])), |
228 | | - response_body=( |
229 | | - b"<ErrorResponse><Error><Code>ServiceUnavailable</Code>" |
230 | | - b"<message>try again</message></Error></ErrorResponse>" |
231 | | - ), |
232 | | - error_registry=TypeRegistry( |
233 | | - {ShapeID("com.test#ServiceUnavailable"): _ModeledQueryError} |
234 | | - ), |
235 | | - ) |
| 234 | + with pytest.raises(_ModeledQueryError) as exc_info: |
| 235 | + await protocol.deserialize_response( |
| 236 | + operation=_mock_operation(_operation_schema("FailingOperation")), |
| 237 | + request=cast(HTTPRequest, Mock()), |
| 238 | + response=HTTPResponse( |
| 239 | + status=503, |
| 240 | + fields=tuples_to_fields([]), |
| 241 | + body=( |
| 242 | + b"<ErrorResponse><Error><Code>ServiceUnavailable</Code>" |
| 243 | + b"<message>try again</message></Error></ErrorResponse>" |
| 244 | + ), |
| 245 | + ), |
| 246 | + error_registry=TypeRegistry( |
| 247 | + {ShapeID("com.test#ServiceUnavailable"): _ModeledQueryError} |
| 248 | + ), |
| 249 | + context=TypedProperties(), |
| 250 | + ) |
| 251 | + |
| 252 | + assert exc_info.value.message == "try again" |
236 | 253 |
|
237 | | - assert isinstance(error, _ModeledQueryError) |
238 | | - assert error.message == "try again" |
239 | 254 |
|
240 | | - |
241 | | -def test_aws_query_returns_generic_error_for_unknown_code() -> None: |
| 255 | +@pytest.mark.asyncio |
| 256 | +async def test_aws_query_returns_generic_error_for_unknown_code() -> None: |
242 | 257 | protocol = AwsQueryClientProtocol(_SERVICE_SCHEMA, "2020-01-08") |
243 | | - error = getattr(protocol, "_create_error")( |
244 | | - operation=_mock_operation(_operation_schema("FailingOperation")), |
245 | | - response=HTTPResponse(status=500, fields=tuples_to_fields([])), |
246 | | - response_body=( |
247 | | - b"<ErrorResponse><Error><Code>UnknownThing</Code>" |
248 | | - b"<message>bad request</message></Error></ErrorResponse>" |
249 | | - ), |
250 | | - error_registry=TypeRegistry({}), |
251 | | - ) |
252 | | - |
253 | | - assert isinstance(error, CallError) |
254 | | - assert not isinstance(error, ModeledError) |
255 | | - assert error.message == ( |
| 258 | + with pytest.raises(CallError) as exc_info: |
| 259 | + await protocol.deserialize_response( |
| 260 | + operation=_mock_operation(_operation_schema("FailingOperation")), |
| 261 | + request=cast(HTTPRequest, Mock()), |
| 262 | + response=HTTPResponse( |
| 263 | + status=500, |
| 264 | + fields=tuples_to_fields([]), |
| 265 | + body=( |
| 266 | + b"<ErrorResponse><Error><Code>UnknownThing</Code>" |
| 267 | + b"<message>bad request</message></Error></ErrorResponse>" |
| 268 | + ), |
| 269 | + ), |
| 270 | + error_registry=TypeRegistry({}), |
| 271 | + context=TypedProperties(), |
| 272 | + ) |
| 273 | + |
| 274 | + assert not isinstance(exc_info.value, ModeledError) |
| 275 | + assert exc_info.value.message == ( |
256 | 276 | "Unknown error for operation com.test#FailingOperation" |
257 | 277 | " - status: 500, code: UnknownThing" |
258 | 278 | ) |
0 commit comments