在Python中,是否存在模拟httplib响应的库?
作者:互联网
我目前正在使用python_flickr_api为我的应用上传照片:它使用httplib来执行多部分POST请求.
问题:我想通过拦截POST请求并创建预先成功的响应来验证上传是否真的是在集成测试中发出,这样我的测试就可以完全脱机运行而不依赖于Flickr(我不想上传相同的测试图像100次,或者!)
为此,我尝试使用两个令人难以置信的库:VCRPy和HTTPretty.它们都没有解决我的问题,因为它们都不支持httplib(HTTPretty最接近,只支持httplib2),我得到的错误看起来像这个:
Failure/Error: [Errno 32] Broken pipe
Traceback:
...
File "/usr/local/lib/python2.7/site-packages/flickr_api/upload.py", line 92, in upload
r = post(UPLOAD_URL,auth.AUTH_HANDLER,args,photo_file)
File "/usr/local/lib/python2.7/site-packages/flickr_api/upload.py", line 52, in post
r = multipart.posturl(url,fields,files)
File "/usr/local/lib/python2.7/site-packages/flickr_api/multipart.py", line 19, in posturl
return post_multipart(urlparts[1], urlparts[2], fields,files)
File "/usr/local/lib/python2.7/site-packages/flickr_api/multipart.py", line 33, in post_multipart
h.send(body)
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 805, in send
self.sock.sendall(data)
File "/usr/local/lib/python2.7/site-packages/httpretty/core.py", line 243, in sendall
return self._true_sendall(data)
File "/usr/local/lib/python2.7/site-packages/httpretty/core.py", line 216, in _true_sendall
self.truesock.sendall(data, *args, **kw)
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
很明显,httpretty正在拦截但导致管道损坏.
我怎样才能解决这个问题?
解决方法:
我是VCR.py的作者.我想我可能已经修复了阻止VCR使用您正在使用的flickr API库的错误.尝试在fix-flickr-api branch中安装该版本,并告诉我这是否适合您.
标签:python,mocking,httplib,fakeweb 来源: https://codeday.me/bug/20190728/1565741.html