pytest-traffic-capture 0.1.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.
- pytest_traffic_capture-0.1.0/LICENSE +21 -0
- pytest_traffic_capture-0.1.0/PKG-INFO +205 -0
- pytest_traffic_capture-0.1.0/README.rst +151 -0
- pytest_traffic_capture-0.1.0/pyproject.toml +55 -0
- pytest_traffic_capture-0.1.0/setup.cfg +4 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture/__init__.py +5 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture/adapters.py +1890 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture/exporters.py +962 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture/model.py +69 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture/multipart.py +75 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture/plugin.py +200 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture/recorder.py +255 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture.egg-info/PKG-INFO +205 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture.egg-info/SOURCES.txt +23 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture.egg-info/dependency_links.txt +1 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture.egg-info/entry_points.txt +2 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture.egg-info/requires.txt +6 -0
- pytest_traffic_capture-0.1.0/src/pytest_traffic_capture.egg-info/top_level.txt +1 -0
- pytest_traffic_capture-0.1.0/tests/test_aiohttp_capture.py +551 -0
- pytest_traffic_capture-0.1.0/tests/test_export_details.py +366 -0
- pytest_traffic_capture-0.1.0/tests/test_httpx_capture.py +268 -0
- pytest_traffic_capture-0.1.0/tests/test_replay.py +311 -0
- pytest_traffic_capture-0.1.0/tests/test_requests_capture.py +381 -0
- pytest_traffic_capture-0.1.0/tests/test_traffic_capture.py +145 -0
- pytest_traffic_capture-0.1.0/tests/test_unit.py +331 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 pytest-traffic-capture contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-traffic-capture
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Capture requests/httpx/aiohttp traffic during tests as HAR 1.2 and cURL commands
|
|
5
|
+
Author: pytest-traffic-capture contributors
|
|
6
|
+
Maintainer: pytest-traffic-capture contributors
|
|
7
|
+
License: MIT License
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2026 pytest-traffic-capture contributors
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
27
|
+
SOFTWARE.
|
|
28
|
+
|
|
29
|
+
Classifier: Framework :: Pytest
|
|
30
|
+
Classifier: Development Status :: 4 - Beta
|
|
31
|
+
Classifier: Intended Audience :: Developers
|
|
32
|
+
Classifier: Topic :: Software Development :: Testing
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Classifier: Programming Language :: Python
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
42
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
43
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
44
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
45
|
+
Requires-Python: >=3.8
|
|
46
|
+
Description-Content-Type: text/x-rst
|
|
47
|
+
License-File: LICENSE
|
|
48
|
+
Requires-Dist: pytest>=8
|
|
49
|
+
Provides-Extra: test
|
|
50
|
+
Requires-Dist: requests; extra == "test"
|
|
51
|
+
Requires-Dist: httpx; extra == "test"
|
|
52
|
+
Requires-Dist: aiohttp; extra == "test"
|
|
53
|
+
Dynamic: license-file
|
|
54
|
+
|
|
55
|
+
pytest-traffic-capture
|
|
56
|
+
======================
|
|
57
|
+
|
|
58
|
+
在 pytest 运行期间捕获 ``requests``、``httpx`` 和 ``aiohttp`` 的流量,并将其写为
|
|
59
|
+
HAR 1.2 文档以及可重放的 cURL 命令。
|
|
60
|
+
|
|
61
|
+
功能特性
|
|
62
|
+
--------
|
|
63
|
+
|
|
64
|
+
* 捕获使用 ``requests``、``httpx`` (同步和异步)以及 ``aiohttp`` 进行的 HTTP
|
|
65
|
+
交换。
|
|
66
|
+
* 每个测试写入一个 ``.har`` 文件和一个 ``.curl.txt`` 文件。
|
|
67
|
+
* 将较大的 body 移到 sidecar 文件,而不是撑大 HAR 文档。
|
|
68
|
+
* 当调用方通过普通的 ``read``/``iter_content``/``iter_bytes`` API 消费 streaming
|
|
69
|
+
响应时,惰性地记录它们。
|
|
70
|
+
* 对 307/308 重定向跳转复用捕获的负载,使重放保留原始字节。
|
|
71
|
+
* 通过使用 sidecar 和 ``--data-binary``,对非 UTF-8 请求 body 强制进行字节精确的
|
|
72
|
+
cURL 重放。
|
|
73
|
+
|
|
74
|
+
环境要求
|
|
75
|
+
------------
|
|
76
|
+
|
|
77
|
+
* Python >= 3.8
|
|
78
|
+
* pytest >= 8
|
|
79
|
+
* 被测项目中至少包含 ``requests``、``httpx`` 或 ``aiohttp`` 之一
|
|
80
|
+
|
|
81
|
+
安装
|
|
82
|
+
------------
|
|
83
|
+
|
|
84
|
+
从项目目录安装插件::
|
|
85
|
+
|
|
86
|
+
pip install -e .
|
|
87
|
+
|
|
88
|
+
用于开发(包含测试依赖)::
|
|
89
|
+
|
|
90
|
+
pip install -e ".[test]"
|
|
91
|
+
|
|
92
|
+
用法
|
|
93
|
+
-----
|
|
94
|
+
|
|
95
|
+
运行 pytest 并加上 ``--capture-traffic`` 以启用捕获::
|
|
96
|
+
|
|
97
|
+
pytest --capture-traffic
|
|
98
|
+
|
|
99
|
+
捕获默认处于禁用状态。可用选项:
|
|
100
|
+
|
|
101
|
+
``--capture-dir DIR``
|
|
102
|
+
输出目录。默认为相对于 pytest 根目录的 ``captured_traffic``。
|
|
103
|
+
|
|
104
|
+
``--capture-max-inline-bytes N``
|
|
105
|
+
保留在 HAR 文件中内联的最大 body 大小。更大的 body 会写入 ``bodies/``。
|
|
106
|
+
默认为 1 MiB。``0`` 会取消对文本请求 body 和响应 body 的大小限制。二进制和
|
|
107
|
+
multipart 请求 body 始终写入 sidecar 文件,以便 cURL 能够重放完全一致的字节。
|
|
108
|
+
|
|
109
|
+
``--capture-max-body-bytes N``
|
|
110
|
+
每个请求或响应 body 捕获的最大字节数。更大的 body 会在产物中被截断,并标记
|
|
111
|
+
``_truncated`` 和 ``_size``;cURL 重放会被标注为不完整。默认为 ``0`` (无限制)。
|
|
112
|
+
|
|
113
|
+
同样的设置也可以在 ``pytest.ini`` 中配置::
|
|
114
|
+
|
|
115
|
+
[pytest]
|
|
116
|
+
capture_traffic_dir = captured_traffic
|
|
117
|
+
capture_traffic_max_inline_bytes = 1048576
|
|
118
|
+
capture_traffic_max_body_bytes = 0
|
|
119
|
+
|
|
120
|
+
输出
|
|
121
|
+
------
|
|
122
|
+
|
|
123
|
+
::
|
|
124
|
+
|
|
125
|
+
captured_traffic/
|
|
126
|
+
|-- traffic_tests_test_api.py_test_login_ab12cd34.har
|
|
127
|
+
|-- traffic_tests_test_api.py_test_login_ab12cd34.curl.txt
|
|
128
|
+
`-- bodies/
|
|
129
|
+
|
|
130
|
+
.. warning::
|
|
131
|
+
|
|
132
|
+
只要安装了本插件,捕获目录就会被排除在 pytest 收集之外,即使没有传入
|
|
133
|
+
``--capture-traffic`` 也是如此。请为 ``--capture-dir`` 使用一个专用的子目录:
|
|
134
|
+
不支持将其指向项目根目录(``.``)或包含真实测试的目录。产物名称以 ``traffic_``
|
|
135
|
+
开头,因此不会匹配 doctest 默认的 ``test*.txt`` glob。旧版本遗留的产物会保留其
|
|
136
|
+
旧名称;升级后请删除一次。
|
|
137
|
+
|
|
138
|
+
HAR 文件可以导入 Chrome DevTools 的 Network 面板。名称以 ``_`` 开头的字段(例如
|
|
139
|
+
``_file``、``_streaming``、``_truncated``、``_redirects_truncated``、
|
|
140
|
+
``_headers_synthesized`` 和 ``_wireFileName``)是用于 sidecar 引用和捕获元数据的
|
|
141
|
+
HAR 扩展字段;浏览器查看器会忽略它们。存储在 ``bodies/`` 中的 body 通过 ``_file``
|
|
142
|
+
引用,浏览器 HAR 查看器不会显示它们;请打开 sidecar 文件来查看。
|
|
143
|
+
|
|
144
|
+
``.curl.txt`` 中的命令使用 POSIX shell 语法(bash/zsh/Git Bash),并通过生成它们的
|
|
145
|
+
机器上的绝对路径来引用 sidecar body。在 WSL 上,重放前请用 ``wslpath`` 改写这些路径。
|
|
146
|
+
|
|
147
|
+
已知限制
|
|
148
|
+
-----------------
|
|
149
|
+
|
|
150
|
+
* streaming body 通过普通的 ``read`` 和字节迭代器 API 收集。直接使用 httpx 的
|
|
151
|
+
``iter_raw()`` 或 ``requests`` 的 ``response.raw`` 会绕过收集。
|
|
152
|
+
* requests、httpx 和 aiohttp 的重定向链都会被记录。无法完成的链会标记
|
|
153
|
+
``_redirects_truncated``;从未到达传输层的请求会标记 ``_not_sent``。
|
|
154
|
+
* aiohttp 在传输时会对 multipart 文件名进行百分号编码。HAR 保留解码后的
|
|
155
|
+
``fileName`` 以便阅读,并将传输形式存储在 ``_wireFileName`` 中;cURL 重放使用
|
|
156
|
+
传输形式。
|
|
157
|
+
* 当 aiohttp 在生成的请求上暴露编码后的 ``bodySize`` 时,aiohttp ``FormData``
|
|
158
|
+
请求会报告实测的编码 ``bodySize``;否则回退为 ``-1``。
|
|
159
|
+
* ``requests`` 的生成器和不可 seek 的流式上传会在客户端消费时通过 tee 捕获。如果
|
|
160
|
+
客户端提前停止,捕获的 body 会标记 ``_truncated``,重放不完整。
|
|
161
|
+
* 从 ``requests`` 响应钩子发起的嵌套请求会被捕获;重定向跳转由外层调用记录一次。
|
|
162
|
+
显式传入 ``allow_redirects=False`` 的嵌套请求与重定向跳转无法区分,不会单独记录。
|
|
163
|
+
* 包含 ``=``、``;``、引号或控制字符的 multipart 字段名无法通过 curl 的 ``-F``
|
|
164
|
+
表达;这些请求改用原始 multipart body 和原始 ``Content-Type`` 头来重放。
|
|
165
|
+
* curl 在重放时会规范化 multipart Content-Type 参数值(例如 ``charset="utf-8"``);
|
|
166
|
+
HAR 保留完全一致的头。
|
|
167
|
+
* 非 ASCII 请求 body 和表单字段通过 sidecar 文件重放。从 Git Bash 调用时,原生
|
|
168
|
+
Windows ``curl`` 仍可能对非 ASCII 的 sidecar 路径或 multipart 文件名编码错误;
|
|
169
|
+
请优先使用 ASCII 捕获目录,或设置 ``MSYS2_ARG_CONV_EXCL='*'``。
|
|
170
|
+
* 在 Windows Git Bash 上,MSYS 路径转换可能改写看起来像 POSIX 路径的参数(例如
|
|
171
|
+
``@/etc/passwd``);重放此类捕获命令时请设置 ``MSYS2_ARG_CONV_EXCL='*'``。
|
|
172
|
+
* 生成的 cURL 命令会为捕获的 body 省略 ``Content-Length``,以便 curl 重新计算它。
|
|
173
|
+
没有 body 的请求可能仍保留 ``Content-Length: 0``。
|
|
174
|
+
* HAR ``timings`` 将实测总时长报告为 ``wait``:requests、httpx 和 aiohttp 都不暴露
|
|
175
|
+
首字节时间或传输时长。httpx/aiohttp 记录的重定向跳转共享相同的起始时间,只有最后
|
|
176
|
+
一跳携带实测时长。
|
|
177
|
+
* ``pytest`` 9.2.0 在 Python 3.10.0 上收集时会因上游 ``NodeId`` 错误而失败;请在该
|
|
178
|
+
解释器上固定 ``pytest<9.2``。即使没有本插件也会发生此失败。
|
|
179
|
+
* 捕获的 streaming body、生成器上传和可 seek 的上传快照会在内存中缓冲,直到测试
|
|
180
|
+
结束。设置 ``--capture-max-body-bytes`` 可限制捕获的部分;客户端仍会收到完整
|
|
181
|
+
body,但产物会被标记为截断,cURL 重放不完整。
|
|
182
|
+
* aiohttp 错误条目会重建 aiohttp 自动添加的头(``Accept``、``Accept-Encoding``、
|
|
183
|
+
``User-Agent``、``Host`` 和 cookies),并标记 ``_headers_synthesized``。
|
|
184
|
+
|
|
185
|
+
.. warning::
|
|
186
|
+
|
|
187
|
+
捕获的产物包含请求和响应头、cookies、查询字符串和 body。它们可能包含凭据或个人
|
|
188
|
+
数据。请勿将 ``captured_traffic/`` 提交到版本控制。文件按进程 umask 创建;在多
|
|
189
|
+
用户主机上,请限制对捕获目录的访问。
|
|
190
|
+
|
|
191
|
+
开发
|
|
192
|
+
-----------
|
|
193
|
+
|
|
194
|
+
运行测试套件::
|
|
195
|
+
|
|
196
|
+
pip install tox
|
|
197
|
+
tox
|
|
198
|
+
|
|
199
|
+
测试套件包含一个本地 HTTP 服务器、重定向/上传/下载场景以及 cURL 重放测试(当
|
|
200
|
+
``bash`` 不可用时会跳过重放)。``aiohttp39`` 环境用于检查与 aiohttp 3.9 的兼容性。
|
|
201
|
+
|
|
202
|
+
许可证
|
|
203
|
+
-------
|
|
204
|
+
|
|
205
|
+
基于 MIT 许可证条款分发。
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
pytest-traffic-capture
|
|
2
|
+
======================
|
|
3
|
+
|
|
4
|
+
在 pytest 运行期间捕获 ``requests``、``httpx`` 和 ``aiohttp`` 的流量,并将其写为
|
|
5
|
+
HAR 1.2 文档以及可重放的 cURL 命令。
|
|
6
|
+
|
|
7
|
+
功能特性
|
|
8
|
+
--------
|
|
9
|
+
|
|
10
|
+
* 捕获使用 ``requests``、``httpx`` (同步和异步)以及 ``aiohttp`` 进行的 HTTP
|
|
11
|
+
交换。
|
|
12
|
+
* 每个测试写入一个 ``.har`` 文件和一个 ``.curl.txt`` 文件。
|
|
13
|
+
* 将较大的 body 移到 sidecar 文件,而不是撑大 HAR 文档。
|
|
14
|
+
* 当调用方通过普通的 ``read``/``iter_content``/``iter_bytes`` API 消费 streaming
|
|
15
|
+
响应时,惰性地记录它们。
|
|
16
|
+
* 对 307/308 重定向跳转复用捕获的负载,使重放保留原始字节。
|
|
17
|
+
* 通过使用 sidecar 和 ``--data-binary``,对非 UTF-8 请求 body 强制进行字节精确的
|
|
18
|
+
cURL 重放。
|
|
19
|
+
|
|
20
|
+
环境要求
|
|
21
|
+
------------
|
|
22
|
+
|
|
23
|
+
* Python >= 3.8
|
|
24
|
+
* pytest >= 8
|
|
25
|
+
* 被测项目中至少包含 ``requests``、``httpx`` 或 ``aiohttp`` 之一
|
|
26
|
+
|
|
27
|
+
安装
|
|
28
|
+
------------
|
|
29
|
+
|
|
30
|
+
从项目目录安装插件::
|
|
31
|
+
|
|
32
|
+
pip install -e .
|
|
33
|
+
|
|
34
|
+
用于开发(包含测试依赖)::
|
|
35
|
+
|
|
36
|
+
pip install -e ".[test]"
|
|
37
|
+
|
|
38
|
+
用法
|
|
39
|
+
-----
|
|
40
|
+
|
|
41
|
+
运行 pytest 并加上 ``--capture-traffic`` 以启用捕获::
|
|
42
|
+
|
|
43
|
+
pytest --capture-traffic
|
|
44
|
+
|
|
45
|
+
捕获默认处于禁用状态。可用选项:
|
|
46
|
+
|
|
47
|
+
``--capture-dir DIR``
|
|
48
|
+
输出目录。默认为相对于 pytest 根目录的 ``captured_traffic``。
|
|
49
|
+
|
|
50
|
+
``--capture-max-inline-bytes N``
|
|
51
|
+
保留在 HAR 文件中内联的最大 body 大小。更大的 body 会写入 ``bodies/``。
|
|
52
|
+
默认为 1 MiB。``0`` 会取消对文本请求 body 和响应 body 的大小限制。二进制和
|
|
53
|
+
multipart 请求 body 始终写入 sidecar 文件,以便 cURL 能够重放完全一致的字节。
|
|
54
|
+
|
|
55
|
+
``--capture-max-body-bytes N``
|
|
56
|
+
每个请求或响应 body 捕获的最大字节数。更大的 body 会在产物中被截断,并标记
|
|
57
|
+
``_truncated`` 和 ``_size``;cURL 重放会被标注为不完整。默认为 ``0`` (无限制)。
|
|
58
|
+
|
|
59
|
+
同样的设置也可以在 ``pytest.ini`` 中配置::
|
|
60
|
+
|
|
61
|
+
[pytest]
|
|
62
|
+
capture_traffic_dir = captured_traffic
|
|
63
|
+
capture_traffic_max_inline_bytes = 1048576
|
|
64
|
+
capture_traffic_max_body_bytes = 0
|
|
65
|
+
|
|
66
|
+
输出
|
|
67
|
+
------
|
|
68
|
+
|
|
69
|
+
::
|
|
70
|
+
|
|
71
|
+
captured_traffic/
|
|
72
|
+
|-- traffic_tests_test_api.py_test_login_ab12cd34.har
|
|
73
|
+
|-- traffic_tests_test_api.py_test_login_ab12cd34.curl.txt
|
|
74
|
+
`-- bodies/
|
|
75
|
+
|
|
76
|
+
.. warning::
|
|
77
|
+
|
|
78
|
+
只要安装了本插件,捕获目录就会被排除在 pytest 收集之外,即使没有传入
|
|
79
|
+
``--capture-traffic`` 也是如此。请为 ``--capture-dir`` 使用一个专用的子目录:
|
|
80
|
+
不支持将其指向项目根目录(``.``)或包含真实测试的目录。产物名称以 ``traffic_``
|
|
81
|
+
开头,因此不会匹配 doctest 默认的 ``test*.txt`` glob。旧版本遗留的产物会保留其
|
|
82
|
+
旧名称;升级后请删除一次。
|
|
83
|
+
|
|
84
|
+
HAR 文件可以导入 Chrome DevTools 的 Network 面板。名称以 ``_`` 开头的字段(例如
|
|
85
|
+
``_file``、``_streaming``、``_truncated``、``_redirects_truncated``、
|
|
86
|
+
``_headers_synthesized`` 和 ``_wireFileName``)是用于 sidecar 引用和捕获元数据的
|
|
87
|
+
HAR 扩展字段;浏览器查看器会忽略它们。存储在 ``bodies/`` 中的 body 通过 ``_file``
|
|
88
|
+
引用,浏览器 HAR 查看器不会显示它们;请打开 sidecar 文件来查看。
|
|
89
|
+
|
|
90
|
+
``.curl.txt`` 中的命令使用 POSIX shell 语法(bash/zsh/Git Bash),并通过生成它们的
|
|
91
|
+
机器上的绝对路径来引用 sidecar body。在 WSL 上,重放前请用 ``wslpath`` 改写这些路径。
|
|
92
|
+
|
|
93
|
+
已知限制
|
|
94
|
+
-----------------
|
|
95
|
+
|
|
96
|
+
* streaming body 通过普通的 ``read`` 和字节迭代器 API 收集。直接使用 httpx 的
|
|
97
|
+
``iter_raw()`` 或 ``requests`` 的 ``response.raw`` 会绕过收集。
|
|
98
|
+
* requests、httpx 和 aiohttp 的重定向链都会被记录。无法完成的链会标记
|
|
99
|
+
``_redirects_truncated``;从未到达传输层的请求会标记 ``_not_sent``。
|
|
100
|
+
* aiohttp 在传输时会对 multipart 文件名进行百分号编码。HAR 保留解码后的
|
|
101
|
+
``fileName`` 以便阅读,并将传输形式存储在 ``_wireFileName`` 中;cURL 重放使用
|
|
102
|
+
传输形式。
|
|
103
|
+
* 当 aiohttp 在生成的请求上暴露编码后的 ``bodySize`` 时,aiohttp ``FormData``
|
|
104
|
+
请求会报告实测的编码 ``bodySize``;否则回退为 ``-1``。
|
|
105
|
+
* ``requests`` 的生成器和不可 seek 的流式上传会在客户端消费时通过 tee 捕获。如果
|
|
106
|
+
客户端提前停止,捕获的 body 会标记 ``_truncated``,重放不完整。
|
|
107
|
+
* 从 ``requests`` 响应钩子发起的嵌套请求会被捕获;重定向跳转由外层调用记录一次。
|
|
108
|
+
显式传入 ``allow_redirects=False`` 的嵌套请求与重定向跳转无法区分,不会单独记录。
|
|
109
|
+
* 包含 ``=``、``;``、引号或控制字符的 multipart 字段名无法通过 curl 的 ``-F``
|
|
110
|
+
表达;这些请求改用原始 multipart body 和原始 ``Content-Type`` 头来重放。
|
|
111
|
+
* curl 在重放时会规范化 multipart Content-Type 参数值(例如 ``charset="utf-8"``);
|
|
112
|
+
HAR 保留完全一致的头。
|
|
113
|
+
* 非 ASCII 请求 body 和表单字段通过 sidecar 文件重放。从 Git Bash 调用时,原生
|
|
114
|
+
Windows ``curl`` 仍可能对非 ASCII 的 sidecar 路径或 multipart 文件名编码错误;
|
|
115
|
+
请优先使用 ASCII 捕获目录,或设置 ``MSYS2_ARG_CONV_EXCL='*'``。
|
|
116
|
+
* 在 Windows Git Bash 上,MSYS 路径转换可能改写看起来像 POSIX 路径的参数(例如
|
|
117
|
+
``@/etc/passwd``);重放此类捕获命令时请设置 ``MSYS2_ARG_CONV_EXCL='*'``。
|
|
118
|
+
* 生成的 cURL 命令会为捕获的 body 省略 ``Content-Length``,以便 curl 重新计算它。
|
|
119
|
+
没有 body 的请求可能仍保留 ``Content-Length: 0``。
|
|
120
|
+
* HAR ``timings`` 将实测总时长报告为 ``wait``:requests、httpx 和 aiohttp 都不暴露
|
|
121
|
+
首字节时间或传输时长。httpx/aiohttp 记录的重定向跳转共享相同的起始时间,只有最后
|
|
122
|
+
一跳携带实测时长。
|
|
123
|
+
* ``pytest`` 9.2.0 在 Python 3.10.0 上收集时会因上游 ``NodeId`` 错误而失败;请在该
|
|
124
|
+
解释器上固定 ``pytest<9.2``。即使没有本插件也会发生此失败。
|
|
125
|
+
* 捕获的 streaming body、生成器上传和可 seek 的上传快照会在内存中缓冲,直到测试
|
|
126
|
+
结束。设置 ``--capture-max-body-bytes`` 可限制捕获的部分;客户端仍会收到完整
|
|
127
|
+
body,但产物会被标记为截断,cURL 重放不完整。
|
|
128
|
+
* aiohttp 错误条目会重建 aiohttp 自动添加的头(``Accept``、``Accept-Encoding``、
|
|
129
|
+
``User-Agent``、``Host`` 和 cookies),并标记 ``_headers_synthesized``。
|
|
130
|
+
|
|
131
|
+
.. warning::
|
|
132
|
+
|
|
133
|
+
捕获的产物包含请求和响应头、cookies、查询字符串和 body。它们可能包含凭据或个人
|
|
134
|
+
数据。请勿将 ``captured_traffic/`` 提交到版本控制。文件按进程 umask 创建;在多
|
|
135
|
+
用户主机上,请限制对捕获目录的访问。
|
|
136
|
+
|
|
137
|
+
开发
|
|
138
|
+
-----------
|
|
139
|
+
|
|
140
|
+
运行测试套件::
|
|
141
|
+
|
|
142
|
+
pip install tox
|
|
143
|
+
tox
|
|
144
|
+
|
|
145
|
+
测试套件包含一个本地 HTTP 服务器、重定向/上传/下载场景以及 cURL 重放测试(当
|
|
146
|
+
``bash`` 不可用时会跳过重放)。``aiohttp39`` 环境用于检查与 aiohttp 3.9 的兼容性。
|
|
147
|
+
|
|
148
|
+
许可证
|
|
149
|
+
-------
|
|
150
|
+
|
|
151
|
+
基于 MIT 许可证条款分发。
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pytest-traffic-capture"
|
|
7
|
+
description = "Capture requests/httpx/aiohttp traffic during tests as HAR 1.2 and cURL commands"
|
|
8
|
+
version = "0.1.0"
|
|
9
|
+
readme = "README.rst"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "pytest-traffic-capture contributors" },
|
|
13
|
+
]
|
|
14
|
+
maintainers = [
|
|
15
|
+
{ name = "pytest-traffic-capture contributors" },
|
|
16
|
+
]
|
|
17
|
+
license = {file = "LICENSE"}
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Framework :: Pytest",
|
|
20
|
+
"Development Status :: 4 - Beta",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"Topic :: Software Development :: Testing",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Programming Language :: Python",
|
|
25
|
+
"Programming Language :: Python :: 3.8",
|
|
26
|
+
"Programming Language :: Python :: 3.9",
|
|
27
|
+
"Programming Language :: Python :: 3.10",
|
|
28
|
+
"Programming Language :: Python :: 3.11",
|
|
29
|
+
"Programming Language :: Python :: 3.12",
|
|
30
|
+
"Programming Language :: Python :: 3.13",
|
|
31
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
32
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
33
|
+
"Programming Language :: Python :: Implementation :: PyPy",
|
|
34
|
+
"License :: OSI Approved :: MIT License",
|
|
35
|
+
]
|
|
36
|
+
dependencies = [
|
|
37
|
+
"pytest>=8",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.optional-dependencies]
|
|
41
|
+
test = [
|
|
42
|
+
"requests",
|
|
43
|
+
"httpx",
|
|
44
|
+
"aiohttp",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[project.entry-points.pytest11]
|
|
48
|
+
pytest_traffic_capture = "pytest_traffic_capture.plugin"
|
|
49
|
+
|
|
50
|
+
[tool.setuptools.packages.find]
|
|
51
|
+
where = ["src"]
|
|
52
|
+
include = ["pytest_traffic_capture*"]
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
testpaths = ["tests"]
|