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
- フォーク
- 390
- 平均マージ
- 1日 17時間
- マージ済み PR(30日)
- 2
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
getsentry/responses のほかの issue
-
json_params_matcher fails on falsy top-level JSON primitives (0, False, "")対応中かも @mayureshsonawane17 が 22 日前に担当しました。 オープン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 件 ·
メンテナーはふだん 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 1週間以上 初心者へのやさしさ 30/100
メンテナーはふだん 3 日以内に返信
getsentry/responses の issue をすべて見る
似ている issue
-
[Bug]: AdaLAM's draw_first_k_couples repeats couples already drawn instead of the next ones (8 of the default 128 RANSAC hypotheses)対応中かも @1sakshm が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
TheAlgorithms/Python#15656 ·
メンテナーはふだん 2 日以内に返信
-
external integration integration-submission
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
langchain-ai/docs#7128 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
kaifcodec/user-scanner#755 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
eclipse-score/mcp-servers#61 ·
メンテナーはふだん 4 日以内に返信