0
我該如何去測試我的應用程序是否已經在pythonic的請求中設置了cookie?我現在做的方式(工作)不會覺得很方便,但在這裏它是:能夠在測試環境中「乾淨地」訪問cookie
def test_mycookie(app, client):
def getcookie(name):
try:
cookie = client.cookie_jar._cookies['mydomain.dev']['/'][name]
except KeyError:
return None
else:
return cookie
with app.test_request_context():
client.get('/non-existing-path/')
assert getcookie('mycookie') is None
client.get('/')
assert getcookie('mycookie').value == '"xyz"'
使用flask.request.cookies
對我不起作用,因爲它總是會返回一個空的字典。也許我做錯了?
def test_mycookie2(app, client):
with app.test_request_context():
client.get('/non-existing-path/')
assert 'mycookie' not in request.cookies
client.get('/')
request.cookies['mycookie'] # Raises KeyError