Compare commits

..

44 Commits

Author SHA1 Message Date
kennethreitz 712ec2410d changes 2019-04-28 09:39:12 -04:00
kennethreitz dea2ca41d2 Merge pull request #351 from kennethreitz/route-params-docs
Add docs for route params convertors
2019-04-28 09:19:42 -04:00
Taoufik ca0f32c02b Black check before tests 2019-04-27 20:44:02 +02:00
Taoufik f21b296fba Add docs for route params convertors 2019-04-27 20:10:31 +02:00
Taoufik 3224479b99 Merge pull request #350 from kennethreitz/cleanup
Cleanup
2019-04-27 13:52:48 +02:00
Taoufik f95950eedc Cleanup 2019-04-27 13:49:54 +02:00
kennethreitz 4467376d0a Merge pull request #333 from taoufik07/custom_specifiers
Route params convertors
2019-04-27 07:32:13 -04:00
kennethreitz ac65dc5361 Merge pull request #347 from s-pace/doc/add_search
[doc website] Add a nice search experience
2019-04-27 07:25:38 -04:00
s-pace 4957793c80 feat: add search to every documentation pages 2019-04-22 22:23:07 +02:00
s-pace ff7f4b502d feat: add search to the main introduction page 2019-04-22 22:22:46 +02:00
Timo Furrer 816cb7188b Merge pull request #339 from jonbeebe/master
Removed asgiref dependency
2019-03-29 18:34:14 +01:00
Jonathan Beebe 6456d435eb Revert Pipfile.lock (with asgiref removed) 2019-03-28 20:36:11 -07:00
Jonathan Beebe 63e338ed6f Removed asgiref dependency
Replaced WsgiToAsgi (asgiref.wsgi) usage with WSGIMiddleware (starlette.middleware.wsgi) to fix breaking changes with asgiref 3.0.0.
2019-03-28 20:12:22 -07:00
Timo Furrer 00211c8f03 Merge pull request #335 from kobayashi/master
add sample of uploading a file
2019-03-21 21:30:14 +01:00
kobayashi ebed9fe3aa fix typos 2019-03-17 19:05:14 -04:00
kobayashi 734b5e7303 add sample of uploading a file 2019-03-17 15:21:27 -04:00
Taoufik 1696d501e2 Merge pull request #334 from MerleLiuKun/fix_test_responder_variable
fix variable error at test responder
2019-03-14 08:19:53 +00:00
ikronskun e65d2f8c50 fix variable error at test responder 2019-03-14 14:25:45 +08:00
taoufik07 9ea705b2ea Specifiers test 2019-03-12 17:39:53 +01:00
taoufik07 5a5a811dca Add routes specifiers 2019-03-12 16:58:36 +01:00
kennethreitz df7b9419c2 Merge pull request #332 from jlewis91/patch-1
Update tour.rst to be openapi compliant
2019-03-10 10:55:38 -04:00
Jeremiah 37318f1106 Update tour.rst 2019-03-10 14:53:44 +01:00
kennethreitz 19e9f6ac5d Merge pull request #320 from taoufik07/static_serve
Fix #303
2019-03-05 08:33:39 -05:00
kennethreitz 658b51a449 Merge pull request #321 from taoufik07/revert_before_requests
Revert before_requests
2019-03-05 08:33:25 -05:00
Parth Shandilya 485303c0f2 Merge pull request #314 from vlcinsky/fix_uvloop_env_marker
Fix #313 incomplete environment marker for uvloop
2019-03-03 23:22:53 +05:30
taoufik07 885d902b7d Revert 2019-02-26 23:46:07 +01:00
taoufik07 a35f02fb64 Add tests 2019-02-26 22:23:43 +01:00
taoufik07 28d1f16ad5 Disable serving when static_dir is None and handle templates_dir 2019-02-26 22:10:13 +01:00
Taoufik a04d7c3a9a Merge pull request #319 from taoufik07/refactor_before_ws
Refactor before_requests and websockets
2019-02-26 19:27:26 +00:00
taoufik07 b876f8484c Update docs 2019-02-26 17:01:13 +01:00
taoufik07 854c6d3d65 Add @before_request 2019-02-26 16:44:12 +01:00
taoufik07 f9a850a8fe Add before_requets for ws and refactor 2019-02-26 15:50:55 +01:00
taoufik07 e808662fe7 Refactor websockets 2019-02-26 15:27:26 +01:00
Taoufik 7bbb02126e Merge pull request #316 from tkamenoko/patch-4
fix typo
2019-02-23 15:15:31 +00:00
Taoufik aa101059a7 Merge pull request #315 from taoufik07/websockets_tests
Websockets tests
2019-02-23 15:11:23 +00:00
T.Kameyama d1f7fe02e4 fix typo 2019-02-24 00:10:47 +09:00
taoufik07 3e26dc1373 Add websockets tests 2019-02-23 16:00:22 +01:00
Jan Vlčinský 0a9d819555 Merge branch 'master' into fix_uvloop_env_marker 2019-02-22 20:47:25 +01:00
Jan Vlcinsky b31dfeefb7 Fix #313 incomplete environment marker for uvloop 2019-02-22 20:44:32 +01:00
Taoufik fc640ec331 Merge pull request #312 from iancleary/bug/242_docs_consistency
fixed flow between OpenAPI and Interactive Docs sections
2019-02-22 13:33:37 +00:00
iancleary 3382723457 fixed flow between OpenAPI and Interactive Docs sections 2019-02-22 06:16:45 -07:00
Taoufik 1fc0722ad6 Merge pull request #310 from taoufik07/fix/req_text
DEFAULT_ENCODING if none is detected
2019-02-22 11:57:11 +00:00
taoufik07 b21e308357 Add tests 2019-02-22 12:44:32 +01:00
taoufik07 738105314b Return DEFAULT_ENCODING if none and remove redundant code 2019-02-22 12:34:22 +01:00
13 changed files with 360 additions and 104 deletions
+1 -1
View File
@@ -9,5 +9,5 @@ install:
# command to run the dependencies
script:
- "pytest"
- "black responder tests setup.py --check"
- "pytest"
Generated
-7
View File
@@ -43,13 +43,6 @@
],
"version": "==0.6.0"
},
"asgiref": {
"hashes": [
"sha256:9b05dcd41a6a89ca8c6e7f7e4089c3f3e76b5af60aebb81ae6d455ad81989c97",
"sha256:b21dc4c43d7aba5a844f4c48b8f49d56277bc34937fd9f9cb93ec97fde7e3082"
],
"version": "==2.3.2"
},
"async-timeout": {
"hashes": [
"sha256:0c3c816a028d47f659d6ff5c745cb2acf1f966da1fe5c19c77a70282b25f4c5f",
+21
View File
@@ -8,6 +8,27 @@
<iframe src="https://ghbtns.com/github-btn.html?user=kennethreitz&repo=responder&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="200px" height="35px"></iframe>
</p>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
<style>
.algolia-autocomplete{
width: 100%;
height: 1.5em
}
.algolia-autocomplete a{
border-bottom: none !important;
}
#doc_search{
width: 100%;
height: 100%;
}
</style>
<input id="doc_search" placeholder="Search the doc" autofocus/>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js" onload="docsearch({
apiKey: 'ac965312db252e0496283c75c6f76f0b',
indexName: 'python-responder',
inputSelector: '#doc_search',
debug: false // Set debug to true if you want to inspect the dropdown
})" async></script>
<p>
<strong>Responder</strong> is a web service framework, written for human beings.
+21
View File
@@ -8,6 +8,27 @@
<iframe src="https://ghbtns.com/github-btn.html?user=kennethreitz&repo=responder&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="200px" height="35px"></iframe>
</p>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
<style>
.algolia-autocomplete{
width: 100%;
height: 1.5em
}
.algolia-autocomplete a{
border-bottom: none !important;
}
#doc_search{
width: 100%;
height: 100%;
}
</style>
<input id="doc_search" placeholder="Search the doc" autofocus/>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js" onload="docsearch({
apiKey: 'ac965312db252e0496283c75c6f76f0b',
indexName: 'python-responder',
inputSelector: '#doc_search',
debug: false // Set debug to true if you want to inspect the dropdown
})" async></script>
<p>
<strong>Responder</strong> is a web service framework, written for human beings.
+34
View File
@@ -48,6 +48,14 @@ If you want dynamic URLs, you can use Python's familiar *f-string syntax* to dec
A ``GET`` request to ``/hello/brettcannon`` will result in a response of ``hello, brettcannon!``.
Type convertors are also available::
@api.route("/add/{a:int}/{b:int}")
async def add(req, resp, *, a, b):
resp.text = f"{a} + {b} = {a + b}"
Supported types: ``str``, ``int`` and ``float``.
Returning JSON / YAML
---------------------
@@ -124,3 +132,29 @@ Here, we'll process our data in the background, while responding immediately to
resp.media = {'success': True}
A ``POST`` request to ``/incoming`` will result in an immediate response of ``{'success': true}``.
Here's a sample code to post a file with background::
@api.route("/")
async def upload_file(req, resp):
@api.background.task
def process_data(data):
f = open('./{}'.format(data['file']['filename']), 'w')
f.write(data['file']['content'].decode('utf-8'))
f.close()
data = await req.media(format='files')
process_data(data)
resp.media = {'success': 'ok'}
You can send a file easily with requests::
import requests
data = {'file': ('hello.txt', 'hello, world!', "text/plain")}
r = requests.post('http://127.0.0.1:8210/file', files=data)
print(r.text)
+36 -11
View File
@@ -79,7 +79,6 @@ Responder comes with built-in support for OpenAPI / marshmallow::
title="Web Service",
version="1.0",
openapi="3.0.2",
docs_route='/docs',
description=description,
terms_of_service=terms_of_service,
contact=contact,
@@ -101,8 +100,10 @@ Responder comes with built-in support for OpenAPI / marshmallow::
responses:
200:
description: A pet to be returned
schema:
$ref = "#/components/schemas/Pet"
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
"""
resp.media = PetSchema().dump({"name": "little orange"})
@@ -113,14 +114,22 @@ Responder comes with built-in support for OpenAPI / marshmallow::
>>> print(r.text)
components:
parameters: {}
schemas:
parameters: {}
responses: {}
schemas:
Pet:
properties:
properties:
name: {type: string}
type: object
info: {title: Web Service, version: 1.0}
openapi: '3.0'
type: object
securitySchemes: {}
info:
contact: {email: support@example.com, name: API Support, url: 'http://www.example.com/support'}
description: This is a sample server for a pet store.
license: {name: Apache 2.0, url: 'https://www.apache.org/licenses/LICENSE-2.0.html'}
termsOfService: http://example.com/terms/
title: Web Service
version: 1.0
openapi: 3.0.2
paths:
/:
get:
@@ -135,7 +144,16 @@ Interactive Documentation
Responder can automatically supply API Documentation for you. Using the example above::
api = responder.API(title="Web Service", version="1.0", openapi="3.0.0", docs_route="/docs")
api = responder.API(
title="Web Service",
version="1.0",
openapi="3.0.2",
docs_route='/docs',
description=description,
terms_of_service=terms_of_service,
contact=contact,
license=license,
)
This will make ``/docs`` render interactive documentation for your API.
@@ -184,7 +202,7 @@ To set cookies directives, you should use `resp.set_cookie`::
Supported directives:
* ``key`` - **Reduired**
* ``key`` - **Required**
* ``value`` - [OPTIONAL] - Defaults to ``""``.
* ``expires`` - Defaults to ``None``.
* ``max_age`` - Defaults to ``None``.
@@ -225,6 +243,13 @@ If you'd like a view to be executed before every request, simply do the followin
Now all requests to your HTTP Service will include an ``X-Pizza`` header.
For ``websockets``::
@api.route(before_request=True, websocket=True)
def prepare_response(ws):
await ws.accept()
WebSocket Support
-----------------
+1 -1
View File
@@ -1 +1 @@
__version__ = "1.3.0"
__version__ = "1.3.1"
+79 -47
View File
@@ -12,7 +12,7 @@ import uvicorn
import yaml
from apispec import APISpec, yaml_utils
from apispec.ext.marshmallow import MarshmallowPlugin
from asgiref.wsgi import WsgiToAsgi
from starlette.middleware.wsgi import WSGIMiddleware
from starlette.middleware.errors import ServerErrorMiddleware
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware.gzip import GZipMiddleware
@@ -89,13 +89,27 @@ class API:
self.contact = contact
self.license = license
self.openapi_version = openapi
self.static_dir = Path(os.path.abspath(static_dir))
self.static_route = f"/{static_route.strip('/')}"
self.templates_dir = Path(os.path.abspath(templates_dir))
if static_dir is not None:
if static_route is None:
static_route = static_dir
static_dir = Path(os.path.abspath(static_dir))
self.static_dir = static_dir
self.static_route = static_route
self.built_in_templates_dir = Path(
os.path.abspath(os.path.dirname(__file__) + "/templates")
)
if templates_dir is not None:
templates_dir = Path(os.path.abspath(templates_dir))
self.templates_dir = templates_dir or self.built_in_templates_dir
self.apps = {}
self.routes = {}
self.before_requests = {"http": [], "ws": []}
self.docs_theme = DEFAULT_API_THEME
self.docs_route = docs_route
self.schemas = {}
@@ -116,19 +130,23 @@ class API:
# Make the static/templates directory if they don't exist.
for _dir in (self.static_dir, self.templates_dir):
os.makedirs(_dir, exist_ok=True)
if _dir is not None:
os.makedirs(_dir, exist_ok=True)
self.whitenoise = WhiteNoise(application=self._notfound_wsgi_app)
self.whitenoise.add_files(str(self.static_dir))
if self.static_dir is not None:
self.whitenoise = WhiteNoise(application=self._notfound_wsgi_app)
self.whitenoise.add_files(str(self.static_dir))
self.whitenoise.add_files(
(
Path(apistar.__file__).parent / "themes" / self.docs_theme / "static"
).resolve()
)
self.whitenoise.add_files(
(
Path(apistar.__file__).parent
/ "themes"
/ self.docs_theme
/ "static"
).resolve()
)
self.apps = {}
self.mount(self.static_route, self.whitenoise)
self.mount(self.static_route, self.whitenoise)
self.formats = get_formats()
@@ -178,14 +196,23 @@ class API:
start_response("404 NOT FOUND", [("Content-Type", "text/plain")])
return [b"Not Found."]
@property
def before_requests(self):
def gen():
for route in self.routes:
if self.routes[route].before_request:
yield self.routes[route]
def before_request(self, websocket=False):
def decorator(f):
if websocket:
self.before_requests.setdefault("ws", []).append(f)
else:
self.before_requests.setdefault("http", []).append(f)
return f
return [g for g in gen()]
return decorator
@property
def before_http_requests(self):
return self.before_requests.get("http", [])
@property
def before_ws_requests(self):
return self.before_requests.get("ws", [])
@property
def _apispec(self):
@@ -243,7 +270,7 @@ class API:
try:
return app(scope)
except TypeError:
app = WsgiToAsgi(app)
app = WSGIMiddleware(app)
return app(scope)
return self.app(scope)
@@ -256,8 +283,7 @@ class API:
if scope["type"] == "lifespan":
return self.lifespan_handler(scope)
elif scope["type"] == "websocket":
ws = WebSocket(scope=scope, receive=receive, send=send)
await self._dispatch_ws(ws)
await self._dispatch_ws(scope=scope, receive=receive, send=send)
else:
req = models.Request(scope, receive=receive, api=self)
resp = await self._dispatch_request(
@@ -267,24 +293,18 @@ class API:
return asgi
async def _dispatch_ws(self, ws):
async def _dispatch_ws(self, scope, receive, send):
ws = WebSocket(scope=scope, receive=receive, send=send)
route = self.path_matches_route(ws.url.path)
route = self.routes.get(route)
try:
try:
# Run the view.
r = self.background(route.endpoint, ws)
# If it's async, await it.
if hasattr(r, "cr_running"):
await r
except TypeError as e:
cont = True
except Exception:
await self.background(
self.default_response, websocket=route.uses_websocket, error=True
)
raise
if route:
for before_request in self.before_ws_requests:
await self.background(before_request, ws=ws)
await self.background(route.endpoint, ws)
else:
await send({"type": "websocket.close", "code": 1000})
def add_schema(self, name, schema, check_existing=True):
"""Adds a mashmallow schema to the API specification."""
@@ -347,8 +367,8 @@ class API:
if route:
resp = models.Response(req=req, formats=self.formats)
for before_request in self.before_requests:
await self._execute_route(route=before_request, req=req, resp=resp)
for before_request in self.before_http_requests:
await self.background(before_request, req=req, resp=resp)
await self._execute_route(route=route, req=req, resp=resp, **options)
else:
@@ -443,22 +463,29 @@ class API:
:param static: If ``True``, and no endpoint was passed, render "static/index.html", and it will become a default route.
:param check_existing: If ``True``, an AssertionError will be raised, if the route is already defined.
"""
if before_request:
if websocket:
self.before_requests.setdefault("ws", []).append(endpoint)
else:
self.before_requests.setdefault("http", []).append(endpoint)
return
if route is None:
route = f"/{uuid4().hex}"
if check_existing:
assert route not in self.routes
if not endpoint and static:
endpoint = self.static_response
default = True
if static:
assert self.static_dir is not None
if not endpoint:
endpoint = self.static_response
default = True
if default:
self.default_endpoint = endpoint
self.routes[route] = Route(
route, endpoint, websocket=websocket, before_request=before_request
)
self.routes[route] = Route(route, endpoint, websocket=websocket)
# TODO: A better data structure or sort it once the app is loaded
self.routes = dict(
sorted(self.routes.items(), key=lambda item: item[1]._weight())
@@ -487,6 +514,9 @@ class API:
resp.html = self.docs
def static_response(self, req, resp):
assert self.static_dir is not None
index = (self.static_dir / "index.html").resolve()
if os.path.exists(index):
with open(index, "r") as f:
@@ -598,6 +628,7 @@ class API:
def static_url(self, asset):
"""Given a static asset, return its URL path."""
assert None not in (self.static_dir, self.static_route)
return f"{self.static_route}/{str(asset)}"
@property
@@ -617,6 +648,7 @@ class API:
template = env.get_template("/".join([self.docs_theme, "index.html"]))
def static_url(asset):
assert None not in (self.static_dir, self.static_route)
return f"{self.static_route}/{asset}"
return template.render(
+3 -9
View File
@@ -185,13 +185,7 @@ class Request:
if self._encoding:
return self._encoding
# Then try what's defined by the Request.
elif await self.declared_encoding:
return self.declared_encoding
# Then, automatically detect the encoding.
else:
return await self.apparent_encoding
return await self.apparent_encoding
@encoding.setter
def encoding(self, value):
@@ -221,8 +215,8 @@ class Request:
if declared_encoding:
return declared_encoding
else:
return chardet.detect(await self.content)["encoding"]
return chardet.detect(await self.content)["encoding"] or DEFAULT_ENCODING
@property
def is_secure(self):
+17 -2
View File
@@ -1,7 +1,22 @@
import re
import functools
import inspect
from parse import parse
from parse import parse, with_pattern
def _make_convertor(type, pattern):
@with_pattern(pattern)
def inner(value):
return type(value)
return inner
_convertors = {
"int": _make_convertor(int, r"\d+"),
"str": _make_convertor(str, r"[^/]+"),
"float": _make_convertor(float, r"\d+(.\d+)?"),
}
class Route:
@@ -46,7 +61,7 @@ class Route:
@functools.lru_cache(maxsize=None)
def incoming_matches(self, s):
results = parse(self.route, s)
results = parse(self.route, s, _convertors)
return results.named if results else {}
def url(self, **params):
+2 -17
View File
@@ -31,13 +31,12 @@ required = [
"graphql-server-core>=1.1",
"jinja2",
"parse",
"uvloop; sys_platform != 'win32'",
"uvloop; sys_platform != 'win32' and sys_platform != 'cygwin' and sys_platform != 'cli'",
"rfc3986",
"python-multipart",
"chardet",
"apispec>=1.0.0b1",
"marshmallow",
"asgiref",
"whitenoise",
"docopt",
"itsdangerous",
@@ -123,21 +122,7 @@ setup(
url="https://github.com/kennethreitz/responder",
packages=find_packages(exclude=["tests"]),
entry_points={"console_scripts": ["responder=responder.cli:cli"]},
package_data={
# "": ["LICENSE", "NOTICES"],
# "pipenv.vendor.requests": ["*.pem"],
# "pipenv.vendor.certifi": ["*.pem"],
# "pipenv.vendor.click_completion": ["*.j2"],
# "pipenv.patched.notpip._vendor.certifi": ["*.pem"],
# "pipenv.patched.notpip._vendor.requests": ["*.pem"],
# "pipenv.patched.notpip._vendor.distlib._backport": ["sysconfig.cfg"],
# "pipenv.patched.notpip._vendor.distlib": [
# "t32.exe",
# "t64.exe",
# "w32.exe",
# "w64.exe",
# ],
},
package_data={},
python_requires=">=3.6",
setup_requires=[],
install_requires=required,
+119 -9
View File
@@ -9,6 +9,7 @@ import string
import io
from starlette.responses import PlainTextResponse
from starlette.testclient import TestClient as StarletteTestClient
def test_api_basic_route(api):
@@ -522,13 +523,71 @@ def test_404(api):
assert r.status_code == responder.status_codes.HTTP_404
def test_kinda_websockets(api):
def test_websockets_text(api):
payload = "Hello via websocket!"
@api.route("/ws", websocket=True)
async def websocket(ws):
await ws.accept()
await ws.send_text("Hello via websocket!")
await ws.send_text(payload)
await ws.close()
client = StarletteTestClient(api)
with client.websocket_connect("ws://;/ws") as websocket:
data = websocket.receive_text()
assert data == payload
def test_websockets_bytes(api):
payload = b"Hello via websocket!"
@api.route("/ws", websocket=True)
async def websocket(ws):
await ws.accept()
await ws.send_bytes(payload)
await ws.close()
client = StarletteTestClient(api)
with client.websocket_connect("ws://;/ws") as websocket:
data = websocket.receive_bytes()
assert data == payload
def test_websockets_json(api):
payload = {"Hello": "via websocket!"}
@api.route("/ws", websocket=True)
async def websocket(ws):
await ws.accept()
await ws.send_json(payload)
await ws.close()
client = StarletteTestClient(api)
with client.websocket_connect("ws://;/ws") as websocket:
data = websocket.receive_json()
assert data == payload
def test_before_websockets(api):
payload = {"Hello": "via websocket!"}
@api.route("/ws", websocket=True)
async def websocket(ws):
await ws.send_json(payload)
await ws.close()
@api.route(before_request=True, websocket=True)
async def before_request(ws):
await ws.accept()
await ws.send_json({"before": "request"})
client = StarletteTestClient(api)
with client.websocket_connect("ws://;/ws") as websocket:
data = websocket.receive_json()
assert data == {"before": "request"}
data = websocket.receive_json()
assert data == payload
def test_startup(api):
who = [None]
@@ -684,16 +743,13 @@ def test_staticfiles(tmpdir):
def test_staticfiles_custom_route(tmpdir):
static_dir = tmpdir.mkdir("static")
static_route = "custom/static/route/"
static_route = "/custom/static/route"
asset = create_asset(static_dir)
api = responder.API(static_dir=str(static_dir), static_route=static_route)
session = api.session()
# Check
assert api.static_route == "/custom/static/route"
static_route = api.static_route
# ok
@@ -709,6 +765,48 @@ def test_staticfiles_custom_route(tmpdir):
assert r.status_code == api.status_codes.HTTP_404
def test_staticfiles_none_dir(tmpdir):
api = responder.API(static_dir=None)
session = api.session()
static_dir = tmpdir.mkdir("static")
asset = create_asset(static_dir)
static_route = api.static_route
# ok
r = session.get(f"{static_route}/{asset.basename}")
assert r.status_code == api.status_codes.HTTP_404
# dir listing
r = session.get(f"{static_route}")
assert r.status_code == api.status_codes.HTTP_404
# SPA
with pytest.raises(Exception) as excinfo:
api.add_route("/spa", static=True)
def test_staticfiles_none_dir_route(tmpdir):
api = responder.API(static_dir=None, static_route=None)
session = api.session()
static_dir = tmpdir.mkdir("static")
asset = create_asset(static_dir)
static_route = api.static_route
# ok
r = session.get(f"{static_route}/{asset.basename}")
assert r.status_code == api.status_codes.HTTP_404
# dir listing
r = session.get(f"{static_route}")
assert r.status_code == api.status_codes.HTTP_404
def test_response_html_property(api):
@api.route("/")
def view(req, resp):
@@ -756,18 +854,30 @@ def test_stream(api, session):
def foo():
pass
res.stream(foo)
resp.stream(foo)
with pytest.raises(AssertionError):
async def foo():
pass
res.stream(foo)
resp.stream(foo)
with pytest.raises(AssertionError):
def foo():
yield "oopsie"
res.stream(foo)
resp.stream(foo)
def test_empty_req_text(api):
content = "It's working"
@api.route("/")
async def home(req, resp):
await req.text
resp.text = content
r = api.requests.post("/")
assert r.text == content
+26
View File
@@ -131,3 +131,29 @@ def test_does_match_with_route(route, match, expected):
def test_weight(path_param, expected_weight):
r = routes.Route(path_param, "test_endpoint")
assert r._weight() == expected_weight
@pytest.mark.parametrize(
"route, path, expected_result",
[
pytest.param("/{greetings:str}", "/hello", {"greetings": "hello"}),
pytest.param(
"/{greetings:str}/{who}",
"/hello/Laidia",
{"greetings": "hello", "who": "Laidia"},
),
pytest.param("/{birth_date:int}", "/1937", {"birth_date": 1937}),
pytest.param(
"/{name:str}/{age:int}", "/Fatna/80", {"name": "Fatna", "age": 80}
),
pytest.param(
"/{x:float}/{y:float}", "/10.20/75", {"x": float(10.20), "y": float(75)}
),
pytest.param("/{name:str}/{age:int}", "/Fatna/eighty", {}),
pytest.param("/{greetings:int}", "/hello", {}),
pytest.param("/{name:float}", "/Fatna", {}),
],
)
def test_custom_specifiers(route, path, expected_result):
r = routes.Route(route, "test_endpoint")
assert r.incoming_matches(path) == expected_result