Skip to content

Commit 2aa9e67

Browse files
chore: remove auth from request.__str__() (#887)
* chore: remove auth from request.__str__() * chore: remove auth from request.__str__() * chore: skip authorization key in headers
1 parent 50a271c commit 2aa9e67

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

tests/unit/http/test_http_client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from twilio.base.version import Version
1111
from twilio.http.http_client import TwilioHttpClient
1212
from twilio.http.response import Response
13+
from twilio.http.request import Request
1314

1415

1516
class TestHttpClientRequest(unittest.TestCase):
@@ -293,6 +294,43 @@ def test_session_not_preserved(self):
293294
self.assertEqual(response_2.content, "response_2")
294295

295296

297+
class TestTwilioRequest(unittest.TestCase):
298+
def test_str(self):
299+
300+
req = Request(
301+
method="POST",
302+
url="https://api.twilio.com/2010-04-01/Accounts.json",
303+
auth=("AC123", "token"),
304+
params={"PageSize": "1"},
305+
data={"FriendlyName": "My New Account"},
306+
headers={"X-Custom-Header": "Value"},
307+
)
308+
expected = (
309+
"POST https://api.twilio.com/2010-04-01/Accounts.json?PageSize=1\n"
310+
' -d "FriendlyName=My New Account"\n'
311+
' -H "X-Custom-Header: Value"'
312+
)
313+
self.assertEqual(expected, req.__str__())
314+
315+
def test_str_excludes_authorization_header(self):
316+
req = Request(
317+
method="POST",
318+
url="https://api.twilio.com/2010-04-01/Accounts.json",
319+
params={"PageSize": "1"},
320+
data={"FriendlyName": "My New Account"},
321+
headers={
322+
"Authorization": "Bearer secret-token",
323+
"X-Custom-Header": "Value"
324+
},
325+
)
326+
expected = (
327+
"POST https://api.twilio.com/2010-04-01/Accounts.json?PageSize=1\n"
328+
' -d "FriendlyName=My New Account"\n'
329+
' -H "X-Custom-Header: Value"'
330+
)
331+
self.assertEqual(expected, req.__str__())
332+
333+
296334
class MyVersion(Version):
297335
def __init__(self, domain):
298336
super().__init__(domain, "v1")

twilio/http/request.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ def __eq__(self, other) -> bool:
5656
)
5757

5858
def __str__(self) -> str:
59-
auth = ""
60-
if self.auth and self.auth != Match.ANY:
61-
auth = "{} ".format(self.auth)
62-
6359
params = ""
6460
if self.params and self.params != Match.ANY:
6561
params = "?{}".format(urlencode(self.params, doseq=True))
@@ -75,11 +71,10 @@ def __str__(self) -> str:
7571
headers = ""
7672
if self.headers and self.headers != Match.ANY:
7773
headers = "\n{}".format(
78-
"\n".join(' -H "{}: {}"'.format(k, v) for k, v in self.headers.items())
74+
"\n".join(' -H "{}: {}"'.format(k, v) for k, v in self.headers.items() if k.lower() != "authorization")
7975
)
8076

81-
return "{auth}{method} {url}{params}{data}{headers}".format(
82-
auth=auth,
77+
return "{method} {url}{params}{data}{headers}".format(
8378
method=self.method,
8479
url=self.url,
8580
params=params,

0 commit comments

Comments
 (0)