Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32f5e415e7 | ||
|
|
c46e429106 | ||
|
|
4eac013087 | ||
|
|
496a288064 | ||
|
|
bcd876fcae | ||
|
|
5e5fc5e93e | ||
|
|
8895af3737 | ||
|
|
0a021462e0 | ||
|
|
482ab6d5ca | ||
|
|
5fe06f6bd5 | ||
|
|
c170e840ec | ||
|
|
3a39b47ea8 |
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
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# 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**!)
|
||||
|
||||
@@ -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
|
||||
@@ -82,7 +82,7 @@ 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.32.0
|
||||
# via -r requirements.in
|
||||
@@ -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.3
|
||||
werkzeug==3.0.6
|
||||
# via
|
||||
# flask
|
||||
# quart
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "microdot"
|
||||
version = "2.0.6"
|
||||
version = "2.0.7"
|
||||
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,6 +774,9 @@ class Response:
|
||||
first.
|
||||
"""
|
||||
if content_type is None:
|
||||
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]
|
||||
@@ -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]
|
||||
|
||||
1
tests/files/test.txt.gz
Normal file
1
tests/files/test.txt.gz
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
@@ -274,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'))
|
||||
@@ -299,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)
|
||||
@@ -280,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')
|
||||
|
||||
Reference in New Issue
Block a user