mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Port Datasette from Sanic to ASGI + Uvicorn (#518)
Datasette now uses ASGI internally, and no longer depends on Sanic. It now uses Uvicorn as the underlying HTTP server. This was thirteen months in the making... for full details see the issue: https://github.com/simonw/datasette/issues/272 And for a full sequence of commits plus commentary, see the pull request: https://github.com/simonw/datasette/pull/518
This commit is contained in:
parent
35429f9089
commit
ba8db9679f
19 changed files with 1510 additions and 947 deletions
|
|
@ -1,5 +1,7 @@
|
|||
from datasette.app import Datasette
|
||||
from datasette.utils import sqlite3
|
||||
from asgiref.testing import ApplicationCommunicator
|
||||
from asgiref.sync import async_to_sync
|
||||
import itertools
|
||||
import json
|
||||
import os
|
||||
|
|
@ -10,16 +12,82 @@ import sys
|
|||
import string
|
||||
import tempfile
|
||||
import time
|
||||
from urllib.parse import unquote
|
||||
|
||||
|
||||
class TestResponse:
|
||||
def __init__(self, status, headers, body):
|
||||
self.status = status
|
||||
self.headers = headers
|
||||
self.body = body
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
return json.loads(self.text)
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
return self.body.decode("utf8")
|
||||
|
||||
|
||||
class TestClient:
|
||||
def __init__(self, sanic_test_client):
|
||||
self.sanic_test_client = sanic_test_client
|
||||
max_redirects = 5
|
||||
|
||||
def get(self, path, allow_redirects=True):
|
||||
return self.sanic_test_client.get(
|
||||
path, allow_redirects=allow_redirects, gather_request=False
|
||||
def __init__(self, asgi_app):
|
||||
self.asgi_app = asgi_app
|
||||
|
||||
@async_to_sync
|
||||
async def get(self, path, allow_redirects=True, redirect_count=0, method="GET"):
|
||||
return await self._get(path, allow_redirects, redirect_count, method)
|
||||
|
||||
async def _get(self, path, allow_redirects=True, redirect_count=0, method="GET"):
|
||||
query_string = b""
|
||||
if "?" in path:
|
||||
path, _, query_string = path.partition("?")
|
||||
query_string = query_string.encode("utf8")
|
||||
instance = ApplicationCommunicator(
|
||||
self.asgi_app,
|
||||
{
|
||||
"type": "http",
|
||||
"http_version": "1.0",
|
||||
"method": method,
|
||||
"path": unquote(path),
|
||||
"raw_path": path.encode("ascii"),
|
||||
"query_string": query_string,
|
||||
"headers": [[b"host", b"localhost"]],
|
||||
},
|
||||
)
|
||||
await instance.send_input({"type": "http.request"})
|
||||
# First message back should be response.start with headers and status
|
||||
messages = []
|
||||
start = await instance.receive_output(2)
|
||||
messages.append(start)
|
||||
assert start["type"] == "http.response.start"
|
||||
headers = dict(
|
||||
[(k.decode("utf8"), v.decode("utf8")) for k, v in start["headers"]]
|
||||
)
|
||||
status = start["status"]
|
||||
# Now loop until we run out of response.body
|
||||
body = b""
|
||||
while True:
|
||||
message = await instance.receive_output(2)
|
||||
messages.append(message)
|
||||
assert message["type"] == "http.response.body"
|
||||
body += message["body"]
|
||||
if not message.get("more_body"):
|
||||
break
|
||||
response = TestResponse(status, headers, body)
|
||||
if allow_redirects and response.status in (301, 302):
|
||||
assert (
|
||||
redirect_count < self.max_redirects
|
||||
), "Redirected {} times, max_redirects={}".format(
|
||||
redirect_count, self.max_redirects
|
||||
)
|
||||
location = response.headers["Location"]
|
||||
return await self._get(
|
||||
location, allow_redirects=True, redirect_count=redirect_count + 1
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
def make_app_client(
|
||||
|
|
@ -32,6 +100,7 @@ def make_app_client(
|
|||
is_immutable=False,
|
||||
extra_databases=None,
|
||||
inspect_data=None,
|
||||
static_mounts=None,
|
||||
):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
filepath = os.path.join(tmpdir, filename)
|
||||
|
|
@ -73,9 +142,10 @@ def make_app_client(
|
|||
plugins_dir=plugins_dir,
|
||||
config=config,
|
||||
inspect_data=inspect_data,
|
||||
static_mounts=static_mounts,
|
||||
)
|
||||
ds.sqlite_functions.append(("sleep", 1, lambda n: time.sleep(float(n))))
|
||||
client = TestClient(ds.app().test_client)
|
||||
client = TestClient(ds.app())
|
||||
client.ds = ds
|
||||
yield client
|
||||
|
||||
|
|
@ -88,7 +158,7 @@ def app_client():
|
|||
@pytest.fixture(scope="session")
|
||||
def app_client_no_files():
|
||||
ds = Datasette([])
|
||||
client = TestClient(ds.app().test_client)
|
||||
client = TestClient(ds.app())
|
||||
client.ds = ds
|
||||
yield client
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue