-
-
Notifications
You must be signed in to change notification settings - Fork 167
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 4 commits
e3bf2ba
7972108
d9419a4
09b8eb5
e19dc9b
31f9eec
3aeaccf
2341ece
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,22 @@ | ||
Added support for using the :meth:`subtraction operator <yarl.URL.__sub__>` | ||
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 - base | ||
URL('path/index.html') | ||
|
||
URLs can also be relative: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> target = URL("/") | ||
>>> base = URL("/path/index.html") | ||
>>> target - base | ||
URL('..') | ||
|
||
-- by :user:`oleksbabieiev` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,89 @@ 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/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? |
||
("path/../replace/me", "path/../replace/", "me"), | ||
("path/to", "spam", "path/to"), | ||
("..", ".", ".."), | ||
(".", "..", "."), | ||
], | ||
) | ||
def test_sub(target: str, base: str, expected: str): | ||
expected_url = URL(expected) | ||
result_url = URL(target) - URL(base) | ||
assert result_url == expected_url | ||
|
||
|
||
@pytest.mark.xfail(reason="Empty segments are not preserved") | ||
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. What does need to be done to fix this? It'd be useful to include this information into the reason so that it's obvious when it can be removed. 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. this is bugs to address - c.f. #1388 (comment) |
||
@pytest.mark.parametrize( | ||
("target", "base", "expected"), | ||
[ | ||
( | ||
"http://example.com/////path/////to", | ||
"http://example.com/////spam", | ||
"path/////to", | ||
), | ||
( | ||
"http://example.com////path/////to", | ||
"http://example.com/////spam", | ||
"..//path/////to", | ||
), | ||
], | ||
) | ||
def test_sub_empty_segments(target: str, base: str, expected: str): | ||
expected_url = URL(expected) | ||
result_url = URL(target) - URL(base) | ||
assert result_url == expected_url | ||
|
||
|
||
def test_sub_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/") - URL("https://example.com/") | ||
|
||
|
||
def test_sub_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/") - URL("https://ham.com/") | ||
|
||
|
||
def test_sub_with_different_anchors(): | ||
expected_error_msg = r"^'path/to' and '/path' have different anchors$" | ||
with pytest.raises(ValueError, match=expected_error_msg): | ||
URL("path/to") - URL("/path/from") | ||
|
||
|
||
def test_sub_with_two_dots_in_base(): | ||
expected_error_msg = r"^'..' segment in '/path/..' cannot be walked$" | ||
with pytest.raises(ValueError, match=expected_error_msg): | ||
URL("path/to") - URL("/path/../from") | ||
|
||
|
||
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.
Could you also document what happens when you do
base - target
?