respx2 2.0.0a1__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- respx2/__init__.py +56 -0
- respx2/__version__.py +1 -0
- respx2/api.py +116 -0
- respx2/fixtures.py +12 -0
- respx2/handlers.py +41 -0
- respx2/mocks.py +341 -0
- respx2/models.py +552 -0
- respx2/patterns.py +782 -0
- respx2/plugin.py +30 -0
- respx2/py.typed +0 -0
- respx2/router.py +490 -0
- respx2/transports.py +93 -0
- respx2/types.py +71 -0
- respx2/utils.py +157 -0
- respx2-2.0.0a1.dist-info/METADATA +109 -0
- respx2-2.0.0a1.dist-info/RECORD +20 -0
- respx2-2.0.0a1.dist-info/WHEEL +6 -0
- respx2-2.0.0a1.dist-info/entry_points.txt +2 -0
- respx2-2.0.0a1.dist-info/licenses/LICENSE.md +29 -0
- respx2-2.0.0a1.dist-info/top_level.txt +1 -0
respx2/__init__.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from .__version__ import __version__
|
|
2
|
+
from .handlers import ASGIHandler, WSGIHandler
|
|
3
|
+
from .models import MockResponse, Route
|
|
4
|
+
from .router import MockRouter, Router
|
|
5
|
+
from .utils import SetCookie
|
|
6
|
+
|
|
7
|
+
from .api import ( # isort:skip
|
|
8
|
+
mock,
|
|
9
|
+
routes,
|
|
10
|
+
calls,
|
|
11
|
+
start,
|
|
12
|
+
stop,
|
|
13
|
+
clear,
|
|
14
|
+
reset,
|
|
15
|
+
pop,
|
|
16
|
+
route,
|
|
17
|
+
add,
|
|
18
|
+
request,
|
|
19
|
+
get,
|
|
20
|
+
post,
|
|
21
|
+
put,
|
|
22
|
+
patch,
|
|
23
|
+
delete,
|
|
24
|
+
head,
|
|
25
|
+
options,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"__version__",
|
|
31
|
+
"MockResponse",
|
|
32
|
+
"MockRouter",
|
|
33
|
+
"ASGIHandler",
|
|
34
|
+
"WSGIHandler",
|
|
35
|
+
"Router",
|
|
36
|
+
"Route",
|
|
37
|
+
"SetCookie",
|
|
38
|
+
"mock",
|
|
39
|
+
"routes",
|
|
40
|
+
"calls",
|
|
41
|
+
"start",
|
|
42
|
+
"stop",
|
|
43
|
+
"clear",
|
|
44
|
+
"reset",
|
|
45
|
+
"pop",
|
|
46
|
+
"route",
|
|
47
|
+
"add",
|
|
48
|
+
"request",
|
|
49
|
+
"get",
|
|
50
|
+
"post",
|
|
51
|
+
"put",
|
|
52
|
+
"patch",
|
|
53
|
+
"delete",
|
|
54
|
+
"head",
|
|
55
|
+
"options",
|
|
56
|
+
]
|
respx2/__version__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "2.0.0a1"
|
respx2/api.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
from typing import Any, Optional, Union, overload
|
|
2
|
+
|
|
3
|
+
from .models import CallList, Route
|
|
4
|
+
from .patterns import Pattern
|
|
5
|
+
from .router import MockRouter
|
|
6
|
+
from .types import DefaultType, URLPatternTypes
|
|
7
|
+
|
|
8
|
+
mock = MockRouter(assert_all_called=False)
|
|
9
|
+
|
|
10
|
+
routes = mock.routes
|
|
11
|
+
calls: CallList = mock.calls
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def start() -> None:
|
|
15
|
+
global mock
|
|
16
|
+
mock.start()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def stop(clear: bool = True, reset: bool = True) -> None:
|
|
20
|
+
global mock
|
|
21
|
+
mock.stop(clear=clear, reset=reset)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def clear() -> None:
|
|
25
|
+
global mock
|
|
26
|
+
mock.clear()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def reset() -> None:
|
|
30
|
+
global mock
|
|
31
|
+
mock.reset()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@overload
|
|
35
|
+
def pop(name: str) -> Route:
|
|
36
|
+
... # pragma: nocover
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@overload
|
|
40
|
+
def pop(name: str, default: DefaultType) -> Union[Route, DefaultType]:
|
|
41
|
+
... # pragma: nocover
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def pop(name, default=...):
|
|
45
|
+
global mock
|
|
46
|
+
return mock.pop(name, default=default)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def route(*patterns: Pattern, name: Optional[str] = None, **lookups: Any) -> Route:
|
|
50
|
+
global mock
|
|
51
|
+
return mock.route(*patterns, name=name, **lookups)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def add(route: Route, *, name: Optional[str] = None) -> Route:
|
|
55
|
+
global mock
|
|
56
|
+
return mock.add(route, name=name)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def request(
|
|
60
|
+
method: str,
|
|
61
|
+
url: Optional[URLPatternTypes] = None,
|
|
62
|
+
*,
|
|
63
|
+
name: Optional[str] = None,
|
|
64
|
+
**lookups: Any,
|
|
65
|
+
) -> Route:
|
|
66
|
+
global mock
|
|
67
|
+
return mock.request(method, url, name=name, **lookups)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def get(
|
|
71
|
+
url: Optional[URLPatternTypes] = None, *, name: Optional[str] = None, **lookups: Any
|
|
72
|
+
) -> Route:
|
|
73
|
+
global mock
|
|
74
|
+
return mock.get(url, name=name, **lookups)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def post(
|
|
78
|
+
url: Optional[URLPatternTypes] = None, *, name: Optional[str] = None, **lookups: Any
|
|
79
|
+
) -> Route:
|
|
80
|
+
global mock
|
|
81
|
+
return mock.post(url, name=name, **lookups)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def put(
|
|
85
|
+
url: Optional[URLPatternTypes] = None, *, name: Optional[str] = None, **lookups: Any
|
|
86
|
+
) -> Route:
|
|
87
|
+
global mock
|
|
88
|
+
return mock.put(url, name=name, **lookups)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def patch(
|
|
92
|
+
url: Optional[URLPatternTypes] = None, *, name: Optional[str] = None, **lookups: Any
|
|
93
|
+
) -> Route:
|
|
94
|
+
global mock
|
|
95
|
+
return mock.patch(url, name=name, **lookups)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def delete(
|
|
99
|
+
url: Optional[URLPatternTypes] = None, *, name: Optional[str] = None, **lookups: Any
|
|
100
|
+
) -> Route:
|
|
101
|
+
global mock
|
|
102
|
+
return mock.delete(url, name=name, **lookups)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def head(
|
|
106
|
+
url: Optional[URLPatternTypes] = None, *, name: Optional[str] = None, **lookups: Any
|
|
107
|
+
) -> Route:
|
|
108
|
+
global mock
|
|
109
|
+
return mock.head(url, name=name, **lookups)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def options(
|
|
113
|
+
url: Optional[URLPatternTypes] = None, *, name: Optional[str] = None, **lookups: Any
|
|
114
|
+
) -> Route:
|
|
115
|
+
global mock
|
|
116
|
+
return mock.options(url, name=name, **lookups)
|
respx2/fixtures.py
ADDED
respx2/handlers.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from typing import Any, Callable
|
|
2
|
+
|
|
3
|
+
import httpx2
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TransportHandler:
|
|
7
|
+
def __init__(self, transport: httpx2.BaseTransport) -> None:
|
|
8
|
+
self.transport = transport
|
|
9
|
+
|
|
10
|
+
def __call__(self, request: httpx2.Request) -> httpx2.Response:
|
|
11
|
+
if not isinstance(
|
|
12
|
+
request.stream,
|
|
13
|
+
httpx2.SyncByteStream,
|
|
14
|
+
): # pragma: nocover
|
|
15
|
+
raise RuntimeError("Attempted to route an async request to a sync app.")
|
|
16
|
+
|
|
17
|
+
return self.transport.handle_request(request)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class AsyncTransportHandler:
|
|
21
|
+
def __init__(self, transport: httpx2.AsyncBaseTransport) -> None:
|
|
22
|
+
self.transport = transport
|
|
23
|
+
|
|
24
|
+
async def __call__(self, request: httpx2.Request) -> httpx2.Response:
|
|
25
|
+
if not isinstance(
|
|
26
|
+
request.stream,
|
|
27
|
+
httpx2.AsyncByteStream,
|
|
28
|
+
): # pragma: nocover
|
|
29
|
+
raise RuntimeError("Attempted to route a sync request to an async app.")
|
|
30
|
+
|
|
31
|
+
return await self.transport.handle_async_request(request)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class WSGIHandler(TransportHandler):
|
|
35
|
+
def __init__(self, app: Callable, **kwargs: Any) -> None:
|
|
36
|
+
super().__init__(httpx2.WSGITransport(app=app, **kwargs))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ASGIHandler(AsyncTransportHandler):
|
|
40
|
+
def __init__(self, app: Callable, **kwargs: Any) -> None:
|
|
41
|
+
super().__init__(httpx2.ASGITransport(app=app, **kwargs))
|
respx2/mocks.py
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from abc import ABC
|
|
3
|
+
from types import MappingProxyType
|
|
4
|
+
from typing import TYPE_CHECKING, ClassVar, Dict, List, Type
|
|
5
|
+
from unittest import mock
|
|
6
|
+
|
|
7
|
+
import httpcore2
|
|
8
|
+
import httpx2
|
|
9
|
+
|
|
10
|
+
from respx2.patterns import parse_url
|
|
11
|
+
|
|
12
|
+
from .models import AllMockedAssertionError, PassThrough
|
|
13
|
+
from .transports import TryTransport
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from .router import Router # pragma: nocover
|
|
17
|
+
|
|
18
|
+
__all__ = ["Mocker", "HTTPCoreMocker"]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Mocker(ABC):
|
|
22
|
+
_patches: ClassVar[List[mock._patch]]
|
|
23
|
+
name: ClassVar[str]
|
|
24
|
+
routers: ClassVar[List["Router"]]
|
|
25
|
+
targets: ClassVar[List[str]]
|
|
26
|
+
target_methods: ClassVar[List[str]]
|
|
27
|
+
|
|
28
|
+
# Automatically register all the subclasses in this dict
|
|
29
|
+
__registry: ClassVar[Dict[str, Type["Mocker"]]] = {}
|
|
30
|
+
registry = MappingProxyType(__registry)
|
|
31
|
+
|
|
32
|
+
def __init_subclass__(cls) -> None:
|
|
33
|
+
if not getattr(cls, "name", None) or ABC in cls.__bases__:
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
if cls.name in cls.__registry:
|
|
37
|
+
raise TypeError(
|
|
38
|
+
"Subclasses of Mocker must define a unique name. "
|
|
39
|
+
f"{cls.name!r} is already defined as {cls.__registry[cls.name]!r}"
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
cls.routers = []
|
|
43
|
+
cls._patches = []
|
|
44
|
+
cls.__registry[cls.name] = cls
|
|
45
|
+
|
|
46
|
+
@classmethod
|
|
47
|
+
def register(cls, router: "Router") -> None:
|
|
48
|
+
cls.routers.append(router)
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def unregister(cls, router: "Router") -> bool:
|
|
52
|
+
if router in cls.routers:
|
|
53
|
+
cls.routers.remove(router)
|
|
54
|
+
return True
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
@classmethod
|
|
58
|
+
def add_targets(cls, *targets: str) -> None:
|
|
59
|
+
targets = tuple(filter(lambda t: t not in cls.targets, targets))
|
|
60
|
+
if targets:
|
|
61
|
+
cls.targets.extend(targets)
|
|
62
|
+
cls.restart()
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def remove_targets(cls, *targets: str) -> None:
|
|
66
|
+
targets = tuple(filter(lambda t: t in cls.targets, targets))
|
|
67
|
+
if targets:
|
|
68
|
+
for target in targets:
|
|
69
|
+
cls.targets.remove(target)
|
|
70
|
+
cls.restart()
|
|
71
|
+
|
|
72
|
+
@classmethod
|
|
73
|
+
def start(cls) -> None:
|
|
74
|
+
# Ensure we only patch once!
|
|
75
|
+
if cls._patches:
|
|
76
|
+
return
|
|
77
|
+
|
|
78
|
+
# Start patching target transports
|
|
79
|
+
for target in cls.targets:
|
|
80
|
+
for method in cls.target_methods:
|
|
81
|
+
try:
|
|
82
|
+
spec = f"{target}.{method}"
|
|
83
|
+
patch = mock.patch(spec, spec=True, new_callable=cls.mock)
|
|
84
|
+
patch.start()
|
|
85
|
+
cls._patches.append(patch)
|
|
86
|
+
except AttributeError:
|
|
87
|
+
pass
|
|
88
|
+
|
|
89
|
+
@classmethod
|
|
90
|
+
def stop(cls, force: bool = False) -> None:
|
|
91
|
+
# Ensure we don't stop patching when registered transports exists
|
|
92
|
+
if cls.routers and not force:
|
|
93
|
+
return
|
|
94
|
+
|
|
95
|
+
# Stop patching HTTPX2
|
|
96
|
+
while cls._patches:
|
|
97
|
+
patch = cls._patches.pop()
|
|
98
|
+
patch.stop()
|
|
99
|
+
|
|
100
|
+
@classmethod
|
|
101
|
+
def restart(cls) -> None:
|
|
102
|
+
# Only stop and start if started
|
|
103
|
+
if cls._patches: # pragma: nocover
|
|
104
|
+
cls.stop(force=True)
|
|
105
|
+
cls.start()
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def handler(cls, httpx2_request):
|
|
109
|
+
httpx2_response = None
|
|
110
|
+
assertion_error = None
|
|
111
|
+
for router in cls.routers:
|
|
112
|
+
try:
|
|
113
|
+
httpx2_response = router.handler(httpx2_request)
|
|
114
|
+
except AllMockedAssertionError as error:
|
|
115
|
+
assertion_error = error
|
|
116
|
+
continue
|
|
117
|
+
else:
|
|
118
|
+
break
|
|
119
|
+
if assertion_error and not httpx2_response:
|
|
120
|
+
raise assertion_error
|
|
121
|
+
return httpx2_response
|
|
122
|
+
|
|
123
|
+
@classmethod
|
|
124
|
+
async def async_handler(cls, httpx2_request):
|
|
125
|
+
httpx2_response = None
|
|
126
|
+
assertion_error = None
|
|
127
|
+
for router in cls.routers:
|
|
128
|
+
try:
|
|
129
|
+
httpx2_response = await router.async_handler(httpx2_request)
|
|
130
|
+
except AllMockedAssertionError as error:
|
|
131
|
+
assertion_error = error
|
|
132
|
+
continue
|
|
133
|
+
else:
|
|
134
|
+
break
|
|
135
|
+
if assertion_error and not httpx2_response:
|
|
136
|
+
raise assertion_error
|
|
137
|
+
return httpx2_response
|
|
138
|
+
|
|
139
|
+
@classmethod
|
|
140
|
+
def mock(cls, spec):
|
|
141
|
+
raise NotImplementedError() # pragma: nocover
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class HTTPX2Mocker(Mocker):
|
|
145
|
+
name = "httpx2"
|
|
146
|
+
targets = [
|
|
147
|
+
"httpx2._client.Client",
|
|
148
|
+
"httpx2._client.AsyncClient",
|
|
149
|
+
]
|
|
150
|
+
target_methods = ["_transport_for_url"]
|
|
151
|
+
|
|
152
|
+
@classmethod
|
|
153
|
+
def mock(cls, spec):
|
|
154
|
+
def _transport_for_url(self, *args, **kwargs):
|
|
155
|
+
handler = (
|
|
156
|
+
cls.async_handler
|
|
157
|
+
if inspect.iscoroutinefunction(self.request)
|
|
158
|
+
else cls.handler
|
|
159
|
+
)
|
|
160
|
+
mock_transport = httpx2.MockTransport(handler)
|
|
161
|
+
pass_through_transport = spec(self, *args, **kwargs)
|
|
162
|
+
transport = TryTransport([mock_transport, pass_through_transport])
|
|
163
|
+
return transport
|
|
164
|
+
|
|
165
|
+
return _transport_for_url
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class AbstractRequestMocker(Mocker):
|
|
169
|
+
@classmethod
|
|
170
|
+
def mock(cls, spec):
|
|
171
|
+
if spec.__name__ not in cls.target_methods:
|
|
172
|
+
# Prevent mocking mock
|
|
173
|
+
return spec
|
|
174
|
+
|
|
175
|
+
argspec = inspect.getfullargspec(spec)
|
|
176
|
+
|
|
177
|
+
def mock(self, *args, **kwargs):
|
|
178
|
+
kwargs = cls._merge_args_and_kwargs(argspec, args, kwargs)
|
|
179
|
+
request = cls.to_httpx2_request(**kwargs)
|
|
180
|
+
request, kwargs = cls.prepare_sync_request(request, **kwargs)
|
|
181
|
+
response = cls._send_sync_request(
|
|
182
|
+
request, target_spec=spec, instance=self, **kwargs
|
|
183
|
+
)
|
|
184
|
+
return response
|
|
185
|
+
|
|
186
|
+
async def amock(self, *args, **kwargs):
|
|
187
|
+
kwargs = cls._merge_args_and_kwargs(argspec, args, kwargs)
|
|
188
|
+
request = cls.to_httpx2_request(**kwargs)
|
|
189
|
+
request, kwargs = await cls.prepare_async_request(request, **kwargs)
|
|
190
|
+
response = await cls._send_async_request(
|
|
191
|
+
request, target_spec=spec, instance=self, **kwargs
|
|
192
|
+
)
|
|
193
|
+
return response
|
|
194
|
+
|
|
195
|
+
return amock if inspect.iscoroutinefunction(spec) else mock
|
|
196
|
+
|
|
197
|
+
@classmethod
|
|
198
|
+
def _merge_args_and_kwargs(cls, argspec, args, kwargs):
|
|
199
|
+
arg_names = argspec.args[1:] # Omit self
|
|
200
|
+
new_kwargs = (
|
|
201
|
+
dict(zip(arg_names[-len(argspec.defaults) :], argspec.defaults))
|
|
202
|
+
if argspec.defaults
|
|
203
|
+
else dict()
|
|
204
|
+
)
|
|
205
|
+
new_kwargs.update(zip(arg_names, args))
|
|
206
|
+
new_kwargs.update(kwargs)
|
|
207
|
+
return new_kwargs
|
|
208
|
+
|
|
209
|
+
@classmethod
|
|
210
|
+
def _send_sync_request(cls, httpx2_request, *, target_spec, instance, **kwargs):
|
|
211
|
+
try:
|
|
212
|
+
httpx2_response = cls.handler(httpx2_request)
|
|
213
|
+
except PassThrough:
|
|
214
|
+
response = target_spec(instance, **kwargs)
|
|
215
|
+
else:
|
|
216
|
+
response = cls.from_sync_httpx2_response(
|
|
217
|
+
httpx2_response, instance, **kwargs
|
|
218
|
+
)
|
|
219
|
+
return response
|
|
220
|
+
|
|
221
|
+
@classmethod
|
|
222
|
+
async def _send_async_request(
|
|
223
|
+
cls, httpx2_request, *, target_spec, instance, **kwargs
|
|
224
|
+
):
|
|
225
|
+
try:
|
|
226
|
+
httpx2_response = await cls.async_handler(httpx2_request)
|
|
227
|
+
except PassThrough:
|
|
228
|
+
response = await target_spec(instance, **kwargs)
|
|
229
|
+
else:
|
|
230
|
+
response = await cls.from_async_httpx2_response(
|
|
231
|
+
httpx2_response, instance, **kwargs
|
|
232
|
+
)
|
|
233
|
+
return response
|
|
234
|
+
|
|
235
|
+
@classmethod
|
|
236
|
+
def prepare_sync_request(cls, httpx2_request, **kwargs):
|
|
237
|
+
"""
|
|
238
|
+
Sync pre-read request body
|
|
239
|
+
"""
|
|
240
|
+
httpx2_request.read()
|
|
241
|
+
return httpx2_request, kwargs
|
|
242
|
+
|
|
243
|
+
@classmethod
|
|
244
|
+
async def prepare_async_request(cls, httpx2_request, **kwargs):
|
|
245
|
+
"""
|
|
246
|
+
Async pre-read request body
|
|
247
|
+
"""
|
|
248
|
+
await httpx2_request.aread()
|
|
249
|
+
return httpx2_request, kwargs
|
|
250
|
+
|
|
251
|
+
@classmethod
|
|
252
|
+
def to_httpx2_request(cls, **kwargs):
|
|
253
|
+
raise NotImplementedError() # pragma: nocover
|
|
254
|
+
|
|
255
|
+
@classmethod
|
|
256
|
+
def from_sync_httpx2_response(cls, httpx2_response, target, **kwargs):
|
|
257
|
+
raise NotImplementedError() # pragma: nocover
|
|
258
|
+
|
|
259
|
+
@classmethod
|
|
260
|
+
async def from_async_httpx2_response(cls, httpx2_response, target, **kwargs):
|
|
261
|
+
raise NotImplementedError() # pragma: nocover
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
class HTTPCoreMocker(AbstractRequestMocker):
|
|
265
|
+
name = "httpcore2"
|
|
266
|
+
targets = [
|
|
267
|
+
"httpcore2._sync.connection.HTTPConnection",
|
|
268
|
+
"httpcore2._sync.connection_pool.ConnectionPool",
|
|
269
|
+
"httpcore2._sync.http_proxy.HTTPProxy",
|
|
270
|
+
"httpcore2._async.connection.AsyncHTTPConnection",
|
|
271
|
+
"httpcore2._async.connection_pool.AsyncConnectionPool",
|
|
272
|
+
"httpcore2._async.http_proxy.AsyncHTTPProxy",
|
|
273
|
+
]
|
|
274
|
+
target_methods = ["handle_request", "handle_async_request"]
|
|
275
|
+
|
|
276
|
+
@classmethod
|
|
277
|
+
def prepare_sync_request(cls, httpx2_request, **kwargs):
|
|
278
|
+
"""
|
|
279
|
+
Sync pre-read request body, and update transport request arg.
|
|
280
|
+
"""
|
|
281
|
+
httpx2_request, kwargs = super().prepare_sync_request(httpx2_request, **kwargs)
|
|
282
|
+
kwargs["request"].stream = httpx2_request.stream
|
|
283
|
+
return httpx2_request, kwargs
|
|
284
|
+
|
|
285
|
+
@classmethod
|
|
286
|
+
async def prepare_async_request(cls, httpx2_request, **kwargs):
|
|
287
|
+
"""
|
|
288
|
+
Async pre-read request body, and update transport request arg.
|
|
289
|
+
"""
|
|
290
|
+
httpx2_request, kwargs = await super().prepare_async_request(
|
|
291
|
+
httpx2_request, **kwargs
|
|
292
|
+
)
|
|
293
|
+
kwargs["request"].stream = httpx2_request.stream
|
|
294
|
+
return httpx2_request, kwargs
|
|
295
|
+
|
|
296
|
+
@classmethod
|
|
297
|
+
def to_httpx2_request(cls, **kwargs):
|
|
298
|
+
"""
|
|
299
|
+
Create a `HTTPX2` request from transport request arg.
|
|
300
|
+
"""
|
|
301
|
+
request = kwargs["request"]
|
|
302
|
+
method = (
|
|
303
|
+
request.method.decode("ascii")
|
|
304
|
+
if isinstance(request.method, bytes)
|
|
305
|
+
else request.method
|
|
306
|
+
)
|
|
307
|
+
raw_url = (
|
|
308
|
+
request.url.scheme,
|
|
309
|
+
request.url.host,
|
|
310
|
+
request.url.port,
|
|
311
|
+
request.url.target if method != "CONNECT" else b"/",
|
|
312
|
+
)
|
|
313
|
+
return httpx2.Request(
|
|
314
|
+
method,
|
|
315
|
+
parse_url(raw_url),
|
|
316
|
+
headers=request.headers,
|
|
317
|
+
stream=request.stream,
|
|
318
|
+
extensions=request.extensions,
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
@classmethod
|
|
322
|
+
def from_sync_httpx2_response(cls, httpx2_response, target, **kwargs):
|
|
323
|
+
"""
|
|
324
|
+
Create a `httpcore2` response from a `HTTPX2` response.
|
|
325
|
+
"""
|
|
326
|
+
return httpcore2.Response(
|
|
327
|
+
status=httpx2_response.status_code,
|
|
328
|
+
headers=httpx2_response.headers.raw,
|
|
329
|
+
content=httpx2_response.stream,
|
|
330
|
+
extensions=httpx2_response.extensions,
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
@classmethod
|
|
334
|
+
async def from_async_httpx2_response(cls, httpx2_response, target, **kwargs):
|
|
335
|
+
"""
|
|
336
|
+
Create a `httpcore2` response from a `HTTPX2` response.
|
|
337
|
+
"""
|
|
338
|
+
return cls.from_sync_httpx2_response(httpx2_response, target, **kwargs)
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
DEFAULT_MOCKER: str = HTTPCoreMocker.name
|