Ejemplo n.º 1
0
def test_url__params():
    url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})
    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"

    url = URL("https://example.org:123/path/to/somewhere?b=456",
              params={"a": "123"})
    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"
Ejemplo n.º 2
0
def test_url_set():
    urls = (
        URL("http://example.org:123/path/to/somewhere"),
        URL("http://example.org:123/path/to/somewhere/else"),
    )

    url_set = set(urls)

    assert all(url in urls for url in url_set)
Ejemplo n.º 3
0
async def test_disallow_redirects():
    client = AsyncClient(dispatch=MockDispatch())
    response = await client.post("https://example.org/redirect_303",
                                 allow_redirects=False)
    assert response.status_code == codes.SEE_OTHER
    assert response.url == URL("https://example.org/redirect_303")
    assert len(response.history) == 0

    response = await response.next()
    assert response.status_code == codes.OK
    assert response.url == URL("https://example.org/")
    assert len(response.history) == 1
Ejemplo n.º 4
0
async def test_disallow_redirects():
    client = RedirectAdapter(MockDispatch())
    response = await client.request("POST",
                                    "https://example.org/redirect_303",
                                    allow_redirects=False)
    assert response.status_code == codes.see_other
    assert response.url == URL("https://example.org/redirect_303")
    assert len(response.history) == 0

    response = await response.next()
    assert response.status_code == codes.ok
    assert response.url == URL("https://example.org/")
    assert len(response.history) == 1
Ejemplo n.º 5
0
def test_url():
    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")
    assert url.scheme == "https"
    assert url.host == "example.org"
    assert url.port == 123
    assert url.authority == "example.org:123"
    assert url.path == "/path/to/somewhere"
    assert url.query == "abc=123"
    assert url.fragment == "anchor"
    assert (repr(url) ==
            "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')")

    new = url.copy_with(scheme="http")
    assert new == URL(
        "http://example.org:123/path/to/somewhere?abc=123#anchor")
    assert new.scheme == "http"
Ejemplo n.º 6
0
async def test_multiple_redirects():
    client = RedirectAdapter(MockDispatch())
    url = "https://example.org/multiple_redirects?count=20"
    response = await client.request("GET", url)
    assert response.status_code == codes.ok
    assert response.url == URL("https://example.org/multiple_redirects")
    assert len(response.history) == 20
Ejemplo n.º 7
0
async def test_fragment_redirect():
    client = RedirectAdapter(MockDispatch())
    url = "https://example.org/relative_redirect#fragment"
    response = await client.request("GET", url)
    assert response.status_code == codes.ok
    assert response.url == URL("https://example.org/#fragment")
    assert len(response.history) == 1
Ejemplo n.º 8
0
async def test_no_scheme_redirect():
    client = RedirectAdapter(MockDispatch())
    response = await client.request("GET",
                                    "https://example.org/no_scheme_redirect")
    assert response.status_code == codes.ok
    assert response.url == URL("https://example.org/")
    assert len(response.history) == 1
Ejemplo n.º 9
0
async def test_body_redirect():
    client = AsyncClient(dispatch=MockDispatch())
    url = "https://example.org/redirect_body"
    data = b"Example request body"
    response = await client.post(url, data=data)
    assert response.url == URL("https://example.org/redirect_body_target")
    assert response.json() == {"body": "Example request body"}
Ejemplo n.º 10
0
async def test_same_domain_redirect():
    client = AsyncClient(dispatch=MockDispatch())
    url = "https://example.org/cross_domain"
    headers = {"Authorization": "abc"}
    response = await client.get(url, headers=headers)
    assert response.url == URL("https://example.org/cross_domain_target")
    assert response.json()["headers"]["authorization"] == "abc"
Ejemplo n.º 11
0
async def test_multiple_redirects():
    client = AsyncClient(dispatch=MockDispatch())
    response = await client.get(
        "https://example.org/multiple_redirects?count=20")
    assert response.status_code == codes.OK
    assert response.url == URL("https://example.org/multiple_redirects")
    assert len(response.history) == 20
Ejemplo n.º 12
0
async def test_fragment_redirect():
    client = AsyncClient(dispatch=MockDispatch())
    response = await client.get(
        "https://example.org/relative_redirect#fragment")
    assert response.status_code == codes.OK
    assert response.url == URL("https://example.org/#fragment")
    assert len(response.history) == 1
Ejemplo n.º 13
0
async def test_body_redirect():
    client = RedirectAdapter(MockDispatch())
    url = "https://example.org/redirect_body"
    data = b"Example request body"
    response = await client.request("POST", url, data=data)
    data = json.loads(response.content.decode())
    assert response.url == URL("https://example.org/redirect_body_target")
    assert data == {"body": "Example request body"}
Ejemplo n.º 14
0
async def test_same_domain_redirect():
    client = RedirectAdapter(MockDispatch())
    url = "https://example.org/cross_domain"
    headers = {"Authorization": "abc"}
    response = await client.request("GET", url, headers=headers)
    data = json.loads(response.content.decode())
    assert response.url == URL("https://example.org/cross_domain_target")
    assert data == {"headers": {"authorization": "abc"}}
Ejemplo n.º 15
0
def test_idna_url():
    url = URL("http://中国.icom.museum:80/")
    assert url == URL("http://xn--fiqs8s.icom.museum:80/")
    assert url.host == "xn--fiqs8s.icom.museum"
Ejemplo n.º 16
0
async def test_redirect_301():
    client = AsyncClient(dispatch=MockDispatch())
    response = await client.post("https://example.org/redirect_301")
    assert response.status_code == codes.OK
    assert response.url == URL("https://example.org/")
    assert len(response.history) == 1