Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/unit/http/test_async_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ async def test_request_called_with_method_and_url(self):
self.assertEqual(request_args["method"], "GET")
self.assertEqual(request_args["url"], "https://mock.twilio.com")

async def test_request_body_is_passed_as_json_for_application_json(self):
body = {"userName": "Alice"}

await self.client.request(
"POST",
"https://mock.twilio.com",
data=body,
headers={"Content-Type": "application/json"},
)

request_args = self.session_mock.request.call_args.kwargs

self.assertEqual(request_args["json"], body)
self.assertNotIn("data", request_args)

async def test_request_body_is_passed_as_json_for_scim_json(self):
body = {"userName": "Alice"}

await self.client.request(
"POST",
"https://mock.twilio.com",
data=body,
headers={"Content-Type": "application/scim+json"},
)

request_args = self.session_mock.request.call_args.kwargs

self.assertEqual(request_args["json"], body)
self.assertNotIn("data", request_args)

async def test_request_called_with_basic_auth(self):
await self.client.request(
"doesnt matter", "doesnt matter", auth=("account_sid", "auth_token")
Expand Down
8 changes: 7 additions & 1 deletion twilio/http/async_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,19 @@ async def request(
"method": method.upper(),
"url": url,
"params": params,
"data": data,
"headers": headers,
"auth": basic_auth,
"timeout": timeout,
"allow_redirects": allow_redirects,
}

content_type = (headers or {}).get("Content-Type", "").lower()

if content_type in ("application/json", "application/scim+json"):
kwargs["json"] = data
else:
kwargs["data"] = data

self.log_request(kwargs)
self._test_only_last_response = None

Expand Down