Add to tests
This commit is contained in:
parent
c4c39cb082
commit
19f642b364
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from enum import IntEnum, auto
|
from enum import IntEnum, auto
|
||||||
from itertools import count
|
from itertools import count
|
||||||
from typing import Deque, Optional, Sequence, Union
|
from typing import Deque, Sequence, Union
|
||||||
|
|
||||||
from sanic.models.handler_types import MiddlewareType
|
from sanic.models.handler_types import MiddlewareType
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ class Middleware:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
func: MiddlewareType,
|
func: MiddlewareType,
|
||||||
location: MiddlewareLocation,
|
location: MiddlewareLocation = MiddlewareLocation.REQUEST,
|
||||||
priority: int = 0,
|
priority: int = 0,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.func = func
|
self.func = func
|
||||||
|
|
|
@ -2,8 +2,10 @@ import logging
|
||||||
|
|
||||||
from asyncio import CancelledError
|
from asyncio import CancelledError
|
||||||
from itertools import count
|
from itertools import count
|
||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
from sanic.exceptions import NotFound
|
from sanic.exceptions import NotFound
|
||||||
|
from sanic.middleware import Middleware, MiddlewareLocation
|
||||||
from sanic.request import Request
|
from sanic.request import Request
|
||||||
from sanic.response import HTTPResponse, json, text
|
from sanic.response import HTTPResponse, json, text
|
||||||
|
|
||||||
|
@ -318,6 +320,15 @@ def test_middleware_return_response(app):
|
||||||
resp1 = await request.respond()
|
resp1 = await request.respond()
|
||||||
return resp1
|
return resp1
|
||||||
|
|
||||||
_, response = app.test_client.get("/")
|
app.test_client.get("/")
|
||||||
assert response_middleware_run_count == 1
|
assert response_middleware_run_count == 1
|
||||||
assert request_middleware_run_count == 1
|
assert request_middleware_run_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_middleware_object():
|
||||||
|
mock = Mock()
|
||||||
|
middleware = Middleware(mock)
|
||||||
|
middleware(1, 2, 3, answer=42)
|
||||||
|
|
||||||
|
mock.assert_called_once_with(1, 2, 3, answer=42)
|
||||||
|
assert middleware.order == (0, 0)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user