|
1 | 1 | import asyncio
|
2 | 2 | import socket
|
3 | 3 | from contextlib import suppress
|
| 4 | +from typing import NoReturn |
4 | 5 | from unittest import mock
|
5 | 6 |
|
6 | 7 | import pytest
|
7 | 8 |
|
8 | 9 | from aiohttp import client, web
|
| 10 | +from aiohttp.http_exceptions import BadHttpMethod, BadStatusLine |
| 11 | +from aiohttp.pytest_plugin import AiohttpClient, AiohttpRawServer |
9 | 12 |
|
10 | 13 |
|
11 | 14 | async def test_simple_server(aiohttp_raw_server, aiohttp_client) -> None:
|
@@ -56,7 +59,125 @@ async def handler(request):
|
56 | 59 | logger.exception.assert_called_with("Error handling request", exc_info=exc)
|
57 | 60 |
|
58 | 61 |
|
59 |
| -async def test_raw_server_handler_timeout(aiohttp_raw_server, aiohttp_client) -> None: |
| 62 | +async def test_raw_server_logs_invalid_method_with_loop_debug( |
| 63 | + aiohttp_raw_server: AiohttpRawServer, |
| 64 | + aiohttp_client: AiohttpClient, |
| 65 | + loop: asyncio.AbstractEventLoop, |
| 66 | +) -> None: |
| 67 | + exc = BadHttpMethod(b"\x16\x03\x03\x01F\x01".decode(), "error") |
| 68 | + |
| 69 | + async def handler(request: web.BaseRequest) -> NoReturn: |
| 70 | + raise exc |
| 71 | + |
| 72 | + loop = asyncio.get_event_loop() |
| 73 | + loop.set_debug(True) |
| 74 | + logger = mock.Mock() |
| 75 | + server = await aiohttp_raw_server(handler, logger=logger) |
| 76 | + cli = await aiohttp_client(server) |
| 77 | + resp = await cli.get("/path/to") |
| 78 | + assert resp.status == 500 |
| 79 | + assert resp.headers["Content-Type"].startswith("text/plain") |
| 80 | + |
| 81 | + txt = await resp.text() |
| 82 | + assert "Traceback (most recent call last):\n" in txt |
| 83 | + |
| 84 | + # BadHttpMethod should be logged as debug |
| 85 | + # on the first request since the client may |
| 86 | + # be probing for TLS/SSL support which is |
| 87 | + # expected to fail |
| 88 | + logger.debug.assert_called_with("Error handling request", exc_info=exc) |
| 89 | + |
| 90 | + |
| 91 | +async def test_raw_server_logs_invalid_method_without_loop_debug( |
| 92 | + aiohttp_raw_server: AiohttpRawServer, |
| 93 | + aiohttp_client: AiohttpClient, |
| 94 | + loop: asyncio.AbstractEventLoop, |
| 95 | +) -> None: |
| 96 | + exc = BadHttpMethod(b"\x16\x03\x03\x01F\x01".decode(), "error") |
| 97 | + |
| 98 | + async def handler(request: web.BaseRequest) -> NoReturn: |
| 99 | + raise exc |
| 100 | + |
| 101 | + loop = asyncio.get_event_loop() |
| 102 | + loop.set_debug(False) |
| 103 | + logger = mock.Mock() |
| 104 | + server = await aiohttp_raw_server(handler, logger=logger, debug=False) |
| 105 | + cli = await aiohttp_client(server) |
| 106 | + resp = await cli.get("/path/to") |
| 107 | + assert resp.status == 500 |
| 108 | + assert resp.headers["Content-Type"].startswith("text/plain") |
| 109 | + |
| 110 | + txt = await resp.text() |
| 111 | + assert "Traceback (most recent call last):\n" not in txt |
| 112 | + |
| 113 | + # BadHttpMethod should be logged as debug |
| 114 | + # on the first request since the client may |
| 115 | + # be probing for TLS/SSL support which is |
| 116 | + # expected to fail |
| 117 | + logger.debug.assert_called_with("Error handling request", exc_info=exc) |
| 118 | + |
| 119 | + |
| 120 | +async def test_raw_server_logs_invalid_method_second_request( |
| 121 | + aiohttp_raw_server: AiohttpRawServer, |
| 122 | + aiohttp_client: AiohttpClient, |
| 123 | + loop: asyncio.AbstractEventLoop, |
| 124 | +) -> None: |
| 125 | + exc = BadHttpMethod(b"\x16\x03\x03\x01F\x01".decode(), "error") |
| 126 | + request_count = 0 |
| 127 | + |
| 128 | + async def handler(request: web.BaseRequest) -> web.Response: |
| 129 | + nonlocal request_count |
| 130 | + request_count += 1 |
| 131 | + if request_count == 2: |
| 132 | + raise exc |
| 133 | + return web.Response() |
| 134 | + |
| 135 | + loop = asyncio.get_event_loop() |
| 136 | + loop.set_debug(False) |
| 137 | + logger = mock.Mock() |
| 138 | + server = await aiohttp_raw_server(handler, logger=logger) |
| 139 | + cli = await aiohttp_client(server) |
| 140 | + resp = await cli.get("/path/to") |
| 141 | + assert resp.status == 200 |
| 142 | + resp = await cli.get("/path/to") |
| 143 | + assert resp.status == 500 |
| 144 | + assert resp.headers["Content-Type"].startswith("text/plain") |
| 145 | + # BadHttpMethod should be logged as an exception |
| 146 | + # if its not the first request since we know |
| 147 | + # that the client already was speaking HTTP |
| 148 | + logger.exception.assert_called_with("Error handling request", exc_info=exc) |
| 149 | + |
| 150 | + |
| 151 | +async def test_raw_server_logs_bad_status_line_as_exception( |
| 152 | + aiohttp_raw_server: AiohttpRawServer, |
| 153 | + aiohttp_client: AiohttpClient, |
| 154 | + loop: asyncio.AbstractEventLoop, |
| 155 | +) -> None: |
| 156 | + exc = BadStatusLine(b"\x16\x03\x03\x01F\x01".decode(), "error") |
| 157 | + |
| 158 | + async def handler(request: web.BaseRequest) -> NoReturn: |
| 159 | + raise exc |
| 160 | + |
| 161 | + loop = asyncio.get_event_loop() |
| 162 | + loop.set_debug(False) |
| 163 | + logger = mock.Mock() |
| 164 | + server = await aiohttp_raw_server(handler, logger=logger, debug=False) |
| 165 | + cli = await aiohttp_client(server) |
| 166 | + resp = await cli.get("/path/to") |
| 167 | + assert resp.status == 500 |
| 168 | + assert resp.headers["Content-Type"].startswith("text/plain") |
| 169 | + |
| 170 | + txt = await resp.text() |
| 171 | + assert "Traceback (most recent call last):\n" not in txt |
| 172 | + |
| 173 | + logger.exception.assert_called_with("Error handling request", exc_info=exc) |
| 174 | + |
| 175 | + |
| 176 | +async def test_raw_server_handler_timeout( |
| 177 | + aiohttp_raw_server: AiohttpRawServer, aiohttp_client: AiohttpClient |
| 178 | +) -> None: |
| 179 | + loop = asyncio.get_event_loop() |
| 180 | + loop.set_debug(True) |
60 | 181 | exc = asyncio.TimeoutError("error")
|
61 | 182 |
|
62 | 183 | async def handler(request):
|
|
0 commit comments