簡體   English   中英

django.test.Client和response.content與streaming_content

[英]django.test.Client and response.content vs. streaming_content

我有一個Django測試,該測試使用Django測試客戶端訪問網頁。

在其中一項測試中,服務器返回一個ZIP文件作為附件。 我使用以下代碼訪問ZIP文件的內容:

zip_content = StringIO(response.content)
zip = ZipFile(zip_content)

這將導致以下棄用警告:

D:/ Developments / Archaeology / DB / ArtefactDatabase / Webserver \\ importexport \\ tests \\ test_import.py:1:棄用警告:不贊成在流式響應上訪問content屬性。 請改用streaming_content屬性。

response.streaming_content返回某種映射,這絕對不是ZipFile所需的類似文件的對象。 我該如何使用streaming_content屬性?

順便說一句,我只是路過時,你得到的棄用警告response.contentStringIO ,當我訪問一個普通的HTML頁面的response.content,沒有警告。

使用Python 3.4。

與字符串:

zip_content = io.StringIO("".join(response.streaming_content))
zip = ZipFile(zip_content)

與字節:

zip_content = io.BytesIO(b"".join(response.streaming_content))
zip = ZipFile(zip_content)

https://github.com/sio2project/oioioi/blob/master/oioioi/filetracker/tests.py的 TestStreamingMixin中找到的解決方案

另請參閱: https : //docs.djangoproject.com/en/1.7/ref/request-response/

您可能想通過檢查response.streaming (布爾值)來測試響應是否為流。

您應該更改測試方法。 response.streaming_content完全按照其意圖進行操作。 只需測試就可以下載。

如果要測試文件生成/完整性方法,則需要單獨測試其功能。 無論您的文件是Django Test的ZIP還是CSV都沒有關系,但是對您的調用是否可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM