Compare commits
73 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f23c78533e | ||
|
|
d29ed6aaa1 | ||
|
|
8e5fb92ff1 | ||
|
|
06015934b8 | ||
|
|
568cd51fd2 | ||
|
|
2fe9793389 | ||
|
|
de9c991a9a | ||
|
|
d75449eb32 | ||
|
|
e508abc333 | ||
|
|
5003a5b3d9 | ||
|
|
4ed101dfc6 | ||
|
|
833fecb105 | ||
|
|
d527bdb7c3 | ||
|
|
2516b296a7 | ||
|
|
5061145f5c | ||
|
|
122c638bae | ||
|
|
bd74bcab74 | ||
|
|
5cd3ace516 | ||
|
|
da32f23e35 | ||
|
|
0641466faa | ||
|
|
dd3fc20507 | ||
|
|
46963ba464 | ||
|
|
1a8db51cb3 | ||
|
|
d903c42370 | ||
|
|
8b4ebbd953 | ||
|
|
a82ed55f56 | ||
|
|
ac87f0542f | ||
|
|
2de57498a8 | ||
|
|
b7b881e3c7 | ||
|
|
9955ac99a6 | ||
|
|
4b101d1597 | ||
|
|
097cd9cf02 | ||
|
|
b0c25a1a72 | ||
|
|
b7b8e58d6a | ||
|
|
12cd60305b | ||
|
|
4eea7adb8f | ||
|
|
a3288a63ed | ||
|
|
3bd7fe8cea | ||
|
|
0ad538df91 | ||
|
|
b810346aa4 | ||
|
|
ae5d330b2d | ||
|
|
4c0afa2bec | ||
|
|
125af4b4a9 | ||
|
|
c5e1873523 | ||
|
|
dfbe2edd79 | ||
|
|
3e29af5775 | ||
|
|
1aacb3cf46 | ||
|
|
64cc172917 | ||
|
|
b9ca036e1d | ||
|
|
b06b6de584 | ||
|
|
e5525c5c48 | ||
|
|
494800ff9f | ||
|
|
ba986a89ff | ||
|
|
89f7f09b9a | ||
|
|
3d9b5d7084 | ||
|
|
03efe46a26 | ||
|
|
b16466f1a9 | ||
|
|
8aa50f171d | ||
|
|
76ab1fa6d7 | ||
|
|
0a373775d5 | ||
|
|
cd71986a50 | ||
|
|
0b95feafc9 | ||
|
|
9b32292f21 | ||
|
|
f741ed7cf8 | ||
|
|
92edc17522 | ||
|
|
4c83cb7563 | ||
|
|
491202de1f | ||
|
|
db29624a45 | ||
|
|
0f2c749f6d | ||
|
|
52f2d0c491 | ||
|
|
2f58c41cc8 | ||
|
|
4fbed286e4 | ||
|
|
e4ff70cf8f |
3
.flake8
Normal file
3
.flake8
Normal file
@@ -0,0 +1,3 @@
|
||||
[flake8]
|
||||
select = C,E,F,W,B,B950
|
||||
per-file-ignores = ./*/__init__.py:F401
|
||||
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
||||
tests/files/* binary
|
||||
3
.github/FUNDING.yml
vendored
Normal file
3
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
github: miguelgrinberg
|
||||
patreon: miguelgrinberg
|
||||
custom: https://paypal.me/miguelgrinberg
|
||||
53
.github/workflows/tests.yml
vendored
Normal file
53
.github/workflows/tests.yml
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
name: build
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
jobs:
|
||||
lint:
|
||||
name: lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-python@v2
|
||||
- run: python -m pip install --upgrade pip wheel
|
||||
- run: pip install tox tox-gh-actions
|
||||
- run: tox -eflake8
|
||||
tests:
|
||||
name: tests
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
python: ['3.6', '3.7', '3.8', '3.9']
|
||||
fail-fast: false
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: ${{ matrix.python }}
|
||||
- run: python -m pip install --upgrade pip wheel
|
||||
- run: pip install tox tox-gh-actions
|
||||
- run: tox
|
||||
tests-micropython:
|
||||
name: tests-micropython
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-python@v2
|
||||
- run: python -m pip install --upgrade pip wheel
|
||||
- run: pip install tox tox-gh-actions
|
||||
- run: tox -eupy
|
||||
coverage:
|
||||
name: coverage
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-python@v2
|
||||
- run: python -m pip install --upgrade pip wheel
|
||||
- run: pip install tox tox-gh-actions codecov
|
||||
- run: tox
|
||||
- run: codecov
|
||||
82
CHANGES.md
Normal file
82
CHANGES.md
Normal file
@@ -0,0 +1,82 @@
|
||||
# Microdot change log
|
||||
|
||||
**Release 0.7.2** - 2021-09-28
|
||||
|
||||
- Document a security risk in the send_file function ([commit](https://github.com/miguelgrinberg/microdot/commit/d29ed6aaa1f2080fcf471bf6ae0f480f95ff1716)) (thanks **Ky Tran**!)
|
||||
- Validate redirect URLs ([commit](https://github.com/miguelgrinberg/microdot/commit/8e5fb92ff1ccd50972b0c1cb5a6c3bd5eb54d86b)) (thanks **Ky Tran**!)
|
||||
- Return a 400 error when request object could not be created ([commit](https://github.com/miguelgrinberg/microdot/commit/06015934b834622d39f52b3e13d16bfee9dc8e5a))
|
||||
|
||||
**Release 0.7.1** - 2021-09-27
|
||||
|
||||
- Breaking change: Limit the size of each request line to 2KB. A different maximum can be set in `Request.max_readline`. ([commit](https://github.com/miguelgrinberg/microdot/commit/de9c991a9ab836d57d5c08bf4282f99f073b502a)) (thanks **Ky Tran**!)
|
||||
|
||||
**Release 0.7.0** - 2021-09-27
|
||||
|
||||
- Breaking change: Limit the size of the request body to 16KB. A different maximum can be set in `Request.max_content_length`. ([commit](https://github.com/miguelgrinberg/microdot/commit/5003a5b3d948a7cf365857b419bebf6e388593a1))
|
||||
- Add documentation for `request.client_addr` [#27](https://github.com/miguelgrinberg/microdot/issues/27) ([commit](https://github.com/miguelgrinberg/microdot/commit/833fecb105ce456b95f1d2a6ea96dceca1075814)) (thanks **Mark Blakeney**!)
|
||||
- Added documentation for reason argument in the Response object ([commit](https://github.com/miguelgrinberg/microdot/commit/d527bdb7c32ab918a1ecf6956cf3a9f544504354))
|
||||
|
||||
**Release 0.6.0** - 2021-08-11
|
||||
|
||||
- Better handling of content types in form and json methods [#24](https://github.com/miguelgrinberg/microdot/issues/24) ([commit](https://github.com/miguelgrinberg/microdot/commit/da32f23e35f871470a40638e7000e84b0ff6d17f))
|
||||
- Accept a custom reason phrase for the HTTP response [#25](https://github.com/miguelgrinberg/microdot/issues/25) ([commit](https://github.com/miguelgrinberg/microdot/commit/bd74bcab74f283c89aadffc8f9c20d6ff0f771ce))
|
||||
- Make mime type check for form submissions more robust ([commit](https://github.com/miguelgrinberg/microdot/commit/dd3fc20507715a23d0fa6fa3aae3715c8fbc0351))
|
||||
- Copy client headers to avoid write back [#23](https://github.com/miguelgrinberg/microdot/issues/23) ([commit](https://github.com/miguelgrinberg/microdot/commit/0641466faa9dda0c54f78939ac05993c0812e84a)) (thanks **Mark Blakeney**!)
|
||||
- Work around a bug in uasyncio's create_server() function ([commit](https://github.com/miguelgrinberg/microdot/commit/46963ba4644d7abc8dc653c99bc76222af526964))
|
||||
- More unit tests ([commit](https://github.com/miguelgrinberg/microdot/commit/5cd3ace5166ec549579b0b1149ae3d7be195974a))
|
||||
- Installation instructions ([commit](https://github.com/miguelgrinberg/microdot/commit/1a8db51cb3754308da6dcc227512dcdeb4ce4557))
|
||||
- Run tests with pytest ([commit](https://github.com/miguelgrinberg/microdot/commit/8b4ebbd9535b3c083fb2a955284609acba07f05e))
|
||||
- Deprecated the microdot-asyncio package ([commit](https://github.com/miguelgrinberg/microdot/commit/a82ed55f56e14fbcea93e8171af86ab42657fa96))
|
||||
|
||||
**Release 0.5.0** - 2021-06-06
|
||||
|
||||
- [Documentation](https://microdot.readthedocs.io/en/latest/) site ([commit](https://github.com/miguelgrinberg/microdot/commit/12cd60305b7b48ab151da52661fc5988684dbcd8))
|
||||
- Support duplicate arguments in query string and form submissions [#21](https://github.com/miguelgrinberg/microdot/issues/21) ([commit](https://github.com/miguelgrinberg/microdot/commit/b0c25a1a7298189373be5df1668e0afb5532cdaf))
|
||||
- Merge `microdot-asyncio` package with `microdot` ([commit](https://github.com/miguelgrinberg/microdot/commit/b7b881e3c7f1c6ede6546e498737e93928425c30))
|
||||
- Added a change log ([commit](https://github.com/miguelgrinberg/microdot/commit/9955ac99a6ac20308644f02d6e6e32847d28b70c))
|
||||
- Improve project structure ([commit](https://github.com/miguelgrinberg/microdot/commit/4b101d15971fa2883d187f0bab0be999ae30b583))
|
||||
|
||||
**Release v0.4.0** - 2021-06-04
|
||||
|
||||
- Add HTTP method-specific route decorators ([commit](https://github.com/miguelgrinberg/microdot/commit/a3288a63ed45f700f79b67d0b57fc4dd20e844c1))
|
||||
- Server shutdown [#19](https://github.com/miguelgrinberg/microdot/issues/19) ([commit](https://github.com/miguelgrinberg/microdot/commit/0ad538df91f8b6b8a3885aa602c014ee7fe4526b))
|
||||
- Update microypthon binary for tests to 1.15 ([commit](https://github.com/miguelgrinberg/microdot/commit/3bd7fe8cea4598a7dbd0efcb9c6ce57ec2b79f9c))
|
||||
|
||||
**Release v0.3.1** - 2021-02-06
|
||||
|
||||
- Support large downloads in send_file [#3](https://github.com/miguelgrinberg/microdot/issues/3) ([commit](https://github.com/miguelgrinberg/microdot/commit/3e29af57753dbb7961ff98719a4fc4f71c0b4e3e))
|
||||
- Move socket import and add simple hello example [#12](https://github.com/miguelgrinberg/microdot/issues/12) ([commit](https://github.com/miguelgrinberg/microdot/commit/c5e1873523b609680ff67d7abfada72568272250)) (thanks **Damien George**!)
|
||||
- Update python versions to build ([commit](https://github.com/miguelgrinberg/microdot/commit/dfbe2edd797153fc9be40bc1928d93bdee7e7be5))
|
||||
- Handle Chrome preconnect [#8](https://github.com/miguelgrinberg/microdot/issues/8) ([commit](https://github.com/miguelgrinberg/microdot/commit/125af4b4a92b1d78acfa9d57ad2f507e759b6938)) (thanks **Ricardo Mendonça Ferreira**!)
|
||||
- Readme update ([commit](https://github.com/miguelgrinberg/microdot/commit/1aacb3cf46bd0b634ec3bc852ff9439f3c5dd773))
|
||||
- Switch to GitHub actions for builds ([commit](https://github.com/miguelgrinberg/microdot/commit/4c0afa2beca0c3b0f167fd25c6849d6937c412ba))
|
||||
|
||||
**Release v0.3.0** - 2019-05-05
|
||||
|
||||
- g, before_request and after_request ([commit](https://github.com/miguelgrinberg/microdot/commit/8aa50f171d2d04bc15c472ab1d9b3288518f7a21))
|
||||
- Threaded mode ([commit](https://github.com/miguelgrinberg/microdot/commit/494800ff9ff474c38644979086057e3584573969))
|
||||
- Optional asyncio support ([commit](https://github.com/miguelgrinberg/microdot/commit/3d9b5d7084d52e749553ca79206ed7060f963f9d))
|
||||
- Debug mode ([commit](https://github.com/miguelgrinberg/microdot/commit/4c83cb75636572066958ef2cc0802909deafe542))
|
||||
- Print exceptions ([commit](https://github.com/miguelgrinberg/microdot/commit/491202de1fce232b9629b7f1db63594fd13f84a3))
|
||||
- Flake8 ([commit](https://github.com/miguelgrinberg/microdot/commit/92edc17522d7490544c7186d62a2964caf35c861))
|
||||
- Unit testing framework ([commit](https://github.com/miguelgrinberg/microdot/commit/f741ed7cf83320d25ce16a1a29796af6fdfb91e9))
|
||||
- More robust header checking in tests ([commit](https://github.com/miguelgrinberg/microdot/commit/03efe46a26e7074f960dd4c9a062c53d6f72bfa0))
|
||||
- Response unit tests ([commit](https://github.com/miguelgrinberg/microdot/commit/cd71986a5042dcc308617a3db89476f28dd13ecf))
|
||||
- Request unit tests ([commit](https://github.com/miguelgrinberg/microdot/commit/0b95feafc96dc91d7d34528ff2d8931a8aa3d612))
|
||||
- More unit tests ([commit](https://github.com/miguelgrinberg/microdot/commit/76ab1fa6d72dd9deaa24aeaf4895a0c6fc883bcb))
|
||||
- Async request and response unit tests ([commit](https://github.com/miguelgrinberg/microdot/commit/89f7f09b9a2d0dfccefabebbe9b83307133bd97c))
|
||||
- More asyncio unit tests ([commit](https://github.com/miguelgrinberg/microdot/commit/ba986a89ff72ebbd9a65307b81ee769879961594))
|
||||
- Improve code structure ([commit](https://github.com/miguelgrinberg/microdot/commit/b16466f1a9432a608eb23769907e8952fe304a9a))
|
||||
- URL pattern matching unit tests ([commit](https://github.com/miguelgrinberg/microdot/commit/0a373775d54df571ceddaac090094bb62dbe6c72))
|
||||
- Rename microdot_async to microdot_asyncio ([commit](https://github.com/miguelgrinberg/microdot/commit/e5525c5c485ae8901c9602da7e4582b58fb2da40))
|
||||
|
||||
**Release 0.2.0** - 2019-04-19
|
||||
|
||||
- Error handlers ([commit](https://github.com/miguelgrinberg/microdot/commit/0f2c749f6d1b9edbf124523160e10449c932ea45))
|
||||
- Fleshed out example GPIO application ([commit](https://github.com/miguelgrinberg/microdot/commit/52f2d0c4918d00d1a7e46cc7fd9a909ef6d259c1))
|
||||
- More robust parsing of cookie header ([commit](https://github.com/miguelgrinberg/microdot/commit/2f58c41cc89946d51646df83d4f9ae0e24e447b9))
|
||||
|
||||
**Release 0.1.1** - 2019-04-17
|
||||
|
||||
- Minor fixes for micropython ([commit](https://github.com/miguelgrinberg/microdot/commit/e4ff70cf8fe839f5b5297157bf028569188b9031))
|
||||
- Initial commit ([commit](https://github.com/miguelgrinberg/microdot/commit/311a82a44430d427948866b09cb6136e60a5b1c9))
|
||||
@@ -1,2 +1,9 @@
|
||||
# microdot
|
||||
[](https://github.com/miguelgrinberg/microdot/actions) [](https://codecov.io/gh/miguelgrinberg/microdot)
|
||||
|
||||
A minimalistic Python web framework for microcontrollers inspired by Flask
|
||||
|
||||
## Resources
|
||||
|
||||
- [Documentation](https://microdot.readthedocs.io/en/latest/)
|
||||
- [Change Log](https://github.com/miguelgrinberg/microdot/blob/main/CHANGES.md)
|
||||
|
||||
9
SECURITY.md
Normal file
9
SECURITY.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Security Policy
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
If you think you've found a vulnerability on this project, please send me (Miguel Grinberg) an email at miguel.grinberg@gmail.com with a description of the problem. I will personally review the issue and respond to you with next steps.
|
||||
|
||||
If the issue is highly sensitive, you are welcome to encrypt your message. Here is my [PGP key](https://keyserver.ubuntu.com/pks/lookup?search=miguel.grinberg%40gmail.com&fingerprint=on&op=index).
|
||||
|
||||
Please do not disclose vulnerabilities publicly before discussing how to proceed with me.
|
||||
BIN
bin/micropython
Executable file
BIN
bin/micropython
Executable file
Binary file not shown.
33
bin/mkrelease
Executable file
33
bin/mkrelease
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
VERSION=$1
|
||||
if [[ "$VERSION" == "" ]]; then
|
||||
echo Usage: $0 "<version>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
git diff --cached --exit-code >/dev/null
|
||||
if [[ "$?" != "0" ]]; then
|
||||
echo Commit your changes before using this script.
|
||||
exit 1
|
||||
fi
|
||||
|
||||
set -e
|
||||
for PKG in microdot*; do
|
||||
echo Building $PKG...
|
||||
cd $PKG
|
||||
sed -i "" "s/version.*$/version=\"$VERSION\",/" setup.py
|
||||
git add setup.py
|
||||
rm -rf dist
|
||||
python setup.py sdist bdist_wheel --universal
|
||||
cd ..
|
||||
done
|
||||
git commit -m "Release v$VERSION"
|
||||
git tag v$VERSION
|
||||
git push --tags origin master
|
||||
|
||||
for PKG in microdot*; do
|
||||
echo Releasing $PKG...
|
||||
cd $PKG
|
||||
twine upload dist/*
|
||||
cd ..
|
||||
done
|
||||
20
docs/Makefile
Normal file
20
docs/Makefile
Normal file
@@ -0,0 +1,20 @@
|
||||
# Minimal makefile for Sphinx documentation
|
||||
#
|
||||
|
||||
# You can set these variables from the command line, and also
|
||||
# from the environment for the first two.
|
||||
SPHINXOPTS ?=
|
||||
SPHINXBUILD ?= sphinx-build
|
||||
SOURCEDIR = .
|
||||
BUILDDIR = _build
|
||||
|
||||
# Put it first so that "make" without argument is like "make help".
|
||||
help:
|
||||
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||
|
||||
.PHONY: help Makefile
|
||||
|
||||
# Catch-all target: route all unknown targets to Sphinx using the new
|
||||
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
||||
%: Makefile
|
||||
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||
3
docs/_static/css/custom.css
vendored
Normal file
3
docs/_static/css/custom.css
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.py .class, .py .method, .py .property {
|
||||
margin-top: 20px;
|
||||
}
|
||||
62
docs/api.rst
Normal file
62
docs/api.rst
Normal file
@@ -0,0 +1,62 @@
|
||||
API Reference
|
||||
=============
|
||||
|
||||
``microdot`` module
|
||||
-------------------
|
||||
|
||||
The ``microdot`` module defines a few classes that help implement HTTP-based
|
||||
servers for MicroPython and standard Python, with multithreading support for
|
||||
Python interpreters that support it.
|
||||
|
||||
``Microdot`` class
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: microdot.Microdot
|
||||
:members:
|
||||
|
||||
``Request`` class
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: microdot.Request
|
||||
:members:
|
||||
|
||||
``Response`` class
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: microdot.Response
|
||||
:members:
|
||||
|
||||
``MultiDict`` class
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: microdot.MultiDict
|
||||
:members:
|
||||
|
||||
``microdot_asyncio`` module
|
||||
---------------------------
|
||||
|
||||
The ``microdot_asyncio`` module defines a few classes that help implement
|
||||
HTTP-based servers for MicroPython and standard Python that use ``asyncio``
|
||||
and coroutines.
|
||||
|
||||
``Microdot`` class
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: microdot_asyncio.Microdot
|
||||
:inherited-members:
|
||||
:members:
|
||||
|
||||
``Request`` class
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: microdot_asyncio.Request
|
||||
:inherited-members:
|
||||
:members:
|
||||
|
||||
``Response`` class
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: microdot_asyncio.Response
|
||||
:inherited-members:
|
||||
:members:
|
||||
|
||||
70
docs/conf.py
Normal file
70
docs/conf.py
Normal file
@@ -0,0 +1,70 @@
|
||||
# Configuration file for the Sphinx documentation builder.
|
||||
#
|
||||
# This file only contains a selection of the most common options. For a full
|
||||
# list see the documentation:
|
||||
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
||||
|
||||
# -- Path setup --------------------------------------------------------------
|
||||
|
||||
# If extensions (or modules to document with autodoc) are in another directory,
|
||||
# add these directories to sys.path here. If the directory is relative to the
|
||||
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
||||
#
|
||||
import os
|
||||
import sys
|
||||
sys.path.insert(0, os.path.abspath('../src'))
|
||||
|
||||
|
||||
# -- Project information -----------------------------------------------------
|
||||
|
||||
project = 'Microdot'
|
||||
copyright = '2021, Miguel Grinberg'
|
||||
author = 'Miguel Grinberg'
|
||||
|
||||
|
||||
# -- General configuration ---------------------------------------------------
|
||||
|
||||
# Add any Sphinx extension module names here, as strings. They can be
|
||||
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
||||
# ones.
|
||||
extensions = [
|
||||
'sphinx.ext.autodoc',
|
||||
]
|
||||
|
||||
# Add any paths that contain templates here, relative to this directory.
|
||||
templates_path = ['_templates']
|
||||
|
||||
# List of patterns, relative to source directory, that match files and
|
||||
# directories to ignore when looking for source files.
|
||||
# This pattern also affects html_static_path and html_extra_path.
|
||||
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
||||
|
||||
|
||||
# -- Options for HTML output -------------------------------------------------
|
||||
|
||||
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||
# a list of builtin themes.
|
||||
#
|
||||
html_theme = 'alabaster'
|
||||
|
||||
# Add any paths that contain custom static files (such as style sheets) here,
|
||||
# relative to this directory. They are copied after the builtin static files,
|
||||
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||
html_static_path = ['_static']
|
||||
|
||||
html_css_files = [
|
||||
'css/custom.css',
|
||||
]
|
||||
|
||||
html_theme_options = {
|
||||
'github_user': 'miguelgrinberg',
|
||||
'github_repo': 'microdot',
|
||||
'github_banner': True,
|
||||
'github_button': True,
|
||||
'github_type': 'star',
|
||||
'fixed_sidebar': True,
|
||||
}
|
||||
|
||||
autodoc_default_options = {
|
||||
'member-order': 'bysource',
|
||||
}
|
||||
21
docs/index.rst
Normal file
21
docs/index.rst
Normal file
@@ -0,0 +1,21 @@
|
||||
.. Microdot documentation master file, created by
|
||||
sphinx-quickstart on Fri Jun 4 17:40:19 2021.
|
||||
You can adapt this file completely to your liking, but it should at least
|
||||
contain the root `toctree` directive.
|
||||
|
||||
Microdot
|
||||
========
|
||||
|
||||
Microdot is a minimalistic Python web framework inspired by
|
||||
`Flask <https://flask.palletsprojects.com/>`_, and designed to run on
|
||||
systems with limited resources such as microcontrollers. It runs on standard
|
||||
Python and on `MicroPython <https://micropython.org>`_.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 3
|
||||
|
||||
intro
|
||||
api
|
||||
|
||||
* :ref:`genindex`
|
||||
* :ref:`search`
|
||||
39
docs/intro.rst
Normal file
39
docs/intro.rst
Normal file
@@ -0,0 +1,39 @@
|
||||
Installation
|
||||
------------
|
||||
|
||||
Microdot can be installed with ``pip``::
|
||||
|
||||
pip install microdot
|
||||
|
||||
For platforms that do not support or cannot run ``pip``, you can also manually
|
||||
copy and install the ``microdot.py`` and ``microdot_asyncio.py`` source files.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
The following is an example of a standard single or multi-threaded web
|
||||
server::
|
||||
|
||||
from microdot import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def hello(request):
|
||||
return 'Hello, world!'
|
||||
|
||||
app.run()
|
||||
|
||||
Microdot also supports the asynchronous model and can be used under
|
||||
``asyncio``. The example that follows is equivalent to the one above, but uses
|
||||
coroutines for concurrency::
|
||||
|
||||
from microdot_asyncio import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
async def hello(request):
|
||||
return 'Hello, world!'
|
||||
|
||||
app.run()
|
||||
35
docs/make.bat
Normal file
35
docs/make.bat
Normal file
@@ -0,0 +1,35 @@
|
||||
@ECHO OFF
|
||||
|
||||
pushd %~dp0
|
||||
|
||||
REM Command file for Sphinx documentation
|
||||
|
||||
if "%SPHINXBUILD%" == "" (
|
||||
set SPHINXBUILD=sphinx-build
|
||||
)
|
||||
set SOURCEDIR=.
|
||||
set BUILDDIR=_build
|
||||
|
||||
if "%1" == "" goto help
|
||||
|
||||
%SPHINXBUILD% >NUL 2>NUL
|
||||
if errorlevel 9009 (
|
||||
echo.
|
||||
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
||||
echo.installed, then set the SPHINXBUILD environment variable to point
|
||||
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
||||
echo.may add the Sphinx directory to PATH.
|
||||
echo.
|
||||
echo.If you don't have Sphinx installed, grab it from
|
||||
echo.http://sphinx-doc.org/
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
||||
goto end
|
||||
|
||||
:help
|
||||
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
||||
|
||||
:end
|
||||
popd
|
||||
@@ -2,14 +2,75 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot GPIO Example</title>
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
|
||||
<script>
|
||||
function getCookie(name) {
|
||||
var value = "; " + document.cookie;
|
||||
var parts = value.split("; " + name + "=");
|
||||
if (parts.length == 2)
|
||||
return parts.pop().split(";").shift();
|
||||
}
|
||||
function showMessage() {
|
||||
document.getElementById('message').innerHTML = getCookie('message');
|
||||
}
|
||||
function onLoad() {
|
||||
showMessage();
|
||||
var form = getCookie('form');
|
||||
if (form) {
|
||||
form = form.split(',')
|
||||
document.getElementById('pin').selectedIndex = parseInt(form[0]);
|
||||
document.getElementById(form[1]).checked = true;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Microdot GPIO Example</h1>
|
||||
<form method="POST" action="">
|
||||
<p>GPIO Pin: <input type="text" name="pin" size="3"></p>
|
||||
<input type="submit" name="read" value="Read">
|
||||
<input type="submit" name="set-low" value="Set Low">
|
||||
<input type="submit" name="set-high" value="Set high">
|
||||
</form>
|
||||
<body onload="onLoad();">
|
||||
<div class="container">
|
||||
<h1>Microdot GPIO Example</h1>
|
||||
<div class="alert alert-primary" role="alert" id="message">
|
||||
</div>
|
||||
<form method="POST" action="">
|
||||
<p>
|
||||
GPIO Pin:
|
||||
<select name="pin" id="pin">
|
||||
<option>0</option>
|
||||
<option>1</option>
|
||||
<option>2</option>
|
||||
<option>3</option>
|
||||
<option>4</option>
|
||||
<option>5</option>
|
||||
<option>6</option>
|
||||
<option>7</option>
|
||||
<option>8</option>
|
||||
<option>9</option>
|
||||
<option>10</option>
|
||||
<option>11</option>
|
||||
<option>12</option>
|
||||
<option>13</option>
|
||||
<option>14</option>
|
||||
<option>15</option>
|
||||
<option>16</option>
|
||||
</select>
|
||||
</p>
|
||||
<div>
|
||||
<p>
|
||||
<input type="radio" name="pull" value="pullup" id="pullup">
|
||||
<label for="pullup">Pull-Up</label>
|
||||
<input type="radio" name="pull" value="pulldown" id="pulldown">
|
||||
<label for="pulldown">Pull-Down</label>
|
||||
<input type="radio" name="pull" value="pullnone" id="pullnone" checked>
|
||||
<label for="pullnone">None</label>
|
||||
<br>
|
||||
<input type="submit" class="btn btn-outline-dark" name="read" value="Read">
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<p>
|
||||
<input type="submit" class="btn btn-outline-dark" name="set-low" value="Set Low">
|
||||
<input type="submit" class="btn btn-outline-dark" name="set-high" value="Set high">
|
||||
</p>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -6,14 +6,38 @@ app = Microdot()
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index(request):
|
||||
form_cookie = None
|
||||
message_cookie = None
|
||||
if request.method == 'POST':
|
||||
if 'set-read' in request.form:
|
||||
pin = machine.Pin(int(request.form['pin']), machine.Pin.IN)
|
||||
form_cookie = '{pin},{pull}'.format(pin=request.form['pin'],
|
||||
pull=request.form['pull'])
|
||||
if 'read' in request.form:
|
||||
pull = None
|
||||
if request.form['pull'] == 'pullup':
|
||||
pull = machine.Pin.PULL_UP
|
||||
elif request.form['pull'] == 'pulldown':
|
||||
pull = machine.Pin.PULL_DOWN
|
||||
pin = machine.Pin(int(request.form['pin']), machine.Pin.IN, pull)
|
||||
message_cookie = 'Input pin {pin} is {state}.'.format(
|
||||
pin=request.form['pin'],
|
||||
state='high' if pin.value() else 'low')
|
||||
else:
|
||||
pin = machine.Pin(int(request.form['pin']), machine.Pin.OUT)
|
||||
pin.value(0 if 'set-low' in request.form else 1)
|
||||
return redirect('/')
|
||||
return send_file('gpio.html')
|
||||
value = 0 if 'set-low' in request.form else 1
|
||||
pin.value(value)
|
||||
message_cookie = 'Output pin {pin} is now {state}.'.format(
|
||||
pin=request.form['pin'],
|
||||
state='high' if value else 'low')
|
||||
response = redirect('/')
|
||||
else:
|
||||
if 'message' not in request.cookies:
|
||||
message_cookie = 'Select a pin and an operation below.'
|
||||
response = send_file('gpio.html')
|
||||
if form_cookie:
|
||||
response.set_cookie('form', form_cookie)
|
||||
if message_cookie:
|
||||
response.set_cookie('message', message_cookie)
|
||||
return response
|
||||
|
||||
|
||||
app.run()
|
||||
app.run(debug=True)
|
||||
|
||||
32
examples/hello.py
Normal file
32
examples/hello.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from microdot import Microdot, Response
|
||||
|
||||
app = Microdot()
|
||||
|
||||
htmldoc = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Example Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h1>Microdot Example Page</h1>
|
||||
<p>Hello from Microdot!</p>
|
||||
<p><a href="/shutdown">Click to shutdown the server</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def hello(request):
|
||||
return Response(body=htmldoc, headers={'Content-Type': 'text/html'})
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
def shutdown(request):
|
||||
request.app.shutdown()
|
||||
return 'The server is shutting down...'
|
||||
|
||||
|
||||
app.run(debug=True)
|
||||
40
examples/hello_async.py
Normal file
40
examples/hello_async.py
Normal file
@@ -0,0 +1,40 @@
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
import asyncio
|
||||
from microdot_asyncio import Microdot, Response
|
||||
|
||||
app = Microdot()
|
||||
|
||||
htmldoc = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Example Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h1>Microdot Example Page</h1>
|
||||
<p>Hello from Microdot!</p>
|
||||
<p><a href="/shutdown">Click to shutdown the server</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
|
||||
@app.route('/')
|
||||
async def hello(request):
|
||||
return Response(body=htmldoc, headers={'Content-Type': 'text/html'})
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
async def shutdown(request):
|
||||
request.app.shutdown()
|
||||
return 'The server is shutting down...'
|
||||
|
||||
|
||||
async def main():
|
||||
await app.start_server(debug=True)
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
5
legacy/README.md
Normal file
5
legacy/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
microdot-asyncio
|
||||
================
|
||||
|
||||
This package has been merged with the ``microdot`` package. It currently
|
||||
installs as an empty package that depends on it.
|
||||
6
legacy/pyproject.toml
Normal file
6
legacy/pyproject.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[build-system]
|
||||
requires = [
|
||||
"setuptools>=42",
|
||||
"wheel"
|
||||
]
|
||||
build-backend = "setuptools.build_meta"
|
||||
24
legacy/setup.cfg
Normal file
24
legacy/setup.cfg
Normal file
@@ -0,0 +1,24 @@
|
||||
[metadata]
|
||||
name = microdot-asyncio
|
||||
version = 0.5.0
|
||||
author = Miguel Grinberg
|
||||
author_email = miguel.grinberg@gmail.com
|
||||
description = AsyncIO support for the Microdot web framework'
|
||||
long_description = file: README.md
|
||||
long_description_content_type = text/markdown
|
||||
url = https://github.com/miguelgrinberg/microdot
|
||||
project_urls =
|
||||
Bug Tracker = https://github.com/miguelgrinberg/microdot/issues
|
||||
classifiers =
|
||||
Environment :: Web Environment
|
||||
Intended Audience :: Developers
|
||||
Programming Language :: Python :: 3
|
||||
Programming Language :: Python :: Implementation :: MicroPython
|
||||
License :: OSI Approved :: MIT License
|
||||
Operating System :: OS Independent
|
||||
[options]
|
||||
zip_safe = False
|
||||
include_package_data = True
|
||||
py_modules =
|
||||
install_requires =
|
||||
microdot
|
||||
3
legacy/setup.py
Normal file
3
legacy/setup.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import setuptools
|
||||
|
||||
setuptools.setup()
|
||||
281
microdot.py
281
microdot.py
@@ -1,281 +0,0 @@
|
||||
import json
|
||||
import re
|
||||
try:
|
||||
import usocket as socket
|
||||
except ImportError:
|
||||
import socket
|
||||
|
||||
|
||||
def urldecode(string):
|
||||
string = string.replace('+', ' ')
|
||||
parts = string.split('%')
|
||||
if len(parts) == 1:
|
||||
return string
|
||||
result = [parts[0]]
|
||||
for item in parts[1:]:
|
||||
if item == '':
|
||||
result.append('%')
|
||||
else:
|
||||
code = item[:2]
|
||||
result.append(chr(int(code, 16)))
|
||||
result.append(item[2:])
|
||||
return ''.join(result)
|
||||
|
||||
|
||||
class Request():
|
||||
def __init__(self, client_sock, client_addr):
|
||||
self.client_sock = client_sock
|
||||
self.client_addr = client_addr
|
||||
|
||||
if not hasattr(client_sock, 'readline'):
|
||||
self.client_stream = client_sock.makefile("rwb")
|
||||
else:
|
||||
self.client_stream = client_sock
|
||||
|
||||
# request line
|
||||
line = self.client_stream.readline().strip().decode('utf-8')
|
||||
self.method, self.path, self.http_version = line.split()
|
||||
if '?' in self.path:
|
||||
self.path, self.query_string = self.path.split('?', 1)
|
||||
else:
|
||||
self.query_string = None
|
||||
|
||||
# headers
|
||||
self.headers = {}
|
||||
self.cookies = {}
|
||||
self.content_length = 0
|
||||
while True:
|
||||
line = self.client_stream.readline().strip().decode('utf-8')
|
||||
if line == '':
|
||||
break
|
||||
header, value = line.split(':', 1)
|
||||
header = header.title()
|
||||
value = value.strip()
|
||||
self.headers[header] = value
|
||||
if header == 'Content-Length':
|
||||
self.content_length = int(value)
|
||||
elif header == 'Content-Type':
|
||||
self.content_type = value
|
||||
elif header == 'Cookie':
|
||||
for cookie in self.headers['Cookie'].split(';'):
|
||||
name, value = cookie.split('=', 1)
|
||||
self.cookies[name] = value
|
||||
|
||||
# body
|
||||
self.body = self.client_stream.read(self.content_length)
|
||||
self._json = None
|
||||
self._form = None
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
if self.content_type != 'application/json':
|
||||
return None
|
||||
if self._json is None:
|
||||
self._json = json.loads(self.body)
|
||||
return self._json
|
||||
|
||||
@property
|
||||
def form(self):
|
||||
if self.content_type != 'application/x-www-form-urlencoded':
|
||||
return None
|
||||
if self._form is None:
|
||||
self._form = {urldecode(key): urldecode(value) for key, value in
|
||||
[pair.split('=', 1) for pair in self.body.decode().split('&')]}
|
||||
return self._form
|
||||
|
||||
def close(self):
|
||||
self.client_stream.close()
|
||||
if self.client_stream != self.client_sock:
|
||||
self.client_sock.close()
|
||||
|
||||
|
||||
class Response():
|
||||
types_map = {
|
||||
'css': 'text/css',
|
||||
'gif': 'image/gif',
|
||||
'html': 'text/html',
|
||||
'jpg': 'image/jpeg',
|
||||
'js': 'application/javascript',
|
||||
'json': 'application/json',
|
||||
'png': 'image/png',
|
||||
'txt': 'text/plain',
|
||||
}
|
||||
|
||||
def __init__(self, body='', status_code=200, headers=None):
|
||||
self.status_code = status_code
|
||||
self.headers = headers or {}
|
||||
self.cookies = []
|
||||
if isinstance(body, (dict, list)):
|
||||
self.body = json.dumps(body).encode()
|
||||
self.headers['Content-Type'] = 'application/json'
|
||||
elif isinstance(body, str):
|
||||
self.body = body.encode()
|
||||
elif isinstance(body, bytes):
|
||||
self.body = body
|
||||
else:
|
||||
self.body = str(body).encode()
|
||||
|
||||
def set_cookie(self, cookie, value, path=None, domain=None, expires=None,
|
||||
max_age=None, secure=False, http_only=False):
|
||||
http_cookie = '{cookie}={value}'.format(cookie=cookie, value=value)
|
||||
if path:
|
||||
http_cookie += '; Path=' + path
|
||||
if domain:
|
||||
http_cookie += '; Domain=' + domain
|
||||
if expires:
|
||||
http_cookie += '; Expires=' + expires.strftime(
|
||||
"%a, %d %b %Y %H:%M:%S GMT")
|
||||
if max_age:
|
||||
http_cookie += '; Max-Age=' + str(max_age)
|
||||
if secure:
|
||||
http_cookie += '; Secure'
|
||||
if http_only:
|
||||
http_cookie += '; httpOnly'
|
||||
if 'Set-Cookie' in self.headers:
|
||||
self.headers['Set-Cookie'].append(http_cookie)
|
||||
else:
|
||||
self.headers['Set-Cookie'] = [http_cookie]
|
||||
|
||||
def write(self, client_stream):
|
||||
# status code
|
||||
client_stream.write('HTTP/1.0 {status_code} {reason}\r\n'.format(
|
||||
status_code=self.status_code,
|
||||
reason='OK' if self.status_code == 200 else 'N/A').encode())
|
||||
|
||||
# headers
|
||||
content_length_found = False
|
||||
content_type_found = False
|
||||
for header, value in self.headers.items():
|
||||
header = header.title()
|
||||
values = value if isinstance(value, list) else [value]
|
||||
for value in values:
|
||||
client_stream.write('{header}: {value}\r\n'.format(
|
||||
header=header, value=value).encode())
|
||||
if header == 'Content-Length':
|
||||
content_length_found = True
|
||||
elif header == 'Content-Type':
|
||||
content_type_found = True
|
||||
if not content_length_found:
|
||||
client_stream.write('Content-Length: {length}\r\n'.format(
|
||||
length=len(self.body)).encode())
|
||||
if not content_type_found:
|
||||
client_stream.write(b'Content-Type: text/plain\r\n')
|
||||
client_stream.write(b'\r\n')
|
||||
|
||||
# body
|
||||
if self.body:
|
||||
client_stream.write(self.body)
|
||||
|
||||
@staticmethod
|
||||
def redirect(location, status_code=302):
|
||||
return Response(status_code=status_code,
|
||||
headers={'Location': location})
|
||||
|
||||
@staticmethod
|
||||
def send_file(filename, status_code=200, content_type=None):
|
||||
if content_type is None:
|
||||
ext = filename.split('.')[-1]
|
||||
if ext in Response.types_map:
|
||||
content_type = Response.types_map[ext]
|
||||
else:
|
||||
content_type = 'application/octet-stream'
|
||||
with open(filename) as f:
|
||||
return Response(body=f.read(), status_code=status_code,
|
||||
headers={'Content-Type': content_type})
|
||||
|
||||
|
||||
class URLPattern():
|
||||
def __init__(self, url_pattern):
|
||||
self.pattern = ''
|
||||
self.args = []
|
||||
use_regex = False
|
||||
for segment in url_pattern.lstrip('/').split('/'):
|
||||
if segment and segment[0] == '<':
|
||||
if segment[-1] != '>':
|
||||
raise ValueError('invalid URL pattern')
|
||||
segment = segment[1:-1]
|
||||
if ':' in segment:
|
||||
type_, name = segment.split(':', 1)
|
||||
else:
|
||||
type_ = 'string'
|
||||
name = segment
|
||||
if type_ == 'string':
|
||||
pattern = '[^/]*'
|
||||
elif type_ == 'int':
|
||||
pattern = '\\d+'
|
||||
elif type_ == 'path':
|
||||
pattern = '.*'
|
||||
elif type_.startswith('regex'):
|
||||
pattern = eval(type_[5:])
|
||||
else:
|
||||
raise ValueError('invalid URL segment type')
|
||||
use_regex = True
|
||||
self.pattern += '/({pattern})'.format(pattern=pattern)
|
||||
self.args.append({'type': type_, 'name': name})
|
||||
else:
|
||||
self.pattern += '/{segment}'.format(segment=segment)
|
||||
if use_regex:
|
||||
self.pattern = re.compile(self.pattern)
|
||||
|
||||
def match(self, path):
|
||||
if isinstance(self.pattern, str):
|
||||
if path != self.pattern:
|
||||
return
|
||||
return {}
|
||||
g = self.pattern.match(path)
|
||||
if not g:
|
||||
return
|
||||
args = {}
|
||||
i = 1
|
||||
for arg in self.args:
|
||||
value = g.group(i)
|
||||
if arg['type'] == 'int':
|
||||
value = int(value)
|
||||
args[arg['name']] = value
|
||||
i += 1
|
||||
return args
|
||||
|
||||
|
||||
class Microdot():
|
||||
def __init__(self) :
|
||||
self.url_map = []
|
||||
|
||||
def route(self, url_pattern, methods=None):
|
||||
def decorated(f):
|
||||
self.url_map.append(
|
||||
(methods or ['GET'], URLPattern(url_pattern), f))
|
||||
return f
|
||||
return decorated
|
||||
|
||||
def run(self, host='0.0.0.0', port=5000):
|
||||
s = socket.socket()
|
||||
ai = socket.getaddrinfo(host, port)
|
||||
addr = ai[0][-1]
|
||||
|
||||
print('Listening on {host}:{port}...'.format(host=host, port=port))
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.bind(addr)
|
||||
s.listen(5)
|
||||
|
||||
while True:
|
||||
req = Request(*s.accept())
|
||||
f = None
|
||||
args = None
|
||||
for route_methods, route_pattern, route_handler in self.url_map:
|
||||
if req.method in route_methods:
|
||||
args = route_pattern.match(req.path)
|
||||
if args is not None:
|
||||
f = route_handler
|
||||
break
|
||||
if f:
|
||||
resp = f(req, **args)
|
||||
if isinstance(resp, tuple):
|
||||
resp = Response(*resp)
|
||||
elif not isinstance(resp, Response):
|
||||
resp = Response(resp)
|
||||
resp.write(req.client_stream)
|
||||
req.close()
|
||||
|
||||
|
||||
redirect = Response.redirect
|
||||
send_file = Response.send_file
|
||||
6
pyproject.toml
Normal file
6
pyproject.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[build-system]
|
||||
requires = [
|
||||
"setuptools>=42",
|
||||
"wheel"
|
||||
]
|
||||
build-backend = "setuptools.build_meta"
|
||||
8
run_tests.py
Normal file
8
run_tests.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, 'src')
|
||||
sys.path.insert(2, 'tests/libs')
|
||||
|
||||
import unittest
|
||||
|
||||
unittest.main('tests')
|
||||
27
setup.cfg
Normal file
27
setup.cfg
Normal file
@@ -0,0 +1,27 @@
|
||||
[metadata]
|
||||
name = microdot
|
||||
version = 0.7.2
|
||||
author = Miguel Grinberg
|
||||
author_email = miguel.grinberg@gmail.com
|
||||
description = The impossibly small web framework for MicroPython
|
||||
long_description = file: README.md
|
||||
long_description_content_type = text/markdown
|
||||
url = https://github.com/miguelgrinberg/microdot
|
||||
project_urls =
|
||||
Bug Tracker = https://github.com/miguelgrinberg/microdot/issues
|
||||
classifiers =
|
||||
Environment :: Web Environment
|
||||
Intended Audience :: Developers
|
||||
Programming Language :: Python :: 3
|
||||
Programming Language :: Python :: Implementation :: MicroPython
|
||||
License :: OSI Approved :: MIT License
|
||||
Operating System :: OS Independent
|
||||
|
||||
[options]
|
||||
zip_safe = False
|
||||
include_package_data = True
|
||||
package_dir =
|
||||
= src
|
||||
py_modules =
|
||||
microdot
|
||||
microdot_asyncio
|
||||
37
setup.py
37
setup.py
@@ -1,36 +1,3 @@
|
||||
"""
|
||||
Microdot
|
||||
--------
|
||||
import setuptools
|
||||
|
||||
Impossibly small web framework for MicroPython.
|
||||
"""
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='microdot',
|
||||
version='0.1.0',
|
||||
url='http://github.com/miguelgrinberg/microdot/',
|
||||
license='MIT',
|
||||
author='Miguel Grinberg',
|
||||
author_email='miguel.grinberg@gmail.com',
|
||||
description='Impossibly small web framework for MicroPython',
|
||||
long_description=__doc__,
|
||||
packages=['microdot'],
|
||||
zip_safe=False,
|
||||
include_package_data=True,
|
||||
platforms='any',
|
||||
tests_require=[
|
||||
'coverage'
|
||||
],
|
||||
classifiers=[
|
||||
'Environment :: Web Environment',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: MIT License',
|
||||
'Operating System :: OS Independent',
|
||||
'Programming Language :: Python',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: Implementation :: MicroPython',
|
||||
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
|
||||
'Topic :: Software Development :: Libraries :: Python Modules'
|
||||
]
|
||||
)
|
||||
setuptools.setup()
|
||||
|
||||
867
src/microdot.py
Normal file
867
src/microdot.py
Normal file
@@ -0,0 +1,867 @@
|
||||
"""
|
||||
microdot
|
||||
--------
|
||||
|
||||
The ``microdot`` module defines a few classes that help implement HTTP-based
|
||||
servers for MicroPython and standard Python, with multithreading support for
|
||||
Python interpreters that support it.
|
||||
"""
|
||||
try:
|
||||
from sys import print_exception
|
||||
except ImportError: # pragma: no cover
|
||||
import traceback
|
||||
|
||||
def print_exception(exc):
|
||||
traceback.print_exc()
|
||||
try:
|
||||
import uerrno as errno
|
||||
except ImportError:
|
||||
import errno
|
||||
|
||||
concurrency_mode = 'threaded'
|
||||
|
||||
try: # pragma: no cover
|
||||
import threading
|
||||
|
||||
def create_thread(f, *args, **kwargs):
|
||||
# use the threading module
|
||||
threading.Thread(target=f, args=args, kwargs=kwargs).start()
|
||||
except ImportError: # pragma: no cover
|
||||
try:
|
||||
import _thread
|
||||
|
||||
def create_thread(f, *args, **kwargs):
|
||||
# use MicroPython's _thread module
|
||||
def run():
|
||||
f(*args, **kwargs)
|
||||
|
||||
_thread.start_new_thread(run, ())
|
||||
except ImportError:
|
||||
def create_thread(f, *args, **kwargs):
|
||||
# no threads available, call function synchronously
|
||||
f(*args, **kwargs)
|
||||
|
||||
concurrency_mode = 'sync'
|
||||
try:
|
||||
import ujson as json
|
||||
except ImportError:
|
||||
import json
|
||||
|
||||
try:
|
||||
import ure as re
|
||||
except ImportError:
|
||||
import re
|
||||
|
||||
try:
|
||||
import usocket as socket
|
||||
except ImportError:
|
||||
try:
|
||||
import socket
|
||||
except ImportError: # pragma: no cover
|
||||
socket = None
|
||||
|
||||
|
||||
def urldecode(string):
|
||||
string = string.replace('+', ' ')
|
||||
parts = string.split('%')
|
||||
if len(parts) == 1:
|
||||
return string
|
||||
result = [parts[0]]
|
||||
for item in parts[1:]:
|
||||
if item == '':
|
||||
result.append('%')
|
||||
else:
|
||||
code = item[:2]
|
||||
result.append(chr(int(code, 16)))
|
||||
result.append(item[2:])
|
||||
return ''.join(result)
|
||||
|
||||
|
||||
class MultiDict(dict):
|
||||
"""A subclass of dictionary that can hold multiple values for the same
|
||||
key. It is used to hold key/value pairs decoded from query strings and
|
||||
form submissions.
|
||||
|
||||
:param initial_dict: an initial dictionary of key/value pairs to
|
||||
initialize this object with.
|
||||
|
||||
Example::
|
||||
|
||||
>>> d = MultiDict()
|
||||
>>> d['sort'] = 'name'
|
||||
>>> d['sort'] = 'email'
|
||||
>>> print(d['sort'])
|
||||
'name'
|
||||
>>> print(d.getlist('sort'))
|
||||
['name', 'email']
|
||||
"""
|
||||
def __init__(self, initial_dict=None):
|
||||
super().__init__()
|
||||
if initial_dict:
|
||||
for key, value in initial_dict.items():
|
||||
self[key] = value
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if key not in self:
|
||||
super().__setitem__(key, [])
|
||||
super().__getitem__(key).append(value)
|
||||
|
||||
def __getitem__(self, key):
|
||||
return super().__getitem__(key)[0]
|
||||
|
||||
def get(self, key, default=None, type=None):
|
||||
"""Return the value for a given key.
|
||||
|
||||
:param key: The key to retrieve.
|
||||
:param default: A default value to use if the key does not exist.
|
||||
:param type: A type conversion callable to apply to the value.
|
||||
|
||||
If the multidict contains more than one value for the requested key,
|
||||
this method returns the first value only.
|
||||
|
||||
Example::
|
||||
|
||||
>>> d = MultiDict()
|
||||
>>> d['age'] = '42'
|
||||
>>> d.get('age')
|
||||
'42'
|
||||
>>> d.get('age', type=int)
|
||||
42
|
||||
>>> d.get('name', default='noname')
|
||||
'noname'
|
||||
"""
|
||||
if key not in self:
|
||||
return default
|
||||
value = self[key]
|
||||
if type is not None:
|
||||
value = type(value)
|
||||
return value
|
||||
|
||||
def getlist(self, key, type=None):
|
||||
"""Return all the values for a given key.
|
||||
|
||||
:param key: The key to retrieve.
|
||||
:param type: A type conversion callable to apply to the values.
|
||||
|
||||
If the requested key does not exist in the dictionary, this method
|
||||
returns an empty list.
|
||||
|
||||
Example::
|
||||
|
||||
>>> d = MultiDict()
|
||||
>>> d.getlist('items')
|
||||
[]
|
||||
>>> d['items'] = '3'
|
||||
>>> d.getlist('items')
|
||||
['3']
|
||||
>>> d['items'] = '56'
|
||||
>>> d.getlist('items')
|
||||
['3', '56']
|
||||
>>> d.getlist('items', type=int)
|
||||
[3, 56]
|
||||
"""
|
||||
if key not in self:
|
||||
return []
|
||||
values = super().__getitem__(key)
|
||||
if type is not None:
|
||||
values = [type(value) for value in values]
|
||||
return values
|
||||
|
||||
|
||||
class Request():
|
||||
"""An HTTP request class.
|
||||
|
||||
:var app: The application instance to which this request belongs.
|
||||
:var client_addr: The address of the client, as a tuple (host, port).
|
||||
:var method: The HTTP method of the request.
|
||||
:var path: The path portion of the URL.
|
||||
:var query_string: The query string portion of the URL.
|
||||
:var args: The parsed query string, as a :class:`MultiDict` object.
|
||||
:var headers: A dictionary with the headers included in the request.
|
||||
:var cookies: A dictionary with the cookies included in the request.
|
||||
:var content_length: The parsed ``Content-Length`` header.
|
||||
:var content_type: The parsed ``Content-Type`` header.
|
||||
:var body: A stream from where the body can be read.
|
||||
:var json: The parsed JSON body, as a dictionary or list, or ``None`` if
|
||||
the request does not have a JSON body.
|
||||
:var form: The parsed form submission body, as a :class:`MultiDict` object,
|
||||
or ``None`` if the request does not have a form submission.
|
||||
:var g: A general purpose container for applications to store data during
|
||||
the life of the request.
|
||||
"""
|
||||
#: Specify the maximum payload size that is accepted. Requests with larger
|
||||
#: payloads will be rejected with a 413 status code. Applications can
|
||||
#: change this maximum as necessary.
|
||||
#:
|
||||
#: Example::
|
||||
#:
|
||||
#: Request.max_content_length = 1 * 1024 * 1024 # 1MB requests allowed
|
||||
max_content_length = 16 * 1024
|
||||
|
||||
#: Specify the maximum length allowed for a line in the request. Requests
|
||||
#: with longer lines will not be correctly interpreted. Applications can
|
||||
#: change this maximum as necessary.
|
||||
#:
|
||||
#: Example::
|
||||
#:
|
||||
#: Request.max_readline = 16 * 1024 # 16KB lines allowed
|
||||
max_readline = 2 * 1024
|
||||
|
||||
class G:
|
||||
pass
|
||||
|
||||
def __init__(self, app, client_addr, method, url, http_version, headers,
|
||||
body):
|
||||
self.app = app
|
||||
self.client_addr = client_addr
|
||||
self.method = method
|
||||
self.path = url
|
||||
self.http_version = http_version
|
||||
if '?' in self.path:
|
||||
self.path, self.query_string = self.path.split('?', 1)
|
||||
self.args = self._parse_urlencoded(self.query_string)
|
||||
else:
|
||||
self.query_string = None
|
||||
self.args = {}
|
||||
self.headers = headers
|
||||
self.cookies = {}
|
||||
self.content_length = 0
|
||||
self.content_type = None
|
||||
for header, value in self.headers.items():
|
||||
if header == 'Content-Length':
|
||||
self.content_length = int(value)
|
||||
elif header == 'Content-Type':
|
||||
self.content_type = value
|
||||
elif header == 'Cookie':
|
||||
for cookie in value.split(';'):
|
||||
name, value = cookie.strip().split('=', 1)
|
||||
self.cookies[name] = value
|
||||
self.body = body
|
||||
self._json = None
|
||||
self._form = None
|
||||
self.g = Request.G()
|
||||
|
||||
@staticmethod
|
||||
def create(app, client_stream, client_addr):
|
||||
"""Create a request object.
|
||||
|
||||
:param app: The Microdot application instance.
|
||||
:param client_stream: An input stream from where the request data can
|
||||
be read.
|
||||
:param client_addr: The address of the client, as a tuple.
|
||||
|
||||
This method returns a newly created ``Request`` object.
|
||||
"""
|
||||
# request line
|
||||
line = Request._safe_readline(client_stream).strip().decode()
|
||||
if not line:
|
||||
return None
|
||||
method, url, http_version = line.split()
|
||||
http_version = http_version.split('/', 1)[1]
|
||||
|
||||
# headers
|
||||
headers = {}
|
||||
content_length = 0
|
||||
while True:
|
||||
line = Request._safe_readline(client_stream).strip().decode()
|
||||
if line == '':
|
||||
break
|
||||
header, value = line.split(':', 1)
|
||||
value = value.strip()
|
||||
headers[header] = value
|
||||
if header == 'Content-Length':
|
||||
content_length = int(value)
|
||||
|
||||
# body
|
||||
body = client_stream.read(content_length) if content_length and \
|
||||
content_length <= Request.max_content_length else b''
|
||||
|
||||
return Request(app, client_addr, method, url, http_version, headers,
|
||||
body)
|
||||
|
||||
def _parse_urlencoded(self, urlencoded):
|
||||
data = MultiDict()
|
||||
for k, v in [pair.split('=', 1) for pair in urlencoded.split('&')]:
|
||||
data[urldecode(k)] = urldecode(v)
|
||||
return data
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
if self._json is None:
|
||||
if self.content_type is None:
|
||||
return None
|
||||
mime_type = self.content_type.split(';')[0]
|
||||
if mime_type != 'application/json':
|
||||
return None
|
||||
self._json = json.loads(self.body.decode())
|
||||
return self._json
|
||||
|
||||
@property
|
||||
def form(self):
|
||||
if self._form is None:
|
||||
if self.content_type is None:
|
||||
return None
|
||||
mime_type = self.content_type.split(';')[0]
|
||||
if mime_type != 'application/x-www-form-urlencoded':
|
||||
return None
|
||||
self._form = self._parse_urlencoded(self.body.decode())
|
||||
return self._form
|
||||
|
||||
@staticmethod
|
||||
def _safe_readline(stream):
|
||||
line = stream.readline(Request.max_readline + 1)
|
||||
if len(line) > Request.max_readline:
|
||||
raise ValueError('line too long')
|
||||
return line
|
||||
|
||||
|
||||
class Response():
|
||||
"""An HTTP response class.
|
||||
|
||||
:param body: The body of the response. If a dictionary or list is given,
|
||||
a JSON formatter is used to generate the body.
|
||||
:param status_code: The numeric HTTP status code of the response. The
|
||||
default is 200.
|
||||
:param headers: A dictionary of headers to include in the response.
|
||||
:param reason: A custom reason phrase to add after the status code. The
|
||||
default is "OK" for responses with a 200 status code and
|
||||
"N/A" for any other status codes.
|
||||
"""
|
||||
types_map = {
|
||||
'css': 'text/css',
|
||||
'gif': 'image/gif',
|
||||
'html': 'text/html',
|
||||
'jpg': 'image/jpeg',
|
||||
'js': 'application/javascript',
|
||||
'json': 'application/json',
|
||||
'png': 'image/png',
|
||||
'txt': 'text/plain',
|
||||
}
|
||||
send_file_buffer_size = 1024
|
||||
|
||||
def __init__(self, body='', status_code=200, headers=None, reason=None):
|
||||
self.status_code = status_code
|
||||
self.headers = headers.copy() if headers else {}
|
||||
self.reason = reason
|
||||
if isinstance(body, (dict, list)):
|
||||
self.body = json.dumps(body).encode()
|
||||
self.headers['Content-Type'] = 'application/json'
|
||||
elif isinstance(body, str):
|
||||
self.body = body.encode()
|
||||
else:
|
||||
# this applies to bytes or file-like objects
|
||||
self.body = body
|
||||
|
||||
def set_cookie(self, cookie, value, path=None, domain=None, expires=None,
|
||||
max_age=None, secure=False, http_only=False):
|
||||
"""Add a cookie to the response.
|
||||
|
||||
:param cookie: The cookie's name.
|
||||
:param value: The cookie's value.
|
||||
:param path: The cookie's path.
|
||||
:param domain: The cookie's domain.
|
||||
:param expires: The cookie expiration time, as a ``datetime`` object.
|
||||
:param max_age: The cookie's ``Max-Age`` value.
|
||||
:param secure: The cookie's ``secure`` flag.
|
||||
:param http_only: The cookie's ``HttpOnly`` flag.
|
||||
"""
|
||||
http_cookie = '{cookie}={value}'.format(cookie=cookie, value=value)
|
||||
if path:
|
||||
http_cookie += '; Path=' + path
|
||||
if domain:
|
||||
http_cookie += '; Domain=' + domain
|
||||
if expires:
|
||||
http_cookie += '; Expires=' + expires.strftime(
|
||||
"%a, %d %b %Y %H:%M:%S GMT")
|
||||
if max_age:
|
||||
http_cookie += '; Max-Age=' + str(max_age)
|
||||
if secure:
|
||||
http_cookie += '; Secure'
|
||||
if http_only:
|
||||
http_cookie += '; HttpOnly'
|
||||
if 'Set-Cookie' in self.headers:
|
||||
self.headers['Set-Cookie'].append(http_cookie)
|
||||
else:
|
||||
self.headers['Set-Cookie'] = [http_cookie]
|
||||
|
||||
def complete(self):
|
||||
if isinstance(self.body, bytes) and \
|
||||
'Content-Length' not in self.headers:
|
||||
self.headers['Content-Length'] = str(len(self.body))
|
||||
if 'Content-Type' not in self.headers:
|
||||
self.headers['Content-Type'] = 'text/plain'
|
||||
|
||||
def write(self, stream):
|
||||
self.complete()
|
||||
|
||||
# status code
|
||||
reason = self.reason if self.reason is not None else \
|
||||
('OK' if self.status_code == 200 else 'N/A')
|
||||
stream.write('HTTP/1.0 {status_code} {reason}\r\n'.format(
|
||||
status_code=self.status_code, reason=reason).encode())
|
||||
|
||||
# headers
|
||||
for header, value in self.headers.items():
|
||||
values = value if isinstance(value, list) else [value]
|
||||
for value in values:
|
||||
stream.write('{header}: {value}\r\n'.format(
|
||||
header=header, value=value).encode())
|
||||
stream.write(b'\r\n')
|
||||
|
||||
# body
|
||||
if self.body:
|
||||
if hasattr(self.body, 'read'):
|
||||
while True:
|
||||
buf = self.body.read(self.send_file_buffer_size)
|
||||
if len(buf):
|
||||
stream.write(buf)
|
||||
if len(buf) < self.send_file_buffer_size:
|
||||
break
|
||||
if hasattr(self.body, 'close'): # pragma: no cover
|
||||
self.body.close()
|
||||
else:
|
||||
stream.write(self.body)
|
||||
|
||||
@classmethod
|
||||
def redirect(cls, location, status_code=302):
|
||||
"""Return a redirect response.
|
||||
|
||||
:param location: The URL to redirect to.
|
||||
:param status_code: The 3xx status code to use for the redirect. The
|
||||
default is 302.
|
||||
"""
|
||||
if '\x0d' in location or '\x0a' in location:
|
||||
raise ValueError('invalid redirect URL')
|
||||
return cls(status_code=status_code, headers={'Location': location})
|
||||
|
||||
@classmethod
|
||||
def send_file(cls, filename, status_code=200, content_type=None):
|
||||
"""Send file contents in a response.
|
||||
|
||||
:param filename: The filename of the file.
|
||||
:param status_code: The 3xx status code to use for the redirect. The
|
||||
default is 302.
|
||||
:param content_type: The ``Content-Type`` header to use in the
|
||||
response. If omitted, it is generated
|
||||
automatically from the file extension.
|
||||
|
||||
Security note: The filename is assumed to be trusted. Never pass
|
||||
filenames provided by the user before validating and sanitizing them
|
||||
first.
|
||||
"""
|
||||
if content_type is None:
|
||||
ext = filename.split('.')[-1]
|
||||
if ext in Response.types_map:
|
||||
content_type = Response.types_map[ext]
|
||||
else:
|
||||
content_type = 'application/octet-stream'
|
||||
f = open(filename, 'rb')
|
||||
return cls(body=f, status_code=status_code,
|
||||
headers={'Content-Type': content_type})
|
||||
|
||||
|
||||
class URLPattern():
|
||||
def __init__(self, url_pattern):
|
||||
self.pattern = ''
|
||||
self.args = []
|
||||
use_regex = False
|
||||
for segment in url_pattern.lstrip('/').split('/'):
|
||||
if segment and segment[0] == '<':
|
||||
if segment[-1] != '>':
|
||||
raise ValueError('invalid URL pattern')
|
||||
segment = segment[1:-1]
|
||||
if ':' in segment:
|
||||
type_, name = segment.rsplit(':', 1)
|
||||
else:
|
||||
type_ = 'string'
|
||||
name = segment
|
||||
if type_ == 'string':
|
||||
pattern = '[^/]+'
|
||||
elif type_ == 'int':
|
||||
pattern = '\\d+'
|
||||
elif type_ == 'path':
|
||||
pattern = '.+'
|
||||
elif type_.startswith('re:'):
|
||||
pattern = type_[3:]
|
||||
else:
|
||||
raise ValueError('invalid URL segment type')
|
||||
use_regex = True
|
||||
self.pattern += '/({pattern})'.format(pattern=pattern)
|
||||
self.args.append({'type': type_, 'name': name})
|
||||
else:
|
||||
self.pattern += '/{segment}'.format(segment=segment)
|
||||
if use_regex:
|
||||
self.pattern = re.compile('^' + self.pattern + '$')
|
||||
|
||||
def match(self, path):
|
||||
if isinstance(self.pattern, str):
|
||||
if path != self.pattern:
|
||||
return
|
||||
return {}
|
||||
g = self.pattern.match(path)
|
||||
if not g:
|
||||
return
|
||||
args = {}
|
||||
i = 1
|
||||
for arg in self.args:
|
||||
value = g.group(i)
|
||||
if arg['type'] == 'int':
|
||||
value = int(value)
|
||||
args[arg['name']] = value
|
||||
i += 1
|
||||
return args
|
||||
|
||||
|
||||
class Microdot():
|
||||
"""An HTTP application class.
|
||||
|
||||
This class implements an HTTP application instance and is heavily
|
||||
influenced by the ``Flask`` class of the Flask framework. It is typically
|
||||
declared near the start of the main application script.
|
||||
|
||||
Example::
|
||||
|
||||
from microdot import Microdot
|
||||
|
||||
app = Microdot()
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.url_map = []
|
||||
self.before_request_handlers = []
|
||||
self.after_request_handlers = []
|
||||
self.error_handlers = {}
|
||||
self.shutdown_requested = False
|
||||
self.debug = False
|
||||
self.server = None
|
||||
|
||||
def route(self, url_pattern, methods=None):
|
||||
"""Decorator that is used to register a function as a request handler
|
||||
for a given URL.
|
||||
|
||||
:param url_pattern: The URL pattern that will be compared against
|
||||
incoming requests.
|
||||
:param methods: The list of HTTP methods to be handled by the
|
||||
decorated function. If omitted, only ``GET`` requests
|
||||
are handled.
|
||||
|
||||
The URL pattern can be a static path (for example, ``/users`` or
|
||||
``/api/invoices/search``) or a path with dynamic components enclosed
|
||||
in ``<`` and ``>`` (for example, ``/users/<id>`` or
|
||||
``/invoices/<number>/products``). Dynamic path components can also
|
||||
include a type prefix, separated from the name with a colon (for
|
||||
example, ``/users/<int:id>``). The type can be ``string`` (the
|
||||
default), ``int``, ``path`` or ``re:[regular-expression]``.
|
||||
|
||||
The first argument of the decorated function must be
|
||||
the request object. Any path arguments that are specified in the URL
|
||||
pattern are passed as keyword arguments. The return value of the
|
||||
function must be a :class:`Response` instance, or the arguments to
|
||||
be passed to this class.
|
||||
|
||||
Example::
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
return 'Hello, world!'
|
||||
"""
|
||||
def decorated(f):
|
||||
self.url_map.append(
|
||||
(methods or ['GET'], URLPattern(url_pattern), f))
|
||||
return f
|
||||
return decorated
|
||||
|
||||
def get(self, url_pattern):
|
||||
"""Decorator that is used to register a function as a ``GET`` request
|
||||
handler for a given URL.
|
||||
|
||||
:param url_pattern: The URL pattern that will be compared against
|
||||
incoming requests.
|
||||
|
||||
This decorator can be used as an alias to the ``route`` decorator with
|
||||
``methods=['GET']``.
|
||||
|
||||
Example::
|
||||
|
||||
@app.get('/users/<int:id>')
|
||||
def get_user(request, id):
|
||||
# ...
|
||||
"""
|
||||
return self.route(url_pattern, methods=['GET'])
|
||||
|
||||
def post(self, url_pattern):
|
||||
"""Decorator that is used to register a function as a ``POST`` request
|
||||
handler for a given URL.
|
||||
|
||||
:param url_pattern: The URL pattern that will be compared against
|
||||
incoming requests.
|
||||
|
||||
This decorator can be used as an alias to the``route`` decorator with
|
||||
``methods=['POST']``.
|
||||
|
||||
Example::
|
||||
|
||||
@app.post('/users')
|
||||
def create_user(request):
|
||||
# ...
|
||||
"""
|
||||
return self.route(url_pattern, methods=['POST'])
|
||||
|
||||
def put(self, url_pattern):
|
||||
"""Decorator that is used to register a function as a ``PUT`` request
|
||||
handler for a given URL.
|
||||
|
||||
:param url_pattern: The URL pattern that will be compared against
|
||||
incoming requests.
|
||||
|
||||
This decorator can be used as an alias to the ``route`` decorator with
|
||||
``methods=['PUT']``.
|
||||
|
||||
Example::
|
||||
|
||||
@app.put('/users/<int:id>')
|
||||
def edit_user(request, id):
|
||||
# ...
|
||||
"""
|
||||
return self.route(url_pattern, methods=['PUT'])
|
||||
|
||||
def patch(self, url_pattern):
|
||||
"""Decorator that is used to register a function as a ``PATCH`` request
|
||||
handler for a given URL.
|
||||
|
||||
:param url_pattern: The URL pattern that will be compared against
|
||||
incoming requests.
|
||||
|
||||
This decorator can be used as an alias to the ``route`` decorator with
|
||||
``methods=['PATCH']``.
|
||||
|
||||
Example::
|
||||
|
||||
@app.patch('/users/<int:id>')
|
||||
def edit_user(request, id):
|
||||
# ...
|
||||
"""
|
||||
return self.route(url_pattern, methods=['PATCH'])
|
||||
|
||||
def delete(self, url_pattern):
|
||||
"""Decorator that is used to register a function as a ``DELETE``
|
||||
request handler for a given URL.
|
||||
|
||||
:param url_pattern: The URL pattern that will be compared against
|
||||
incoming requests.
|
||||
|
||||
This decorator can be used as an alias to the ``route`` decorator with
|
||||
``methods=['DELETE']``.
|
||||
|
||||
Example::
|
||||
|
||||
@app.delete('/users/<int:id>')
|
||||
def delete_user(request, id):
|
||||
# ...
|
||||
"""
|
||||
return self.route(url_pattern, methods=['DELETE'])
|
||||
|
||||
def before_request(self, f):
|
||||
"""Decorator to register a function to run before each request is
|
||||
handled. The decorated function must take a single argument, the
|
||||
request object.
|
||||
|
||||
Example::
|
||||
|
||||
@app.before_request
|
||||
def func(request):
|
||||
# ...
|
||||
"""
|
||||
self.before_request_handlers.append(f)
|
||||
return f
|
||||
|
||||
def after_request(self, f):
|
||||
"""Decorator to register a function to run after each request is
|
||||
handled. The decorated function must take two arguments, the request
|
||||
and response objects. The return value of the function must be an
|
||||
updated response object.
|
||||
|
||||
Example::
|
||||
|
||||
@app.before_request
|
||||
def func(request, response):
|
||||
# ...
|
||||
"""
|
||||
self.after_request_handlers.append(f)
|
||||
return f
|
||||
|
||||
def errorhandler(self, status_code_or_exception_class):
|
||||
"""Decorator to register a function as an error handler. Error handler
|
||||
functions for numeric HTTP status codes must accept a single argument,
|
||||
the request object. Error handler functions for Python exceptions
|
||||
must accept two arguments, the request object and the exception
|
||||
object.
|
||||
|
||||
:param status_code_or_exception_class: The numeric HTTP status code or
|
||||
Python exception class to
|
||||
handle.
|
||||
|
||||
Examples::
|
||||
|
||||
@app.errorhandler(404)
|
||||
def not_found(request):
|
||||
return 'Not found'
|
||||
|
||||
@app.errorhandler(RuntimeError)
|
||||
def runtime_error(request, exception):
|
||||
return 'Runtime error'
|
||||
"""
|
||||
def decorated(f):
|
||||
self.error_handlers[status_code_or_exception_class] = f
|
||||
return f
|
||||
return decorated
|
||||
|
||||
def run(self, host='0.0.0.0', port=5000, debug=False):
|
||||
"""Start the web server. This function does not normally return, as
|
||||
the server enters an endless listening loop. The :func:`shutdown`
|
||||
function provides a method for terminating the server gracefully.
|
||||
|
||||
:param host: The hostname or IP address of the network interface that
|
||||
will be listening for requests. A value of ``'0.0.0.0'``
|
||||
(the default) indicates that the server should listen for
|
||||
requests on all the available interfaces, and a value of
|
||||
``127.0.0.1`` indicates that the server should listen
|
||||
for requests only on the internal networking interface of
|
||||
the host.
|
||||
:param port: The port number to listen for requests. The default is
|
||||
port 5000.
|
||||
:param debug: If ``True``, the server logs debugging information. The
|
||||
default is ``False``.
|
||||
|
||||
Example::
|
||||
|
||||
from microdot import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'Hello, world!'
|
||||
|
||||
app.run(debug=True)
|
||||
"""
|
||||
self.debug = debug
|
||||
self.shutdown_requested = False
|
||||
|
||||
self.server = socket.socket()
|
||||
ai = socket.getaddrinfo(host, port)
|
||||
addr = ai[0][-1]
|
||||
|
||||
if self.debug: # pragma: no cover
|
||||
print('Starting {mode} server on {host}:{port}...'.format(
|
||||
mode=concurrency_mode, host=host, port=port))
|
||||
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
self.server.bind(addr)
|
||||
self.server.listen(5)
|
||||
|
||||
while not self.shutdown_requested:
|
||||
try:
|
||||
sock, addr = self.server.accept()
|
||||
except OSError as exc: # pragma: no cover
|
||||
if exc.args[0] == errno.ECONNABORTED:
|
||||
break
|
||||
else:
|
||||
raise
|
||||
create_thread(self.dispatch_request, sock, addr)
|
||||
|
||||
def shutdown(self):
|
||||
"""Request a server shutdown. The server will then exit its request
|
||||
listening loop and the :func:`run` function will return. This function
|
||||
can be safely called from a route handler, as it only schedules the
|
||||
server to terminate as soon as the request completes.
|
||||
|
||||
Example::
|
||||
|
||||
@app.route('/shutdown')
|
||||
def shutdown(request):
|
||||
request.app.shutdown()
|
||||
return 'The server is shutting down...'
|
||||
"""
|
||||
self.shutdown_requested = True
|
||||
|
||||
def find_route(self, req):
|
||||
f = None
|
||||
for route_methods, route_pattern, route_handler in self.url_map:
|
||||
if req.method in route_methods:
|
||||
req.url_args = route_pattern.match(req.path)
|
||||
if req.url_args is not None:
|
||||
f = route_handler
|
||||
break
|
||||
return f
|
||||
|
||||
def dispatch_request(self, sock, addr):
|
||||
if not hasattr(sock, 'readline'): # pragma: no cover
|
||||
stream = sock.makefile("rwb")
|
||||
else:
|
||||
stream = sock
|
||||
|
||||
req = None
|
||||
try:
|
||||
req = Request.create(self, stream, addr)
|
||||
except Exception as exc: # pragma: no cover
|
||||
print_exception(exc)
|
||||
if req:
|
||||
if req.content_length > req.max_content_length:
|
||||
if 413 in self.error_handlers:
|
||||
res = self.error_handlers[413](req)
|
||||
else:
|
||||
res = 'Payload too large', 413
|
||||
else:
|
||||
f = self.find_route(req)
|
||||
try:
|
||||
res = None
|
||||
if f:
|
||||
for handler in self.before_request_handlers:
|
||||
res = handler(req)
|
||||
if res:
|
||||
break
|
||||
if res is None:
|
||||
res = f(req, **req.url_args)
|
||||
if isinstance(res, tuple):
|
||||
res = Response(*res)
|
||||
elif not isinstance(res, Response):
|
||||
res = Response(res)
|
||||
for handler in self.after_request_handlers:
|
||||
res = handler(req, res) or res
|
||||
elif 404 in self.error_handlers:
|
||||
res = self.error_handlers[404](req)
|
||||
else:
|
||||
res = 'Not found', 404
|
||||
except Exception as exc:
|
||||
print_exception(exc)
|
||||
res = None
|
||||
if exc.__class__ in self.error_handlers:
|
||||
try:
|
||||
res = self.error_handlers[exc.__class__](req, exc)
|
||||
except Exception as exc2: # pragma: no cover
|
||||
print_exception(exc2)
|
||||
if res is None:
|
||||
if 500 in self.error_handlers:
|
||||
res = self.error_handlers[500](req)
|
||||
else:
|
||||
res = 'Internal server error', 500
|
||||
else:
|
||||
res = 'Bad request', 400
|
||||
if isinstance(res, tuple):
|
||||
res = Response(*res)
|
||||
elif not isinstance(res, Response):
|
||||
res = Response(res)
|
||||
res.write(stream)
|
||||
stream.close()
|
||||
if stream != sock: # pragma: no cover
|
||||
sock.close()
|
||||
if self.shutdown_requested: # pragma: no cover
|
||||
self.server.close()
|
||||
if self.debug and req: # pragma: no cover
|
||||
print('{method} {path} {status_code}'.format(
|
||||
method=req.method, path=req.path,
|
||||
status_code=res.status_code))
|
||||
|
||||
|
||||
redirect = Response.redirect
|
||||
send_file = Response.send_file
|
||||
294
src/microdot_asyncio.py
Normal file
294
src/microdot_asyncio.py
Normal file
@@ -0,0 +1,294 @@
|
||||
"""
|
||||
microdot_asyncio
|
||||
----------------
|
||||
|
||||
The ``microdot_asyncio`` module defines a few classes that help implement
|
||||
HTTP-based servers for MicroPython and standard Python that use ``asyncio``
|
||||
and coroutines.
|
||||
"""
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
import asyncio
|
||||
from microdot import Microdot as BaseMicrodot
|
||||
from microdot import print_exception
|
||||
from microdot import Request as BaseRequest
|
||||
from microdot import Response as BaseResponse
|
||||
|
||||
|
||||
def _iscoroutine(coro):
|
||||
return hasattr(coro, 'send') and hasattr(coro, 'throw')
|
||||
|
||||
|
||||
class Request(BaseRequest):
|
||||
@staticmethod
|
||||
async def create(app, client_stream, client_addr):
|
||||
"""Create a request object.
|
||||
|
||||
:param app: The Microdot application instance.
|
||||
:param client_stream: An input stream from where the request data can
|
||||
be read.
|
||||
:param client_addr: The address of the client, as a tuple.
|
||||
|
||||
This method is a coroutine. It returns a newly created ``Request``
|
||||
object.
|
||||
"""
|
||||
# request line
|
||||
line = (await Request._safe_readline(client_stream)).strip().decode()
|
||||
if not line: # pragma: no cover
|
||||
return None
|
||||
method, url, http_version = line.split()
|
||||
http_version = http_version.split('/', 1)[1]
|
||||
|
||||
# headers
|
||||
headers = {}
|
||||
content_length = 0
|
||||
while True:
|
||||
line = (await Request._safe_readline(
|
||||
client_stream)).strip().decode()
|
||||
if line == '':
|
||||
break
|
||||
header, value = line.split(':', 1)
|
||||
value = value.strip()
|
||||
headers[header] = value
|
||||
if header == 'Content-Length':
|
||||
content_length = int(value)
|
||||
|
||||
# body
|
||||
body = await client_stream.read(content_length) if content_length and \
|
||||
content_length <= Request.max_content_length else b''
|
||||
|
||||
return Request(app, client_addr, method, url, http_version, headers,
|
||||
body)
|
||||
|
||||
@staticmethod
|
||||
async def _safe_readline(stream):
|
||||
line = (await stream.readline())
|
||||
if len(line) > Request.max_readline:
|
||||
raise ValueError('line too long')
|
||||
return line
|
||||
|
||||
|
||||
class Response(BaseResponse):
|
||||
"""An HTTP response class.
|
||||
|
||||
:param body: The body of the response. If a dictionary or list is given,
|
||||
a JSON formatter is used to generate the body.
|
||||
:param status_code: The numeric HTTP status code of the response. The
|
||||
default is 200.
|
||||
:param headers: A dictionary of headers to include in the response.
|
||||
:param reason: A custom reason phrase to add after the status code. The
|
||||
default is "OK" for responses with a 200 status code and
|
||||
"N/A" for any other status codes.
|
||||
"""
|
||||
async def write(self, stream):
|
||||
self.complete()
|
||||
|
||||
# status code
|
||||
reason = self.reason if self.reason is not None else \
|
||||
('OK' if self.status_code == 200 else 'N/A')
|
||||
await stream.awrite('HTTP/1.0 {status_code} {reason}\r\n'.format(
|
||||
status_code=self.status_code, reason=reason).encode())
|
||||
|
||||
# headers
|
||||
for header, value in self.headers.items():
|
||||
values = value if isinstance(value, list) else [value]
|
||||
for value in values:
|
||||
await stream.awrite('{header}: {value}\r\n'.format(
|
||||
header=header, value=value).encode())
|
||||
await stream.awrite(b'\r\n')
|
||||
|
||||
# body
|
||||
if self.body:
|
||||
if hasattr(self.body, 'read'):
|
||||
while True:
|
||||
buf = self.body.read(self.send_file_buffer_size)
|
||||
if len(buf):
|
||||
await stream.awrite(buf)
|
||||
if len(buf) < self.send_file_buffer_size:
|
||||
break
|
||||
if hasattr(self.body, 'close'): # pragma: no cover
|
||||
self.body.close()
|
||||
else:
|
||||
await stream.awrite(self.body)
|
||||
|
||||
|
||||
class Microdot(BaseMicrodot):
|
||||
async def start_server(self, host='0.0.0.0', port=5000, debug=False):
|
||||
"""Start the Microdot web server as a coroutine. This coroutine does
|
||||
not normally return, as the server enters an endless listening loop.
|
||||
The :func:`shutdown` function provides a method for terminating the
|
||||
server gracefully.
|
||||
|
||||
:param host: The hostname or IP address of the network interface that
|
||||
will be listening for requests. A value of ``'0.0.0.0'``
|
||||
(the default) indicates that the server should listen for
|
||||
requests on all the available interfaces, and a value of
|
||||
``127.0.0.1`` indicates that the server should listen
|
||||
for requests only on the internal networking interface of
|
||||
the host.
|
||||
:param port: The port number to listen for requests. The default is
|
||||
port 5000.
|
||||
:param debug: If ``True``, the server logs debugging information. The
|
||||
default is ``False``.
|
||||
|
||||
This method is a coroutine.
|
||||
|
||||
Example::
|
||||
|
||||
import asyncio
|
||||
from microdot_asyncio import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
async def index():
|
||||
return 'Hello, world!'
|
||||
|
||||
async def main():
|
||||
await app.start_server(debug=True)
|
||||
|
||||
asyncio.run(main())
|
||||
"""
|
||||
self.debug = debug
|
||||
|
||||
async def serve(reader, writer):
|
||||
if not hasattr(writer, 'awrite'): # pragma: no cover
|
||||
# CPython provides the awrite and aclose methods in 3.8+
|
||||
async def awrite(self, data):
|
||||
self.write(data)
|
||||
await self.drain()
|
||||
|
||||
async def aclose(self):
|
||||
self.close()
|
||||
await self.wait_closed()
|
||||
|
||||
from types import MethodType
|
||||
writer.awrite = MethodType(awrite, writer)
|
||||
writer.aclose = MethodType(aclose, writer)
|
||||
|
||||
await self.dispatch_request(reader, writer)
|
||||
|
||||
if self.debug: # pragma: no cover
|
||||
print('Starting async server on {host}:{port}...'.format(
|
||||
host=host, port=port))
|
||||
|
||||
self.server = await asyncio.start_server(serve, host, port)
|
||||
while True:
|
||||
try:
|
||||
await self.server.wait_closed()
|
||||
break
|
||||
except AttributeError: # pragma: no cover
|
||||
# the task hasn't been initialized in the server object yet
|
||||
# wait a bit and try again
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
def run(self, host='0.0.0.0', port=5000, debug=False):
|
||||
"""Start the web server. This function does not normally return, as
|
||||
the server enters an endless listening loop. The :func:`shutdown`
|
||||
function provides a method for terminating the server gracefully.
|
||||
|
||||
:param host: The hostname or IP address of the network interface that
|
||||
will be listening for requests. A value of ``'0.0.0.0'``
|
||||
(the default) indicates that the server should listen for
|
||||
requests on all the available interfaces, and a value of
|
||||
``127.0.0.1`` indicates that the server should listen
|
||||
for requests only on the internal networking interface of
|
||||
the host.
|
||||
:param port: The port number to listen for requests. The default is
|
||||
port 5000.
|
||||
:param debug: If ``True``, the server logs debugging information. The
|
||||
default is ``False``.
|
||||
|
||||
Example::
|
||||
|
||||
from microdot_asyncio import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
async def index():
|
||||
return 'Hello, world!'
|
||||
|
||||
app.run(debug=True)
|
||||
"""
|
||||
asyncio.run(self.start_server(host=host, port=port, debug=debug))
|
||||
|
||||
def shutdown(self):
|
||||
self.server.close()
|
||||
|
||||
async def dispatch_request(self, reader, writer):
|
||||
req = None
|
||||
try:
|
||||
req = await Request.create(self, reader,
|
||||
writer.get_extra_info('peername'))
|
||||
except Exception as exc: # pragma: no cover
|
||||
print_exception(exc)
|
||||
if req:
|
||||
if req.content_length > req.max_content_length:
|
||||
if 413 in self.error_handlers:
|
||||
res = await self._invoke_handler(
|
||||
self.error_handlers[413], req)
|
||||
else:
|
||||
res = 'Payload too large', 413
|
||||
else:
|
||||
f = self.find_route(req)
|
||||
try:
|
||||
res = None
|
||||
if f:
|
||||
for handler in self.before_request_handlers:
|
||||
res = await self._invoke_handler(handler, req)
|
||||
if res:
|
||||
break
|
||||
if res is None:
|
||||
res = await self._invoke_handler(
|
||||
f, req, **req.url_args)
|
||||
if isinstance(res, tuple):
|
||||
res = Response(*res)
|
||||
elif not isinstance(res, Response):
|
||||
res = Response(res)
|
||||
for handler in self.after_request_handlers:
|
||||
res = await self._invoke_handler(
|
||||
handler, req, res) or res
|
||||
elif 404 in self.error_handlers:
|
||||
res = await self._invoke_handler(
|
||||
self.error_handlers[404], req)
|
||||
else:
|
||||
res = 'Not found', 404
|
||||
except Exception as exc:
|
||||
print_exception(exc)
|
||||
res = None
|
||||
if exc.__class__ in self.error_handlers:
|
||||
try:
|
||||
res = await self._invoke_handler(
|
||||
self.error_handlers[exc.__class__], req, exc)
|
||||
except Exception as exc2: # pragma: no cover
|
||||
print_exception(exc2)
|
||||
if res is None:
|
||||
if 500 in self.error_handlers:
|
||||
res = await self._invoke_handler(
|
||||
self.error_handlers[500], req)
|
||||
else:
|
||||
res = 'Internal server error', 500
|
||||
else:
|
||||
res = 'Bad request', 400
|
||||
if isinstance(res, tuple):
|
||||
res = Response(*res)
|
||||
elif not isinstance(res, Response):
|
||||
res = Response(res)
|
||||
await res.write(writer)
|
||||
await writer.aclose()
|
||||
if self.debug and req: # pragma: no cover
|
||||
print('{method} {path} {status_code}'.format(
|
||||
method=req.method, path=req.path,
|
||||
status_code=res.status_code))
|
||||
|
||||
async def _invoke_handler(self, f_or_coro, *args, **kwargs):
|
||||
ret = f_or_coro(*args, **kwargs)
|
||||
if _iscoroutine(ret):
|
||||
ret = await ret
|
||||
return ret
|
||||
|
||||
|
||||
redirect = Response.redirect
|
||||
send_file = Response.send_file
|
||||
9
tests/__init__.py
Normal file
9
tests/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from tests.microdot.test_multidict import TestMultiDict
|
||||
from tests.microdot.test_request import TestRequest
|
||||
from tests.microdot.test_response import TestResponse
|
||||
from tests.microdot.test_url_pattern import TestURLPattern
|
||||
from tests.microdot.test_microdot import TestMicrodot
|
||||
|
||||
from tests.microdot_asyncio.test_request_asyncio import TestRequestAsync
|
||||
from tests.microdot_asyncio.test_response_asyncio import TestResponseAsync
|
||||
from tests.microdot_asyncio.test_microdot_asyncio import TestMicrodotAsync
|
||||
1
tests/files/test.bin
Normal file
1
tests/files/test.bin
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
1
tests/files/test.css
Normal file
1
tests/files/test.css
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
1
tests/files/test.gif
Normal file
1
tests/files/test.gif
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
1
tests/files/test.html
Normal file
1
tests/files/test.html
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
1
tests/files/test.jpg
Normal file
1
tests/files/test.jpg
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
1
tests/files/test.js
Normal file
1
tests/files/test.js
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
1
tests/files/test.json
Normal file
1
tests/files/test.json
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
1
tests/files/test.png
Normal file
1
tests/files/test.png
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
1
tests/files/test.txt
Normal file
1
tests/files/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
2143
tests/libs/datetime.py
Normal file
2143
tests/libs/datetime.py
Normal file
File diff suppressed because it is too large
Load Diff
46
tests/libs/ffilib.py
Normal file
46
tests/libs/ffilib.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import sys
|
||||
try:
|
||||
import ffi
|
||||
except ImportError:
|
||||
ffi = None
|
||||
|
||||
_cache = {}
|
||||
|
||||
def open(name, maxver=10, extra=()):
|
||||
if not ffi:
|
||||
return None
|
||||
try:
|
||||
return _cache[name]
|
||||
except KeyError:
|
||||
pass
|
||||
def libs():
|
||||
if sys.platform == "linux":
|
||||
yield '%s.so' % name
|
||||
for i in range(maxver, -1, -1):
|
||||
yield '%s.so.%u' % (name, i)
|
||||
else:
|
||||
for ext in ('dylib', 'dll'):
|
||||
yield '%s.%s' % (name, ext)
|
||||
for n in extra:
|
||||
yield n
|
||||
err = None
|
||||
for n in libs():
|
||||
try:
|
||||
l = ffi.open(n)
|
||||
_cache[name] = l
|
||||
return l
|
||||
except OSError as e:
|
||||
err = e
|
||||
raise err
|
||||
|
||||
def libc():
|
||||
return open("libc", 6)
|
||||
|
||||
# Find out bitness of the platform, even if long ints are not supported
|
||||
# TODO: All bitness differences should be removed from micropython-lib, and
|
||||
# this snippet too.
|
||||
bitness = 1
|
||||
v = sys.maxsize
|
||||
while v:
|
||||
bitness += 1
|
||||
v >>= 1
|
||||
76
tests/libs/time.py
Normal file
76
tests/libs/time.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from utime import *
|
||||
from ucollections import namedtuple
|
||||
import ustruct
|
||||
import uctypes
|
||||
import ffi
|
||||
import ffilib
|
||||
import array
|
||||
|
||||
libc = ffilib.libc()
|
||||
|
||||
# struct tm *gmtime(const time_t *timep);
|
||||
# struct tm *localtime(const time_t *timep);
|
||||
# size_t strftime(char *s, size_t max, const char *format,
|
||||
# const struct tm *tm);
|
||||
gmtime_ = libc.func("P", "gmtime", "P")
|
||||
localtime_ = libc.func("P", "localtime", "P")
|
||||
strftime_ = libc.func("i", "strftime", "sisP")
|
||||
mktime_ = libc.func("i", "mktime", "P")
|
||||
|
||||
_struct_time = namedtuple("struct_time",
|
||||
["tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst"])
|
||||
|
||||
def _tuple_to_c_tm(t):
|
||||
return ustruct.pack("@iiiiiiiii", t[5], t[4], t[3], t[2], t[1] - 1, t[0] - 1900, (t[6] + 1) % 7, t[7] - 1, t[8])
|
||||
|
||||
|
||||
def _c_tm_to_tuple(tm):
|
||||
t = ustruct.unpack("@iiiiiiiii", tm)
|
||||
return _struct_time(t[5] + 1900, t[4] + 1, t[3], t[2], t[1], t[0], (t[6] - 1) % 7, t[7] + 1, t[8])
|
||||
|
||||
def struct_time(tm):
|
||||
return _struct_time(*tm)
|
||||
|
||||
|
||||
def strftime(format, t=None):
|
||||
if t is None:
|
||||
t = localtime()
|
||||
|
||||
buf = bytearray(32)
|
||||
l = strftime_(buf, 32, format, _tuple_to_c_tm(t))
|
||||
return str(buf[:l], "utf-8")
|
||||
|
||||
|
||||
def localtime(t=None):
|
||||
if t is None:
|
||||
t = time()
|
||||
|
||||
t = int(t)
|
||||
a = ustruct.pack('l', t)
|
||||
tm_p = localtime_(a)
|
||||
return _c_tm_to_tuple(uctypes.bytearray_at(tm_p, 36))
|
||||
|
||||
|
||||
def gmtime(t=None):
|
||||
if t is None:
|
||||
t = time()
|
||||
|
||||
t = int(t)
|
||||
a = ustruct.pack('l', t)
|
||||
tm_p = gmtime_(a)
|
||||
return _c_tm_to_tuple(uctypes.bytearray_at(tm_p, 36))
|
||||
|
||||
|
||||
def mktime(tt):
|
||||
return mktime_(_tuple_to_c_tm(tt))
|
||||
|
||||
|
||||
def perf_counter():
|
||||
return time()
|
||||
|
||||
def process_time():
|
||||
return clock()
|
||||
|
||||
|
||||
daylight = 0
|
||||
timezone = 0
|
||||
30
tests/libs/uasyncio/__init__.py
Normal file
30
tests/libs/uasyncio/__init__.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# MicroPython uasyncio module
|
||||
# MIT license; Copyright (c) 2019 Damien P. George
|
||||
|
||||
from .core import *
|
||||
|
||||
__version__ = (3, 0, 0)
|
||||
|
||||
_attrs = {
|
||||
"wait_for": "funcs",
|
||||
"wait_for_ms": "funcs",
|
||||
"gather": "funcs",
|
||||
"Event": "event",
|
||||
"ThreadSafeFlag": "event",
|
||||
"Lock": "lock",
|
||||
"open_connection": "stream",
|
||||
"start_server": "stream",
|
||||
"StreamReader": "stream",
|
||||
"StreamWriter": "stream",
|
||||
}
|
||||
|
||||
# Lazy loader, effectively does:
|
||||
# global attr
|
||||
# from .mod import attr
|
||||
def __getattr__(attr):
|
||||
mod = _attrs.get(attr, None)
|
||||
if mod is None:
|
||||
raise AttributeError(attr)
|
||||
value = getattr(__import__(mod, None, None, True, 1), attr)
|
||||
globals()[attr] = value
|
||||
return value
|
||||
281
tests/libs/uasyncio/core.py
Normal file
281
tests/libs/uasyncio/core.py
Normal file
@@ -0,0 +1,281 @@
|
||||
# MicroPython uasyncio module
|
||||
# MIT license; Copyright (c) 2019 Damien P. George
|
||||
|
||||
from time import ticks_ms as ticks, ticks_diff, ticks_add
|
||||
import sys, select
|
||||
|
||||
# Import TaskQueue and Task, preferring built-in C code over Python code
|
||||
try:
|
||||
from _uasyncio import TaskQueue, Task
|
||||
except:
|
||||
from .task import TaskQueue, Task
|
||||
|
||||
|
||||
################################################################################
|
||||
# Exceptions
|
||||
|
||||
|
||||
class CancelledError(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
class TimeoutError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
# Used when calling Loop.call_exception_handler
|
||||
_exc_context = {"message": "Task exception wasn't retrieved", "exception": None, "future": None}
|
||||
|
||||
|
||||
################################################################################
|
||||
# Sleep functions
|
||||
|
||||
# "Yield" once, then raise StopIteration
|
||||
class SingletonGenerator:
|
||||
def __init__(self):
|
||||
self.state = None
|
||||
self.exc = StopIteration()
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
if self.state is not None:
|
||||
_task_queue.push_sorted(cur_task, self.state)
|
||||
self.state = None
|
||||
return None
|
||||
else:
|
||||
self.exc.__traceback__ = None
|
||||
raise self.exc
|
||||
|
||||
|
||||
# Pause task execution for the given time (integer in milliseconds, uPy extension)
|
||||
# Use a SingletonGenerator to do it without allocating on the heap
|
||||
def sleep_ms(t, sgen=SingletonGenerator()):
|
||||
assert sgen.state is None
|
||||
sgen.state = ticks_add(ticks(), max(0, t))
|
||||
return sgen
|
||||
|
||||
|
||||
# Pause task execution for the given time (in seconds)
|
||||
def sleep(t):
|
||||
return sleep_ms(int(t * 1000))
|
||||
|
||||
|
||||
################################################################################
|
||||
# Queue and poller for stream IO
|
||||
|
||||
|
||||
class IOQueue:
|
||||
def __init__(self):
|
||||
self.poller = select.poll()
|
||||
self.map = {} # maps id(stream) to [task_waiting_read, task_waiting_write, stream]
|
||||
|
||||
def _enqueue(self, s, idx):
|
||||
if id(s) not in self.map:
|
||||
entry = [None, None, s]
|
||||
entry[idx] = cur_task
|
||||
self.map[id(s)] = entry
|
||||
self.poller.register(s, select.POLLIN if idx == 0 else select.POLLOUT)
|
||||
else:
|
||||
sm = self.map[id(s)]
|
||||
assert sm[idx] is None
|
||||
assert sm[1 - idx] is not None
|
||||
sm[idx] = cur_task
|
||||
self.poller.modify(s, select.POLLIN | select.POLLOUT)
|
||||
# Link task to this IOQueue so it can be removed if needed
|
||||
cur_task.data = self
|
||||
|
||||
def _dequeue(self, s):
|
||||
del self.map[id(s)]
|
||||
self.poller.unregister(s)
|
||||
|
||||
def queue_read(self, s):
|
||||
self._enqueue(s, 0)
|
||||
|
||||
def queue_write(self, s):
|
||||
self._enqueue(s, 1)
|
||||
|
||||
def remove(self, task):
|
||||
while True:
|
||||
del_s = None
|
||||
for k in self.map: # Iterate without allocating on the heap
|
||||
q0, q1, s = self.map[k]
|
||||
if q0 is task or q1 is task:
|
||||
del_s = s
|
||||
break
|
||||
if del_s is not None:
|
||||
self._dequeue(s)
|
||||
else:
|
||||
break
|
||||
|
||||
def wait_io_event(self, dt):
|
||||
for s, ev in self.poller.ipoll(dt):
|
||||
sm = self.map[id(s)]
|
||||
# print('poll', s, sm, ev)
|
||||
if ev & ~select.POLLOUT and sm[0] is not None:
|
||||
# POLLIN or error
|
||||
_task_queue.push_head(sm[0])
|
||||
sm[0] = None
|
||||
if ev & ~select.POLLIN and sm[1] is not None:
|
||||
# POLLOUT or error
|
||||
_task_queue.push_head(sm[1])
|
||||
sm[1] = None
|
||||
if sm[0] is None and sm[1] is None:
|
||||
self._dequeue(s)
|
||||
elif sm[0] is None:
|
||||
self.poller.modify(s, select.POLLOUT)
|
||||
else:
|
||||
self.poller.modify(s, select.POLLIN)
|
||||
|
||||
|
||||
################################################################################
|
||||
# Main run loop
|
||||
|
||||
# Ensure the awaitable is a task
|
||||
def _promote_to_task(aw):
|
||||
return aw if isinstance(aw, Task) else create_task(aw)
|
||||
|
||||
|
||||
# Create and schedule a new task from a coroutine
|
||||
def create_task(coro):
|
||||
if not hasattr(coro, "send"):
|
||||
raise TypeError("coroutine expected")
|
||||
t = Task(coro, globals())
|
||||
_task_queue.push_head(t)
|
||||
return t
|
||||
|
||||
|
||||
# Keep scheduling tasks until there are none left to schedule
|
||||
def run_until_complete(main_task=None):
|
||||
global cur_task
|
||||
excs_all = (CancelledError, Exception) # To prevent heap allocation in loop
|
||||
excs_stop = (CancelledError, StopIteration) # To prevent heap allocation in loop
|
||||
while True:
|
||||
# Wait until the head of _task_queue is ready to run
|
||||
dt = 1
|
||||
while dt > 0:
|
||||
dt = -1
|
||||
t = _task_queue.peek()
|
||||
if t:
|
||||
# A task waiting on _task_queue; "ph_key" is time to schedule task at
|
||||
dt = max(0, ticks_diff(t.ph_key, ticks()))
|
||||
elif not _io_queue.map:
|
||||
# No tasks can be woken so finished running
|
||||
return
|
||||
# print('(poll {})'.format(dt), len(_io_queue.map))
|
||||
_io_queue.wait_io_event(dt)
|
||||
|
||||
# Get next task to run and continue it
|
||||
t = _task_queue.pop_head()
|
||||
cur_task = t
|
||||
try:
|
||||
# Continue running the coroutine, it's responsible for rescheduling itself
|
||||
exc = t.data
|
||||
if not exc:
|
||||
t.coro.send(None)
|
||||
else:
|
||||
t.data = None
|
||||
t.coro.throw(exc)
|
||||
except excs_all as er:
|
||||
# Check the task is not on any event queue
|
||||
assert t.data is None
|
||||
# This task is done, check if it's the main task and then loop should stop
|
||||
if t is main_task:
|
||||
if isinstance(er, StopIteration):
|
||||
return er.value
|
||||
raise er
|
||||
# Schedule any other tasks waiting on the completion of this task
|
||||
waiting = False
|
||||
if hasattr(t, "waiting"):
|
||||
while t.waiting.peek():
|
||||
_task_queue.push_head(t.waiting.pop_head())
|
||||
waiting = True
|
||||
t.waiting = None # Free waiting queue head
|
||||
if not waiting and not isinstance(er, excs_stop):
|
||||
# An exception ended this detached task, so queue it for later
|
||||
# execution to handle the uncaught exception if no other task retrieves
|
||||
# the exception in the meantime (this is handled by Task.throw).
|
||||
_task_queue.push_head(t)
|
||||
# Indicate task is done by setting coro to the task object itself
|
||||
t.coro = t
|
||||
# Save return value of coro to pass up to caller
|
||||
t.data = er
|
||||
|
||||
|
||||
# Create a new task from a coroutine and run it until it finishes
|
||||
def run(coro):
|
||||
return run_until_complete(create_task(coro))
|
||||
|
||||
|
||||
################################################################################
|
||||
# Event loop wrapper
|
||||
|
||||
|
||||
async def _stopper():
|
||||
pass
|
||||
|
||||
|
||||
_stop_task = None
|
||||
|
||||
|
||||
class Loop:
|
||||
_exc_handler = None
|
||||
|
||||
def create_task(coro):
|
||||
return create_task(coro)
|
||||
|
||||
def run_forever():
|
||||
global _stop_task
|
||||
_stop_task = Task(_stopper(), globals())
|
||||
run_until_complete(_stop_task)
|
||||
# TODO should keep running until .stop() is called, even if there're no tasks left
|
||||
|
||||
def run_until_complete(aw):
|
||||
return run_until_complete(_promote_to_task(aw))
|
||||
|
||||
def stop():
|
||||
global _stop_task
|
||||
if _stop_task is not None:
|
||||
_task_queue.push_head(_stop_task)
|
||||
# If stop() is called again, do nothing
|
||||
_stop_task = None
|
||||
|
||||
def close():
|
||||
pass
|
||||
|
||||
def set_exception_handler(handler):
|
||||
Loop._exc_handler = handler
|
||||
|
||||
def get_exception_handler():
|
||||
return Loop._exc_handler
|
||||
|
||||
def default_exception_handler(loop, context):
|
||||
print(context["message"])
|
||||
print("future:", context["future"], "coro=", context["future"].coro)
|
||||
sys.print_exception(context["exception"])
|
||||
|
||||
def call_exception_handler(context):
|
||||
(Loop._exc_handler or Loop.default_exception_handler)(Loop, context)
|
||||
|
||||
|
||||
# The runq_len and waitq_len arguments are for legacy uasyncio compatibility
|
||||
def get_event_loop(runq_len=0, waitq_len=0):
|
||||
return Loop
|
||||
|
||||
|
||||
def current_task():
|
||||
return cur_task
|
||||
|
||||
|
||||
def new_event_loop():
|
||||
global _task_queue, _io_queue
|
||||
# TaskQueue of Task instances
|
||||
_task_queue = TaskQueue()
|
||||
# Task queue and poller for stream IO
|
||||
_io_queue = IOQueue()
|
||||
return Loop
|
||||
|
||||
|
||||
# Initialise default event loop
|
||||
new_event_loop()
|
||||
62
tests/libs/uasyncio/event.py
Normal file
62
tests/libs/uasyncio/event.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# MicroPython uasyncio module
|
||||
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||
|
||||
from . import core
|
||||
|
||||
# Event class for primitive events that can be waited on, set, and cleared
|
||||
class Event:
|
||||
def __init__(self):
|
||||
self.state = False # False=unset; True=set
|
||||
self.waiting = core.TaskQueue() # Queue of Tasks waiting on completion of this event
|
||||
|
||||
def is_set(self):
|
||||
return self.state
|
||||
|
||||
def set(self):
|
||||
# Event becomes set, schedule any tasks waiting on it
|
||||
# Note: This must not be called from anything except the thread running
|
||||
# the asyncio loop (i.e. neither hard or soft IRQ, or a different thread).
|
||||
while self.waiting.peek():
|
||||
core._task_queue.push_head(self.waiting.pop_head())
|
||||
self.state = True
|
||||
|
||||
def clear(self):
|
||||
self.state = False
|
||||
|
||||
async def wait(self):
|
||||
if not self.state:
|
||||
# Event not set, put the calling task on the event's waiting queue
|
||||
self.waiting.push_head(core.cur_task)
|
||||
# Set calling task's data to the event's queue so it can be removed if needed
|
||||
core.cur_task.data = self.waiting
|
||||
yield
|
||||
return True
|
||||
|
||||
|
||||
# MicroPython-extension: This can be set from outside the asyncio event loop,
|
||||
# such as other threads, IRQs or scheduler context. Implementation is a stream
|
||||
# that asyncio will poll until a flag is set.
|
||||
# Note: Unlike Event, this is self-clearing.
|
||||
try:
|
||||
import uio
|
||||
|
||||
class ThreadSafeFlag(uio.IOBase):
|
||||
def __init__(self):
|
||||
self._flag = 0
|
||||
|
||||
def ioctl(self, req, flags):
|
||||
if req == 3: # MP_STREAM_POLL
|
||||
return self._flag * flags
|
||||
return None
|
||||
|
||||
def set(self):
|
||||
self._flag = 1
|
||||
|
||||
async def wait(self):
|
||||
if not self._flag:
|
||||
yield core._io_queue.queue_read(self)
|
||||
self._flag = 0
|
||||
|
||||
|
||||
except ImportError:
|
||||
pass
|
||||
74
tests/libs/uasyncio/funcs.py
Normal file
74
tests/libs/uasyncio/funcs.py
Normal file
@@ -0,0 +1,74 @@
|
||||
# MicroPython uasyncio module
|
||||
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||
|
||||
from . import core
|
||||
|
||||
|
||||
async def wait_for(aw, timeout, sleep=core.sleep):
|
||||
aw = core._promote_to_task(aw)
|
||||
if timeout is None:
|
||||
return await aw
|
||||
|
||||
def runner(waiter, aw):
|
||||
nonlocal status, result
|
||||
try:
|
||||
result = await aw
|
||||
s = True
|
||||
except BaseException as er:
|
||||
s = er
|
||||
if status is None:
|
||||
# The waiter is still waiting, set status for it and cancel it.
|
||||
status = s
|
||||
waiter.cancel()
|
||||
|
||||
# Run aw in a separate runner task that manages its exceptions.
|
||||
status = None
|
||||
result = None
|
||||
runner_task = core.create_task(runner(core.cur_task, aw))
|
||||
|
||||
try:
|
||||
# Wait for the timeout to elapse.
|
||||
await sleep(timeout)
|
||||
except core.CancelledError as er:
|
||||
if status is True:
|
||||
# aw completed successfully and cancelled the sleep, so return aw's result.
|
||||
return result
|
||||
elif status is None:
|
||||
# This wait_for was cancelled externally, so cancel aw and re-raise.
|
||||
status = True
|
||||
runner_task.cancel()
|
||||
raise er
|
||||
else:
|
||||
# aw raised an exception, propagate it out to the caller.
|
||||
raise status
|
||||
|
||||
# The sleep finished before aw, so cancel aw and raise TimeoutError.
|
||||
status = True
|
||||
runner_task.cancel()
|
||||
await runner_task
|
||||
raise core.TimeoutError
|
||||
|
||||
|
||||
def wait_for_ms(aw, timeout):
|
||||
return wait_for(aw, timeout, core.sleep_ms)
|
||||
|
||||
|
||||
async def gather(*aws, return_exceptions=False):
|
||||
ts = [core._promote_to_task(aw) for aw in aws]
|
||||
for i in range(len(ts)):
|
||||
try:
|
||||
# TODO handle cancel of gather itself
|
||||
# if ts[i].coro:
|
||||
# iter(ts[i]).waiting.push_head(cur_task)
|
||||
# try:
|
||||
# yield
|
||||
# except CancelledError as er:
|
||||
# # cancel all waiting tasks
|
||||
# raise er
|
||||
ts[i] = await ts[i]
|
||||
except Exception as er:
|
||||
if return_exceptions:
|
||||
ts[i] = er
|
||||
else:
|
||||
raise er
|
||||
return ts
|
||||
53
tests/libs/uasyncio/lock.py
Normal file
53
tests/libs/uasyncio/lock.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# MicroPython uasyncio module
|
||||
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||
|
||||
from . import core
|
||||
|
||||
# Lock class for primitive mutex capability
|
||||
class Lock:
|
||||
def __init__(self):
|
||||
# The state can take the following values:
|
||||
# - 0: unlocked
|
||||
# - 1: locked
|
||||
# - <Task>: unlocked but this task has been scheduled to acquire the lock next
|
||||
self.state = 0
|
||||
# Queue of Tasks waiting to acquire this Lock
|
||||
self.waiting = core.TaskQueue()
|
||||
|
||||
def locked(self):
|
||||
return self.state == 1
|
||||
|
||||
def release(self):
|
||||
if self.state != 1:
|
||||
raise RuntimeError("Lock not acquired")
|
||||
if self.waiting.peek():
|
||||
# Task(s) waiting on lock, schedule next Task
|
||||
self.state = self.waiting.pop_head()
|
||||
core._task_queue.push_head(self.state)
|
||||
else:
|
||||
# No Task waiting so unlock
|
||||
self.state = 0
|
||||
|
||||
async def acquire(self):
|
||||
if self.state != 0:
|
||||
# Lock unavailable, put the calling Task on the waiting queue
|
||||
self.waiting.push_head(core.cur_task)
|
||||
# Set calling task's data to the lock's queue so it can be removed if needed
|
||||
core.cur_task.data = self.waiting
|
||||
try:
|
||||
yield
|
||||
except core.CancelledError as er:
|
||||
if self.state == core.cur_task:
|
||||
# Cancelled while pending on resume, schedule next waiting Task
|
||||
self.state = 1
|
||||
self.release()
|
||||
raise er
|
||||
# Lock available, set it as locked
|
||||
self.state = 1
|
||||
return True
|
||||
|
||||
async def __aenter__(self):
|
||||
return await self.acquire()
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return self.release()
|
||||
13
tests/libs/uasyncio/manifest.py
Normal file
13
tests/libs/uasyncio/manifest.py
Normal file
@@ -0,0 +1,13 @@
|
||||
# This list of frozen files doesn't include task.py because that's provided by the C module.
|
||||
freeze(
|
||||
"..",
|
||||
(
|
||||
"uasyncio/__init__.py",
|
||||
"uasyncio/core.py",
|
||||
"uasyncio/event.py",
|
||||
"uasyncio/funcs.py",
|
||||
"uasyncio/lock.py",
|
||||
"uasyncio/stream.py",
|
||||
),
|
||||
opt=3,
|
||||
)
|
||||
158
tests/libs/uasyncio/stream.py
Normal file
158
tests/libs/uasyncio/stream.py
Normal file
@@ -0,0 +1,158 @@
|
||||
# MicroPython uasyncio module
|
||||
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||
|
||||
from . import core
|
||||
|
||||
|
||||
class Stream:
|
||||
def __init__(self, s, e={}):
|
||||
self.s = s
|
||||
self.e = e
|
||||
self.out_buf = b""
|
||||
|
||||
def get_extra_info(self, v):
|
||||
return self.e[v]
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
await self.close()
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
async def wait_closed(self):
|
||||
# TODO yield?
|
||||
self.s.close()
|
||||
|
||||
async def read(self, n):
|
||||
yield core._io_queue.queue_read(self.s)
|
||||
return self.s.read(n)
|
||||
|
||||
async def readexactly(self, n):
|
||||
r = b""
|
||||
while n:
|
||||
yield core._io_queue.queue_read(self.s)
|
||||
r2 = self.s.read(n)
|
||||
if r2 is not None:
|
||||
if not len(r2):
|
||||
raise EOFError
|
||||
r += r2
|
||||
n -= len(r2)
|
||||
return r
|
||||
|
||||
async def readline(self):
|
||||
l = b""
|
||||
while True:
|
||||
yield core._io_queue.queue_read(self.s)
|
||||
l2 = self.s.readline() # may do multiple reads but won't block
|
||||
l += l2
|
||||
if not l2 or l[-1] == 10: # \n (check l in case l2 is str)
|
||||
return l
|
||||
|
||||
def write(self, buf):
|
||||
self.out_buf += buf
|
||||
|
||||
async def drain(self):
|
||||
mv = memoryview(self.out_buf)
|
||||
off = 0
|
||||
while off < len(mv):
|
||||
yield core._io_queue.queue_write(self.s)
|
||||
ret = self.s.write(mv[off:])
|
||||
if ret is not None:
|
||||
off += ret
|
||||
self.out_buf = b""
|
||||
|
||||
|
||||
# Stream can be used for both reading and writing to save code size
|
||||
StreamReader = Stream
|
||||
StreamWriter = Stream
|
||||
|
||||
|
||||
# Create a TCP stream connection to a remote host
|
||||
async def open_connection(host, port):
|
||||
from uerrno import EINPROGRESS
|
||||
import usocket as socket
|
||||
|
||||
ai = socket.getaddrinfo(host, port)[0] # TODO this is blocking!
|
||||
s = socket.socket()
|
||||
s.setblocking(False)
|
||||
ss = Stream(s)
|
||||
try:
|
||||
s.connect(ai[-1])
|
||||
except OSError as er:
|
||||
if er.errno != EINPROGRESS:
|
||||
raise er
|
||||
yield core._io_queue.queue_write(s)
|
||||
return ss, ss
|
||||
|
||||
|
||||
# Class representing a TCP stream server, can be closed and used in "async with"
|
||||
class Server:
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
self.close()
|
||||
await self.wait_closed()
|
||||
|
||||
def close(self):
|
||||
self.task.cancel()
|
||||
|
||||
async def wait_closed(self):
|
||||
await self.task
|
||||
|
||||
async def _serve(self, cb, host, port, backlog):
|
||||
import usocket as socket
|
||||
|
||||
ai = socket.getaddrinfo(host, port)[0] # TODO this is blocking!
|
||||
s = socket.socket()
|
||||
s.setblocking(False)
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.bind(ai[-1])
|
||||
s.listen(backlog)
|
||||
self.task = core.cur_task
|
||||
# Accept incoming connections
|
||||
while True:
|
||||
try:
|
||||
yield core._io_queue.queue_read(s)
|
||||
except core.CancelledError:
|
||||
# Shutdown server
|
||||
s.close()
|
||||
return
|
||||
try:
|
||||
s2, addr = s.accept()
|
||||
except:
|
||||
# Ignore a failed accept
|
||||
continue
|
||||
s2.setblocking(False)
|
||||
s2s = Stream(s2, {"peername": addr})
|
||||
core.create_task(cb(s2s, s2s))
|
||||
|
||||
|
||||
# Helper function to start a TCP stream server, running as a new task
|
||||
# TODO could use an accept-callback on socket read activity instead of creating a task
|
||||
async def start_server(cb, host, port, backlog=5):
|
||||
s = Server()
|
||||
core.create_task(s._serve(cb, host, port, backlog))
|
||||
return s
|
||||
|
||||
|
||||
################################################################################
|
||||
# Legacy uasyncio compatibility
|
||||
|
||||
|
||||
async def stream_awrite(self, buf, off=0, sz=-1):
|
||||
if off != 0 or sz != -1:
|
||||
buf = memoryview(buf)
|
||||
if sz == -1:
|
||||
sz = len(buf)
|
||||
buf = buf[off : off + sz]
|
||||
self.write(buf)
|
||||
await self.drain()
|
||||
|
||||
|
||||
Stream.aclose = Stream.wait_closed
|
||||
Stream.awrite = stream_awrite
|
||||
Stream.awritestr = stream_awrite # TODO explicitly convert to bytes?
|
||||
184
tests/libs/uasyncio/task.py
Normal file
184
tests/libs/uasyncio/task.py
Normal file
@@ -0,0 +1,184 @@
|
||||
# MicroPython uasyncio module
|
||||
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||
|
||||
# This file contains the core TaskQueue based on a pairing heap, and the core Task class.
|
||||
# They can optionally be replaced by C implementations.
|
||||
|
||||
from . import core
|
||||
|
||||
|
||||
# pairing-heap meld of 2 heaps; O(1)
|
||||
def ph_meld(h1, h2):
|
||||
if h1 is None:
|
||||
return h2
|
||||
if h2 is None:
|
||||
return h1
|
||||
lt = core.ticks_diff(h1.ph_key, h2.ph_key) < 0
|
||||
if lt:
|
||||
if h1.ph_child is None:
|
||||
h1.ph_child = h2
|
||||
else:
|
||||
h1.ph_child_last.ph_next = h2
|
||||
h1.ph_child_last = h2
|
||||
h2.ph_next = None
|
||||
h2.ph_rightmost_parent = h1
|
||||
return h1
|
||||
else:
|
||||
h1.ph_next = h2.ph_child
|
||||
h2.ph_child = h1
|
||||
if h1.ph_next is None:
|
||||
h2.ph_child_last = h1
|
||||
h1.ph_rightmost_parent = h2
|
||||
return h2
|
||||
|
||||
|
||||
# pairing-heap pairing operation; amortised O(log N)
|
||||
def ph_pairing(child):
|
||||
heap = None
|
||||
while child is not None:
|
||||
n1 = child
|
||||
child = child.ph_next
|
||||
n1.ph_next = None
|
||||
if child is not None:
|
||||
n2 = child
|
||||
child = child.ph_next
|
||||
n2.ph_next = None
|
||||
n1 = ph_meld(n1, n2)
|
||||
heap = ph_meld(heap, n1)
|
||||
return heap
|
||||
|
||||
|
||||
# pairing-heap delete of a node; stable, amortised O(log N)
|
||||
def ph_delete(heap, node):
|
||||
if node is heap:
|
||||
child = heap.ph_child
|
||||
node.ph_child = None
|
||||
return ph_pairing(child)
|
||||
# Find parent of node
|
||||
parent = node
|
||||
while parent.ph_next is not None:
|
||||
parent = parent.ph_next
|
||||
parent = parent.ph_rightmost_parent
|
||||
# Replace node with pairing of its children
|
||||
if node is parent.ph_child and node.ph_child is None:
|
||||
parent.ph_child = node.ph_next
|
||||
node.ph_next = None
|
||||
return heap
|
||||
elif node is parent.ph_child:
|
||||
child = node.ph_child
|
||||
next = node.ph_next
|
||||
node.ph_child = None
|
||||
node.ph_next = None
|
||||
node = ph_pairing(child)
|
||||
parent.ph_child = node
|
||||
else:
|
||||
n = parent.ph_child
|
||||
while node is not n.ph_next:
|
||||
n = n.ph_next
|
||||
child = node.ph_child
|
||||
next = node.ph_next
|
||||
node.ph_child = None
|
||||
node.ph_next = None
|
||||
node = ph_pairing(child)
|
||||
if node is None:
|
||||
node = n
|
||||
else:
|
||||
n.ph_next = node
|
||||
node.ph_next = next
|
||||
if next is None:
|
||||
node.ph_rightmost_parent = parent
|
||||
parent.ph_child_last = node
|
||||
return heap
|
||||
|
||||
|
||||
# TaskQueue class based on the above pairing-heap functions.
|
||||
class TaskQueue:
|
||||
def __init__(self):
|
||||
self.heap = None
|
||||
|
||||
def peek(self):
|
||||
return self.heap
|
||||
|
||||
def push_sorted(self, v, key):
|
||||
v.data = None
|
||||
v.ph_key = key
|
||||
v.ph_child = None
|
||||
v.ph_next = None
|
||||
self.heap = ph_meld(v, self.heap)
|
||||
|
||||
def push_head(self, v):
|
||||
self.push_sorted(v, core.ticks())
|
||||
|
||||
def pop_head(self):
|
||||
v = self.heap
|
||||
self.heap = ph_pairing(self.heap.ph_child)
|
||||
return v
|
||||
|
||||
def remove(self, v):
|
||||
self.heap = ph_delete(self.heap, v)
|
||||
|
||||
|
||||
# Task class representing a coroutine, can be waited on and cancelled.
|
||||
class Task:
|
||||
def __init__(self, coro, globals=None):
|
||||
self.coro = coro # Coroutine of this Task
|
||||
self.data = None # General data for queue it is waiting on
|
||||
self.ph_key = 0 # Pairing heap
|
||||
self.ph_child = None # Paring heap
|
||||
self.ph_child_last = None # Paring heap
|
||||
self.ph_next = None # Paring heap
|
||||
self.ph_rightmost_parent = None # Paring heap
|
||||
|
||||
def __iter__(self):
|
||||
if self.coro is self:
|
||||
# Signal that the completed-task has been await'ed on.
|
||||
self.waiting = None
|
||||
elif not hasattr(self, "waiting"):
|
||||
# Lazily allocated head of linked list of Tasks waiting on completion of this task.
|
||||
self.waiting = TaskQueue()
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
if self.coro is self:
|
||||
# Task finished, raise return value to caller so it can continue.
|
||||
raise self.data
|
||||
else:
|
||||
# Put calling task on waiting queue.
|
||||
self.waiting.push_head(core.cur_task)
|
||||
# Set calling task's data to this task that it waits on, to double-link it.
|
||||
core.cur_task.data = self
|
||||
|
||||
def done(self):
|
||||
return self.coro is self
|
||||
|
||||
def cancel(self):
|
||||
# Check if task is already finished.
|
||||
if self.coro is self:
|
||||
return False
|
||||
# Can't cancel self (not supported yet).
|
||||
if self is core.cur_task:
|
||||
raise RuntimeError("can't cancel self")
|
||||
# If Task waits on another task then forward the cancel to the one it's waiting on.
|
||||
while isinstance(self.data, Task):
|
||||
self = self.data
|
||||
# Reschedule Task as a cancelled task.
|
||||
if hasattr(self.data, "remove"):
|
||||
# Not on the main running queue, remove the task from the queue it's on.
|
||||
self.data.remove(self)
|
||||
core._task_queue.push_head(self)
|
||||
elif core.ticks_diff(self.ph_key, core.ticks()) > 0:
|
||||
# On the main running queue but scheduled in the future, so bring it forward to now.
|
||||
core._task_queue.remove(self)
|
||||
core._task_queue.push_head(self)
|
||||
self.data = core.CancelledError
|
||||
return True
|
||||
|
||||
def throw(self, value):
|
||||
# This task raised an exception which was uncaught; handle that now.
|
||||
# Set the data because it was cleared by the main scheduling loop.
|
||||
self.data = value
|
||||
if not hasattr(self, "waiting"):
|
||||
# Nothing await'ed on the task so call the exception handler.
|
||||
core._exc_context["exception"] = value
|
||||
core._exc_context["future"] = self
|
||||
core.Loop.call_exception_handler(core._exc_context)
|
||||
224
tests/libs/unittest.py
Normal file
224
tests/libs/unittest.py
Normal file
@@ -0,0 +1,224 @@
|
||||
import sys
|
||||
|
||||
|
||||
class SkipTest(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class AssertRaisesContext:
|
||||
|
||||
def __init__(self, exc):
|
||||
self.expected = exc
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, tb):
|
||||
if exc_type is None:
|
||||
assert False, "%r not raised" % self.expected
|
||||
if issubclass(exc_type, self.expected):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class TestCase:
|
||||
|
||||
def fail(self, msg=''):
|
||||
assert False, msg
|
||||
|
||||
def assertEqual(self, x, y, msg=''):
|
||||
if not msg:
|
||||
msg = "%r vs (expected) %r" % (x, y)
|
||||
assert x == y, msg
|
||||
|
||||
def assertNotEqual(self, x, y, msg=''):
|
||||
if not msg:
|
||||
msg = "%r not expected to be equal %r" % (x, y)
|
||||
assert x != y, msg
|
||||
|
||||
def assertAlmostEqual(self, x, y, places=None, msg='', delta=None):
|
||||
if x == y:
|
||||
return
|
||||
if delta is not None and places is not None:
|
||||
raise TypeError("specify delta or places not both")
|
||||
|
||||
if delta is not None:
|
||||
if abs(x - y) <= delta:
|
||||
return
|
||||
if not msg:
|
||||
msg = '%r != %r within %r delta' % (x, y, delta)
|
||||
else:
|
||||
if places is None:
|
||||
places = 7
|
||||
if round(abs(y-x), places) == 0:
|
||||
return
|
||||
if not msg:
|
||||
msg = '%r != %r within %r places' % (x, y, places)
|
||||
|
||||
assert False, msg
|
||||
|
||||
def assertNotAlmostEqual(self, x, y, places=None, msg='', delta=None):
|
||||
if delta is not None and places is not None:
|
||||
raise TypeError("specify delta or places not both")
|
||||
|
||||
if delta is not None:
|
||||
if not (x == y) and abs(x - y) > delta:
|
||||
return
|
||||
if not msg:
|
||||
msg = '%r == %r within %r delta' % (x, y, delta)
|
||||
else:
|
||||
if places is None:
|
||||
places = 7
|
||||
if not (x == y) and round(abs(y-x), places) != 0:
|
||||
return
|
||||
if not msg:
|
||||
msg = '%r == %r within %r places' % (x, y, places)
|
||||
|
||||
assert False, msg
|
||||
|
||||
def assertIs(self, x, y, msg=''):
|
||||
if not msg:
|
||||
msg = "%r is not %r" % (x, y)
|
||||
assert x is y, msg
|
||||
|
||||
def assertIsNot(self, x, y, msg=''):
|
||||
if not msg:
|
||||
msg = "%r is %r" % (x, y)
|
||||
assert x is not y, msg
|
||||
|
||||
def assertIsNone(self, x, msg=''):
|
||||
if not msg:
|
||||
msg = "%r is not None" % x
|
||||
assert x is None, msg
|
||||
|
||||
def assertIsNotNone(self, x, msg=''):
|
||||
if not msg:
|
||||
msg = "%r is None" % x
|
||||
assert x is not None, msg
|
||||
|
||||
def assertTrue(self, x, msg=''):
|
||||
if not msg:
|
||||
msg = "Expected %r to be True" % x
|
||||
assert x, msg
|
||||
|
||||
def assertFalse(self, x, msg=''):
|
||||
if not msg:
|
||||
msg = "Expected %r to be False" % x
|
||||
assert not x, msg
|
||||
|
||||
def assertIn(self, x, y, msg=''):
|
||||
if not msg:
|
||||
msg = "Expected %r to be in %r" % (x, y)
|
||||
assert x in y, msg
|
||||
|
||||
def assertIsInstance(self, x, y, msg=''):
|
||||
assert isinstance(x, y), msg
|
||||
|
||||
def assertRaises(self, exc, func=None, *args, **kwargs):
|
||||
if func is None:
|
||||
return AssertRaisesContext(exc)
|
||||
|
||||
try:
|
||||
func(*args, **kwargs)
|
||||
assert False, "%r not raised" % exc
|
||||
except Exception as e:
|
||||
if isinstance(e, exc):
|
||||
return
|
||||
raise
|
||||
|
||||
|
||||
|
||||
def skip(msg):
|
||||
def _decor(fun):
|
||||
# We just replace original fun with _inner
|
||||
def _inner(self):
|
||||
raise SkipTest(msg)
|
||||
return _inner
|
||||
return _decor
|
||||
|
||||
def skipIf(cond, msg):
|
||||
if not cond:
|
||||
return lambda x: x
|
||||
return skip(msg)
|
||||
|
||||
def skipUnless(cond, msg):
|
||||
if cond:
|
||||
return lambda x: x
|
||||
return skip(msg)
|
||||
|
||||
|
||||
class TestSuite:
|
||||
def __init__(self):
|
||||
self.tests = []
|
||||
def addTest(self, cls):
|
||||
self.tests.append(cls)
|
||||
|
||||
class TestRunner:
|
||||
def run(self, suite):
|
||||
res = TestResult()
|
||||
for c in suite.tests:
|
||||
run_class(c, res)
|
||||
|
||||
print("Ran %d tests\n" % res.testsRun)
|
||||
if res.failuresNum > 0 or res.errorsNum > 0:
|
||||
print("FAILED (failures=%d, errors=%d)" % (res.failuresNum, res.errorsNum))
|
||||
else:
|
||||
msg = "OK"
|
||||
if res.skippedNum > 0:
|
||||
msg += " (%d skipped)" % res.skippedNum
|
||||
print(msg)
|
||||
|
||||
return res
|
||||
|
||||
class TestResult:
|
||||
def __init__(self):
|
||||
self.errorsNum = 0
|
||||
self.failuresNum = 0
|
||||
self.skippedNum = 0
|
||||
self.testsRun = 0
|
||||
|
||||
def wasSuccessful(self):
|
||||
return self.errorsNum == 0 and self.failuresNum == 0
|
||||
|
||||
# TODO: Uncompliant
|
||||
def run_class(c, test_result):
|
||||
o = c()
|
||||
set_up = getattr(o, "setUp", lambda: None)
|
||||
tear_down = getattr(o, "tearDown", lambda: None)
|
||||
for name in dir(o):
|
||||
if name.startswith("test"):
|
||||
print("%s (%s) ..." % (name, c.__qualname__), end="")
|
||||
m = getattr(o, name)
|
||||
set_up()
|
||||
try:
|
||||
test_result.testsRun += 1
|
||||
m()
|
||||
print(" ok")
|
||||
except SkipTest as e:
|
||||
print(" skipped:", e.args[0])
|
||||
test_result.skippedNum += 1
|
||||
except:
|
||||
print(" FAIL")
|
||||
test_result.failuresNum += 1
|
||||
# Uncomment to investigate failure in detail
|
||||
#raise
|
||||
continue
|
||||
finally:
|
||||
tear_down()
|
||||
|
||||
|
||||
def main(module="__main__"):
|
||||
def test_cases(m):
|
||||
for tn in dir(m):
|
||||
c = getattr(m, tn)
|
||||
if isinstance(c, object) and isinstance(c, type) and issubclass(c, TestCase):
|
||||
yield c
|
||||
|
||||
m = __import__(module)
|
||||
suite = TestSuite()
|
||||
for c in test_cases(m):
|
||||
suite.addTest(c)
|
||||
runner = TestRunner()
|
||||
result = runner.run(suite)
|
||||
# Terminate with non zero return code in case of failures
|
||||
sys.exit(result.failuresNum > 0)
|
||||
0
tests/microdot/__init__.py
Normal file
0
tests/microdot/__init__.py
Normal file
288
tests/microdot/test_microdot.py
Normal file
288
tests/microdot/test_microdot.py
Normal file
@@ -0,0 +1,288 @@
|
||||
import sys
|
||||
import unittest
|
||||
from microdot import Microdot, Response
|
||||
from tests import mock_socket
|
||||
|
||||
|
||||
def mock_create_thread(f, *args, **kwargs):
|
||||
f(*args, **kwargs)
|
||||
|
||||
|
||||
class TestMicrodot(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# mock socket module
|
||||
self.original_socket = sys.modules['microdot'].socket
|
||||
self.original_create_thread = sys.modules['microdot'].create_thread
|
||||
sys.modules['microdot'].socket = mock_socket
|
||||
sys.modules['microdot'].create_thread = mock_create_thread
|
||||
|
||||
def tearDown(self):
|
||||
# restore original socket module
|
||||
sys.modules['microdot'].socket = self.original_socket
|
||||
sys.modules['microdot'].create_thread = self.original_create_thread
|
||||
|
||||
def _add_shutdown(self, app):
|
||||
@app.route('/shutdown')
|
||||
def shutdown(req):
|
||||
app.shutdown()
|
||||
return ''
|
||||
|
||||
mock_socket.add_request('GET', '/shutdown')
|
||||
|
||||
def test_get_request(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nfoo'))
|
||||
|
||||
def test_post_request(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
@app.route('/', methods=['POST'])
|
||||
def index_post(req):
|
||||
return Response('bar')
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('POST', '/')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nbar'))
|
||||
|
||||
def test_empty_request(self):
|
||||
app = Microdot()
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.FakeStream(b'\n')
|
||||
mock_socket._requests.append(fd)
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 400 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 11\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nBad request'))
|
||||
|
||||
def test_method_decorators(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.get('/get')
|
||||
def get(req):
|
||||
return 'GET'
|
||||
|
||||
@app.post('/post')
|
||||
def post(req):
|
||||
return 'POST'
|
||||
|
||||
@app.put('/put')
|
||||
def put(req):
|
||||
return 'PUT'
|
||||
|
||||
@app.patch('/patch')
|
||||
def patch(req):
|
||||
return 'PATCH'
|
||||
|
||||
@app.delete('/delete')
|
||||
def delete(req):
|
||||
return 'DELETE'
|
||||
|
||||
methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']
|
||||
mock_socket.clear_requests()
|
||||
fds = [mock_socket.add_request(method, '/' + method.lower())
|
||||
for method in methods]
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
for fd, method in zip(fds, methods):
|
||||
self.assertTrue(fd.response.endswith(
|
||||
b'\r\n\r\n' + method.encode()))
|
||||
|
||||
def test_before_after_request(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.before_request
|
||||
def before_request(req):
|
||||
if req.path == '/bar':
|
||||
return 'bar', 202
|
||||
req.g.message = 'baz'
|
||||
|
||||
@app.after_request
|
||||
def after_request_one(req, res):
|
||||
res.headers['X-One'] = '1'
|
||||
|
||||
@app.after_request
|
||||
def after_request_two(req, res):
|
||||
res.set_cookie('foo', 'bar')
|
||||
return res
|
||||
|
||||
@app.route('/bar')
|
||||
def bar(req):
|
||||
return 'foo'
|
||||
|
||||
@app.route('/baz')
|
||||
def baz(req):
|
||||
return req.g.message
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/bar')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 202 N/A\r\n'))
|
||||
self.assertIn(b'X-One: 1\r\n', fd.response)
|
||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nbar'))
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/baz')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'X-One: 1\r\n', fd.response)
|
||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nbaz'))
|
||||
|
||||
def test_404(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/foo')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 404 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 9\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nNot found'))
|
||||
|
||||
def test_404_handler(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
@app.errorhandler(404)
|
||||
def handle_404(req):
|
||||
return '404'
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/foo')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n404'))
|
||||
|
||||
def test_413(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/foo', body='x' * 17000)
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 413 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 17\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nPayload too large'))
|
||||
|
||||
def test_413_handler(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
@app.errorhandler(413)
|
||||
def handle_413(req):
|
||||
return '413', 400
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/foo', body='x' * 17000)
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 400 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n413'))
|
||||
|
||||
def test_500(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 1 / 0
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 500 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 21\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nInternal server error'))
|
||||
|
||||
def test_500_handler(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 1 / 0
|
||||
|
||||
@app.errorhandler(500)
|
||||
def handle_500(req):
|
||||
return '501', 501
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n501'))
|
||||
|
||||
def test_exception_handler(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 1 / 0
|
||||
|
||||
@app.errorhandler(ZeroDivisionError)
|
||||
def handle_div_zero(req, exc):
|
||||
return '501', 501
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n501'))
|
||||
31
tests/microdot/test_multidict.py
Normal file
31
tests/microdot/test_multidict.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import unittest
|
||||
from microdot import MultiDict
|
||||
|
||||
|
||||
class TestMultiDict(unittest.TestCase):
|
||||
def test_multidict(self):
|
||||
d = MultiDict()
|
||||
|
||||
assert dict(d) == {}
|
||||
assert d.get('zero') is None
|
||||
assert d.get('zero', default=0) == 0
|
||||
assert d.getlist('zero') == []
|
||||
assert d.getlist('zero', type=int) == []
|
||||
|
||||
d['one'] = 1
|
||||
assert d['one'] == 1
|
||||
assert d.get('one') == 1
|
||||
assert d.get('one', default=2) == 1
|
||||
assert d.get('one', type=int) == 1
|
||||
assert d.get('one', type=str) == '1'
|
||||
|
||||
d['two'] = 1
|
||||
d['two'] = 2
|
||||
assert d['two'] == 1
|
||||
assert d.get('two') == 1
|
||||
assert d.get('two', default=2) == 1
|
||||
assert d.get('two', type=int) == 1
|
||||
assert d.get('two', type=str) == '1'
|
||||
assert d.getlist('two') == [1, 2]
|
||||
assert d.getlist('two', type=int) == [1, 2]
|
||||
assert d.getlist('two', type=str) == ['1', '2']
|
||||
104
tests/microdot/test_request.py
Normal file
104
tests/microdot/test_request.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import unittest
|
||||
from microdot import Request, MultiDict
|
||||
from tests.mock_socket import get_request_fd
|
||||
|
||||
|
||||
class TestRequest(unittest.TestCase):
|
||||
def test_create_request(self):
|
||||
fd = get_request_fd('GET', '/foo')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
self.assertEqual(req.app, 'app')
|
||||
self.assertEqual(req.client_addr, 'addr')
|
||||
self.assertEqual(req.method, 'GET')
|
||||
self.assertEqual(req.path, '/foo')
|
||||
self.assertEqual(req.http_version, '1.0')
|
||||
self.assertIsNone(req.query_string)
|
||||
self.assertEqual(req.args, {})
|
||||
self.assertEqual(req.headers, {'Host': 'example.com:1234'})
|
||||
self.assertEqual(req.cookies, {})
|
||||
self.assertEqual(req.content_length, 0)
|
||||
self.assertEqual(req.content_type, None)
|
||||
self.assertEqual(req.body, b'')
|
||||
self.assertEqual(req.json, None)
|
||||
self.assertEqual(req.form, None)
|
||||
|
||||
def test_headers(self):
|
||||
fd = get_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/json',
|
||||
'Cookie': 'foo=bar;abc=def',
|
||||
'Content-Length': '3'}, body='aaa')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
self.assertEqual(req.headers, {
|
||||
'Host': 'example.com:1234',
|
||||
'Content-Type': 'application/json',
|
||||
'Cookie': 'foo=bar;abc=def',
|
||||
'Content-Length': '3'})
|
||||
self.assertEqual(req.content_type, 'application/json')
|
||||
self.assertEqual(req.cookies, {'foo': 'bar', 'abc': 'def'})
|
||||
self.assertEqual(req.content_length, 3)
|
||||
self.assertEqual(req.body, b'aaa')
|
||||
|
||||
def test_args(self):
|
||||
fd = get_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%')
|
||||
self.assertEqual(req.args, MultiDict(
|
||||
{'foo': 'bar', 'abc': 'def', 'x': '/%%'}))
|
||||
|
||||
def test_json(self):
|
||||
fd = get_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/json'}, body='{"foo":"bar"}')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
json = req.json
|
||||
self.assertEqual(json, {'foo': 'bar'})
|
||||
self.assertTrue(req.json is json)
|
||||
|
||||
fd = get_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/json'}, body='[1, "2"]')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
self.assertEqual(req.json, [1, '2'])
|
||||
|
||||
fd = get_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/xml'}, body='[1, "2"]')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
self.assertIsNone(req.json)
|
||||
|
||||
def test_form(self):
|
||||
fd = get_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body='foo=bar&abc=def&x=%2f%%')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
form = req.form
|
||||
self.assertEqual(form, MultiDict(
|
||||
{'foo': 'bar', 'abc': 'def', 'x': '/%%'}))
|
||||
self.assertTrue(req.form is form)
|
||||
|
||||
fd = get_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/json'},
|
||||
body='foo=bar&abc=def&x=%2f%%')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
self.assertIsNone(req.form)
|
||||
|
||||
def test_large_line(self):
|
||||
saved_max_readline = Request.max_readline
|
||||
Request.max_readline = 16
|
||||
|
||||
fd = get_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body='foo=bar&abc=def&x=y')
|
||||
with self.assertRaises(ValueError):
|
||||
Request.create('app', fd, 'addr')
|
||||
|
||||
Request.max_readline = saved_max_readline
|
||||
|
||||
def test_large_payload(self):
|
||||
saved_max_content_length = Request.max_content_length
|
||||
Request.max_content_length = 16
|
||||
|
||||
fd = get_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body='foo=bar&abc=def&x=y')
|
||||
req = Request.create('app', fd, 'addr')
|
||||
self.assertEqual(req.body, b'')
|
||||
|
||||
Request.max_content_length = saved_max_content_length
|
||||
216
tests/microdot/test_response.py
Normal file
216
tests/microdot/test_response.py
Normal file
@@ -0,0 +1,216 @@
|
||||
from datetime import datetime
|
||||
|
||||
try:
|
||||
import uio as io
|
||||
except ImportError:
|
||||
import io
|
||||
|
||||
import unittest
|
||||
from microdot import Response
|
||||
|
||||
|
||||
class TestResponse(unittest.TestCase):
|
||||
def test_create_from_string(self):
|
||||
res = Response('foo')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.body, b'foo')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||
self.assertIn(b'Content-Length: 3\r\n', response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||
self.assertTrue(response.endswith(b'\r\n\r\nfoo'))
|
||||
|
||||
def test_create_from_string_with_content_length(self):
|
||||
res = Response('foo', headers={'Content-Length': '2'})
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'Content-Length': '2'})
|
||||
self.assertEqual(res.body, b'foo')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||
self.assertIn(b'Content-Length: 2\r\n', response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||
self.assertTrue(response.endswith(b'\r\n\r\nfoo'))
|
||||
|
||||
def test_create_from_bytes(self):
|
||||
res = Response(b'foo')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.body, b'foo')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||
self.assertIn(b'Content-Length: 3\r\n', response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||
self.assertTrue(response.endswith(b'\r\n\r\nfoo'))
|
||||
|
||||
def test_create_empty(self):
|
||||
res = Response(headers={'X-Foo': 'Bar'})
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'X-Foo': 'Bar'})
|
||||
self.assertEqual(res.body, b'')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||
self.assertIn(b'X-Foo: Bar\r\n', response)
|
||||
self.assertIn(b'Content-Length: 0\r\n', response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||
self.assertTrue(response.endswith(b'\r\n\r\n'))
|
||||
|
||||
def test_create_json(self):
|
||||
res = Response({'foo': 'bar'})
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'Content-Type': 'application/json'})
|
||||
self.assertEqual(res.body, b'{"foo": "bar"}')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||
self.assertIn(b'Content-Length: 14\r\n', response)
|
||||
self.assertIn(b'Content-Type: application/json\r\n', response)
|
||||
self.assertTrue(response.endswith(b'\r\n\r\n{"foo": "bar"}'))
|
||||
|
||||
res = Response([1, '2'])
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'Content-Type': 'application/json'})
|
||||
self.assertEqual(res.body, b'[1, "2"]')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||
self.assertIn(b'Content-Length: 8\r\n', response)
|
||||
self.assertIn(b'Content-Type: application/json\r\n', response)
|
||||
self.assertTrue(response.endswith(b'\r\n\r\n[1, "2"]'))
|
||||
|
||||
def test_create_from_other(self):
|
||||
res = Response(123)
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.body, 123)
|
||||
|
||||
def test_create_with_status_code(self):
|
||||
res = Response('not found', 404)
|
||||
self.assertEqual(res.status_code, 404)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.body, b'not found')
|
||||
|
||||
def test_create_with_headers(self):
|
||||
res = Response('foo', headers={'X-Test': 'Foo'})
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'X-Test': 'Foo'})
|
||||
self.assertEqual(res.body, b'foo')
|
||||
|
||||
def test_create_with_status_code_and_headers(self):
|
||||
res = Response('foo', 202, {'X-Test': 'Foo'})
|
||||
self.assertEqual(res.status_code, 202)
|
||||
self.assertEqual(res.headers, {'X-Test': 'Foo'})
|
||||
self.assertEqual(res.body, b'foo')
|
||||
|
||||
def test_create_with_reason(self):
|
||||
res = Response('foo', reason='ALL GOOD!')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.reason, 'ALL GOOD!')
|
||||
self.assertEqual(res.body, b'foo')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 200 ALL GOOD!\r\n', response)
|
||||
|
||||
def test_create_with_status_and_reason(self):
|
||||
res = Response('not found', 404, reason='NOT FOUND')
|
||||
self.assertEqual(res.status_code, 404)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.reason, 'NOT FOUND')
|
||||
self.assertEqual(res.body, b'not found')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 404 NOT FOUND\r\n', response)
|
||||
|
||||
def test_cookies(self):
|
||||
res = Response('ok')
|
||||
res.set_cookie('foo1', 'bar1')
|
||||
res.set_cookie('foo2', 'bar2', path='/')
|
||||
res.set_cookie('foo3', 'bar3', domain='example.com:1234')
|
||||
res.set_cookie('foo4', 'bar4',
|
||||
expires=datetime(2019, 11, 5, 2, 23, 54))
|
||||
res.set_cookie('foo5', 'bar5', max_age=123)
|
||||
res.set_cookie('foo6', 'bar6', secure=True, http_only=True)
|
||||
res.set_cookie('foo7', 'bar7', path='/foo', domain='example.com:1234',
|
||||
expires=datetime(2019, 11, 5, 2, 23, 54), max_age=123,
|
||||
secure=True, http_only=True)
|
||||
self.assertEqual(res.headers, {'Set-Cookie': [
|
||||
'foo1=bar1',
|
||||
'foo2=bar2; Path=/',
|
||||
'foo3=bar3; Domain=example.com:1234',
|
||||
'foo4=bar4; Expires=Tue, 05 Nov 2019 02:23:54 GMT',
|
||||
'foo5=bar5; Max-Age=123',
|
||||
'foo6=bar6; Secure; HttpOnly',
|
||||
'foo7=bar7; Path=/foo; Domain=example.com:1234; '
|
||||
'Expires=Tue, 05 Nov 2019 02:23:54 GMT; Max-Age=123; Secure; '
|
||||
'HttpOnly'
|
||||
]})
|
||||
|
||||
def test_redirect(self):
|
||||
res = Response.redirect('/foo')
|
||||
self.assertEqual(res.status_code, 302)
|
||||
self.assertEqual(res.headers['Location'], '/foo')
|
||||
|
||||
res = Response.redirect('/foo', status_code=301)
|
||||
self.assertEqual(res.status_code, 301)
|
||||
self.assertEqual(res.headers['Location'], '/foo')
|
||||
|
||||
def test_send_file(self):
|
||||
files = [
|
||||
('test.txt', 'text/plain'),
|
||||
('test.gif', 'image/gif'),
|
||||
('test.jpg', 'image/jpeg'),
|
||||
('test.png', 'image/png'),
|
||||
('test.html', 'text/html'),
|
||||
('test.css', 'text/css'),
|
||||
('test.js', 'application/javascript'),
|
||||
('test.json', 'application/json'),
|
||||
('test.bin', 'application/octet-stream'),
|
||||
]
|
||||
for file, content_type in files:
|
||||
res = Response.send_file('tests/files/' + file)
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers['Content-Type'], content_type)
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertEqual(response, (
|
||||
b'HTTP/1.0 200 OK\r\nContent-Type: ' + content_type.encode()
|
||||
+ b'\r\n\r\nfoo\n'))
|
||||
res = Response.send_file('tests/files/test.txt',
|
||||
content_type='text/html')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertEqual(
|
||||
response,
|
||||
b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
|
||||
|
||||
def test_send_file_small_buffer(self):
|
||||
original_buffer_size = Response.send_file_buffer_size
|
||||
Response.send_file_buffer_size = 2
|
||||
res = Response.send_file('tests/files/test.txt',
|
||||
content_type='text/html')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertEqual(
|
||||
response,
|
||||
b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
|
||||
Response.send_file_buffer_size = original_buffer_size
|
||||
97
tests/microdot/test_url_pattern.py
Normal file
97
tests/microdot/test_url_pattern.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import unittest
|
||||
from microdot import URLPattern
|
||||
|
||||
|
||||
class TestURLPattern(unittest.TestCase):
|
||||
def test_static(self):
|
||||
p = URLPattern('/')
|
||||
self.assertEqual(p.match('/'), {})
|
||||
self.assertIsNone(p.match('/foo'))
|
||||
|
||||
p = URLPattern('/foo/bar')
|
||||
self.assertEqual(p.match('/foo/bar'), {})
|
||||
self.assertIsNone(p.match('/foo'))
|
||||
self.assertIsNone(p.match('/foo/bar/'))
|
||||
|
||||
p = URLPattern('/foo//bar/baz/')
|
||||
self.assertEqual(p.match('/foo//bar/baz/'), {})
|
||||
self.assertIsNone(p.match('/foo/bar/baz/'))
|
||||
self.assertIsNone(p.match('/foo'))
|
||||
self.assertIsNone(p.match('/foo/bar/baz'))
|
||||
|
||||
def test_string_argument(self):
|
||||
p = URLPattern('/<arg>')
|
||||
self.assertEqual(p.match('/foo'), {'arg': 'foo'})
|
||||
self.assertIsNone(p.match('/'))
|
||||
self.assertIsNone(p.match('/foo/'))
|
||||
|
||||
p = URLPattern('/<arg>/')
|
||||
self.assertEqual(p.match('/foo/'), {'arg': 'foo'})
|
||||
self.assertIsNone(p.match('/'))
|
||||
self.assertIsNone(p.match('/foo'))
|
||||
|
||||
p = URLPattern('/<string:arg>')
|
||||
self.assertEqual(p.match('/foo'), {'arg': 'foo'})
|
||||
self.assertIsNone(p.match('/'))
|
||||
self.assertIsNone(p.match('/foo/'))
|
||||
|
||||
p = URLPattern('/<string:arg>/')
|
||||
self.assertEqual(p.match('/foo/'), {'arg': 'foo'})
|
||||
self.assertIsNone(p.match('/'))
|
||||
self.assertIsNone(p.match('/foo'))
|
||||
|
||||
p = URLPattern('/foo/<arg1>/bar/<arg2>')
|
||||
self.assertEqual(p.match('/foo/one/bar/two'),
|
||||
{'arg1': 'one', 'arg2': 'two'})
|
||||
self.assertIsNone(p.match('/'))
|
||||
self.assertIsNone(p.match('/foo/'))
|
||||
|
||||
def test_int_argument(self):
|
||||
p = URLPattern('/users/<int:id>')
|
||||
self.assertEqual(p.match('/users/123'), {'id': 123})
|
||||
self.assertIsNone(p.match('/users/'))
|
||||
self.assertIsNone(p.match('/users/abc'))
|
||||
self.assertIsNone(p.match('/users/123abc'))
|
||||
self.assertIsNone(p.match('/users/123/abc'))
|
||||
|
||||
p = URLPattern('/users/<int:id>/<int:id2>/')
|
||||
self.assertEqual(p.match('/users/123/456/'), {'id': 123, 'id2': 456})
|
||||
self.assertIsNone(p.match('/users/'))
|
||||
self.assertIsNone(p.match('/users/123/456'))
|
||||
self.assertIsNone(p.match('/users/123/abc/'))
|
||||
self.assertIsNone(p.match('/users/123/456/abc'))
|
||||
|
||||
def test_path_argument(self):
|
||||
p = URLPattern('/users/<path:path>')
|
||||
self.assertEqual(p.match('/users/123'), {'path': '123'})
|
||||
self.assertEqual(p.match('/users/123/'), {'path': '123/'})
|
||||
self.assertEqual(p.match('/users/abc/def'), {'path': 'abc/def'})
|
||||
self.assertIsNone(p.match('/users/'))
|
||||
|
||||
p = URLPattern('/users/<path:path>/foo')
|
||||
self.assertEqual(p.match('/users/123/foo'), {'path': '123'})
|
||||
self.assertEqual(p.match('/users/foo/foo'), {'path': 'foo'})
|
||||
self.assertEqual(p.match('/users/abc/def/foo'), {'path': 'abc/def'})
|
||||
self.assertIsNone(p.match('/users/'))
|
||||
self.assertIsNone(p.match('/users/foo'))
|
||||
self.assertIsNone(p.match('/users/foo/'))
|
||||
|
||||
def test_regex_argument(self):
|
||||
p = URLPattern('/users/<re:[a-c]+:id>')
|
||||
self.assertEqual(p.match('/users/ab'), {'id': 'ab'})
|
||||
self.assertEqual(p.match('/users/bca'), {'id': 'bca'})
|
||||
self.assertIsNone(p.match('/users/abcd'))
|
||||
|
||||
def test_many_arguments(self):
|
||||
p = URLPattern('/foo/<path:path>/<int:id>/bar/<name>')
|
||||
self.assertEqual(p.match('/foo/abc/def/123/bar/test'),
|
||||
{'path': 'abc/def', 'id': 123, 'name': 'test'})
|
||||
self.assertIsNone(p.match('/foo/123/bar/test'))
|
||||
self.assertIsNone(p.match('/foo/abc/def/ghi/bar/test'))
|
||||
self.assertIsNone(p.match('/foo/abc/def/123/bar'))
|
||||
self.assertIsNone(p.match('/foo/abc/def/123/bar/'))
|
||||
self.assertIsNone(p.match('/foo/abc/def/123/test'))
|
||||
|
||||
def test_invalid_url_patterns(self):
|
||||
self.assertRaises(ValueError, URLPattern, '/users/<foo/bar')
|
||||
self.assertRaises(ValueError, URLPattern, '/users/<badtype:id>')
|
||||
0
tests/microdot_asyncio/__init__.py
Normal file
0
tests/microdot_asyncio/__init__.py
Normal file
266
tests/microdot_asyncio/test_microdot_asyncio.py
Normal file
266
tests/microdot_asyncio/test_microdot_asyncio.py
Normal file
@@ -0,0 +1,266 @@
|
||||
import sys
|
||||
import unittest
|
||||
from microdot_asyncio import Microdot, Response
|
||||
from tests import mock_asyncio, mock_socket
|
||||
|
||||
|
||||
class TestMicrodotAsync(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# mock socket module
|
||||
self.original_asyncio = sys.modules['microdot_asyncio'].asyncio
|
||||
sys.modules['microdot_asyncio'].asyncio = mock_asyncio
|
||||
|
||||
def tearDown(self):
|
||||
# restore original socket module
|
||||
sys.modules['microdot_asyncio'].asyncio = self.original_asyncio
|
||||
|
||||
def _add_shutdown(self, app):
|
||||
@app.route('/shutdown')
|
||||
def shutdown(req):
|
||||
app.shutdown()
|
||||
return ''
|
||||
|
||||
mock_socket.add_request('GET', '/shutdown')
|
||||
|
||||
def test_get_request(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
@app.route('/async')
|
||||
async def index2(req):
|
||||
return 'foo-async'
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/')
|
||||
fd2 = mock_socket.add_request('GET', '/async')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nfoo'))
|
||||
self.assertTrue(fd2.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'Content-Length: 9\r\n', fd2.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd2.response)
|
||||
self.assertTrue(fd2.response.endswith(b'\r\n\r\nfoo-async'))
|
||||
|
||||
def test_post_request(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
@app.route('/', methods=['POST'])
|
||||
def index_post(req):
|
||||
return Response('bar')
|
||||
|
||||
@app.route('/async', methods=['POST'])
|
||||
async def index_post2(req):
|
||||
return Response('bar-async')
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('POST', '/')
|
||||
fd2 = mock_socket.add_request('POST', '/async')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nbar'))
|
||||
self.assertTrue(fd2.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'Content-Length: 9\r\n', fd2.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd2.response)
|
||||
self.assertTrue(fd2.response.endswith(b'\r\n\r\nbar-async'))
|
||||
|
||||
def test_empty_request(self):
|
||||
app = Microdot()
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.FakeStream(b'\n')
|
||||
mock_socket._requests.append(fd)
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 400 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 11\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nBad request'))
|
||||
|
||||
def test_before_after_request(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.before_request
|
||||
def before_request(req):
|
||||
if req.path == '/bar':
|
||||
return 'bar', 202
|
||||
req.g.message = 'baz'
|
||||
|
||||
@app.after_request
|
||||
def after_request_one(req, res):
|
||||
res.headers['X-One'] = '1'
|
||||
|
||||
@app.after_request
|
||||
async def after_request_two(req, res):
|
||||
res.set_cookie('foo', 'bar')
|
||||
return res
|
||||
|
||||
@app.route('/bar')
|
||||
def bar(req):
|
||||
return 'foo'
|
||||
|
||||
@app.route('/baz')
|
||||
def baz(req):
|
||||
return req.g.message
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/bar')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 202 N/A\r\n'))
|
||||
self.assertIn(b'X-One: 1\r\n', fd.response)
|
||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nbar'))
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/baz')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'X-One: 1\r\n', fd.response)
|
||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nbaz'))
|
||||
|
||||
def test_404(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/foo')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 404 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 9\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nNot found'))
|
||||
|
||||
def test_404_handler(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
@app.errorhandler(404)
|
||||
async def handle_404(req):
|
||||
return '404'
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/foo')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n404'))
|
||||
|
||||
def test_413(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/foo', body='x' * 17000)
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 413 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 17\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nPayload too large'))
|
||||
|
||||
def test_413_handler(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 'foo'
|
||||
|
||||
@app.errorhandler(413)
|
||||
async def handle_413(req):
|
||||
return '413', 400
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/foo', body='x' * 17000)
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 400 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n413'))
|
||||
|
||||
def test_500(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 1 / 0
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 500 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 21\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nInternal server error'))
|
||||
|
||||
def test_500_handler(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 1 / 0
|
||||
|
||||
@app.errorhandler(500)
|
||||
def handle_500(req):
|
||||
return '501', 501
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n501'))
|
||||
|
||||
def test_exception_handler(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return 1 / 0
|
||||
|
||||
@app.errorhandler(ZeroDivisionError)
|
||||
async def handle_div_zero(req, exc):
|
||||
return '501', 501
|
||||
|
||||
mock_socket.clear_requests()
|
||||
fd = mock_socket.add_request('GET', '/')
|
||||
self._add_shutdown(app)
|
||||
app.run()
|
||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n501'))
|
||||
114
tests/microdot_asyncio/test_request_asyncio.py
Normal file
114
tests/microdot_asyncio/test_request_asyncio.py
Normal file
@@ -0,0 +1,114 @@
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
import asyncio
|
||||
|
||||
import unittest
|
||||
from microdot import MultiDict
|
||||
from microdot_asyncio import Request
|
||||
from tests.mock_socket import get_async_request_fd
|
||||
|
||||
|
||||
def _run(coro):
|
||||
return asyncio.get_event_loop().run_until_complete(coro)
|
||||
|
||||
|
||||
class TestRequestAsync(unittest.TestCase):
|
||||
def test_create_request(self):
|
||||
fd = get_async_request_fd('GET', '/foo')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
self.assertEqual(req.app, 'app')
|
||||
self.assertEqual(req.client_addr, 'addr')
|
||||
self.assertEqual(req.method, 'GET')
|
||||
self.assertEqual(req.path, '/foo')
|
||||
self.assertEqual(req.http_version, '1.0')
|
||||
self.assertIsNone(req.query_string)
|
||||
self.assertEqual(req.args, {})
|
||||
self.assertEqual(req.headers, {'Host': 'example.com:1234'})
|
||||
self.assertEqual(req.cookies, {})
|
||||
self.assertEqual(req.content_length, 0)
|
||||
self.assertEqual(req.content_type, None)
|
||||
self.assertEqual(req.body, b'')
|
||||
self.assertEqual(req.json, None)
|
||||
self.assertEqual(req.form, None)
|
||||
|
||||
def test_headers(self):
|
||||
fd = get_async_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/json',
|
||||
'Cookie': 'foo=bar;abc=def',
|
||||
'Content-Length': '3'}, body='aaa')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
self.assertEqual(req.headers, {
|
||||
'Host': 'example.com:1234',
|
||||
'Content-Type': 'application/json',
|
||||
'Cookie': 'foo=bar;abc=def',
|
||||
'Content-Length': '3'})
|
||||
self.assertEqual(req.content_type, 'application/json')
|
||||
self.assertEqual(req.cookies, {'foo': 'bar', 'abc': 'def'})
|
||||
self.assertEqual(req.content_length, 3)
|
||||
self.assertEqual(req.body, b'aaa')
|
||||
|
||||
def test_args(self):
|
||||
fd = get_async_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%')
|
||||
self.assertEqual(req.args, MultiDict(
|
||||
{'foo': 'bar', 'abc': 'def', 'x': '/%%'}))
|
||||
|
||||
def test_json(self):
|
||||
fd = get_async_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/json'}, body='{"foo":"bar"}')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
json = req.json
|
||||
self.assertEqual(json, {'foo': 'bar'})
|
||||
self.assertTrue(req.json is json)
|
||||
|
||||
fd = get_async_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/json'}, body='[1, "2"]')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
self.assertEqual(req.json, [1, '2'])
|
||||
|
||||
fd = get_async_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/xml'}, body='[1, "2"]')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
self.assertIsNone(req.json)
|
||||
|
||||
def test_form(self):
|
||||
fd = get_async_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body='foo=bar&abc=def&x=%2f%%')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
form = req.form
|
||||
self.assertEqual(form, MultiDict(
|
||||
{'foo': 'bar', 'abc': 'def', 'x': '/%%'}))
|
||||
self.assertTrue(req.form is form)
|
||||
|
||||
fd = get_async_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/json'},
|
||||
body='foo=bar&abc=def&x=%2f%%')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
self.assertIsNone(req.form)
|
||||
|
||||
def test_large_line(self):
|
||||
saved_max_readline = Request.max_readline
|
||||
Request.max_readline = 16
|
||||
|
||||
fd = get_async_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body='foo=bar&abc=def&x=y')
|
||||
with self.assertRaises(ValueError):
|
||||
_run(Request.create('app', fd, 'addr'))
|
||||
|
||||
Request.max_readline = saved_max_readline
|
||||
|
||||
def test_large_payload(self):
|
||||
saved_max_content_length = Request.max_content_length
|
||||
Request.max_content_length = 16
|
||||
|
||||
fd = get_async_request_fd('GET', '/foo', headers={
|
||||
'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body='foo=bar&abc=def&x=y')
|
||||
req = _run(Request.create('app', fd, 'addr'))
|
||||
self.assertEqual(req.body, b'')
|
||||
|
||||
Request.max_content_length = saved_max_content_length
|
||||
131
tests/microdot_asyncio/test_response_asyncio.py
Normal file
131
tests/microdot_asyncio/test_response_asyncio.py
Normal file
@@ -0,0 +1,131 @@
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
import asyncio
|
||||
|
||||
import unittest
|
||||
from microdot_asyncio import Response
|
||||
from tests.mock_socket import FakeStreamAsync
|
||||
|
||||
|
||||
def _run(coro):
|
||||
return asyncio.get_event_loop().run_until_complete(coro)
|
||||
|
||||
|
||||
class TestResponseAsync(unittest.TestCase):
|
||||
def test_create_from_string(self):
|
||||
res = Response('foo')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.body, b'foo')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nfoo'))
|
||||
|
||||
def test_create_from_string_with_content_length(self):
|
||||
res = Response('foo', headers={'Content-Length': '2'})
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'Content-Length': '2'})
|
||||
self.assertEqual(res.body, b'foo')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 2\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nfoo'))
|
||||
|
||||
def test_create_from_bytes(self):
|
||||
res = Response(b'foo')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.body, b'foo')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\nfoo'))
|
||||
|
||||
def test_create_empty(self):
|
||||
res = Response(headers={'X-Foo': 'Bar'})
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'X-Foo': 'Bar'})
|
||||
self.assertEqual(res.body, b'')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
|
||||
self.assertIn(b'X-Foo: Bar\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 0\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n'))
|
||||
|
||||
def test_create_json(self):
|
||||
res = Response({'foo': 'bar'})
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'Content-Type': 'application/json'})
|
||||
self.assertEqual(res.body, b'{"foo": "bar"}')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 14\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: application/json\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n{"foo": "bar"}'))
|
||||
|
||||
res = Response([1, '2'])
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {'Content-Type': 'application/json'})
|
||||
self.assertEqual(res.body, b'[1, "2"]')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
|
||||
self.assertIn(b'Content-Length: 8\r\n', fd.response)
|
||||
self.assertIn(b'Content-Type: application/json\r\n', fd.response)
|
||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n[1, "2"]'))
|
||||
|
||||
def test_create_with_reason(self):
|
||||
res = Response('foo', reason='ALL GOOD!')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.reason, 'ALL GOOD!')
|
||||
self.assertEqual(res.body, b'foo')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertIn(b'HTTP/1.0 200 ALL GOOD!\r\n', fd.response)
|
||||
|
||||
def test_create_with_status_and_reason(self):
|
||||
res = Response('not found', 404, reason='NOT FOUND')
|
||||
self.assertEqual(res.status_code, 404)
|
||||
self.assertEqual(res.headers, {})
|
||||
self.assertEqual(res.reason, 'NOT FOUND')
|
||||
self.assertEqual(res.body, b'not found')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertIn(b'HTTP/1.0 404 NOT FOUND\r\n', fd.response)
|
||||
|
||||
def test_send_file(self):
|
||||
res = Response.send_file('tests/files/test.txt',
|
||||
content_type='text/html')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertEqual(
|
||||
fd.response,
|
||||
b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
|
||||
|
||||
def test_send_file_small_buffer(self):
|
||||
original_buffer_size = Response.send_file_buffer_size
|
||||
Response.send_file_buffer_size = 2
|
||||
res = Response.send_file('tests/files/test.txt',
|
||||
content_type='text/html')
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
||||
fd = FakeStreamAsync()
|
||||
_run(res.write(fd))
|
||||
self.assertEqual(
|
||||
fd.response,
|
||||
b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
|
||||
Response.send_file_buffer_size = original_buffer_size
|
||||
39
tests/mock_asyncio.py
Normal file
39
tests/mock_asyncio.py
Normal file
@@ -0,0 +1,39 @@
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
import asyncio
|
||||
|
||||
from tests import mock_socket
|
||||
|
||||
|
||||
def get_event_loop():
|
||||
return asyncio.get_event_loop()
|
||||
|
||||
|
||||
async def start_server(cb, host, port):
|
||||
class MockServer:
|
||||
def __init__(self):
|
||||
self.closed = False
|
||||
|
||||
async def run(self):
|
||||
s = mock_socket.socket()
|
||||
while not self.closed:
|
||||
fd, addr = s.accept()
|
||||
fd = mock_socket.FakeStreamAsync(fd)
|
||||
await cb(fd, fd)
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
async def wait_closed(self):
|
||||
while not self.closed:
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
server = MockServer()
|
||||
asyncio.get_event_loop().create_task(server.run())
|
||||
return server
|
||||
|
||||
|
||||
def run(coro):
|
||||
loop = asyncio.get_event_loop()
|
||||
return loop.run_until_complete(coro)
|
||||
102
tests/mock_socket.py
Normal file
102
tests/mock_socket.py
Normal file
@@ -0,0 +1,102 @@
|
||||
try:
|
||||
import uio as io
|
||||
except ImportError:
|
||||
import io
|
||||
|
||||
SOL_SOCKET = 'SOL_SOCKET'
|
||||
SO_REUSEADDR = 'SO_REUSEADDR'
|
||||
|
||||
_requests = []
|
||||
|
||||
|
||||
def getaddrinfo(host, port):
|
||||
return (('family', 'addr'), 'socktype', 'proto', 'canonname', 'sockaddr')
|
||||
|
||||
|
||||
class socket:
|
||||
def __init__(self):
|
||||
self.request_index = 0
|
||||
|
||||
def setsockopt(self, level, optname, value):
|
||||
pass
|
||||
|
||||
def bind(self, addr):
|
||||
pass
|
||||
|
||||
def listen(self, backlog):
|
||||
pass
|
||||
|
||||
def accept(self):
|
||||
self.request_index += 1
|
||||
return _requests[self.request_index - 1], 'addr'
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
|
||||
class FakeStream(io.BytesIO):
|
||||
def __init__(self, input_data):
|
||||
super().__init__(input_data)
|
||||
self.response = b''
|
||||
|
||||
def write(self, data):
|
||||
self.response += data
|
||||
|
||||
|
||||
class FakeStreamAsync:
|
||||
def __init__(self, stream=None):
|
||||
if stream is None:
|
||||
stream = FakeStream(b'')
|
||||
self.stream = stream
|
||||
|
||||
@property
|
||||
def response(self):
|
||||
return self.stream.response
|
||||
|
||||
async def readline(self):
|
||||
return self.stream.readline()
|
||||
|
||||
async def read(self, n):
|
||||
return self.stream.read(n)
|
||||
|
||||
async def awrite(self, data):
|
||||
self.stream.write(data)
|
||||
|
||||
async def aclose(self):
|
||||
pass
|
||||
|
||||
def get_extra_info(self, name, default=None):
|
||||
return name
|
||||
|
||||
|
||||
def get_request_fd(method, path, headers=None, body=None):
|
||||
if headers is None:
|
||||
headers = {}
|
||||
if body is None:
|
||||
body = ''
|
||||
elif 'Content-Length' not in headers:
|
||||
headers['Content-Length'] = str(len(body))
|
||||
request_bytes = '{method} {path} HTTP/1.0\n'.format(
|
||||
method=method, path=path)
|
||||
if 'Host' not in headers:
|
||||
headers['Host'] = 'example.com:1234'
|
||||
for header, value in headers.items():
|
||||
request_bytes += '{header}: {value}\n'.format(
|
||||
header=header, value=value)
|
||||
request_bytes += '\n' + body
|
||||
return FakeStream(request_bytes.encode())
|
||||
|
||||
|
||||
def get_async_request_fd(method, path, headers=None, body=None):
|
||||
fd = get_request_fd(method, path, headers=headers, body=body)
|
||||
return FakeStreamAsync(fd)
|
||||
|
||||
|
||||
def clear_requests():
|
||||
_requests.clear()
|
||||
|
||||
|
||||
def add_request(method, path, headers=None, body=None):
|
||||
fd = get_request_fd(method, path, headers=headers, body=body)
|
||||
_requests.append(fd)
|
||||
return fd
|
||||
25
tools/Dockerfile
Normal file
25
tools/Dockerfile
Normal file
@@ -0,0 +1,25 @@
|
||||
FROM ubuntu:latest
|
||||
|
||||
ARG DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
RUN apt-get update && \
|
||||
apt-get install -y build-essential libffi-dev git pkg-config python python3 && \
|
||||
rm -rf /var/lib/apt/lists/* && \
|
||||
git clone https://github.com/micropython/micropython.git && \
|
||||
cd micropython && \
|
||||
git checkout v1.15 && \
|
||||
git submodule update --init && \
|
||||
cd mpy-cross && \
|
||||
make && \
|
||||
cd .. && \
|
||||
cd ports/unix && \
|
||||
make axtls && \
|
||||
make && \
|
||||
make test && \
|
||||
make install && \
|
||||
apt-get purge --auto-remove -y build-essential libffi-dev git pkg-config python python3 && \
|
||||
cd ../../.. && \
|
||||
rm -rf micropython
|
||||
|
||||
CMD ["/usr/local/bin/micropython"]
|
||||
|
||||
6
tools/update-micropython.sh
Executable file
6
tools/update-micropython.sh
Executable file
@@ -0,0 +1,6 @@
|
||||
# this script updates the micropython binary in the /bin directory that is
|
||||
# used to run unit tests under GitHub Actions builds
|
||||
docker build -t micropython .
|
||||
docker create -it --name dummy-micropython micropython
|
||||
docker cp dummy-micropython:/usr/local/bin/micropython ../bin/micropython
|
||||
docker rm dummy-micropython
|
||||
35
tox.ini
Normal file
35
tox.ini
Normal file
@@ -0,0 +1,35 @@
|
||||
[tox]
|
||||
envlist=flake8,py36,py37,py38,py39,upy
|
||||
skipsdist=True
|
||||
skip_missing_interpreters=True
|
||||
|
||||
[gh-actions]
|
||||
python =
|
||||
3.6: py36
|
||||
3.7: py37
|
||||
3.8: py38
|
||||
3.9: py39
|
||||
pypy3: pypy3
|
||||
|
||||
[testenv]
|
||||
commands=
|
||||
pip install -e .
|
||||
pytest -p no:logging --cov=src --cov-branch --cov-report=term-missing
|
||||
deps=
|
||||
pytest
|
||||
pytest-cov
|
||||
|
||||
[testenv:flake8]
|
||||
deps=
|
||||
flake8
|
||||
commands=
|
||||
flake8 --ignore=W503 --exclude tests/libs src tests
|
||||
|
||||
[testenv:upy]
|
||||
whitelist_externals=sh
|
||||
commands=sh -c "bin/micropython run_tests.py"
|
||||
|
||||
[testenv:upy-mac]
|
||||
whitelist_externals=micropython
|
||||
commands=micropython run_tests.py
|
||||
deps=
|
||||
Reference in New Issue
Block a user