Bug when record the binary response to yaml
维护者通常 3 天内回复
评估
调研方向
从复现中显示的 _recorder.record 和 _add_from_file 入口点开始,然后跟踪二进制响应如何写入 danbooru.yaml 以及如何从中读取。使用提供的图像复现下载比较,并验证模拟文件生成的字节和大小与原始响应相同。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
When recording the binary response (e.g. image), the response is broken when added from file again.
Additional context
No response
Version of responses
0.23.3
Steps to Reproduce
Use this code to record the image's response from danbooru to danbooru.yaml
import logging
import requests
from responses import _recorder
logging.basicConfig(level=logging.DEBUG)
IMAGE_URL = 'https://cdn.donmai.us/original/e1/f8/e1f815ed0d489dddda86ac55815ce37f.jpg'
@_recorder.record(file_path='danbooru.yaml')
def main():
resp = requests.get(IMAGE_URL)
resp.raise_for_status()
if __name__ == '__main__':
main()
and then use the following code to download this image both from original danbooru site (without mocking) and from mocked responses
import logging
import os
import requests
import responses
from tqdm.auto import tqdm
logging.basicConfig(level=logging.DEBUG)
def download_file(url, filename, expected_size: int = None, desc=None, **kwargs):
session = requests.session()
response = session.get(url, stream=True, allow_redirects=True, **kwargs)
expected_size = expected_size or response.headers.get('Content-Length', None)
expected_size = int(expected_size) if expected_size is not None else expected_size
desc = desc or os.path.basename(filename)
directory = os.path.dirname(filename)
if directory:
os.makedirs(directory, exist_ok=True)
with open(filename, 'wb') as f:
with tqdm(total=expected_size, unit='B', unit_scale=True, unit_divisor=1024, desc=desc) as pbar:
for chunk in response.iter_content(chunk_size=1024):
f.write(chunk)
pbar.update(len(chunk))
actual_size = os.path.getsize(filename)
if expected_size is not None and actual_size != expected_size:
os.remove(filename)
raise requests.exceptions.HTTPError(f"Downloaded file is not of expected size, "
f"{expected_size} expected but {actual_size} found.")
return filename
responses._add_from_file('danbooru.yaml')
IMAGE_URL = 'https://cdn.donmai.us/original/e1/f8/e1f815ed0d489dddda86ac55815ce37f.jpg'
def test_danbooru_origin():
download_file(IMAGE_URL, 'test_img_actual.jpg')
@responses.activate
def test_danbooru():
download_file(IMAGE_URL, 'test_img.jpg')
if __name__ == '__main__':
test_danbooru_origin()
test_danbooru()
The you'll fine the test_img_actual.jpg is okay, its size is about 833KB.
But test_img.jpg is broken, its size is about 1.47MB.
Expected Result
The expected result of file test_img.jpg should be the same as test_img_origin.jpg.
Actual Result
And now it is broken, its size is obviously abnormal.
- 主要语言
- Python
- 星标
- 4.3k
- 派生
- 389
- 平均合并
- 1 天 17 小时
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
getsentry/responses 的其他 Issue
-
json_params_matcher fails on falsy top-level JSON primitives (0, False, "")可能已有人在做 @mayureshsonawane17 于 24 天前认领。 未关闭Waiting for: Product Owner
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 3 天内回复
-
enhancement Waiting for: Product Owner
难度 4/5 3-5 天 新手友好度 35/100
getsentry/responses#783 · 1 条评论 · 3 个 reaction ·
维护者通常 3 天内回复
-
bug Waiting for: Product Owner
难度 3/5 1-2 天 新手友好度 35/100
getsentry/responses#780 · 1 条评论 ·
维护者通常 3 天内回复
-
question
难度 4/5 3-5 天 新手友好度 45/100
维护者通常 3 天内回复
-
enhancement question
难度 5/5 一周以上 新手友好度 30/100
维护者通常 3 天内回复
查看 getsentry/responses 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
ComposioHQ/awesome-claude-skills#2398 ·
维护者通常 2 天内回复
-
bug converter
难度 2/5 1-3 小时 新手友好度 62/100
grafana/sigma-rule-deployment#435 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 73/100
datamodel-code-generator/datamodel-code-generator#5589 ·
维护者通常 1 天内回复
-
Shell: serve the home app's Cookie Policy on a hard reload可能已有人在做 @2001anshkaushik 今天认领。 未关闭feature module:ai
难度 2/5 1-3 小时 新手友好度 88/100
rocketride-org/rocketride-server#2899 · 1 条评论 ·
维护者通常 2 天内回复
-
GEFSv12
难度 2/5 1-3 小时 新手友好度 70/100