Skip to content

Commit df92cf1

Browse files
chore: add test for default aiohttp error message
1 parent 7d69fc5 commit df92cf1

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/unit/test_client.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,35 @@ async def test_cloud_sql_error_messages_get_metadata(
179179
await client.close()
180180

181181

182+
async def test_get_metadata_error_parsing_json(
183+
fake_credentials: Credentials,
184+
) -> None:
185+
"""
186+
Test that aiohttp default error messages are raised when _get_metadata gets
187+
a bad JSON response.
188+
"""
189+
# mock Cloud SQL Admin API calls with exceptions
190+
client = CloudSQLClient(
191+
sqladmin_api_endpoint="https://sqladmin.googleapis.com",
192+
quota_project=None,
193+
credentials=fake_credentials,
194+
)
195+
get_url = "https://sqladmin.googleapis.com/sql/v1beta4/projects/my-project/instances/my-instance/connectSettings"
196+
resp_body = ["error"] # invalid JSON
197+
with aioresponses() as mocked:
198+
mocked.get(
199+
get_url,
200+
status=403,
201+
payload=resp_body,
202+
repeat=True,
203+
)
204+
with pytest.raises(ClientResponseError) as exc_info:
205+
await client._get_metadata("my-project", "my-region", "my-instance")
206+
assert exc_info.value.status == 403
207+
assert exc_info.value.message == "Forbidden"
208+
await client.close()
209+
210+
182211
async def test_cloud_sql_error_messages_get_ephemeral(
183212
fake_credentials: Credentials,
184213
) -> None:
@@ -210,3 +239,32 @@ async def test_cloud_sql_error_messages_get_ephemeral(
210239
assert exc_info.value.status == 404
211240
assert exc_info.value.message == "The Cloud SQL instance does not exist."
212241
await client.close()
242+
243+
244+
async def test_get_ephemeral_error_parsing_json(
245+
fake_credentials: Credentials,
246+
) -> None:
247+
"""
248+
Test that aiohttp default error messages are raised when _get_ephemeral gets
249+
a bad JSON response.
250+
"""
251+
# mock Cloud SQL Admin API calls with exceptions
252+
client = CloudSQLClient(
253+
sqladmin_api_endpoint="https://sqladmin.googleapis.com",
254+
quota_project=None,
255+
credentials=fake_credentials,
256+
)
257+
post_url = "https://sqladmin.googleapis.com/sql/v1beta4/projects/my-project/instances/my-instance:generateEphemeralCert"
258+
resp_body = ["error"] # invalid JSON
259+
with aioresponses() as mocked:
260+
mocked.post(
261+
post_url,
262+
status=404,
263+
payload=resp_body,
264+
repeat=True,
265+
)
266+
with pytest.raises(ClientResponseError) as exc_info:
267+
await client._get_ephemeral("my-project", "my-instance", "my-key")
268+
assert exc_info.value.status == 404
269+
assert exc_info.value.message == "Not Found"
270+
await client.close()

0 commit comments

Comments
 (0)