Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.3k
Introduce Hypercorn as a test HTTP/2 server#412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,6 +8,7 @@ flake8 | ||
| flake8-bugbear | ||
| flake8-comprehensions | ||
| flake8-pie | ||
| hypercorn | ||
| isort | ||
| mypy | ||
florimondmanca marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| pytest | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| import asyncio | ||
| import functools | ||
| import json | ||
| import os | ||
| import threading | ||
| import time | ||
| import typing | ||
| import sys | ||
| import pytest | ||
| import trio | ||
| import trustme | ||
| from cryptography.hazmat.backends import default_backend | ||
| from cryptography.hazmat.primitives.serialization import ( | ||
| @@ -14,6 +17,8 @@ | ||
| PrivateFormat, | ||
| load_pem_private_key, | ||
| ) | ||
| import hypercorn.config | ||
| import hypercorn.trio | ||
| from uvicorn.config import Config | ||
| from uvicorn.main import Server | ||
| @@ -288,3 +293,83 @@ def https_server(cert_pem_file, cert_private_key_file): | ||
| ) | ||
| server = TestServer(config=config) | ||
| yield from serve_in_thread(server) | ||
| async def h2_app(scope, receive, send): | ||
| assert scope["type"] == "http" | ||
| assert scope["http_version"] == "2" | ||
| body = b"" | ||
| more_body = True | ||
| while more_body: | ||
| message = await receive() | ||
| body += message.get("body", b"") | ||
| more_body = message.get("more_body", False) | ||
| data = {"method": scope["method"], "path": scope["path"], "body": body.decode()} | ||
| content = json.dumps(data).encode() | ||
| headers = [(b"content-length", b"%d" % len(content))] | ||
| await send({"type": "http.response.start", "status": 200, "headers": headers}) | ||
| await send({"type": "http.response.body", "body": content}) | ||
| class H2Server: | ||
| """ | ||
| An HTTP/2 ASGI server class. | ||
| This is a wrapper around Hypercorn that matches the parts of the | ||
| uvicorn `Server` interface we use in our tests. | ||
| """ | ||
| def __init__( | ||
| self, app: typing.Callable, host: str, port: int, certfile: str, keyfile: str | ||
| ): | ||
| self.app = app | ||
| self.config = hypercorn.config.Config() | ||
| self.config.bind = [f"{host}:{port}"] | ||
| self.config.certfile = certfile | ||
| self.config.keyfile = keyfile | ||
| self.config.worker_class = "trio" | ||
| self.started = False | ||
| self.should_exit = False | ||
| @property | ||
| def url(self) -> URL: | ||
| authority = self.config.bind[0] | ||
| return URL(f"https://{authority}") | ||
| def run(self) -> None: | ||
| async def shutdown_trigger() -> None: | ||
| while not self.should_exit: | ||
| await trio.sleep(0.1) | ||
| bound_serve = functools.partial( | ||
| hypercorn.trio.serve, shutdown_trigger=shutdown_trigger | ||
| ) | ||
| async def main() -> None: | ||
| async with trio.open_nursery() as nursery: | ||
| await nursery.start(bound_serve, self.app, self.config) | ||
| self.started = True | ||
| trio.run(main) | ||
| @pytest.fixture(scope=SERVER_SCOPE) | ||
| def h2_server( | ||
| cert_pem_file: str, cert_private_key_file: str | ||
| ) -> typing.Iterator[H2Server]: | ||
| if sys.version_info < (3, 7): | ||
| pytest.skip(reason="Hypercorn requires Python 3.7 or higher") | ||
florimondmanca marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| server = H2Server( | ||
| app=h2_app, | ||
| host="127.0.0.1", | ||
| port=8002, | ||
| certfile=cert_pem_file, | ||
| keyfile=cert_private_key_file, | ||
| ) | ||
| yield from serve_in_thread(server) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.