-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Integration test fixtures.""" | ||
|
||
import socket | ||
from typing import AsyncIterator, Callable | ||
|
||
import pytest | ||
import pytest_asyncio | ||
import tornado | ||
from tornado.web import RequestHandler | ||
|
||
from rest_tools.client import RestClient | ||
from rest_tools.server import RestServer, RestHandlerSetup | ||
|
||
|
||
@pytest.fixture | ||
def port() -> int: | ||
"""Get an ephemeral port number.""" | ||
# unix.stackexchange.com/a/132524 | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
s.bind(("", 0)) | ||
addr = s.getsockname() | ||
ephemeral_port = addr[1] | ||
s.close() | ||
return ephemeral_port | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def server(port: int) -> AsyncIterator[Callable[[], RestClient]]: | ||
"""Start up REST server and attach handlers.""" | ||
|
||
class TestHandler(RequestHandler): | ||
ROUTE = "/echo/this" | ||
|
||
def post(self) -> None: | ||
if self.get_argument("raise", None): | ||
raise tornado.web.HTTPError(400, self.get_argument("raise")) | ||
self.write(self.get_argument("echo")) | ||
|
||
args = RestHandlerSetup({"debug": True}) | ||
rs = RestServer(debug=True) | ||
rs.add_route(TestHandler.ROUTE, TestHandler, args) | ||
rs.startup(address="localhost", port=port) | ||
|
||
def client() -> RestClient: | ||
return RestClient(f"http://localhost:{port}", retries=0) | ||
|
||
try: | ||
yield client | ||
finally: | ||
await rs.stop() # type: ignore[no-untyped-call] |
This file contains 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