Debian Codegraph @

tests/unit/test_handlers/test_http_handlers/test_kwarg_handling.py

raw symbols SCIP
1 from typing import Any, Optional, Type
2
3 import pytest
4 from hypothesis import given
5 from hypothesis import strategies as st
6
7 from litestar import HttpMethod, MediaType, Response, delete, get, patch, post, put
8 from litestar.exceptions import ImproperlyConfiguredException
9 from litestar.handlers.http_handlers import HTTPRouteHandler
10 from litestar.handlers.http_handlers._utils import get_default_status_code
11 from litestar.status_codes import HTTP_200_OK, HTTP_201_CREATED, HTTP_204_NO_CONTENT
12 from litestar.utils import normalize_path
13
14
15 def dummy_method() -> None:
16 pass
17
18
19 @given(
20 http_method=st.one_of(st.sampled_from(HttpMethod), st.lists(st.sampled_from(HttpMethod))),
21 media_type=st.sampled_from(MediaType),
22 include_in_schema=st.booleans(),
23 response_class=st.one_of(st.none(), st.just(Response)),
24 response_headers=st.one_of(st.none(), st.builds(list)),
25 status_code=st.one_of(st.none(), st.integers(min_value=200, max_value=204)),
26 path=st.one_of(st.none(), st.text()),
27 )
28 def test_route_handler_kwarg_handling(
29 http_method: Any,
30 media_type: MediaType,
31 include_in_schema: bool,
32 response_class: Optional[Type[Response]],
33 response_headers: Any,
34 status_code: Any,
35 path: Any,
36 ) -> None:
37 if not http_method:
38 with pytest.raises(ImproperlyConfiguredException):
39 HTTPRouteHandler(http_method=http_method)
40 else:
41 decorator = HTTPRouteHandler(
42 http_method=http_method,
43 media_type=media_type,
44 include_in_schema=include_in_schema,
45 response_class=response_class,
46 response_headers=response_headers,
47 status_code=status_code,
48 path=path,
49 )
50 result = decorator(dummy_method)
51 if isinstance(http_method, list):
52 assert all(method in result.http_methods for method in http_method)
53 else:
54 assert http_method in result.http_methods
55 assert result.media_type == media_type
56 assert result.include_in_schema == include_in_schema
57 assert result.response_class == response_class
58 assert result.response_headers == response_headers
59 if not path:
60 assert result.paths == {"/"}
61 else:
62 assert next(iter(result.paths)) == normalize_path(path)
63 assert result.status_code == status_code or get_default_status_code(http_methods=result.http_methods)
64
65
66 @pytest.mark.parametrize(
67 "sub, http_method, expected_status_code",
68 [
69 (post, HttpMethod.POST, HTTP_201_CREATED),
70 (delete, HttpMethod.DELETE, HTTP_204_NO_CONTENT),
71 (get, HttpMethod.GET, HTTP_200_OK),
72 (put, HttpMethod.PUT, HTTP_200_OK),
73 (patch, HttpMethod.PATCH, HTTP_200_OK),
74 ],
75 )
76 def test_semantic_route_handlers_disallow_http_method_assignment(
77 sub: Any, http_method: Any, expected_status_code: int
78 ) -> None:
79 result = sub()(dummy_method)
80 assert http_method in result.http_methods
81 assert result.status_code == expected_status_code
82
83 with pytest.raises(ImproperlyConfiguredException):
84 sub(http_method=HttpMethod.GET if http_method != HttpMethod.GET else HttpMethod.POST)