Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d864b81b65 | ||
|
|
d7459f23b2 | ||
|
|
32f5e415e7 | ||
|
|
c46e429106 | ||
|
|
4eac013087 | ||
|
|
496a288064 | ||
|
|
bcd876fcae | ||
|
|
5e5fc5e93e | ||
|
|
8895af3737 | ||
|
|
0a021462e0 | ||
|
|
482ab6d5ca | ||
|
|
5fe06f6bd5 | ||
|
|
c170e840ec | ||
|
|
3a39b47ea8 | ||
|
|
53287217ae | ||
|
|
6ffb8a8fe9 | ||
|
|
0151611fc8 | ||
|
|
4204db61e5 | ||
|
|
12438743a8 | ||
|
|
7cbb1edf59 |
3
.github/FUNDING.yml
vendored
3
.github/FUNDING.yml
vendored
@@ -1,3 +0,0 @@
|
||||
github: miguelgrinberg
|
||||
patreon: miguelgrinberg
|
||||
custom: https://paypal.me/miguelgrinberg
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -25,6 +25,8 @@ wheels/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
requirements.txt
|
||||
requirements-dev.txt
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
@@ -90,6 +92,8 @@ venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
.direnv
|
||||
.envrc
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
|
||||
18
CHANGES.md
18
CHANGES.md
@@ -1,5 +1,23 @@
|
||||
# Microdot change log
|
||||
|
||||
**Release 2.0.7** - 2024-11-10
|
||||
|
||||
- Accept responses with just a status code [#263](https://github.com/miguelgrinberg/microdot/issues/263) ([commit #1](https://github.com/miguelgrinberg/microdot/commit/4eac013087f807cafa244b8a6b7b0ed4c82ff150) [commit #2](https://github.com/miguelgrinberg/microdot/commit/c46e4291061046f1be13f300dd08645b71c16635))
|
||||
- Fixed compressed file content-type assignment [#251](https://github.com/miguelgrinberg/microdot/issues/251) ([commit](https://github.com/miguelgrinberg/microdot/commit/482ab6d5ca068d71ea6301f45918946161e9fcc1)) (thanks **Lukas Kremla**!)
|
||||
- Better documentation for start_server[#252](https://github.com/miguelgrinberg/microdot/issues/252) ([commit](https://github.com/miguelgrinberg/microdot/commit/0a021462e0c42c249d587a2d600f5a21a408adfc))
|
||||
- Fix URLs in documentation [#253](https://github.com/miguelgrinberg/microdot/issues/253) ([commit](https://github.com/miguelgrinberg/microdot/commit/5e5fc5e93e11cbf6e3dc8036494e8732d1815d3e)) (thanks **Stanislav Garanzha**!)
|
||||
|
||||
**Release 2.0.6** - 2024-06-18
|
||||
|
||||
- Add event ID to the SSE implementation [#213](https://github.com/miguelgrinberg/microdot/issues/213) ([commit](https://github.com/miguelgrinberg/microdot/commit/904d5fcaa2d19d939a719b8e68c4dee3eb470739)) (thanks **Hamsanger**!)
|
||||
- Configurable session cookie options [#242](https://github.com/miguelgrinberg/microdot/issues/242) ([commit](https://github.com/miguelgrinberg/microdot/commit/0151611fc84fec450820d673f4c4d70c32c990a7))
|
||||
- Improved cookie support in the test client ([commit](https://github.com/miguelgrinberg/microdot/commit/4cb155ee411dc2d9c9f15714cb32b25ba79b156a))
|
||||
- Cookie path support in session extension and test client ([commit](https://github.com/miguelgrinberg/microdot/commit/6ffb8a8fe920111c4d8c16e98715a0d5ee2d1da3))
|
||||
- Refactor `Session` class to make it more reusable ([commit](https://github.com/miguelgrinberg/microdot/commit/dea79c5ce224dec7858ffef45a42bed442fd3a5a))
|
||||
- Use `@functools.wraps` on decorated functions ([commit](https://github.com/miguelgrinberg/microdot/commit/f6876c0d154adcae96098405fb6a1fdf1ea4ec28))
|
||||
- Removed outdated import from documentation [#216](https://github.com/miguelgrinberg/microdot/issues/216) ([commit](https://github.com/miguelgrinberg/microdot/commit/6b1fd6191702e7a9ad934fddfcdd0a3cebea7c94)) (thanks **Carlo Colombo**!)
|
||||
- Add roadmap details to readme ([commit](https://github.com/miguelgrinberg/microdot/commit/a0ea439def238084c4d68309c0992b66ffd28ad6))
|
||||
|
||||
**Release 2.0.5** - 2024-03-09
|
||||
|
||||
- Correct handling of 0 as an integer argument (regression from #207) [#212](https://github.com/miguelgrinberg/microdot/issues/212) ([commit](https://github.com/miguelgrinberg/microdot/commit/d0a4cf8fa7dfb1da7466157b18d3329a8cf9a5df))
|
||||
|
||||
@@ -286,7 +286,7 @@ Cross-Origin Resource Sharing (CORS)
|
||||
- | None
|
||||
|
||||
* - Examples
|
||||
- | `cors.py <https://github.com/miguelgrinberg/microdot/blob/main/examples/cors/cors.py>`_
|
||||
- | `app.py <https://github.com/miguelgrinberg/microdot/blob/main/examples/cors/app.py>`_
|
||||
|
||||
The CORS extension provides support for `Cross-Origin Resource Sharing
|
||||
(CORS) <https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS>`_. CORS is a
|
||||
@@ -363,7 +363,7 @@ Using an ASGI Web Server
|
||||
- | `asgi.py <https://github.com/miguelgrinberg/microdot/tree/main/src/microdot/asgi.py>`_
|
||||
|
||||
* - Required external dependencies
|
||||
- | An ASGI web server, such as `Uvicorn <https://uvicorn.org/>`_.
|
||||
- | An ASGI web server, such as `Uvicorn <https://www.uvicorn.org/>`_.
|
||||
|
||||
* - Examples
|
||||
- | `hello_asgi.py <https://github.com/miguelgrinberg/microdot/blob/main/examples/hello/hello_asgi.py>`_
|
||||
|
||||
@@ -82,8 +82,34 @@ handler functions can be defined as ``async def`` or ``def`` functions, but
|
||||
``async def`` functions are recommended for performance.
|
||||
|
||||
The :func:`run() <microdot.Microdot.run>` method starts the application's web
|
||||
server on port 5000 by default. This method blocks while it waits for
|
||||
connections from clients.
|
||||
server on port 5000 by default, and creates its own asynchronous loop. This
|
||||
method blocks while it waits for connections from clients.
|
||||
|
||||
For some applications it may be necessary to run the web server alongside other
|
||||
asynchronous tasks, on an already running loop. In that case, instead of
|
||||
``app.run()`` the web server can be started by invoking the
|
||||
:func:`start_server() <microdot.Microdot.start_server>` coroutine as shown in
|
||||
the following example::
|
||||
|
||||
import asyncio
|
||||
from microdot import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
async def index(request):
|
||||
return 'Hello, world!'
|
||||
|
||||
async def main():
|
||||
# start the server in a background task
|
||||
server = asyncio.create_task(app.start_server())
|
||||
|
||||
# ... do other asynchronous work here ...
|
||||
|
||||
# cleanup before ending the application
|
||||
await server
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
Running with CPython
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
@@ -92,7 +118,7 @@ Running with CPython
|
||||
:align: left
|
||||
|
||||
* - Required Microdot source files
|
||||
- | `microdot.py <https://github.com/miguelgrinberg/microdot/tree/main/src/microdot.py>`_
|
||||
- | `microdot.py <https://github.com/miguelgrinberg/microdot/blob/main/src/microdot/microdot.py>`_
|
||||
|
||||
* - Required external dependencies
|
||||
- | None
|
||||
@@ -118,7 +144,7 @@ Running with MicroPython
|
||||
:align: left
|
||||
|
||||
* - Required Microdot source files
|
||||
- | `microdot.py <https://github.com/miguelgrinberg/microdot/tree/main/src/microdot.py>`_
|
||||
- | `microdot.py <https://github.com/miguelgrinberg/microdot/blob/main/src/microdot/microdot.py>`_
|
||||
|
||||
* - Required external dependencies
|
||||
- | None
|
||||
@@ -145,8 +171,9 @@ changed by passing the ``port`` argument to the ``run()`` method.
|
||||
Web Server Configuration
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
The :func:`run() <microdot.Microdot.run>` method supports a few arguments to
|
||||
configure the web server.
|
||||
The :func:`run() <microdot.Microdot.run>` and
|
||||
:func:`start_server() <microdot.Microdot.start_server>` methods support a few
|
||||
arguments to configure the web server.
|
||||
|
||||
- ``port``: The port number to listen on. Pass the desired port number in this
|
||||
argument to use a port different than the default of 5000. For example::
|
||||
|
||||
@@ -16,7 +16,7 @@ blinker==1.7.0
|
||||
# quart
|
||||
build==1.0.3
|
||||
# via pip-tools
|
||||
certifi==2023.11.17
|
||||
certifi==2024.7.4
|
||||
# via requests
|
||||
charset-normalizer==3.3.2
|
||||
# via requests
|
||||
@@ -57,7 +57,7 @@ itsdangerous==2.1.2
|
||||
# via
|
||||
# flask
|
||||
# quart
|
||||
jinja2==3.1.3
|
||||
jinja2==3.1.4
|
||||
# via
|
||||
# flask
|
||||
# quart
|
||||
@@ -82,9 +82,9 @@ pydantic-core==2.14.5
|
||||
# via pydantic
|
||||
pyproject-hooks==1.0.0
|
||||
# via build
|
||||
quart==0.19.4
|
||||
quart==0.19.7
|
||||
# via -r requirements.in
|
||||
requests==2.31.0
|
||||
requests==2.32.0
|
||||
# via -r requirements.in
|
||||
sniffio==1.3.0
|
||||
# via anyio
|
||||
@@ -95,11 +95,11 @@ typing-extensions==4.9.0
|
||||
# fastapi
|
||||
# pydantic
|
||||
# pydantic-core
|
||||
urllib3==2.1.0
|
||||
urllib3==2.2.2
|
||||
# via requests
|
||||
uvicorn==0.24.0.post1
|
||||
# via -r requirements.in
|
||||
werkzeug==3.0.1
|
||||
werkzeug==3.0.6
|
||||
# via
|
||||
# flask
|
||||
# quart
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "microdot"
|
||||
version = "2.0.6.dev0"
|
||||
version = "2.0.8.dev0"
|
||||
authors = [
|
||||
{ name = "Miguel Grinberg", email = "miguel.grinberg@gmail.com" },
|
||||
]
|
||||
@@ -14,6 +14,8 @@ classifiers = [
|
||||
"Operating System :: OS Independent",
|
||||
]
|
||||
requires-python = ">=3.8"
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
[project.readme]
|
||||
file = "README.md"
|
||||
@@ -24,9 +26,12 @@ Homepage = "https://github.com/miguelgrinberg/microdot"
|
||||
"Bug Tracker" = "https://github.com/miguelgrinberg/microdot/issues"
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = [
|
||||
"tox",
|
||||
]
|
||||
docs = [
|
||||
"sphinx",
|
||||
"pyjwt"
|
||||
"pyjwt",
|
||||
]
|
||||
|
||||
[tool.setuptools]
|
||||
|
||||
@@ -774,7 +774,10 @@ class Response:
|
||||
first.
|
||||
"""
|
||||
if content_type is None:
|
||||
ext = filename.split('.')[-1]
|
||||
if compressed and filename.endswith('.gz'):
|
||||
ext = filename[:-3].split('.')[-1]
|
||||
else:
|
||||
ext = filename.split('.')[-1]
|
||||
if ext in Response.types_map:
|
||||
content_type = Response.types_map[ext]
|
||||
else:
|
||||
@@ -1366,7 +1369,12 @@ class Microdot:
|
||||
if res is None:
|
||||
res = await invoke_handler(
|
||||
f, req, **req.url_args)
|
||||
if isinstance(res, int):
|
||||
res = '', res
|
||||
if isinstance(res, tuple):
|
||||
if isinstance(res[0], int):
|
||||
res = ('', res[0],
|
||||
res[1] if len(res) > 1 else {})
|
||||
body = res[0]
|
||||
if isinstance(res[1], int):
|
||||
status_code = res[1]
|
||||
|
||||
@@ -29,14 +29,21 @@ class Session:
|
||||
"""
|
||||
secret_key = None
|
||||
|
||||
def __init__(self, app=None, secret_key=None):
|
||||
def __init__(self, app=None, secret_key=None, cookie_options=None):
|
||||
self.secret_key = secret_key
|
||||
self.cookie_options = cookie_options or {}
|
||||
if app is not None:
|
||||
self.initialize(app)
|
||||
|
||||
def initialize(self, app, secret_key=None):
|
||||
def initialize(self, app, secret_key=None, cookie_options=None):
|
||||
if secret_key is not None:
|
||||
self.secret_key = secret_key
|
||||
if cookie_options is not None:
|
||||
self.cookie_options = cookie_options
|
||||
if 'path' not in self.cookie_options:
|
||||
self.cookie_options['path'] = '/'
|
||||
if 'http_only' not in self.cookie_options:
|
||||
self.cookie_options['http_only'] = True
|
||||
app._session = self
|
||||
|
||||
def get(self, request):
|
||||
@@ -86,7 +93,8 @@ class Session:
|
||||
|
||||
@request.after_request
|
||||
def _update_session(request, response):
|
||||
response.set_cookie('session', encoded_session, http_only=True)
|
||||
response.set_cookie('session', encoded_session,
|
||||
**self.cookie_options)
|
||||
return response
|
||||
|
||||
def delete(self, request):
|
||||
@@ -109,8 +117,7 @@ class Session:
|
||||
"""
|
||||
@request.after_request
|
||||
def _delete_session(request, response):
|
||||
response.set_cookie('session', '', http_only=True,
|
||||
expires='Thu, 01 Jan 1970 00:00:01 GMT')
|
||||
response.delete_cookie('session', **self.cookie_options)
|
||||
return response
|
||||
|
||||
def encode(self, payload, secret_key=None):
|
||||
|
||||
@@ -112,9 +112,13 @@ class TestClient:
|
||||
headers['Host'] = 'example.com:1234'
|
||||
return body, headers
|
||||
|
||||
def _process_cookies(self, headers):
|
||||
def _process_cookies(self, path, headers):
|
||||
cookies = ''
|
||||
for name, value in self.cookies.items():
|
||||
if isinstance(value, tuple):
|
||||
value, cookie_path = value
|
||||
if not path.startswith(cookie_path):
|
||||
continue
|
||||
if cookies:
|
||||
cookies += '; '
|
||||
cookies += name + '=' + value
|
||||
@@ -123,7 +127,7 @@ class TestClient:
|
||||
headers['Cookie'] += '; ' + cookies
|
||||
else:
|
||||
headers['Cookie'] = cookies
|
||||
return cookies, headers
|
||||
return headers
|
||||
|
||||
def _render_request(self, method, path, headers, body):
|
||||
request_bytes = '{method} {path} HTTP/1.0\n'.format(
|
||||
@@ -139,36 +143,45 @@ class TestClient:
|
||||
for cookie in cookies:
|
||||
cookie_name, cookie_value = cookie.split('=', 1)
|
||||
cookie_options = cookie_value.split(';')
|
||||
path = '/'
|
||||
delete = False
|
||||
for option in cookie_options[1:]:
|
||||
if option.strip().lower().startswith(
|
||||
option = option.strip().lower()
|
||||
if option.startswith(
|
||||
'max-age='): # pragma: no cover
|
||||
_, age = option.strip().split('=', 1)
|
||||
_, age = option.split('=', 1)
|
||||
try:
|
||||
age = int(age)
|
||||
except ValueError: # pragma: no cover
|
||||
age = 0
|
||||
if age <= 0:
|
||||
delete = True
|
||||
break
|
||||
elif option.strip().lower().startswith('expires='):
|
||||
_, e = option.strip().split('=', 1)
|
||||
elif option.startswith('expires='):
|
||||
_, e = option.split('=', 1)
|
||||
# this is a very limited parser for cookie expiry
|
||||
# that only detects a cookie deletion request when
|
||||
# the date is 1/1/1970
|
||||
if '1 jan 1970' in e.lower(): # pragma: no branch
|
||||
delete = True
|
||||
break
|
||||
elif option.startswith('path='):
|
||||
_, path = option.split('=', 1)
|
||||
if delete:
|
||||
if cookie_name in self.cookies: # pragma: no branch
|
||||
del self.cookies[cookie_name]
|
||||
cookie_path = self.cookies[cookie_name][1] \
|
||||
if isinstance(self.cookies[cookie_name], tuple) \
|
||||
else '/'
|
||||
if path == cookie_path:
|
||||
del self.cookies[cookie_name]
|
||||
else:
|
||||
self.cookies[cookie_name] = cookie_options[0]
|
||||
if path == '/':
|
||||
self.cookies[cookie_name] = cookie_options[0]
|
||||
else:
|
||||
self.cookies[cookie_name] = (cookie_options[0], path)
|
||||
|
||||
async def request(self, method, path, headers=None, body=None, sock=None):
|
||||
headers = headers or {}
|
||||
body, headers = self._process_body(body, headers)
|
||||
cookies, headers = self._process_cookies(headers)
|
||||
headers = self._process_cookies(path, headers)
|
||||
request_bytes = self._render_request(method, path, headers, body)
|
||||
if sock:
|
||||
reader = sock[0]
|
||||
|
||||
1
tests/files/test.txt.gz
Normal file
1
tests/files/test.txt.gz
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
@@ -203,6 +203,7 @@ class TestMicrodot(unittest.TestCase):
|
||||
req.cookies['one'] + req.cookies['two'] + req.cookies['three'])
|
||||
res.set_cookie('four', '4')
|
||||
res.delete_cookie('two', path='/')
|
||||
res.delete_cookie('one', path='/bad')
|
||||
return res
|
||||
|
||||
client = TestClient(app, cookies={'one': '1', 'two': '2'})
|
||||
@@ -273,6 +274,14 @@ class TestMicrodot(unittest.TestCase):
|
||||
return '<p>four</p>', 202, \
|
||||
{'Content-Type': 'text/html; charset=UTF-8'}
|
||||
|
||||
@app.route('/status')
|
||||
def five(req):
|
||||
return 202
|
||||
|
||||
@app.route('/status-headers')
|
||||
def six(req):
|
||||
return 202, {'Content-Type': 'text/html; charset=UTF-8'}
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
res = self._run(client.get('/body'))
|
||||
@@ -298,6 +307,18 @@ class TestMicrodot(unittest.TestCase):
|
||||
'text/html; charset=UTF-8')
|
||||
self.assertEqual(res.text, '<p>four</p>')
|
||||
|
||||
res = self._run(client.get('/status'))
|
||||
self.assertEqual(res.text, '')
|
||||
self.assertEqual(res.status_code, 202)
|
||||
self.assertEqual(res.headers['Content-Type'],
|
||||
'text/plain; charset=UTF-8')
|
||||
|
||||
res = self._run(client.get('/status-headers'))
|
||||
self.assertEqual(res.text, '')
|
||||
self.assertEqual(res.status_code, 202)
|
||||
self.assertEqual(res.headers['Content-Type'],
|
||||
'text/html; charset=UTF-8')
|
||||
|
||||
def test_before_after_request(self):
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@@ -136,10 +136,10 @@ class TestResponse(unittest.TestCase):
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nfoobar'))
|
||||
|
||||
def test_create_from_other(self):
|
||||
res = Response(123)
|
||||
res = Response(23.7)
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.body, 123)
|
||||
self.assertEqual(res.body, 23.7)
|
||||
|
||||
def test_create_with_status_code(self):
|
||||
res = Response('not found', 404)
|
||||
@@ -193,6 +193,7 @@ class TestResponse(unittest.TestCase):
|
||||
expires='Tue, 05 Nov 2019 02:23:54 GMT', max_age=123,
|
||||
secure=True, http_only=True)
|
||||
res.delete_cookie('foo8', http_only=True)
|
||||
res.delete_cookie('foo9', path='/s')
|
||||
self.assertEqual(res.headers, {'Set-Cookie': [
|
||||
'foo1=bar1',
|
||||
'foo2=bar2; Path=/; Partitioned',
|
||||
@@ -205,6 +206,8 @@ class TestResponse(unittest.TestCase):
|
||||
'HttpOnly',
|
||||
('foo8=; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0; '
|
||||
'HttpOnly'),
|
||||
('foo9=; Path=/s; Expires=Thu, 01 Jan 1970 00:00:01 GMT; '
|
||||
'Max-Age=0'),
|
||||
]})
|
||||
|
||||
def test_redirect(self):
|
||||
@@ -277,6 +280,17 @@ class TestResponse(unittest.TestCase):
|
||||
'application/octet-stream')
|
||||
self.assertEqual(res.headers['Content-Encoding'], 'gzip')
|
||||
|
||||
def test_send_file_gzip_handling(self):
|
||||
res = Response.send_file('tests/files/test.txt.gz')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers['Content-Type'],
|
||||
'application/octet-stream')
|
||||
|
||||
res = Response.send_file('tests/files/test.txt.gz', compressed=True)
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers['Content-Type'], 'text/plain')
|
||||
self.assertEqual(res.headers['Content-Encoding'], 'gzip')
|
||||
|
||||
def test_default_content_type(self):
|
||||
original_content_type = Response.default_content_type
|
||||
res = Response('foo')
|
||||
|
||||
@@ -82,3 +82,77 @@ class TestSession(unittest.TestCase):
|
||||
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
|
||||
def test_session_default_path(self):
|
||||
app = Microdot()
|
||||
Session(app, secret_key='some-other-secret')
|
||||
client = TestClient(app)
|
||||
|
||||
@app.get('/')
|
||||
@with_session
|
||||
def index(req, session):
|
||||
session['foo'] = 'bar'
|
||||
session.save()
|
||||
return ''
|
||||
|
||||
@app.get('/child')
|
||||
@with_session
|
||||
def child(req, session):
|
||||
return str(session.get('foo'))
|
||||
|
||||
@app.get('/delete')
|
||||
@with_session
|
||||
def delete(req, session):
|
||||
session.delete()
|
||||
return ''
|
||||
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
res = self._run(client.get('/child'))
|
||||
self.assertEqual(res.text, 'bar')
|
||||
res = self._run(client.get('/delete'))
|
||||
res = self._run(client.get('/child'))
|
||||
self.assertEqual(res.text, 'None')
|
||||
|
||||
def test_session_custom_path(self):
|
||||
app = Microdot()
|
||||
session_ext = Session()
|
||||
session_ext.initialize(app, secret_key='some-other-secret',
|
||||
cookie_options={'path': '/child',
|
||||
'http_only': False})
|
||||
client = TestClient(app)
|
||||
|
||||
@app.get('/')
|
||||
@with_session
|
||||
def index(req, session):
|
||||
return str(session.get('foo'))
|
||||
|
||||
@app.get('/child')
|
||||
@with_session
|
||||
def child(req, session):
|
||||
session['foo'] = 'bar'
|
||||
session.save()
|
||||
return ''
|
||||
|
||||
@app.get('/child/foo')
|
||||
@with_session
|
||||
def foo(req, session):
|
||||
return str(session.get('foo'))
|
||||
|
||||
@app.get('/child/delete')
|
||||
@with_session
|
||||
def delete(req, session):
|
||||
session.delete()
|
||||
return ''
|
||||
|
||||
res = self._run(client.get('/child'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.text, 'None')
|
||||
res = self._run(client.get('/child/foo'))
|
||||
self.assertEqual(res.text, 'bar')
|
||||
res = self._run(client.get('/child/delete'))
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.text, 'None')
|
||||
res = self._run(client.get('/child/foo'))
|
||||
self.assertEqual(res.text, 'None')
|
||||
|
||||
Reference in New Issue
Block a user