[英]Mocking download of a file using Python requests and responses
我有一些 python 代码,它使用requests从 URL 成功下载图像,并将其保存到/tmp/
。 我想测试它应该做什么。 我正在使用响应来测试 JSON 文件的获取,但我不确定如何模拟获取文件的行为。
我认为它类似于模拟标准响应,如下所示,但我想我对如何将body
设置为文件感到茫然......
@responses.activate
def test_download():
responses.add(responses.GET, 'http://example.org/images/my_image.jpg',
body='', status=200,
content_type='image/jpeg')
#...
更新:按照 Ashafix 的评论,我正在尝试这个(python 3):
from io import BytesIO
@responses.activate
def test_download():
with open('tests/images/tester.jpg', 'rb') as img1:
imgIO = BytesIO(img1.read())
responses.add(responses.GET, 'http://example.org/images/my_image.jpg',
body=imgIO, status=200,
content_type='image/jpeg')
imgIO.seek(0)
#...
但是,随后,当我正在测试的代码尝试执行我收到的请求时:
a bytes-like object is required, not '_io.BytesIO'
感觉几乎是对的,但我很难过。
更新 2:尝试遵循 Steve Jessop 的建议:
@responses.activate
def test_download():
with open('tests/images/tester.jpg', 'rb') as img1:
responses.add(responses.GET, 'http://example.org/images/my_image.jpg',
body=img1.read(), status=200,
content_type='image/jpeg')
#...
但这一次被测试的代码提出了这个:
I/O operation on closed file.
当然图像应该仍然在with
块内打开吗?
更新 3:我正在测试的代码是这样的:
r = requests.get(url, stream=True)
if r.status_code == 200:
with open('/tmp/temp.jpg', 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
似乎最后的shutil
行正在生成“对已关闭文件的 I/O 操作”。 错误。 我不太了解这一点 - 文件的流媒体 - 不知道如何最好地模拟这种行为,以测试下载的文件是否保存到/tmp/
。
您可能需要将stream=True
传递给responses.add
调用。 类似的东西:
@responses.activate
def test_download():
with open("tests/images/tester.jpg", "rb") as img1:
responses.add(
responses.GET,
"http://example.org/images/my_image.jpg",
body=img1.read(),
status=200,
content_type="image/jpeg",
stream=True,
)
首先,总结一下我现在过长的问题......我正在测试一些类似的代码:
def download_file(url):
r = requests.get(url, stream=True)
if r.status_code == 200:
filename = os.path.basename(url)
with open('/tmp/%s' % filename, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
return filename
它下载一个图像,并流式传输它,将其保存到/tmp/
。 我想模拟这个请求,这样我就可以测试其他东西。
@responses.activate
def test_downloads_file(self):
url = 'http://example.org/test.jpg'
with open('tests/images/tester.jpg', 'rb') as img:
responses.add(responses.GET, url,
body=img.read(), status=200,
content_type='image/jpg',
adding_headers={'Transfer-Encoding': 'chunked'})
filename = download_file(url)
# assert things here.
一旦我找到了为此使用open()
的方法,我仍然收到“关闭文件上的 I/O 操作”。 从shutil.copyfileobj()
。 停止此操作的是添加Transfer-Encoding
标头,当我发出真正的请求时,该标头出现在标头中。
非常欢迎对其他更好的解决方案的任何建议!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.