Merge pull request #56 from yeraydiazdiaz/test-additional-http-methods
Additional tests for SyncClient
This commit is contained in:
commit
87cab4c5a4
@ -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})>"
|
||||
|
||||
@ -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!"
|
||||
@ -25,11 +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 response.is_redirect is False
|
||||
assert repr(response) == "<SyncResponse(200, 'OK')>"
|
||||
|
||||
|
||||
@threadpool
|
||||
@ -69,3 +77,59 @@ def test_raw_iterator(server):
|
||||
for chunk in response.raw():
|
||||
body += chunk
|
||||
assert body == b"Hello, world!"
|
||||
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.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.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.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"
|
||||
|
||||
|
||||
@threadpool
|
||||
def test_patch(server):
|
||||
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"
|
||||
|
||||
|
||||
@threadpool
|
||||
def test_delete(server):
|
||||
with httpcore.Client() as http:
|
||||
response = http.delete("http://127.0.0.1:8000/")
|
||||
assert response.status_code == 200
|
||||
assert response.reason_phrase == "OK"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user