Skip to content

Commit 6be8f3e

Browse files
committed
add tests for HttpMessageHandler
1 parent 2b44645 commit 6be8f3e

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

martin_eden/base.py

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def __call__(
1616
) -> None:
1717
pass
1818

19+
def __await__(self):
20+
pass
21+
1922

2023
class CustomSchema(Schema):
2124
def __init__(

poetry.lock

+19-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ marshmallow-enum = "^1.5.1"
2525
ruff = "^0.0.287"
2626
pytest = "^7.4.3"
2727
coverage = "^7.3.2"
28+
pytest-asyncio = "^0.21.1"
2829

2930
[build-system]
3031
requires = ["poetry-core"]

tests/test_http_message_handler.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import pytest
2+
from martin_eden.routing import ControllerDefinitionError, get_controller, register_route
3+
import json
4+
from martin_eden.openapi import OpenApiBuilder
5+
import asyncio
6+
from martin_eden.core import HttpMessageHandler
7+
8+
pytest_plugins = ('pytest_asyncio',)
9+
10+
11+
@register_route('/test/', 'get')
12+
async def get_openapi_schema() -> str:
13+
return 'test'
14+
15+
16+
@pytest.fixture
17+
def http_get_request():
18+
return (
19+
b'GET /users/ HTTP/1.1\n'
20+
b'Accept: text/html,application/xhtml+xml, '
21+
b'application/xml;q=0.9,image/avif,image/webp, '
22+
b'image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\n'
23+
b'Accept-Encoding: gzip, deflate, br\n'
24+
b'Accept-Language: en-US,en;q=0.9,ru;q=0.8,ru-RU;q=0.7\n'
25+
b'Cache-Control: no-cache\n'
26+
b'Connection: keep-alive\n'
27+
b'Cookie: token=a0966813f9b27b2a545c75966fd87815660787a3; '
28+
b'person_pk=1; session=bc8145dc-7e09-46f5-b5fa-789863559f3a. '
29+
b'EetDaRq0uVnaC8vMKj2YhEYL5Fs; '
30+
b'csrftoken=njiUhm1sab5u2zOuaa9oHluQay6HOy11c'
31+
b'J5so09np8mFEF78rk2gzbF7QgsGr8Ox; '
32+
b'sessionid=vva6mb5l3ugbzxljtorsjl449xn2cco0\n'
33+
b'Host: localhost:8001\n'
34+
b'Pragma: no-cache\n'
35+
b'Sec-Fetch-Dest: document\n'
36+
b'Sec-Fetch-Mode: navigate\n'
37+
b'Sec-Fetch-Site: none\n'
38+
b'Sec-Fetch-User: ?1\n'
39+
b'Upgrade-Insecure-Requests: 1\n'
40+
b'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '
41+
b'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36\n'
42+
b'sec-ch-ua: "Google Chrome";v="119", "Chromium";v="119", '
43+
b'"Not?A_Brand";v="24"\n'
44+
b'sec-ch-ua-mobile: ?0\n'
45+
b'sec-ch-ua-platform: "macOS"\n'
46+
)
47+
48+
49+
@pytest.mark.asyncio
50+
async def test_not_existing_url(http_get_request):
51+
handler = HttpMessageHandler(http_get_request)
52+
response = await handler.handle_request()
53+
assert response == (
54+
b'HTTP/1.0 200\n'
55+
b'Access-Control-Allow-Origin: *\n'
56+
b'Access-Control-Allow-Methods: POST, GET, OPTIONS\n'
57+
b'Access-Control-Allow-Headers: origin, content-type, accept\n'
58+
b'Access-Control-Allow-Credentials: true\n'
59+
b'Access-Control-Max-Age: 86400\n'
60+
b'Content-Type: application/json;charset=UTF-8\n\n'
61+
b'404 not found'
62+
)
63+
64+
65+
@pytest.mark.asyncio
66+
async def test_existing_url(http_get_request):
67+
http_get_request = http_get_request.replace(b'/users/', b'/test/')
68+
handler = HttpMessageHandler(http_get_request)
69+
response = await handler.handle_request()
70+
assert response == (
71+
b'HTTP/1.0 200\n'
72+
b'Access-Control-Allow-Origin: *\n'
73+
b'Access-Control-Allow-Methods: POST, GET, OPTIONS\n'
74+
b'Access-Control-Allow-Headers: origin, content-type, accept\n'
75+
b'Access-Control-Allow-Credentials: true\n'
76+
b'Access-Control-Max-Age: 86400\n'
77+
b'Content-Type: application/json;charset=UTF-8\n\n'
78+
b'test'
79+
)
80+

0 commit comments

Comments
 (0)