Skip to content

Commit d728b2f

Browse files
authored
[core] add HttpRequest and HttpResponse reprs (#16972)
1 parent 2e2d3af commit d728b2f

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

sdk/core/azure-core/CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# Release History
22

3-
## 1.12.0 (Unreleased)
3+
## 1.12.0 (2021-03-08)
44

55
### Features
66

77
- Added `azure.core.messaging.CloudEvent` model that follows the cloud event spec.
8-
- Added `azure.core.serialization.NULL` sentinel value
8+
- Added `azure.core.serialization.NULL` sentinel value
9+
10+
### Bug Fixes
11+
12+
- Improve `repr`s for `HttpRequest` and `HttpResponse`s #16972
913

1014
## 1.11.0 (2021-02-08)
1115

sdk/core/azure-core/azure/core/pipeline/transport/_base.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ def __init__(self, method, url, headers=None, files=None, data=None):
234234
self.multipart_mixed_info = None # type: Optional[Tuple]
235235

236236
def __repr__(self):
237-
return "<HttpRequest [%s]>" % (self.method)
237+
return "<HttpRequest [{}], url: '{}'>".format(
238+
self.method, self.url
239+
)
238240

239241
def __deepcopy__(self, memo=None):
240242
try:
@@ -592,6 +594,15 @@ def raise_for_status(self):
592594
if self.status_code >= 400:
593595
raise HttpResponseError(response=self)
594596

597+
def __repr__(self):
598+
# there doesn't have to be a content type
599+
content_type_str = (
600+
", Content-Type: {}".format(self.content_type) if self.content_type else ""
601+
)
602+
return "<{}: {} {}{}>".format(
603+
type(self).__name__, self.status_code, self.reason, content_type_str
604+
)
605+
595606

596607
class HttpResponse(_HttpResponseBase): # pylint: disable=abstract-method
597608
def stream_download(self, pipeline):

sdk/core/azure-core/tests/async_tests/test_universal_http_async.py

+11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ def __init__(self, body_bytes, headers=None):
9898
self._body = body_bytes
9999
self._headers = headers
100100
self._cache = {}
101+
self.status = 200
102+
self.reason = "OK"
101103

102104
req_response = MockAiohttpClientResponse(body_bytes, headers)
103105

@@ -120,3 +122,12 @@ async def test_aiohttp_response_text():
120122
{'Content-Type': 'text/plain'}
121123
)
122124
assert res.text(encoding) == '56', "Encoding {} didn't work".format(encoding)
125+
126+
def test_repr():
127+
res = _create_aiohttp_response(
128+
b'\xef\xbb\xbf56',
129+
{}
130+
)
131+
res.content_type = "text/plain"
132+
133+
assert repr(res) == "<AioHttpTransportResponse: 200 OK, Content-Type: text/plain>"

sdk/core/azure-core/tests/test_pipeline.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def test_request_url_with_params(self):
285285
request.format_parameters({"g": "h"})
286286

287287
self.assertIn(request.url, ["a/b/c?g=h&t=y", "a/b/c?t=y&g=h"])
288-
288+
289289
def test_request_url_with_params_as_list(self):
290290

291291
request = HttpRequest("GET", "/")
@@ -300,7 +300,7 @@ def test_request_url_with_params_with_none_in_list(self):
300300
request.url = "a/b/c?t=y"
301301
with pytest.raises(ValueError):
302302
request.format_parameters({"g": ["h",None]})
303-
303+
304304
def test_request_url_with_params_with_none(self):
305305

306306
request = HttpRequest("GET", "/")
@@ -328,6 +328,10 @@ def test_request_text(self):
328328
# We want a direct string
329329
assert request.data == "foo"
330330

331+
def test_repr(self):
332+
request = HttpRequest("GET", "hello.com")
333+
assert repr(request) == "<HttpRequest [GET], url: 'hello.com'>"
334+
331335

332336
if __name__ == "__main__":
333337
unittest.main()

sdk/core/azure-core/tests/test_requests_universal.py

+7
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,10 @@ def test_requests_response_text():
8282
{'Content-Type': 'text/plain'}
8383
)
8484
assert res.text(encoding) == '56', "Encoding {} didn't work".format(encoding)
85+
86+
def test_repr():
87+
res = _create_requests_response(
88+
b'\xef\xbb\xbf56',
89+
{'Content-Type': 'text/plain'}
90+
)
91+
assert repr(res) == "<RequestsTransportResponse: 200 OK, Content-Type: text/plain>"

0 commit comments

Comments
 (0)