pook 2.1.6__tar.gz → 2.2.0__tar.gz

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.
Files changed (42) hide show
  1. {pook-2.1.6 → pook-2.2.0}/PKG-INFO +5 -3
  2. {pook-2.1.6 → pook-2.2.0}/README.rst +2 -1
  3. {pook-2.1.6 → pook-2.2.0}/pook/__init__.py +1 -1
  4. {pook-2.1.6 → pook-2.2.0}/pook/interceptors/__init__.py +9 -0
  5. pook-2.2.0/pook/interceptors/_httpx.py +30 -0
  6. pook-2.2.0/pook/interceptors/_httpx2.py +30 -0
  7. pook-2.1.6/pook/interceptors/_httpx.py → pook-2.2.0/pook/interceptors/_httpx_common.py +32 -44
  8. {pook-2.1.6 → pook-2.2.0}/pook/interceptors/aiohttp.py +13 -2
  9. {pook-2.1.6 → pook-2.2.0}/pook/interceptors/urllib3.py +5 -1
  10. {pook-2.1.6 → pook-2.2.0}/pook/matchers/query.py +17 -7
  11. {pook-2.1.6 → pook-2.2.0}/pook/mock.py +8 -0
  12. {pook-2.1.6 → pook-2.2.0}/pyproject.toml +4 -2
  13. {pook-2.1.6 → pook-2.2.0}/.gitignore +0 -0
  14. {pook-2.1.6 → pook-2.2.0}/LICENSE +0 -0
  15. {pook-2.1.6 → pook-2.2.0}/pook/activate_async.py +0 -0
  16. {pook-2.1.6 → pook-2.2.0}/pook/api.py +0 -0
  17. {pook-2.1.6 → pook-2.2.0}/pook/assertion.py +0 -0
  18. {pook-2.1.6 → pook-2.2.0}/pook/compare.py +0 -0
  19. {pook-2.1.6 → pook-2.2.0}/pook/constants.py +0 -0
  20. {pook-2.1.6 → pook-2.2.0}/pook/engine.py +0 -0
  21. {pook-2.1.6 → pook-2.2.0}/pook/exceptions.py +0 -0
  22. {pook-2.1.6 → pook-2.2.0}/pook/headers.py +0 -0
  23. {pook-2.1.6 → pook-2.2.0}/pook/helpers.py +0 -0
  24. {pook-2.1.6 → pook-2.2.0}/pook/interceptors/base.py +0 -0
  25. {pook-2.1.6 → pook-2.2.0}/pook/interceptors/http.py +0 -0
  26. {pook-2.1.6 → pook-2.2.0}/pook/matcher.py +0 -0
  27. {pook-2.1.6 → pook-2.2.0}/pook/matchers/__init__.py +0 -0
  28. {pook-2.1.6 → pook-2.2.0}/pook/matchers/api.py +0 -0
  29. {pook-2.1.6 → pook-2.2.0}/pook/matchers/base.py +0 -0
  30. {pook-2.1.6 → pook-2.2.0}/pook/matchers/body.py +0 -0
  31. {pook-2.1.6 → pook-2.2.0}/pook/matchers/headers.py +0 -0
  32. {pook-2.1.6 → pook-2.2.0}/pook/matchers/json.py +0 -0
  33. {pook-2.1.6 → pook-2.2.0}/pook/matchers/json_schema.py +0 -0
  34. {pook-2.1.6 → pook-2.2.0}/pook/matchers/method.py +0 -0
  35. {pook-2.1.6 → pook-2.2.0}/pook/matchers/path.py +0 -0
  36. {pook-2.1.6 → pook-2.2.0}/pook/matchers/url.py +0 -0
  37. {pook-2.1.6 → pook-2.2.0}/pook/matchers/xml.py +0 -0
  38. {pook-2.1.6 → pook-2.2.0}/pook/mock_engine.py +0 -0
  39. {pook-2.1.6 → pook-2.2.0}/pook/pytest_fixture.py +0 -0
  40. {pook-2.1.6 → pook-2.2.0}/pook/regex.py +0 -0
  41. {pook-2.1.6 → pook-2.2.0}/pook/request.py +0 -0
  42. {pook-2.1.6 → pook-2.2.0}/pook/response.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: pook
3
- Version: 2.1.6
3
+ Version: 2.2.0
4
4
  Summary: HTTP traffic mocking and expectations made easy
5
5
  Project-URL: Homepage, https://github.com/h2non/pook
6
6
  Author-email: Tomas Aparicio <tomas@aparicio.me>, Sara Marcondes <git@sarayourfriend.pictures>
@@ -17,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
18
  Classifier: Programming Language :: Python :: 3.13
19
19
  Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Programming Language :: Python :: 3.15
20
21
  Classifier: Programming Language :: Python :: Implementation :: CPython
21
22
  Classifier: Programming Language :: Python :: Implementation :: PyPy
22
23
  Classifier: Topic :: Software Development
@@ -76,7 +77,7 @@ the following HTTP clients:
76
77
  - ✔ `requests`_ v2+
77
78
  - ✔ `aiohttp`_ v3+
78
79
  - ✔ `urllib`_ / `http.client`_
79
- - ✔ `httpx`_
80
+ - ✔ `httpx`_ / `httpx2`_
80
81
 
81
82
  More HTTP clients can be supported progressively.
82
83
 
@@ -287,6 +288,7 @@ MIT - Tomas Aparicio
287
288
  .. _examples: http://pook.readthedocs.io/en/latest/examples.html
288
289
  .. _aiohttp: https://github.com/KeepSafe/aiohttp
289
290
  .. _httpx: https://www.python-httpx.org/
291
+ .. _httpx2: https://pydantic.dev/docs/httpx2/
290
292
  .. _requests: http://docs.python-requests.org/en/master/
291
293
  .. _urllib3: https://github.com/shazow/urllib3
292
294
  .. _urllib: https://docs.python.org/3/library/urllib.html
@@ -47,7 +47,7 @@ the following HTTP clients:
47
47
  - ✔ `requests`_ v2+
48
48
  - ✔ `aiohttp`_ v3+
49
49
  - ✔ `urllib`_ / `http.client`_
50
- - ✔ `httpx`_
50
+ - ✔ `httpx`_ / `httpx2`_
51
51
 
52
52
  More HTTP clients can be supported progressively.
53
53
 
@@ -258,6 +258,7 @@ MIT - Tomas Aparicio
258
258
  .. _examples: http://pook.readthedocs.io/en/latest/examples.html
259
259
  .. _aiohttp: https://github.com/KeepSafe/aiohttp
260
260
  .. _httpx: https://www.python-httpx.org/
261
+ .. _httpx2: https://pydantic.dev/docs/httpx2/
261
262
  .. _requests: http://docs.python-requests.org/en/master/
262
263
  .. _urllib3: https://github.com/shazow/urllib3
263
264
  .. _urllib: https://docs.python.org/3/library/urllib.html
@@ -44,4 +44,4 @@ __author__ = "Tomas Aparicio"
44
44
  __license__ = "MIT"
45
45
 
46
46
  # Current version
47
- __version__ = "2.1.6"
47
+ __version__ = "2.2.0"
@@ -11,6 +11,7 @@ __all__ = (
11
11
  "Urllib3Interceptor",
12
12
  "HTTPClientInterceptor",
13
13
  "HttpxInterceptor",
14
+ "Httpx2Interceptor",
14
15
  "AIOHTTPInterceptor",
15
16
  )
16
17
 
@@ -33,6 +34,14 @@ try:
33
34
  except ImportError:
34
35
  pass
35
36
 
37
+ try:
38
+ import httpx2 # noqa
39
+ from ._httpx2 import Httpx2Interceptor
40
+
41
+ interceptors.append(Httpx2Interceptor)
42
+ except ImportError:
43
+ pass
44
+
36
45
 
37
46
  def add(*custom_interceptors):
38
47
  """
@@ -0,0 +1,30 @@
1
+ import httpx
2
+
3
+ from pook.interceptors._httpx_common import (
4
+ SyncTransportMixin,
5
+ AsyncTransportMixin,
6
+ HttpxInterceptorBase,
7
+ )
8
+
9
+
10
+ class SyncTransport(SyncTransportMixin, httpx.BaseTransport):
11
+ response_cls = httpx.Response
12
+
13
+
14
+ class AsyncTransport(AsyncTransportMixin, httpx.AsyncBaseTransport):
15
+ response_cls = httpx.Response
16
+
17
+
18
+ class HttpxInterceptor(HttpxInterceptorBase):
19
+ """
20
+ Interceptor for httpx.
21
+
22
+ Intercepts synchronous and asynchronous httpx traffic.
23
+ """
24
+
25
+ patch_paths = (
26
+ "httpx.Client._transport_for_url",
27
+ "httpx.AsyncClient._transport_for_url",
28
+ )
29
+ sync_transport_cls = SyncTransport
30
+ async_transport_cls = AsyncTransport
@@ -0,0 +1,30 @@
1
+ import httpx2
2
+
3
+ from pook.interceptors._httpx_common import (
4
+ SyncTransportMixin,
5
+ AsyncTransportMixin,
6
+ HttpxInterceptorBase,
7
+ )
8
+
9
+
10
+ class SyncTransport(SyncTransportMixin, httpx2.BaseTransport):
11
+ response_cls = httpx2.Response
12
+
13
+
14
+ class AsyncTransport(AsyncTransportMixin, httpx2.AsyncBaseTransport):
15
+ response_cls = httpx2.Response
16
+
17
+
18
+ class Httpx2Interceptor(HttpxInterceptorBase):
19
+ """
20
+ Interceptor for httpx2.
21
+
22
+ Intercepts synchronous and asynchronous httpx2 traffic.
23
+ """
24
+
25
+ patch_paths = (
26
+ "httpx2.Client._transport_for_url",
27
+ "httpx2.AsyncClient._transport_for_url",
28
+ )
29
+ sync_transport_cls = SyncTransport
30
+ async_transport_cls = AsyncTransport
@@ -1,40 +1,29 @@
1
1
  import asyncio
2
+ from collections.abc import Callable
2
3
  from http.client import responses as http_reasons
4
+ from typing import Any, ClassVar
3
5
  from unittest import mock
4
- import typing as t
5
6
 
6
- import httpx
7
-
8
- from pook.request import Request # type: ignore
9
- from pook.response import Response # type: ignore
10
7
  from pook.interceptors.base import BaseInterceptor
8
+ from pook.request import Request
9
+ from pook.response import Response
11
10
 
12
-
13
- PATCHES = (
14
- "httpx.Client._transport_for_url",
15
- "httpx.AsyncClient._transport_for_url",
16
- )
17
-
18
-
19
- HttpxClient = t.Union[httpx.Client, httpx.AsyncClient]
20
- TransportForUrl = t.Callable[
21
- [HttpxClient, httpx.URL], t.Union[httpx.BaseTransport, httpx.AsyncBaseTransport]
22
- ]
11
+ HttpxClient = Any
12
+ TransportForUrl = Callable[[Any, Any], Any]
23
13
 
24
14
 
25
- class HttpxInterceptor(BaseInterceptor):
15
+ class HttpxInterceptorBase(BaseInterceptor):
26
16
  """
27
- httpx client traffic interceptor.
17
+ Base class for httpx and httpx2 interceptors.
28
18
 
29
- Intercepts synchronous and asynchronous httpx traffic.
19
+ Intercepts synchronous and asynchronous httpx and httpx2 traffic.
30
20
  """
31
21
 
32
- def _patch(self, path):
33
- if "AsyncClient" in path:
34
- transport_cls = AsyncTransport
35
- else:
36
- transport_cls = SyncTransport
22
+ patch_paths: ClassVar[tuple[str, ...]]
23
+ sync_transport_cls: ClassVar[type]
24
+ async_transport_cls: ClassVar[type]
37
25
 
26
+ def _patch(self, path, transport_cls):
38
27
  def handler(client, *_):
39
28
  return transport_cls(self, client, _original_transport_for_url)
40
29
 
@@ -48,39 +37,38 @@ class HttpxInterceptor(BaseInterceptor):
48
37
  self.patchers.append(patcher)
49
38
 
50
39
  def activate(self):
51
- [self._patch(path) for path in PATCHES]
40
+ sync_path, async_path = self.patch_paths
41
+ self._patch(sync_path, self.sync_transport_cls)
42
+ self._patch(async_path, self.async_transport_cls)
52
43
 
53
44
  def disable(self):
54
45
  [patch.stop() for patch in self.patchers]
55
46
 
56
47
 
57
- T = t.TypeVar("T", httpx.BaseTransport, httpx.AsyncBaseTransport)
58
-
59
-
60
- class MockedTransport(httpx.BaseTransport, t.Generic[T]):
61
- _original_transport_for_url: t.Callable[[HttpxClient, httpx.URL], T]
48
+ class MockedTransportMixin:
49
+ response_cls: ClassVar[type]
50
+ _original_transport_for_url: TransportForUrl
62
51
 
63
52
  def __init__(
64
53
  self,
65
- interceptor: HttpxInterceptor,
54
+ interceptor: HttpxInterceptorBase,
66
55
  client: HttpxClient,
67
- _original_transport_for_url: t.Callable[[HttpxClient, httpx.URL], T],
56
+ _original_transport_for_url: TransportForUrl,
68
57
  ):
69
58
  self._interceptor = interceptor
70
59
  self._client = client
71
60
  self._original_transport_for_url = _original_transport_for_url
72
61
 
73
- def _get_pook_request(self, httpx_request: httpx.Request) -> Request:
62
+ @staticmethod
63
+ def _build_pook_request(httpx_request) -> Request:
74
64
  req = Request(httpx_request.method)
75
65
  req.url = str(httpx_request.url)
76
66
  req.headers = httpx_request.headers
77
67
 
78
68
  return req
79
69
 
80
- def _get_httpx_response(
81
- self, httpx_request: httpx.Request, mock_response: Response
82
- ) -> httpx.Response:
83
- res = httpx.Response(
70
+ def _build_response(self, httpx_request, mock_response: Response):
71
+ res = self.response_cls(
84
72
  status_code=mock_response._status,
85
73
  headers=mock_response._headers,
86
74
  content=mock_response._body,
@@ -104,9 +92,9 @@ class MockedTransport(httpx.BaseTransport, t.Generic[T]):
104
92
  return res
105
93
 
106
94
 
107
- class AsyncTransport(MockedTransport[httpx.AsyncBaseTransport]):
108
- async def _get_pook_request(self, httpx_request):
109
- req = super()._get_pook_request(httpx_request)
95
+ class AsyncTransportMixin(MockedTransportMixin):
96
+ async def _get_pook_request(self, httpx_request) -> Request:
97
+ req = self._build_pook_request(httpx_request)
110
98
  req.body = await httpx_request.aread()
111
99
  return req
112
100
 
@@ -122,12 +110,12 @@ class AsyncTransport(MockedTransport[httpx.AsyncBaseTransport]):
122
110
  if mock._delay:
123
111
  await asyncio.sleep(mock._delay / 1000)
124
112
 
125
- return self._get_httpx_response(request, mock._response)
113
+ return self._build_response(request, mock._response)
126
114
 
127
115
 
128
- class SyncTransport(MockedTransport[httpx.BaseTransport]):
116
+ class SyncTransportMixin(MockedTransportMixin):
129
117
  def _get_pook_request(self, httpx_request):
130
- req = super()._get_pook_request(httpx_request)
118
+ req = self._build_pook_request(httpx_request)
131
119
  req.body = httpx_request.read()
132
120
  return req
133
121
 
@@ -140,4 +128,4 @@ class SyncTransport(MockedTransport[httpx.BaseTransport]):
140
128
  transport = self._original_transport_for_url(self._client, request.url)
141
129
  return transport.handle_request(request)
142
130
 
143
- return self._get_httpx_response(request, mock._response)
131
+ return self._build_response(request, mock._response)
@@ -1,3 +1,4 @@
1
+ import inspect
1
2
  from http.client import responses as http_reasons
2
3
  from typing import Optional
3
4
  from unittest import mock
@@ -116,7 +117,14 @@ class SimpleContent(EmptyStreamReader):
116
117
 
117
118
 
118
119
  def HTTPResponse(session: aiohttp.ClientSession, *args, **kw):
119
- return session._response_class(
120
+ response_cls = session._response_class
121
+
122
+ # aiohttp 3.14 added a required keyword-only ``stream_writer`` argument.
123
+ # Only pass it when the installed version expects it.
124
+ if "stream_writer" in inspect.signature(response_cls.__init__).parameters:
125
+ kw.setdefault("stream_writer", mock.Mock(output_size=0))
126
+
127
+ return response_cls(
120
128
  *args,
121
129
  request_info=mock.Mock(),
122
130
  writer=None,
@@ -124,6 +132,9 @@ def HTTPResponse(session: aiohttp.ClientSession, *args, **kw):
124
132
  timer=TimerNoop(),
125
133
  traces=[],
126
134
  loop=mock.Mock(),
127
- session=mock.Mock(),
135
+ # The response copies its charset resolver from the session, so this
136
+ # must be the real session or ``text()`` fails whenever the reply
137
+ # carries no charset.
138
+ session=session,
128
139
  **kw,
129
140
  )
@@ -94,7 +94,9 @@ class FakeChunkedResponseBody:
94
94
 
95
95
  return chunk
96
96
 
97
- def readline(self):
97
+ def readline(self, size=-1):
98
+ # urllib3 >= 2.8 passes a maximum line length; chunk-size lines
99
+ # produced by _encode are always far shorter, so it can be ignored.
98
100
  return self.read_chunk(whole=True)
99
101
 
100
102
  def read(self, amt=-1):
@@ -163,6 +165,8 @@ class Urllib3Interceptor(BaseInterceptor):
163
165
  preload_content=False,
164
166
  reason=http_reasons.get(res._status),
165
167
  original_response=FakeResponse(method, headers),
168
+ version=11,
169
+ version_string="HTTP/1.1",
166
170
  )
167
171
 
168
172
  def _patch(self, path):
@@ -1,8 +1,16 @@
1
1
  from urllib.parse import parse_qs
2
2
 
3
+ from ..assertion import equal
3
4
  from .base import BaseMatcher, ExistsMatcher
4
5
 
5
6
 
7
+ def _to_query_value(value):
8
+ # Query string values are always strings; coerce numeric expectations
9
+ if isinstance(value, (int, float)) and not isinstance(value, bool):
10
+ return str(value)
11
+ return value
12
+
13
+
6
14
  class QueryMatcher(BaseMatcher):
7
15
  """
8
16
  QueryMatcher implements an URL query params matcher.
@@ -14,11 +22,13 @@ class QueryMatcher(BaseMatcher):
14
22
  if match is None:
15
23
  return False
16
24
 
17
- # Normalize param value
18
- param = [param] if not isinstance(param, list) else param
19
-
20
- # Compare query params
21
- [[self.compare(value, expect) for expect in match] for value in param]
25
+ if isinstance(param, list):
26
+ equal(len(param), len(match))
27
+ for value, expect in zip(param, match):
28
+ self.compare(_to_query_value(value), expect)
29
+ else:
30
+ for expect in match:
31
+ self.compare(_to_query_value(param), expect)
22
32
 
23
33
  return True
24
34
 
@@ -30,14 +40,14 @@ class QueryMatcher(BaseMatcher):
30
40
 
31
41
  # Parse and assert type
32
42
  if isinstance(query, str):
33
- query = parse_qs(self.expectation)
43
+ query = parse_qs(self.expectation, keep_blank_values=True)
34
44
 
35
45
  # Validate query params
36
46
  if not isinstance(query, dict):
37
47
  raise ValueError("query params must be a str or dict")
38
48
 
39
49
  # Parse request URL query
40
- req_query = parse_qs(req.url.query)
50
+ req_query = parse_qs(req.url.query, keep_blank_values=True)
41
51
 
42
52
  # Match query params
43
53
  return self.match_query(query, req_query)
@@ -385,6 +385,14 @@ class Mock:
385
385
  """
386
386
  Defines a set of URL query params to match.
387
387
 
388
+ List values match repeated parameters in the same order and number.
389
+ A scalar value must match every received value for that parameter.
390
+ Other query parameter names are ignored.
391
+
392
+ Empty values require the parameter to be present. For example,
393
+ ``params({"cursor": ""})`` matches ``?cursor`` or ``?cursor=``,
394
+ but does not match a request without ``cursor``.
395
+
388
396
  Arguments:
389
397
  params (dict): set of params to match.
390
398
 
@@ -26,6 +26,7 @@ classifiers = [
26
26
  "Programming Language :: Python :: 3.12",
27
27
  "Programming Language :: Python :: 3.13",
28
28
  "Programming Language :: Python :: 3.14",
29
+ "Programming Language :: Python :: 3.15",
29
30
  "Topic :: Software Development",
30
31
  "Topic :: Software Development :: Libraries :: Python Modules",
31
32
  ]
@@ -66,12 +67,13 @@ dependencies = [
66
67
  "requests~=2.20",
67
68
  "urllib3~=2.2",
68
69
  "httpx~=0.26",
70
+ "httpx2~=2.12",
69
71
 
70
72
  "aiohttp~=3.10",
71
73
  "async-timeout~=4.0",
72
74
 
73
75
  # mocket relies on httptools which does not support PyPy
74
- "mocket[pook]~=3.12.2; platform_python_implementation != 'PyPy'",
76
+ "mocket[pook]~=3.15.0; platform_python_implementation != 'PyPy'",
75
77
  ]
76
78
 
77
79
  [tool.hatch.envs.default.scripts]
@@ -99,7 +101,7 @@ build = "sphinx-build -b html docs docs/_build/html {args}"
99
101
 
100
102
  [tool.hatch.envs.test]
101
103
  [[tool.hatch.envs.test.matrix]]
102
- python = ["3.10", "3.11", "3.12", "3.13", "3.14", "pypy3.11"]
104
+ python = ["3.10", "3.11", "3.12", "3.13", "3.14", "3.15", "pypy3.11"]
103
105
 
104
106
  [tool.pytest.ini_options]
105
107
  asyncio_mode = "auto"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes