Skip to content

Commit

Permalink
Merge pull request #638 from JWCook/path-url-0.9
Browse files Browse the repository at this point in the history
(0.9) Add CachedRequest.path_url property
  • Loading branch information
JWCook committed May 18, 2022
2 parents a47fe91 + 0d6c381 commit 52e1ba2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -2,6 +2,7 @@

## 0.9.5 (Unreleased)
* Fix usage of memory backend with `install_cache()`
* Add `CachedRequest.path_url` property
* Add compatibility with cattrs 22.1

## 0.9.4 (2022-04-22)
Expand Down
8 changes: 8 additions & 0 deletions requests_cache/models/request.py
@@ -1,4 +1,5 @@
from logging import getLogger
from urllib.parse import urlsplit

from attr import asdict, define, field, fields_dict
from requests import PreparedRequest
Expand Down Expand Up @@ -27,6 +28,13 @@ def from_request(cls, original_request: PreparedRequest) -> 'CachedRequest':
kwargs['cookies'] = getattr(original_request, '_cookies', None)
return cls(**kwargs) # type: ignore # False positive in mypy 0.920+?

@property
def path_url(self):
p = urlsplit(self.url)
url = p.path or '/'
url += f'?{p.query}' if p.query else ''
return url

def copy(self) -> 'CachedRequest':
"""Return a copy of the CachedRequest"""
return self.__class__(**asdict(self))
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/models/test_request.py
Expand Up @@ -10,7 +10,7 @@ def test_from_request(mock_session):
expected_headers = {**default_headers(), 'Content-Length': '12', 'foo': 'bar'}

assert response.request.body == request.body == b'mock request'
assert response.request._cookies == request.cookies == {}
assert response.request.headers == request.headers == expected_headers
assert response.request.method == request.method == 'GET'
assert response.request.path_url == request.path_url == '/text'
assert response.request.url == request.url == MOCKED_URL

0 comments on commit 52e1ba2

Please sign in to comment.