Skip to content

Commit

Permalink
Add URL with fragment to ClientResponse (#2925)
Browse files Browse the repository at this point in the history
* Fixed ClientResponse URL fragment

* Added news entry

* ClientResponse real_url property + doc + test

* Fixed flake8 and doc-spelling errors

* Updated docs
  • Loading branch information
hdk5 authored and asvetlov committed Apr 17, 2018
1 parent 544716c commit cb17696
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/2925.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ClientResponse and RequestInfo now have real_url property, which is request url without fragment part being stripped
1 change: 0 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ async def _request(self, method, url, *,
"with AUTH argument or credentials "
"encoded in URL")

url = url.with_fragment(None)
cookies = self._cookie_jar.filter_cookies(url)

if proxy is not None:
Expand Down
17 changes: 14 additions & 3 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class RequestInfo:
url = attr.ib(type=URL)
method = attr.ib(type=str)
headers = attr.ib(type=CIMultiDictProxy)
real_url = attr.ib(type=URL)

@real_url.default
def real_url_default(self):
return self.url


class Fingerprint:
Expand Down Expand Up @@ -191,8 +196,8 @@ def __init__(self, method, url, *,
url2 = url.with_query(params)
q.extend(url2.query)
url = url.with_query(q)
self.url = url.with_fragment(None)
self.original_url = url
self.url = url.with_fragment(None)
self.method = method.upper()
self.chunked = chunked
self.compress = compress
Expand Down Expand Up @@ -244,7 +249,8 @@ def port(self):

@property
def request_info(self):
return RequestInfo(self.url, self.method, self.headers)
return RequestInfo(self.url, self.method,
self.headers, self.original_url)

def update_host(self, url):
"""Update destination host, port and connection type (ssl)."""
Expand Down Expand Up @@ -582,7 +588,8 @@ def __init__(self, method, url, *,
self.headers = None
self.cookies = SimpleCookie()

self._url = url
self._real_url = url
self._url = url.with_fragment(None)
self._body = None
self._writer = writer
self._continue = continue100 # None by default
Expand All @@ -608,6 +615,10 @@ def url_obj(self):
"Deprecated, use .url #1654", DeprecationWarning, stacklevel=2)
return self._url

@property
def real_url(self):
return self._real_url

@property
def host(self):
return self._url.host
Expand Down
12 changes: 12 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,12 @@ Response object

URL of request (:class:`~yarl.URL`).

.. attribute:: real_url

Unmodified URL of request (:class:`~yarl.URL`).

.. versionadded:: 3.2

.. attribute:: connection

:class:`Connection` used for handling response.
Expand Down Expand Up @@ -1453,6 +1459,12 @@ RequestInfo

HTTP headers for request, :class:`multidict.CIMultiDict` instance.

.. attribute:: real_url

Requested *url* with URL fragment unstripped, :class:`yarl.URL` instance.

.. versionadded:: 3.2


BasicAuth
^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ unittest
Unittest
unix
unsets
unstripped
upstr
url
urldispatcher
Expand Down
8 changes: 8 additions & 0 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def test_request_info(make_request):
req.headers)


def test_request_info_with_fragment(make_request):
req = make_request('get', 'http://python.org/#urlfragment')
assert req.request_info == aiohttp.RequestInfo(
URL('http://python.org/'),
'GET', req.headers,
URL('http://python.org/#urlfragment'))


def test_version_err(make_request):
with pytest.raises(ValueError):
make_request('get', 'http://python.org/', version='1.c')
Expand Down
15 changes: 15 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,3 +1018,18 @@ def side_effect(*args, **kwargs):
trace.send_response_chunk_received.call_args ==
mock.call(response_body)
)


def test_response_real_url(loop, session):
url = URL('http://def-cl-resp.org/#urlfragment')
response = ClientResponse('get', url,
request_info=mock.Mock(),
writer=mock.Mock(),
continue100=None,
timer=TimerNoop(),
auto_decompress=True,
traces=[],
loop=loop,
session=session)
assert response.url == url.with_fragment(None)
assert response.real_url == url

0 comments on commit cb17696

Please sign in to comment.