|
| 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