-
-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework of the URL subtraction feature #1392
base: master
Are you sure you want to change the base?
Changes from all commits
e3bf2ba
7972108
d9419a4
09b8eb5
e19dc9b
31f9eec
3aeaccf
2341ece
69b062f
ae99021
4033cb7
6a9d13b
a373000
ad61c24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Added a new method :py:meth:`~yarl.URL.relative_to` | ||
to get the relative path between URLs. | ||
|
||
Note that both URLs must have the same scheme, user, password, host and port: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> target = URL("http://example.com/path/index.html") | ||
>>> base = URL("http://example.com/") | ||
>>> target.relative_to(base) | ||
URL('path/index.html') | ||
>>> base.relative_to(target) | ||
URL('..') | ||
|
||
URLs can also be relative: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> target = URL("/path/index.html") | ||
>>> base = URL("/") | ||
>>> target.relative_to(base) | ||
URL('path/index.html') | ||
>>> base.relative_to(target) | ||
URL('..') | ||
|
||
-- by :user:`oleksbabieiev` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,69 @@ def test_str(): | |
assert str(url) == "http://example.com:8888/path/to?a=1&b=2" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("target", "base", "expected"), | ||
[ | ||
("http://example.com/path/to", "http://example.com/", "path/to"), | ||
("http://example.com/path/to", "http://example.com/spam", "path/to"), | ||
("http://example.com/this/is/a/test", "http://example.com/this/", "is/a/test"), | ||
( | ||
"http://example.com/this/./is/a/test", | ||
"http://example.com/this/", | ||
"is/a/test", | ||
), | ||
( | ||
"http://example.com/////path/////to", | ||
"http://example.com/////spam", | ||
"path/////to", | ||
), | ||
( | ||
"http://example.com////path/////to", | ||
"http://example.com/////spam", | ||
"../path/////to", | ||
), | ||
( | ||
"http://example.com/this/is/../a//test", | ||
"http://example.com/this/", | ||
"a//test", | ||
), | ||
("http://example.com/path/to", "http://example.com/spam/", "../path/to"), | ||
("http://example.com/path", "http://example.com/path/to/", ".."), | ||
("http://example.com/path", "http://example.com/other/../path/to/", ".."), | ||
("http://example.com/", "http://example.com/", ""), | ||
("http://example.com", "http://example.com", ""), | ||
("http://example.com/", "http://example.com", "/"), | ||
("http://example.com", "http://example.com/", ""), | ||
("//example.com", "//example.com", ""), | ||
("/path/to", "/spam/", "../path/to"), | ||
("path/to", "spam/", "../path/to"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no path("/", "/to", ".."), normal("/path", "/path/to", ".."), trailing / - empy segment at the end("/path", "/path/", ".."), There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. c.f. #1388 (comment) |
||
("path/../to", "path/", "../to"), | ||
("path/..", ".", "path/.."), | ||
("path/../replace/me", "path/../replace", "replace/me"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this different to the one below? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the last segment of the path does not have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, we can say that if the path ends with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pathlib PosixPath can not be used to relativize URL pathes as URL pathes can have empty segments, and PosixPath strips trailing / anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I've already noticed that... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll think about what to replace it with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I've managed to solve it |
||
("path/../replace/me", "path/../replace/", "me"), | ||
("path/to", "spam", "path/to"), | ||
("..", ".", ".."), | ||
(".", "..", "."), | ||
], | ||
) | ||
def test_relative_to(target: str, base: str, expected: str): | ||
expected_url = URL(expected) | ||
result_url = URL(target).relative_to(URL(base)) | ||
assert result_url == expected_url | ||
|
||
|
||
def test_relative_to_with_different_schemes(): | ||
expected_error_msg = r"^Both URLs should have the same scheme$" | ||
with pytest.raises(ValueError, match=expected_error_msg): | ||
URL("http://example.com/").relative_to(URL("https://example.com/")) | ||
|
||
|
||
def test_relative_to_with_different_netlocs(): | ||
expected_error_msg = r"^Both URLs should have the same netloc$" | ||
with pytest.raises(ValueError, match=expected_error_msg): | ||
URL("https://spam.com/").relative_to(URL("https://ham.com/")) | ||
|
||
|
||
def test_repr(): | ||
url = URL("http://example.com") | ||
assert "URL('http://example.com')" == repr(url) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
../path/to
?