2017-08-09 93 views
1

如何在燒瓶test_request_context中設置remote_addr屬性?這個問題:Get IP Address when testing flask application through nosetests(和其他類似問題)解釋瞭如何使用get/post/whatever測試客戶端調用來完成它,但我沒有使用這些(在這種情況下)。相反,我得到一個test_request_context,然後調用一個函數,從而允許我單獨測試由我的視圖函數調用的函數。在燒瓶中設置remote_addr test_request_context

編輯:爲了澄清,我的測試代碼看起來是這樣的:

with app.test_request_context(): 
    result=my_function_which_expects_to_be_called_from_a_request_context() 
<check result however> 

所以在任何時候,我使用的測試客戶端調用。

回答

1

將相同的參數傳遞給test_request_context,與client.get一樣。兩者都在內部以相同的方式設置WSGI環境。

with app.test_request_context(environ_base={'REMOTE_ADDR': '10.1.2.3'}):