|
| 1 | +import pytest |
| 2 | + |
| 3 | +from rfc3986 import iri_reference |
| 4 | +from rfc3986 import uri_reference |
| 5 | +from rfc3986 import urlparse |
| 6 | +from rfc3986.parseresult import ParseResult |
| 7 | +from rfc3986.parseresult import ParseResultBytes |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize("parse", [uri_reference, iri_reference, urlparse]) |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "text", ["foo:///", "file:///tmp/a", "//", "///a?x#y"] |
| 13 | +) |
| 14 | +def test_empty_authority_round_trip(parse, text): |
| 15 | + reference = parse(text) |
| 16 | + assert reference.authority == "" |
| 17 | + assert reference.unsplit() == text |
| 18 | + assert ( |
| 19 | + reference.copy_with(fragment="new").unsplit() |
| 20 | + == text.split("#")[0] + "#new" |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize("text", ["foo:///", "file:///tmp/a", "//"]) |
| 25 | +def test_normalization_and_encoding_preserve_empty_authority(text): |
| 26 | + assert uri_reference(text).normalize().unsplit() == text |
| 27 | + assert iri_reference(text).encode().unsplit() == text |
| 28 | + assert urlparse(text).encode().unsplit() == text.encode() |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("text", ["foo:/", "/tmp/a", "a", ""]) |
| 32 | +def test_absent_authority_remains_absent(text): |
| 33 | + reference = uri_reference(text) |
| 34 | + assert reference.authority is None |
| 35 | + assert reference.normalize().authority is None |
| 36 | + assert reference.unsplit() == text |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("cls", [ParseResult, ParseResultBytes]) |
| 40 | +def test_from_parts_distinguishes_empty_and_absent_host(cls): |
| 41 | + absent = cls.from_parts(scheme="foo", path="/a") |
| 42 | + empty = cls.from_parts(scheme="foo", host="", path="/a") |
| 43 | + |
| 44 | + def as_string(value): |
| 45 | + return value.decode() if isinstance(value, bytes) else value |
| 46 | + |
| 47 | + assert as_string(absent.unsplit()) == "foo:/a" |
| 48 | + assert as_string(empty.unsplit()) == "foo:///a" |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize( |
| 52 | + "relative, base, expected", |
| 53 | + [ |
| 54 | + ("///new", "foo://example.com/old", "foo:///new"), |
| 55 | + ("new", "foo://", "foo:///new"), |
| 56 | + ("", "foo://", "foo://"), |
| 57 | + ], |
| 58 | +) |
| 59 | +def test_resolve_with_empty_authority(relative, base, expected): |
| 60 | + result = uri_reference(relative).resolve_with(base) |
| 61 | + assert result.authority == "" |
| 62 | + assert result.unsplit() == expected |
0 commit comments