]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add tests for additional HTTP methods on async client 56/head
authorYeray Diaz Diaz <yeraydiazdiaz@gmail.com>
Mon, 13 May 2019 20:01:13 +0000 (21:01 +0100)
committerYeray Diaz Diaz <yeraydiazdiaz@gmail.com>
Mon, 13 May 2019 20:01:13 +0000 (21:01 +0100)
Add tests for raise_for_status and SyncResponse properties

httpcore/models.py
tests/client/test_async_client.py
tests/client/test_client.py

index b05dd81421770c223c96e67cd1742647b546cfc6..421f19e8403d7175ff9b4fc7aa9f15bebbd8003e 100644 (file)
@@ -822,3 +822,7 @@ class SyncResponse:
 
     def close(self) -> None:
         return self._loop.run_until_complete(self._response.close())
+
+    def __repr__(self) -> str:
+        class_name = self.__class__.__name__
+        return f"<{class_name}({self.status_code}, {self.reason_phrase!r})>"
index 21368ad649e477e18e84df2ebe53b45541327dd3..27602d12d6455091b4518619561224c117b3063f 100644 (file)
@@ -10,6 +10,9 @@ async def test_get(server):
         response = await client.get(url)
     assert response.status_code == 200
     assert response.text == "Hello, world!"
+    assert response.protocol == "HTTP/1.1"
+    assert response.headers
+    assert repr(response) == "<Response(200, 'OK')>"
 
 
 @pytest.mark.asyncio
@@ -65,3 +68,46 @@ async def test_raise_for_status(server):
                     response.raise_for_status()
             else:
                 assert response.raise_for_status() is None
+
+
+@pytest.mark.asyncio
+async def test_options(server):
+    url = "http://127.0.0.1:8000/"
+    async with httpcore.AsyncClient() as client:
+        response = await client.options(url)
+    assert response.status_code == 200
+    assert response.text == "Hello, world!"
+
+
+@pytest.mark.asyncio
+async def test_head(server):
+    url = "http://127.0.0.1:8000/"
+    async with httpcore.AsyncClient() as client:
+        response = await client.head(url)
+    assert response.status_code == 200
+    assert response.text == ""
+
+
+@pytest.mark.asyncio
+async def test_put(server):
+    url = "http://127.0.0.1:8000/"
+    async with httpcore.AsyncClient() as client:
+        response = await client.put(url, data=b"Hello, world!")
+    assert response.status_code == 200
+
+
+@pytest.mark.asyncio
+async def test_patch(server):
+    url = "http://127.0.0.1:8000/"
+    async with httpcore.AsyncClient() as client:
+        response = await client.patch(url, data=b"Hello, world!")
+    assert response.status_code == 200
+
+
+@pytest.mark.asyncio
+async def test_delete(server):
+    url = "http://127.0.0.1:8000/"
+    async with httpcore.AsyncClient() as client:
+        response = await client.delete(url)
+    assert response.status_code == 200
+    assert response.text == "Hello, world!"
index 29587b4bc3dc6002939a4ab5f530d48720c848ee..76f2a5bdf534fb1b297414b0d62bbd97c1b8ee3a 100644 (file)
@@ -25,14 +25,19 @@ def threadpool(func):
 
 @threadpool
 def test_get(server):
+    url = "http://127.0.0.1:8000/"
     with httpcore.Client() as http:
-        response = http.get("http://127.0.0.1:8000/")
+        response = http.get(url)
     assert response.status_code == 200
+    assert response.url == httpcore.URL(url)
     assert response.content == b"Hello, world!"
     assert response.text == "Hello, world!"
     assert response.protocol == "HTTP/1.1"
+    assert response.encoding == "iso-8859-1"
+    assert response.request.url == httpcore.URL(url)
     assert response.headers
-    assert repr(response) == "<SyncResponse(status_code=200)>"
+    assert response.is_redirect is False
+    assert repr(response) == "<SyncResponse(200, 'OK')>"
 
 
 @threadpool
@@ -75,27 +80,40 @@ def test_raw_iterator(server):
     response.close()  # TODO: should Response be available as context managers?
 
 
+@threadpool
+def test_raise_for_status(server):
+    with httpcore.Client() as client:
+        for status_code in (200, 400, 404, 500, 505):
+            response = client.request(
+                "GET", "http://127.0.0.1:8000/status/{}".format(status_code)
+            )
+
+            if 400 <= status_code < 600:
+                with pytest.raises(httpcore.exceptions.HttpError):
+                    response.raise_for_status()
+            else:
+                assert response.raise_for_status() is None
+
+
 @threadpool
 def test_options(server):
-    with httpcore.SyncClient() as http:
+    with httpcore.Client() as http:
         response = http.options("http://127.0.0.1:8000/")
-
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 @threadpool
 def test_head(server):
-    with httpcore.SyncClient() as http:
+    with httpcore.Client() as http:
         response = http.head("http://127.0.0.1:8000/")
-
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 @threadpool
 def test_put(server):
-    with httpcore.SyncClient() as http:
+    with httpcore.Client() as http:
         response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
@@ -103,7 +121,7 @@ def test_put(server):
 
 @threadpool
 def test_patch(server):
-    with httpcore.SyncClient() as http:
+    with httpcore.Client() as http:
         response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
@@ -111,7 +129,7 @@ def test_patch(server):
 
 @threadpool
 def test_delete(server):
-    with httpcore.SyncClient() as http:
+    with httpcore.Client() as http:
         response = http.delete("http://127.0.0.1:8000/")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"