Skip to content

Commit

Permalink
better place for test case
Browse files Browse the repository at this point in the history
  • Loading branch information
vvanglro committed Aug 19, 2024
1 parent 3734d31 commit b7a1b27
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
17 changes: 0 additions & 17 deletions tests/protocols/test_http.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
from __future__ import annotations

import asyncio
import logging
import socket
import threading
import time
from typing import TYPE_CHECKING, Any

import httpx
import pytest

from tests.response import Response
from tests.test_default_headers import app
from tests.utils import run_server
from uvicorn import Server
from uvicorn._types import ASGIApplication, ASGIReceiveCallable, ASGISendCallable, Scope
from uvicorn.config import WS_PROTOCOLS, Config
Expand Down Expand Up @@ -1098,19 +1094,6 @@ async def app(scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable
assert not expected_states # consumed


async def test_request_than_limit_max_requests_warn_log(
unused_tcp_port: int, http_protocol_cls: HTTPProtocol, caplog: pytest.LogCaptureFixture
):
caplog.set_level(logging.WARNING, logger="uvicorn.error")
config = Config(app=app, limit_max_requests=1, port=unused_tcp_port, http=http_protocol_cls)
async with run_server(config):
async with httpx.AsyncClient() as client:
tasks = [client.get(f"http://127.0.0.1:{unused_tcp_port}") for _ in range(3)]
responses = await asyncio.gather(*tasks)
assert len(responses) == 3
assert "Exceeded max request limit ending process." in caplog.text


async def test_header_upgrade_is_not_websocket_depend_installed(
caplog: pytest.LogCaptureFixture, http_protocol_cls: HTTPProtocol
):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
import configparser
import io
import json
Expand All @@ -12,10 +13,14 @@
from typing import Any, Literal
from unittest.mock import MagicMock

import httpx
import pytest
import yaml
from pytest_mock import MockerFixture
from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol

from tests.test_default_headers import app
from tests.utils import run_server
from tests.utils import as_cwd
from uvicorn._types import (
ASGIApplication,
Expand All @@ -30,6 +35,8 @@
from uvicorn.middleware.wsgi import WSGIMiddleware
from uvicorn.protocols.http.h11_impl import H11Protocol

pytestmark = pytest.mark.anyio


@pytest.fixture
def mocked_logging_config_module(mocker: MockerFixture) -> MagicMock:
Expand Down Expand Up @@ -545,3 +552,16 @@ def test_warn_when_using_reload_and_workers(caplog: pytest.LogCaptureFixture) ->
Config(app=asgi_app, reload=True, workers=2)
assert len(caplog.records) == 1
assert '"workers" flag is ignored when reloading is enabled.' in caplog.records[0].message


async def test_request_than_limit_max_requests_warn_log(
unused_tcp_port: int, http_protocol_cls: type[H11Protocol | HttpToolsProtocol], caplog: pytest.LogCaptureFixture
):
caplog.set_level(logging.WARNING, logger="uvicorn.error")
config = Config(app=app, limit_max_requests=1, port=unused_tcp_port, http=http_protocol_cls)
async with run_server(config):
async with httpx.AsyncClient() as client:
tasks = [client.get(f"http://127.0.0.1:{unused_tcp_port}") for _ in range(2)]
responses = await asyncio.gather(*tasks)
assert len(responses) == 2
assert f"Maximum request limit of {config.limit_max_requests} exceeded. Terminating process." in caplog.text

0 comments on commit b7a1b27

Please sign in to comment.