From 19f642b3649e610162b69a3ae03a9cf392d15ef3 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 15 Sep 2022 18:46:09 +0300 Subject: [PATCH] Add to tests --- sanic/middleware.py | 4 ++-- tests/test_middleware.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/sanic/middleware.py b/sanic/middleware.py index 1304531d..377bb9c0 100644 --- a/sanic/middleware.py +++ b/sanic/middleware.py @@ -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 diff --git a/tests/test_middleware.py b/tests/test_middleware.py index 2163e47c..d57ebe5a 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -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)