예제 #1
0
def test_multi_with_identity():
    body = b"test 123"
    compressed_body = brotli.compress(body)

    headers = [(b"Content-Encoding", b"br, identity")]
    response = http3.Response(200, headers=headers, content=compressed_body)
    assert response.content == body

    headers = [(b"Content-Encoding", b"identity, br")]
    response = http3.Response(200, headers=headers, content=compressed_body)
    assert response.content == body
예제 #2
0
def test_response_default_encoding():
    """
    Default to utf-8 if all else fails.
    """
    response = http3.Response(200, content=b"")
    assert response.text == ""
    assert response.encoding == "utf-8"
예제 #3
0
def test_cannot_read_after_response_closed():
    response = http3.Response(200, content=streaming_body())

    response.close()

    with pytest.raises(http3.ResponseClosed):
        response.read()
예제 #4
0
def test_brotli():
    body = b"test 123"
    compressed_body = brotli.compress(body)

    headers = [(b"Content-Encoding", b"br")]
    response = http3.Response(200, headers=headers, content=compressed_body)
    assert response.content == body
예제 #5
0
def test_raw_interface():
    response = http3.Response(200, content=b"Hello, world!")

    raw = b""
    for part in response.raw():
        raw += part
    assert raw == b"Hello, world!"
예제 #6
0
def test_stream_interface():
    response = http3.Response(200, content=b"Hello, world!")

    content = b""
    for part in response.stream():
        content += part
    assert content == b"Hello, world!"
예제 #7
0
def test_response_force_encoding():
    response = http3.Response(200, content="Snowman: ☃".encode("utf-8"))
    response.encoding = "iso-8859-1"
    assert response.status_code == 200
    assert response.reason_phrase == "OK"
    assert response.text == "Snowman: â\x98\x83"
    assert response.encoding == "iso-8859-1"
예제 #8
0
def test_decoding_errors(header_value):
    headers = [(b"Content-Encoding", header_value)]
    body = b"test 123"
    compressed_body = brotli.compress(body)[3:]
    with pytest.raises(http3.exceptions.DecodingError):
        response = http3.Response(200, headers=headers, content=compressed_body)
        response.content
예제 #9
0
def test_response_non_text_encoding():
    """
    Default to apparent encoding for non-text content-type headers.
    """
    headers = {"Content-Type": "image/png"}
    response = http3.Response(200, content=b"xyz", headers=headers)
    assert response.text == "xyz"
    assert response.encoding == "ascii"
예제 #10
0
def test_deflate():
    body = b"test 123"
    compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
    compressed_body = compressor.compress(body) + compressor.flush()

    headers = [(b"Content-Encoding", b"deflate")]
    response = http3.Response(200, headers=headers, content=compressed_body)
    assert response.content == body
예제 #11
0
def test_response_autodetect_encoding():
    """
    Autodetect encoding if there is no charset info in a Content-Type header.
    """
    content = "おはようございます。".encode("EUC-JP")
    response = http3.Response(200, content=content)
    assert response.text == "おはようございます。"
    assert response.encoding == "EUC-JP"
예제 #12
0
def test_cannot_read_after_stream_consumed():
    response = http3.Response(200, content=streaming_body())

    content = b""
    for part in response.stream():
        content += part

    with pytest.raises(http3.StreamConsumed):
        response.read()
예제 #13
0
def test_json_without_specified_encoding_decode_error():
    data = dict(greeting="hello", recipient="world")
    content = json.dumps(data).encode("utf-32-be")
    headers = {"Content-Type": "application/json"}
    # force incorrect guess from `guess_json_utf` to trigger error
    with mock.patch("http3.models.guess_json_utf", return_value="utf-32"):
        response = http3.Response(200, content=content, headers=headers)
        with pytest.raises(json.JSONDecodeError):
            response.json()
예제 #14
0
def test_response_content_type_encoding():
    """
    Use the charset encoding in the Content-Type header if possible.
    """
    headers = {"Content-Type": "text-plain; charset=latin-1"}
    content = "Latin 1: ÿ".encode("latin-1")
    response = http3.Response(200, content=content, headers=headers)
    assert response.text == "Latin 1: ÿ"
    assert response.encoding == "latin-1"
예제 #15
0
def test_response_fallback_to_autodetect():
    """
    Fallback to autodetection if we get an invalid charset in the Content-Type header.
    """
    headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}
    content = "おはようございます。".encode("EUC-JP")
    response = http3.Response(200, content=content, headers=headers)
    assert response.text == "おはようございます。"
    assert response.encoding == "EUC-JP"
예제 #16
0
def test_response_set_explicit_encoding():
    headers = {
        "Content-Type": "text-plain; charset=utf-8"
    }  # Deliberately incorrect charset
    response = http3.Response(200,
                              content="Latin 1: ÿ".encode("latin-1"),
                              headers=headers)
    response.encoding = "latin-1"
    assert response.text == "Latin 1: ÿ"
    assert response.encoding == "latin-1"
예제 #17
0
def test_streaming_response():
    response = http3.Response(200, content=streaming_body())

    assert response.status_code == 200
    assert not response.is_closed

    content = response.read()

    assert content == b"Hello, world!"
    assert response.content == b"Hello, world!"
    assert response.is_closed
예제 #18
0
def test_response_default_text_encoding():
    """
    A media type of 'text/*' with no charset should default to ISO-8859-1.
    See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1
    """
    content = b"Hello, world!"
    headers = {"Content-Type": "text/plain"}
    response = http3.Response(200, content=content, headers=headers)
    assert response.status_code == 200
    assert response.encoding == "iso-8859-1"
    assert response.text == "Hello, world!"
예제 #19
0
def test_streaming():
    body = b"test 123"
    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)

    def compress(body):
        yield compressor.compress(body)
        yield compressor.flush()

    headers = [(b"Content-Encoding", b"gzip")]
    response = http3.Response(200, headers=headers, content=compress(body))
    assert not hasattr(response, "body")
    assert response.read() == body
예제 #20
0
def test_read_response():
    response = http3.Response(200, content=b"Hello, world!")

    assert response.status_code == 200
    assert response.text == "Hello, world!"
    assert response.encoding == "ascii"
    assert response.is_closed

    content = response.read()

    assert content == b"Hello, world!"
    assert response.content == b"Hello, world!"
    assert response.is_closed
예제 #21
0
def test_multi():
    body = b"test 123"

    deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
    compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()

    gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
    compressed_body = (
        gzip_compressor.compress(compressed_body) + gzip_compressor.flush()
    )

    headers = [(b"Content-Encoding", b"deflate, gzip")]
    response = http3.Response(200, headers=headers, content=compressed_body)
    assert response.content == body
예제 #22
0
def test_json_with_options():
    data = dict(greeting="hello", recipient="world", amount=1)
    content = json.dumps(data).encode("utf-16")
    headers = {"Content-Type": "application/json, charset=utf-16"}
    response = http3.Response(200, content=content, headers=headers)
    assert response.json(parse_int=str)["amount"] == "1"
예제 #23
0
def test_response():
    response = http3.Response(200, content=b"Hello, world!")
    assert response.status_code == 200
    assert response.reason_phrase == "OK"
    assert response.text == "Hello, world!"
예제 #24
0
def test_response_repr():
    response = http3.Response(200, content=b"Hello, world!")
    assert repr(response) == "<Response [200 OK]>"
예제 #25
0
def test_unknown_status_code():
    response = http3.Response(600)
    assert response.status_code == 600
    assert response.reason_phrase == ""
    assert response.text == ""
예제 #26
0
def test_json_without_specified_encoding():
    data = dict(greeting="hello", recipient="world")
    content = json.dumps(data).encode("utf-32-be")
    headers = {"Content-Type": "application/json"}
    response = http3.Response(200, content=content, headers=headers)
    assert response.json() == data