]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Ensure TestClient sets HTTP reason phrase
authorTom Christie <tom@tomchristie.com>
Mon, 17 Sep 2018 13:59:09 +0000 (14:59 +0100)
committerTom Christie <tom@tomchristie.com>
Mon, 17 Sep 2018 13:59:09 +0000 (14:59 +0100)
starlette/testclient.py
tests/test_responses.py

index 0a94fadbaa9136f5a84e48bd09b1b57a7ebd1db9..d23b194e89376f2dcad452197278317d9ebef39a 100644 (file)
@@ -1,4 +1,5 @@
 import asyncio
+import http
 import io
 import json
 import threading
@@ -34,6 +35,13 @@ class _Upgrade(Exception):
         self.session = session
 
 
+def _get_reason_phrase(status_code):
+    try:
+        return http.HTTPStatus(status_code).phrase
+    except ValueError:
+        return ""
+
+
 class _ASGIAdapter(requests.adapters.HTTPAdapter):
     def __init__(self, app: typing.Callable, raise_server_exceptions=True) -> None:
         self.app = app
@@ -111,6 +119,7 @@ class _ASGIAdapter(requests.adapters.HTTPAdapter):
             if message["type"] == "http.response.start":
                 raw_kwargs["version"] = 11
                 raw_kwargs["status"] = message["status"]
+                raw_kwargs["reason"] = _get_reason_phrase(message["status"])
                 raw_kwargs["headers"] = [
                     (key.decode(), value.decode()) for key, value in message["headers"]
                 ]
@@ -141,6 +150,7 @@ class _ASGIAdapter(requests.adapters.HTTPAdapter):
                 raw_kwargs = {
                     "version": 11,
                     "status": 500,
+                    "reason": "Internal Server Error",
                     "headers": [],
                     "preload_content": False,
                     "original_response": _MockOriginalResponse([]),
index e0f841e2abb2a185551e25bf2ad08f207c1c4b45..4171178c6aa3de2b6fd7881c77b3adbde0fbfcbe 100644 (file)
@@ -91,6 +91,23 @@ def test_response_headers():
     assert response.headers["x-header-2"] == "789"
 
 
+
+def test_response_phrase():
+    def app(scope):
+        return Response(b'', status_code=200)
+
+    client = TestClient(app)
+    response = client.get("/")
+    assert response.reason == "OK"
+
+    def app(scope):
+        return Response(b'', status_code=123)
+
+    client = TestClient(app)
+    response = client.get("/")
+    assert response.reason == ""
+
+
 def test_file_response(tmpdir):
     path = os.path.join(tmpdir, "xyz")
     content = b"<file content>" * 1000