flask客戶端測試使用設置cookie參數


今天在對flask客戶端進行測試,然后看到我們服務器端用請求前鈎子寫了這樣的代碼

@app.before_request
def before_request():
  
  session = request.cookies.get('P', '')

這段代碼的意思是,在將請求發向flask路由的之前先運行這段代碼,我們都知道要在requests里面設置cookies的參數只需要

import requests

print requests.request('get', 'http://nuanhi.com', cookies={'P': '123'}).text

只要在request方法里面帶上cookies字典就可以讓上面語句獲得對應內容。

問題是,要如何讓flask 測試客戶端也能設置上cookies這個參數呢。

self.app_client.set_cookie(domain, cookie_name, cookie_value)

其中 domain 可以自己填,第二個參數是cookie的name 這里就是'P' 然后后面設置上值就行了

 

 

Reference:

http://stackoverflow.com/questions/16282576/flask-unit-test-send-cookies-after-modifying-the-session  flask-unit-test-send-cookies-after-modifying-the-session

http://stackoverflow.com/questions/14890478/how-do-i-send-cookies-with-request-when-testing-flask-applications-through-noset  how-do-i-send-cookies-with-request-when-testing-flask-applications-through-noset


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM