Add to tests

This commit is contained in:
Adam Hopkins 2022-09-15 18:46:09 +03:00
parent c4c39cb082
commit 19f642b364
No known key found for this signature in database
GPG Key ID: 9F85EE6C807303FB
2 changed files with 14 additions and 3 deletions

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from collections import deque
from enum import IntEnum, auto
from itertools import count
from typing import Deque, Optional, Sequence, Union
from typing import Deque, Sequence, Union
from sanic.models.handler_types import MiddlewareType
@ -21,7 +21,7 @@ class Middleware:
def __init__(
self,
func: MiddlewareType,
location: MiddlewareLocation,
location: MiddlewareLocation = MiddlewareLocation.REQUEST,
priority: int = 0,
) -> None:
self.func = func

View File

@ -2,8 +2,10 @@ import logging
from asyncio import CancelledError
from itertools import count
from unittest.mock import Mock
from sanic.exceptions import NotFound
from sanic.middleware import Middleware, MiddlewareLocation
from sanic.request import Request
from sanic.response import HTTPResponse, json, text
@ -318,6 +320,15 @@ def test_middleware_return_response(app):
resp1 = await request.respond()
return resp1
_, response = app.test_client.get("/")
app.test_client.get("/")
assert response_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)