Module spangle.testing¶
Test client for ASGI app without ASGI server.
Classes¶
AsyncHttpTestClient¶
class AsyncHttpTestClient(
self,
app: ASGIApp,
timeout: Union[int, float, None] = 1,
host="www.example.com",
client=("127.0.0.1", 123),)
Mock HTTP client without running server. Lifespan-event is supported by
async with
statement.
Args
- app (
ASGIApp
): Application instance. - timeout (
Optional[int]
): Timeout seconds. - host (
str
): Temporary host name. - client (
tuple[str, int]
): Client address.
Base classes¶
spangle.testing._BaseClient
Methods¶
async def delete(
self,
path: str,
queries: Optional[Params] = None,
headers: Optional[Headers] = None,
cookies: Optional[dict[str, str]] = None,
timeout: Optional[float] = None,
allow_redirects=True,
) -> HttpTestResponse
Send DELETE
request to app
. See
spangle.testing.AsyncHttpTestClient.request
.
async def get(
self,
path: str,
queries: Optional[Params] = None,
headers: Optional[Headers] = None,
cookies: Optional[dict[str, str]] = None,
timeout: Optional[float] = None,
allow_redirects=True,
) -> HttpTestResponse
Send GET
request to app
. See
spangle.testing.AsyncHttpTestClient.request
.
async def patch(
self,
path: str,
queries: Optional[Params] = None,
headers: Optional[Headers] = None,
cookies: Optional[dict[str, str]] = None,
json: Optional[Mapping] = None,
files: Optional[Mapping] = None,
form: Optional[Mapping] = None,
content: Optional[bytes] = None,
timeout: Optional[float] = None,
allow_redirects=True,
) -> HttpTestResponse
Send PATCH
request to app
. See
spangle.testing.AsyncHttpTestClient.request
.
async def post(
self,
path: str,
queries: Optional[Params] = None,
headers: Optional[Headers] = None,
cookies: Optional[dict[str, str]] = None,
json: Optional[Mapping] = None,
files: Optional[Mapping] = None,
form: Optional[Mapping] = None,
content: Optional[bytes] = None,
timeout: Optional[float] = None,
allow_redirects=True,
) -> HttpTestResponse
Send POST
request to app
. See
spangle.testing.AsyncHttpTestClient.request
.
async def put(
self,
path: str,
queries: Optional[Params] = None,
headers: Optional[Headers] = None,
cookies: Optional[dict[str, str]] = None,
json: Optional[Mapping] = None,
files: Optional[Mapping] = None,
form: Optional[Mapping] = None,
content: Optional[bytes] = None,
timeout: Optional[float] = None,
allow_redirects=True,
) -> HttpTestResponse
Send PUT
request to app
. See
spangle.testing.AsyncHttpTestClient.request
.
async def request(
self,
method: str,
path: str,
queries: Optional[Params] = None,
headers: Optional[Headers] = None,
cookies: Optional[dict[str, str]] = None,
json: Optional[Mapping] = None,
files: Optional[Mapping] = None,
form: Optional[Mapping] = None,
content: Optional[bytes] = None,
timeout: Optional[float] = None,
allow_redirects=True,
) -> HttpTestResponse
Send request to app
.
Args
- method (
str
): HTTP request method. - path (
str
): Requesting location. - queries (
Params
): Querystring asdict
orlist
of(name, value)
. - headers (
Headers
): HTTP headers. - cookies (
dict[str, str]
): Sending HTTP cookies. - json (
Mapping
): Request body as json. - files (
Mapping
): Multipart form. - form (
Mapping
): URL encoded form. - content (
bytes
): Request body as bytes. - timeout (
int
): Wait limits. - allow_redirects (
bool
): IfFalse
, a client gets30X
response instead of redirection.
Returns
def ws_connect(
self,
path: str,
subprotocols: Optional[list[str]] = None,
queries: Optional[Params] = None,
headers: Optional[Headers] = None,
cookies: Optional[Mapping] = None,
timeout: Optional[float] = None,
) -> AsyncWebsocketClient
Create asynchronous WebSocket Connection.
AsyncWebsocketClient¶
class AsyncWebsocketClient(
self,
http: "AsyncHttpTestClient",
path: str = "",
headers: Optional[MutableHeaders] = None,
queries: Optional[Params] = None,
cookies: Optional[Mapping] = None,
timeout: Optional[float] = None,)
Asynchronous WebSocket test client. It is expected to be called from
AsyncHttpTestClient
.
Attributes
- host (
str
): Dummy domain. - path (
str
): WebSocket endpoint. - headers (
MutableHeaders
): Headers used to connect. - queries (
QueryParams
): Parsed querystrings. - timeout (
Optional[int]
): How long test client waits for.
Do not use manually.
Base classes¶
spangle.testing._BaseWebSocket
Methods¶
async def close(self, status_code=1000)
Close the connection.
Args
- status_code (
int
): WebSocket status code.
async def connect(self, path: str = None)
Emulate WebSocket Connection.
Args
- path (
Optional[str]
): Overwriteself.path
.
async def receive(self, mode: type[AnyStr]) -> AnyStr
Receive data from the endpoint.
Args
- mode (
type[AnyStr]
): Receiving type,str
orbytes
.
Returns
AnyStr
: Data with specified type.
async def send(self, data: AnyStr)
Send data to the endpoint.
Args
- data (
AnyStr
): Data sent to the endpoint, must bestr
orbytes
.
HttpTestResponse¶
class HttpTestResponse(self, resp: Response)
Response for testing.
Attributes
- status_code (
int
):HTTPStatus
if available, or justint
.
Do not use manually.
Instance attributes¶
-
content (
Optional[bytes]
): Response body, asbytes
. -
cookies (
Cookies
): Dict-like response cookies. -
headers (
Headers
): Response header, asdict
. -
json (
addict.Dict
): Json response. Dot access available, likeresp.json.what.you.want
. -
text (
Optional[str]
): Response body, as UTF-8 text.