Skip to content
Myr

test_wsgi.py

This file

Path

tests/test_wsgi.py

118 lines · 2.9 KiB · Python

Last changed by

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

History of this file

Take it

1import sys2from functools import partial34import pytest56import httpx789def application_factory(output):10    def application(environ, start_response):11        status = "200 OK"1213        response_headers = [14            ("Content-type", "text/plain"),15        ]1617        start_response(status, response_headers)1819        for item in output:20            yield item2122    return application232425def echo_body(environ, start_response):26    status = "200 OK"27    output = environ["wsgi.input"].read()2829    response_headers = [30        ("Content-type", "text/plain"),31    ]3233    start_response(status, response_headers)3435    return [output]363738def echo_body_with_response_stream(environ, start_response):39    status = "200 OK"4041    response_headers = [("Content-Type", "text/plain")]4243    start_response(status, response_headers)4445    def output_generator(f):46        while True:47            output = f.read(2)48            if not output:49                break50            yield output5152    return output_generator(f=environ["wsgi.input"])535455def raise_exc(environ, start_response, exc=ValueError):56    status = "500 Server Error"57    output = b"Nope!"5859    response_headers = [60        ("Content-type", "text/plain"),61    ]6263    try:64        raise exc()65    except exc:66        exc_info = sys.exc_info()67        start_response(status, response_headers, exc_info=exc_info)6869    return [output]707172def test_wsgi():73    client = httpx.Client(app=application_factory([b"Hello, World!"]))74    response = client.get("http://www.example.org/")75    assert response.status_code == 20076    assert response.text == "Hello, World!"777879def test_wsgi_upload():80    client = httpx.Client(app=echo_body)81    response = client.post("http://www.example.org/", data=b"example")82    assert response.status_code == 20083    assert response.text == "example"848586def test_wsgi_upload_with_response_stream():87    client = httpx.Client(app=echo_body_with_response_stream)88    response = client.post("http://www.example.org/", data=b"example")89    assert response.status_code == 20090    assert response.text == "example"919293def test_wsgi_exc():94    client = httpx.Client(app=raise_exc)95    with pytest.raises(ValueError):96        client.get("http://www.example.org/")979899def test_wsgi_http_error():100    client = httpx.Client(app=partial(raise_exc, exc=httpx.HTTPError))101    with pytest.raises(httpx.HTTPError):102        client.get("http://www.example.org/")103104105def test_wsgi_generator():106    output = [b"", b"", b"Some content", b" and more content"]107    client = httpx.Client(app=application_factory(output))108    response = client.get("http://www.example.org/")109    assert response.status_code == 200110    assert response.text == "Some content and more content"111112113def test_wsgi_generator_empty():114    output = [b"", b"", b"", b""]115    client = httpx.Client(app=application_factory(output))116    response = client.get("http://www.example.org/")117    assert response.status_code == 200118    assert response.text == ""