userver: en/testsuite/mockserver/classes.py Source File
Loading...
Searching...
No Matches
classes.py
1import dataclasses
2import pathlib
3import socket
4import typing
5
6import aiohttp.web
7
8from testsuite import types
9from testsuite.utils import callinfo, http, url_util
10
11GenericRequestHandler = typing.Callable[
12 ...,
13 types.MaybeAsyncResult[aiohttp.web.Response],
14]
15GenericRequestDecorator = typing.Callable[
16 [GenericRequestHandler],
17 callinfo.AsyncCallQueue,
18]
19JsonRequestHandler = typing.Callable[
20 ...,
21 types.MaybeAsyncResult[
22 typing.Union[aiohttp.web.Response, types.JsonAnyOptional]
23 ],
24]
25JsonRequestDecorator = typing.Callable[
26 [JsonRequestHandler],
27 callinfo.AsyncCallQueue,
28]
29MockserverRequest = http.Request
30
31
32@dataclasses.dataclass(frozen=True)
34 cert_path: str
35 private_key_path: str
36
37
38@dataclasses.dataclass(frozen=True)
40 host: str
41 port: int
42 base_url: str
43 socket_path: pathlib.Path | None = None
44 https: bool = False
45
46 def url(self, path: str) -> str:
47 """Concats ``base_url`` and provided ``path``."""
48 return url_util.join(self.base_url, path)
49
50 def get_host_header(self) -> str:
51 if self.socket_path:
52 return str(self.socket_path)
53
54 if not self.host and not self.port:
55 raise RuntimeError(
56 'either host and port or socket_path must be set in mockserver info'
57 )
58
59 if self.port == 80:
60 return str(self.host)
61 return f'{self.host}:{self.port}'
62
63 def ws_url(self, path: str) -> str:
64 schema = 'wss' if self.https else 'ws'
65 return url_util.join(f'{schema}://{self.host}:{self.port}/', path)
66
67
68@dataclasses.dataclass(frozen=True)
70 info: MockserverInfo
71 sockets: list[socket.socket]
72
73
74@dataclasses.dataclass(frozen=True)
76 nofail: bool = False
77 debug: bool = False
78 tracing_enabled: bool = False
79 trace_id_header: str = ''
80 span_id_header: str = ''
81 http_proxy_enabled: bool = False
82
83
84MockserverInfoFixture = MockserverInfo
85MockserverSslInfoFixture = typing.Optional[MockserverInfo]