diff --git a/.gitignore b/.gitignore index 5d381cc..f295d3d 100644 --- a/.gitignore +++ b/.gitignore @@ -158,5 +158,5 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/README.md b/README.md index 2b44bc5..1e4b094 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # falcon-example -学习 Python 的 Web 框架 Falcon \ No newline at end of file +学习 Python 的 Web 框架 Falcon + +## 安装依赖 + +``` +python3 -m pip install -r requirements.txt +``` \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/app.py b/app/app.py new file mode 100644 index 0000000..a982732 --- /dev/null +++ b/app/app.py @@ -0,0 +1,7 @@ +import falcon + +from .images import Resource + +app = application = falcon.App() + +app.add_route('/images', Resource()) \ No newline at end of file diff --git a/app/images.py b/app/images.py new file mode 100644 index 0000000..03a7a3e --- /dev/null +++ b/app/images.py @@ -0,0 +1,18 @@ +import falcon +import msgpack + + +class Resource: + + def on_get(self, req, resp): + doc = { + 'images': [ + { + 'href': '/images/1eaf6ef1-7f2d-4ecc-a8d5-6e8adba7cc0e.png', + } + ] + } + + resp.data = msgpack.packb(doc, use_bin_type=True) + resp.content_type = falcon.MEDIA_MSGPACK + resp.status = falcon.HTTP_200 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..be22d9c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +falcon +requests +gunicorn +httpie +orjson==3.9.2 +msgpack-python==0.5.6 +pytest==7.4.0 \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000..5196e7c --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,27 @@ +import falcon +from falcon import testing +import msgpack +import pytest + +from app.app import app + + +@pytest.fixture() +def client(): + return testing.TestClient(app) + + +def test_list_images(client): + doc = { + 'images': [ + { + 'href': '/images/1eaf6ef1-7f2d-4ecc-a8d5-6e8adba7cc0e.png', + } + ] + } + + response = client.simulate_get('/images') + result_doc = msgpack.unpackb(response.content, raw=False) + + assert result_doc == doc + assert response.status == falcon.HTTP_OK \ No newline at end of file