Skip to content

Commit

Permalink
[core] add HttpRequest and HttpResponse reprs (#16972)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Mar 5, 2021
1 parent 2e2d3af commit d728b2f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
8 changes: 6 additions & 2 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Release History

## 1.12.0 (Unreleased)
## 1.12.0 (2021-03-08)

### Features

- Added `azure.core.messaging.CloudEvent` model that follows the cloud event spec.
- Added `azure.core.serialization.NULL` sentinel value
- Added `azure.core.serialization.NULL` sentinel value

### Bug Fixes

- Improve `repr`s for `HttpRequest` and `HttpResponse`s #16972

## 1.11.0 (2021-02-08)

Expand Down
13 changes: 12 additions & 1 deletion sdk/core/azure-core/azure/core/pipeline/transport/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ def __init__(self, method, url, headers=None, files=None, data=None):
self.multipart_mixed_info = None # type: Optional[Tuple]

def __repr__(self):
return "<HttpRequest [%s]>" % (self.method)
return "<HttpRequest [{}], url: '{}'>".format(
self.method, self.url
)

def __deepcopy__(self, memo=None):
try:
Expand Down Expand Up @@ -592,6 +594,15 @@ def raise_for_status(self):
if self.status_code >= 400:
raise HttpResponseError(response=self)

def __repr__(self):
# there doesn't have to be a content type
content_type_str = (
", Content-Type: {}".format(self.content_type) if self.content_type else ""
)
return "<{}: {} {}{}>".format(
type(self).__name__, self.status_code, self.reason, content_type_str
)


class HttpResponse(_HttpResponseBase): # pylint: disable=abstract-method
def stream_download(self, pipeline):
Expand Down
11 changes: 11 additions & 0 deletions sdk/core/azure-core/tests/async_tests/test_universal_http_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def __init__(self, body_bytes, headers=None):
self._body = body_bytes
self._headers = headers
self._cache = {}
self.status = 200
self.reason = "OK"

req_response = MockAiohttpClientResponse(body_bytes, headers)

Expand All @@ -120,3 +122,12 @@ async def test_aiohttp_response_text():
{'Content-Type': 'text/plain'}
)
assert res.text(encoding) == '56', "Encoding {} didn't work".format(encoding)

def test_repr():
res = _create_aiohttp_response(
b'\xef\xbb\xbf56',
{}
)
res.content_type = "text/plain"

assert repr(res) == "<AioHttpTransportResponse: 200 OK, Content-Type: text/plain>"
8 changes: 6 additions & 2 deletions sdk/core/azure-core/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def test_request_url_with_params(self):
request.format_parameters({"g": "h"})

self.assertIn(request.url, ["a/b/c?g=h&t=y", "a/b/c?t=y&g=h"])

def test_request_url_with_params_as_list(self):

request = HttpRequest("GET", "/")
Expand All @@ -300,7 +300,7 @@ def test_request_url_with_params_with_none_in_list(self):
request.url = "a/b/c?t=y"
with pytest.raises(ValueError):
request.format_parameters({"g": ["h",None]})

def test_request_url_with_params_with_none(self):

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

def test_repr(self):
request = HttpRequest("GET", "hello.com")
assert repr(request) == "<HttpRequest [GET], url: 'hello.com'>"


if __name__ == "__main__":
unittest.main()
7 changes: 7 additions & 0 deletions sdk/core/azure-core/tests/test_requests_universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ def test_requests_response_text():
{'Content-Type': 'text/plain'}
)
assert res.text(encoding) == '56', "Encoding {} didn't work".format(encoding)

def test_repr():
res = _create_requests_response(
b'\xef\xbb\xbf56',
{'Content-Type': 'text/plain'}
)
assert repr(res) == "<RequestsTransportResponse: 200 OK, Content-Type: text/plain>"

0 comments on commit d728b2f

Please sign in to comment.