Skip to content

Commit 6ffb8a8

Browse files
Cookie path support in session and test client
1 parent 0151611 commit 6ffb8a8

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

src/microdot/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def index(request, session):
117117
"""
118118
@request.after_request
119119
def _delete_session(request, response):
120-
response.delete_cookie('session')
120+
response.delete_cookie('session', **self.cookie_options)
121121
return response
122122

123123
def encode(self, payload, secret_key=None):

src/microdot/test_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,22 @@ def _update_cookies(self, res):
156156
age = 0
157157
if age <= 0:
158158
delete = True
159-
break
160159
elif option.startswith('expires='):
161160
_, e = option.split('=', 1)
162161
# this is a very limited parser for cookie expiry
163162
# that only detects a cookie deletion request when
164163
# the date is 1/1/1970
165164
if '1 jan 1970' in e.lower(): # pragma: no branch
166165
delete = True
167-
break
168166
elif option.startswith('path='):
169167
_, path = option.split('=', 1)
170168
if delete:
171169
if cookie_name in self.cookies: # pragma: no branch
172-
del self.cookies[cookie_name]
170+
cookie_path = self.cookies[cookie_name][1] \
171+
if isinstance(self.cookies[cookie_name], tuple) \
172+
else '/'
173+
if path == cookie_path:
174+
del self.cookies[cookie_name]
173175
else:
174176
if path == '/':
175177
self.cookies[cookie_name] = cookie_options[0]

tests/test_microdot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def index(req):
203203
req.cookies['one'] + req.cookies['two'] + req.cookies['three'])
204204
res.set_cookie('four', '4')
205205
res.delete_cookie('two', path='/')
206+
res.delete_cookie('one', path='/bad')
206207
return res
207208

208209
client = TestClient(app, cookies={'one': '1', 'two': '2'})

tests/test_response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def test_cookies(self):
193193
expires='Tue, 05 Nov 2019 02:23:54 GMT', max_age=123,
194194
secure=True, http_only=True)
195195
res.delete_cookie('foo8', http_only=True)
196+
res.delete_cookie('foo9', path='/s')
196197
self.assertEqual(res.headers, {'Set-Cookie': [
197198
'foo1=bar1',
198199
'foo2=bar2; Path=/; Partitioned',
@@ -205,6 +206,8 @@ def test_cookies(self):
205206
'HttpOnly',
206207
('foo8=; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0; '
207208
'HttpOnly'),
209+
('foo9=; Path=/s; Expires=Thu, 01 Jan 1970 00:00:01 GMT; '
210+
'Max-Age=0'),
208211
]})
209212

210213
def test_redirect(self):

tests/test_session.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def index(req):
8585

8686
def test_session_default_path(self):
8787
app = Microdot()
88-
session_ext.initialize(app, secret_key='some-other-secret')
88+
Session(app, secret_key='some-other-secret')
8989
client = TestClient(app)
9090

9191
@app.get('/')
@@ -100,15 +100,26 @@ def index(req, session):
100100
def child(req, session):
101101
return str(session.get('foo'))
102102

103+
@app.get('/delete')
104+
@with_session
105+
def delete(req, session):
106+
session.delete()
107+
return ''
108+
103109
res = self._run(client.get('/'))
104110
self.assertEqual(res.status_code, 200)
105111
res = self._run(client.get('/child'))
106112
self.assertEqual(res.text, 'bar')
113+
res = self._run(client.get('/delete'))
114+
res = self._run(client.get('/child'))
115+
self.assertEqual(res.text, 'None')
107116

108117
def test_session_custom_path(self):
109118
app = Microdot()
119+
session_ext = Session()
110120
session_ext.initialize(app, secret_key='some-other-secret',
111-
cookie_options={'path': '/child'})
121+
cookie_options={'path': '/child',
122+
'http_only': False})
112123
client = TestClient(app)
113124

114125
@app.get('/')
@@ -128,9 +139,20 @@ def child(req, session):
128139
def foo(req, session):
129140
return str(session.get('foo'))
130141

142+
@app.get('/child/delete')
143+
@with_session
144+
def delete(req, session):
145+
session.delete()
146+
return ''
147+
131148
res = self._run(client.get('/child'))
132149
self.assertEqual(res.status_code, 200)
133150
res = self._run(client.get('/'))
134151
self.assertEqual(res.text, 'None')
135152
res = self._run(client.get('/child/foo'))
136153
self.assertEqual(res.text, 'bar')
154+
res = self._run(client.get('/child/delete'))
155+
res = self._run(client.get('/'))
156+
self.assertEqual(res.text, 'None')
157+
res = self._run(client.get('/child/foo'))
158+
self.assertEqual(res.text, 'None')

0 commit comments

Comments
 (0)