Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: wsgi.error should be TextIO, not BytesIO in WSGI transport #1828

Merged
merged 6 commits into from Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion httpx/_transports/wsgi.py
@@ -1,5 +1,6 @@
import io
import itertools
import sys
import typing
from urllib.parse import unquote

Expand Down Expand Up @@ -62,11 +63,13 @@ def __init__(
raise_app_exceptions: bool = True,
script_name: str = "",
remote_addr: str = "127.0.0.1",
log_file: typing.Optional[typing.TextIO] = None,
) -> None:
self.app = app
self.raise_app_exceptions = raise_app_exceptions
self.script_name = script_name
self.remote_addr = remote_addr
self.log_file = log_file
adriangb marked this conversation as resolved.
Show resolved Hide resolved

def handle_request(
self,
Expand All @@ -89,7 +92,7 @@ def handle_request(
"wsgi.version": (1, 0),
"wsgi.url_scheme": scheme.decode("ascii"),
"wsgi.input": wsgi_input,
"wsgi.errors": io.BytesIO(),
"wsgi.errors": self.log_file or sys.stderr,
"wsgi.multithread": True,
"wsgi.multiprocess": False,
"wsgi.run_once": False,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_wsgi.py
@@ -1,5 +1,6 @@
import sys
from functools import partial
from io import StringIO

import pytest

Expand Down Expand Up @@ -69,6 +70,12 @@ def raise_exc(environ, start_response, exc=ValueError):
return [output]


def log_to_wsgi_log_buffer(environ, start_response):
print("test1", file=environ["wsgi.errors"])
environ["wsgi.errors"].write("test2")
return echo_body(environ, start_response)


def test_wsgi():
client = httpx.Client(app=application_factory([b"Hello, World!"]))
response = client.get("http://www.example.org/")
Expand Down Expand Up @@ -118,6 +125,16 @@ def test_wsgi_generator_empty():
assert response.text == ""


def test_logging():
buffer = StringIO()
transport = httpx.WSGITransport(app=log_to_wsgi_log_buffer, log_file=buffer)
client = httpx.Client(transport=transport)
response = client.post("http://www.example.org/", content=b"example")
assert response.status_code == 200 # no errors
buffer.seek(0)
assert buffer.read() == "test1\ntest2"


@pytest.mark.parametrize(
"url, expected_server_port",
[
Expand Down