Example #1
0
def test_headers():
    h = httpcore.Headers([("a", "123"), ("a", "456"), ("b", "789")])
    assert "a" in h
    assert "A" in h
    assert "b" in h
    assert "B" in h
    assert "c" not in h
    assert h["a"] == "123, 456"
    assert h.get("a") == "123, 456"
    assert h.get("nope", default=None) is None
    assert h.getlist("a") == ["123", "456"]
    assert h.keys() == ["a", "a", "b"]
    assert h.values() == ["123", "456", "789"]
    assert h.items() == [("a", "123"), ("a", "456"), ("b", "789")]
    assert list(h) == ["a", "a", "b"]
    assert dict(h) == {"a": "123, 456", "b": "789"}
    assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"
    assert h == httpcore.Headers([("a", "123"), ("b", "789"), ("a", "456")])
    assert h != [("a", "123"), ("A", "456"), ("b", "789")]

    h = httpcore.Headers({"a": "123", "b": "789"})
    assert h["A"] == "123"
    assert h["B"] == "789"
    assert h.raw == [(b"a", b"123"), (b"b", b"789")]
    assert repr(h) == "Headers({'a': '123', 'b': '789'})"
Example #2
0
def test_multiple_headers():
    """
    Most headers should split by commas for `getlist`, except 'Set-Cookie'.
    """
    h = httpcore.Headers([("set-cookie", "a, b"), ("set-cookie", "c")])
    h.getlist("Set-Cookie") == ["a, b", "b"]

    h = httpcore.Headers([("vary", "a, b"), ("vary", "c")])
    h.getlist("Vary") == ["a", "b", "c"]
Example #3
0
def test_headers_encoding_in_repr():
    """
    Headers should display an encoding in the repr if required.
    """
    headers = httpcore.Headers({b"custom": "example ☃".encode("utf-8")})
    assert repr(
        headers) == "Headers({'custom': 'example ☃'}, encoding='utf-8')"
Example #4
0
def test_content_length_header():
    request = httpcore.Request("POST", "http://example.org", data=b"test 123")
    request.prepare()
    assert request.headers == httpcore.Headers([
        (b"host", b"example.org"),
        (b"content-length", b"8"),
        (b"accept-encoding", b"deflate, gzip, br"),
    ])
Example #5
0
def test_headers_decode_utf_8():
    """
    Headers containing non-ascii codepoints should default to decoding as utf-8.
    """
    raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]
    headers = httpcore.Headers(raw_headers)
    assert dict(headers) == {"custom": "Code point: ☃"}
    assert headers.encoding == "utf-8"
Example #6
0
def test_override_host_header():
    headers = [(b"host", b"1.2.3.4:80")]

    request = httpcore.Request("GET", "http://example.org", headers=headers)
    request.prepare()
    assert request.headers == httpcore.Headers([(b"accept-encoding",
                                                 b"deflate, gzip, br"),
                                                (b"host", b"1.2.3.4:80")])
Example #7
0
def test_override_accept_encoding_header():
    headers = [(b"accept-encoding", b"identity")]

    request = httpcore.Request("GET", "http://example.org", headers=headers)
    request.prepare()
    assert request.headers == httpcore.Headers([(b"host", b"example.org"),
                                                (b"accept-encoding",
                                                 b"identity")])
Example #8
0
def test_headers_decode_iso_8859_1():
    """
    Headers containing non-UTF-8 codepoints should default to decoding as iso-8859-1.
    """
    raw_headers = [(b"Custom", "Code point: ÿ".encode("iso-8859-1"))]
    headers = httpcore.Headers(raw_headers)
    assert dict(headers) == {"custom": "Code point: ÿ"}
    assert headers.encoding == "iso-8859-1"
Example #9
0
def test_headers_list_repr():
    """
    Headers should display with a list repr if they include multiple identical keys.
    """
    headers = httpcore.Headers([("custom", "example 1"),
                                ("custom", "example 2")])
    assert (repr(headers) ==
            "Headers([('custom', 'example 1'), ('custom', 'example 2')])")
Example #10
0
def test_headers_decode_ascii():
    """
    Headers should decode as ascii by default.
    """
    raw_headers = [(b"Custom", b"Example")]
    headers = httpcore.Headers(raw_headers)
    assert dict(headers) == {"custom": "Example"}
    assert headers.encoding == "ascii"
Example #11
0
def test_headers_decode_explicit_encoding():
    """
    An explicit encoding may be set on headers in order to force a
    particular decoding.
    """
    raw_headers = [(b"Custom", "Code point: ☃".encode("utf-8"))]
    headers = httpcore.Headers(raw_headers)
    headers.encoding = "iso-8859-1"
    print(headers)
    assert dict(headers) == {"custom": "Code point: â\x98\x83"}
    assert headers.encoding == "iso-8859-1"
Example #12
0
def test_url_encoded_data():
    request = httpcore.Request("POST",
                               "http://example.org",
                               data={"test": "123"})
    request.prepare()
    assert request.headers == httpcore.Headers([
        (b"host", b"example.org"),
        (b"content-length", b"8"),
        (b"accept-encoding", b"deflate, gzip, br"),
        (b"content-type", b"application/x-www-form-urlencoded"),
    ])
    assert request.content == b"test=123"
Example #13
0
def test_transfer_encoding_header():
    async def streaming_body(data):
        yield data  # pragma: nocover

    data = streaming_body(b"test 123")

    request = httpcore.Request("POST", "http://example.org", data=data)
    request.prepare()
    assert request.headers == httpcore.Headers([
        (b"host", b"example.org"),
        (b"transfer-encoding", b"chunked"),
        (b"accept-encoding", b"deflate, gzip, br"),
    ])
Example #14
0
def test_header_mutations():
    h = httpcore.Headers()
    assert dict(h) == {}
    h["a"] = "1"
    assert dict(h) == {"a": "1"}
    h["a"] = "2"
    assert dict(h) == {"a": "2"}
    h.setdefault("a", "3")
    assert dict(h) == {"a": "2"}
    h.setdefault("b", "4")
    assert dict(h) == {"a": "2", "b": "4"}
    del h["a"]
    assert dict(h) == {"b": "4"}
    assert h.raw == [(b"b", b"4")]
Example #15
0
def test_override_content_length_header():
    async def streaming_body(data):
        yield data  # pragma: nocover

    data = streaming_body(b"test 123")
    headers = [(b"content-length", b"8")]

    request = httpcore.Request("POST",
                               "http://example.org",
                               data=data,
                               headers=headers)
    request.prepare()
    assert request.headers == httpcore.Headers([
        (b"host", b"example.org"),
        (b"accept-encoding", b"deflate, gzip, br"),
        (b"content-length", b"8"),
    ])
Example #16
0
def test_copy_headers():
    headers = httpcore.Headers({"custom": "example"})
    headers_copy = httpcore.Headers(headers)
    assert headers == headers_copy
Example #17
0
def test_headers_delete_removes_all_existing():
    headers = httpcore.Headers([("a", "123"), ("a", "456")])
    del headers["a"]
    assert dict(headers) == {}
Example #18
0
def test_headers_insert_retains_ordering():
    headers = httpcore.Headers({"a": "a", "b": "b", "c": "c"})
    headers["b"] = "123"
    assert list(headers.values()) == ["a", "123", "c"]
Example #19
0
def test_headers_insert_appends_if_new():
    headers = httpcore.Headers({"a": "a", "b": "b", "c": "c"})
    headers["d"] = "123"
    assert list(headers.values()) == ["a", "b", "c", "123"]
Example #20
0
def test_headers_insert_removes_all_existing():
    headers = httpcore.Headers([("a", "123"), ("a", "456")])
    headers["a"] = "789"
    assert dict(headers) == {"a": "789"}
Example #21
0
def test_headers_dict_repr():
    """
    Headers should display with a dict repr by default.
    """
    headers = httpcore.Headers({"custom": "example"})
    assert repr(headers) == "Headers({'custom': 'example'})"
Example #22
0
def test_host_header():
    request = httpcore.Request("GET", "http://example.org")
    request.prepare()
    assert request.headers == httpcore.Headers([(b"host", b"example.org"),
                                                (b"accept-encoding",
                                                 b"deflate, gzip, br")])