From 746b863c4e4ea920484ca95f366c8af2f422ec78 Mon Sep 17 00:00:00 2001 From: Jonas Lundberg Date: Wed, 14 Oct 2020 10:53:44 +0200 Subject: [PATCH] Expose text, html and json in highlevel api --- respx/api.py | 113 ++++++++++++++++++++++++++++++------------- respx/transports.py | 114 ++++++++++++++++++++++++++++++++------------ tests/test_api.py | 5 +- 3 files changed, 166 insertions(+), 66 deletions(-) diff --git a/respx/api.py b/respx/api.py index 1aa5849..8da5660 100644 --- a/respx/api.py +++ b/respx/api.py @@ -6,6 +6,7 @@ ContentDataTypes, DefaultType, HeaderTypes, + JSONTypes, QueryParamTypes, RequestPattern, ) @@ -58,9 +59,12 @@ def add( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -70,9 +74,12 @@ def add( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -83,9 +90,12 @@ def get( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -94,9 +104,12 @@ def get( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -107,9 +120,12 @@ def post( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -118,9 +134,12 @@ def post( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -131,9 +150,12 @@ def put( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -142,9 +164,12 @@ def put( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -155,9 +180,12 @@ def patch( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -166,9 +194,12 @@ def patch( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -179,9 +210,12 @@ def delete( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -190,9 +224,12 @@ def delete( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -203,9 +240,12 @@ def head( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -214,9 +254,12 @@ def head( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -227,9 +270,12 @@ def options( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -238,9 +284,12 @@ def options( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) diff --git a/respx/transports.py b/respx/transports.py index 41e202f..38b6d2e 100644 --- a/respx/transports.py +++ b/respx/transports.py @@ -16,6 +16,7 @@ DefaultType, Headers, HeaderTypes, + JSONTypes, QueryParamTypes, Request, RequestPattern, @@ -94,9 +95,12 @@ def add( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -108,7 +112,13 @@ def add( else: response = ResponseTemplate( - status_code, headers=headers, content=content, content_type=content_type + status_code, + headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, ) pattern = RequestPattern( method, @@ -132,9 +142,12 @@ def get( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -143,9 +156,12 @@ def get( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -156,9 +172,12 @@ def post( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -167,9 +186,12 @@ def post( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -180,9 +202,12 @@ def put( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -191,9 +216,12 @@ def put( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -204,9 +232,12 @@ def patch( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -215,9 +246,12 @@ def patch( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -228,9 +262,12 @@ def delete( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -239,9 +276,12 @@ def delete( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -252,9 +292,12 @@ def head( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -263,9 +306,12 @@ def head( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) @@ -276,9 +322,12 @@ def options( *, params: Optional[QueryParamTypes] = None, status_code: Optional[int] = None, - content: Optional[ContentDataTypes] = None, - content_type: Optional[str] = None, headers: Optional[HeaderTypes] = None, + content_type: Optional[str] = None, + content: Optional[ContentDataTypes] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[JSONTypes] = None, pass_through: bool = False, alias: Optional[str] = None, ) -> RequestPattern: @@ -287,9 +336,12 @@ def options( url=url, params=params, status_code=status_code, - content=content, - content_type=content_type, headers=headers, + content_type=content_type, + content=content, + text=text, + html=html, + json=json, pass_through=pass_through, alias=alias, ) diff --git a/tests/test_api.py b/tests/test_api.py index b771ae0..fed7fb2 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -10,7 +10,7 @@ import respx from respx import MockTransport -from respx.models import RequestPattern, ResponseTemplate +from respx.models import RequestPattern @pytest.mark.asyncio @@ -190,8 +190,7 @@ async def test_text_encoding(client, content, expected): async def test_content_variants(client, key, value, expected_content_type): async with MockTransport() as respx_mock: url = "https://foo.bar/" - pattern = RequestPattern("GET", url, response=ResponseTemplate(**{key: value})) - request = respx_mock.add(pattern) + request = respx_mock.get(url, **{key: value}) async_response = await client.get(url) assert request.called is True