Skip to content

Commit

Permalink
set session accessed for setdefault
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed May 20, 2017
1 parent ae133aa commit 5d9dd0b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
5 changes: 5 additions & 0 deletions flask/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def get(self, key, default=None):
self.accessed = True
return super(SecureCookieSession, self).get(key, default)

def setdefault(self, key, default=None):
self.accessed = True
return super(SecureCookieSession, self).setdefault(key, default)


class NullSession(SecureCookieSession):
"""Class used to generate nicer error messages if sessions are not
available. Will still allow read-only access to the empty session
Expand Down
40 changes: 22 additions & 18 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,38 +533,42 @@ def test_session_vary_cookie():
app = flask.Flask(__name__)
app.secret_key = 'testkey'

@app.route('/set-session')
@app.route('/set')
def set_session():
flask.session['test'] = 'test'
return ''

@app.route('/get-session')
def get_session():
s = flask.session.get('test')
return ''
@app.route('/get')
def get():
return flask.session.get('test')

@app.route('/get-session-with-dictionary')
def get_session_with_dictionary():
s = flask.session['test']
return ''
@app.route('/getitem')
def getitem():
return flask.session['test']

@app.route('/setdefault')
def setdefault():
return flask.session.setdefault('test', 'default')

@app.route('/no-vary-header')
def no_vary_header():
return ''

c = app.test_client()

rv = c.get('/set-session')
assert rv.headers['Vary'] == 'Cookie'

rv = c.get('/get-session')
assert rv.headers['Vary'] == 'Cookie'
def expect(path, header=True):
rv = c.get(path)

rv = c.get('/get-session-with-dictionary')
assert rv.headers['Vary'] == 'Cookie'
if header:
assert rv.headers['Vary'] == 'Cookie'
else:
assert 'Vary' not in rv.headers

rv = c.get('/no-vary-header')
assert 'Vary' not in rv.headers
expect('/set')
expect('/get')
expect('/getitem')
expect('/setdefault')
expect('/no-vary-header', False)


def test_flashes():
Expand Down

0 comments on commit 5d9dd0b

Please sign in to comment.