Support responses with more than one cookie in WSGI and ASGI extensions

This commit is contained in:
Miguel Grinberg
2022-08-04 11:20:05 +01:00
parent c9e148bd04
commit e8d16cf3f9
4 changed files with 41 additions and 11 deletions

View File

@@ -44,6 +44,11 @@ class TestMicrodotASGI(unittest.TestCase):
return Response(body=R(), headers={'Content-Length': '8'})
@app.after_request
def after_request(req, res):
res.set_cookie('foo', 'foo')
res.set_cookie('bar', 'bar', http_only=True)
scope = {
'type': 'http',
'path': '/foo/bar',
@@ -77,9 +82,13 @@ class TestMicrodotASGI(unittest.TestCase):
async def send(packet):
if packet['type'] == 'http.response.start':
self.assertEqual(packet['status'], 200)
self.assertEqual(
packet['headers'],
[('Content-Length', '8'), ('Content-Type', 'text/plain')])
expected_headers = [('Content-Length', '8'),
('Content-Type', 'text/plain'),
('Set-Cookie', 'foo=foo'),
('Set-Cookie', 'bar=bar; HttpOnly')]
self.assertEqual(len(packet['headers']), len(expected_headers))
for header in expected_headers:
self.assertIn(header, packet['headers'])
elif packet['type'] == 'http.response.body':
self.assertIn(packet['body'],
[b're', b'sp', b'on', b'se', b''])

View File

@@ -32,6 +32,11 @@ class TestMicrodotWSGI(unittest.TestCase):
self.assertEqual(req.body, b'body')
return 'response'
@app.after_request
def after_request(req, resp):
resp.set_cookie('foo', 'foo')
resp.set_cookie('bar', 'bar', http_only=True)
environ = {
'SCRIPT_NAME': '/foo',
'PATH_INFO': '/bar',
@@ -48,9 +53,13 @@ class TestMicrodotWSGI(unittest.TestCase):
def start_response(status, headers):
self.assertEqual(status, '200 OK')
self.assertEqual(
headers,
[('Content-Length', '8'), ('Content-Type', 'text/plain')])
expected_headers = [('Content-Length', '8'),
('Content-Type', 'text/plain'),
('Set-Cookie', 'foo=foo'),
('Set-Cookie', 'bar=bar; HttpOnly')]
self.assertEqual(len(headers), len(expected_headers))
for header in expected_headers:
self.assertIn(header, headers)
r = app(environ, start_response)
self.assertEqual(next(r), b'response')