-
Notifications
You must be signed in to change notification settings - Fork 16
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
19 changed files
with
500 additions
and
151 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
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
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,3 @@ | ||
name: FastAPITest | ||
packages: | ||
- path: fastapiapp |
Empty file.
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,67 @@ | ||
""" | ||
Rudimentary FastAPI application for testing. | ||
NB: This should not explicitly reference the `appmap` module in any way. Doing so invalidates | ||
testing of record-by-default. | ||
""" | ||
# pylint: disable=missing-function-docstring | ||
|
||
from typing import List | ||
|
||
from fastapi import FastAPI, Query, Request, Response | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/") | ||
def hello_world(): | ||
return {"Hello": "World!"} | ||
|
||
|
||
@app.post("/echo") | ||
async def echo(request: Request): | ||
body = await request.body() | ||
return Response(content=body, media_type="application/json") | ||
|
||
|
||
@app.get("/test") | ||
async def get_test(my_params: List[str] = Query(None)): | ||
response = Response(content="test", media_type="text/html; charset=utf-8") | ||
response.headers["ETag"] = "W/01" | ||
return response | ||
|
||
|
||
@app.post("/test") | ||
async def post_test(request: Request): | ||
await request.json() | ||
response = Response(content='{"test":true}', media_type="application/json") | ||
response.headers["ETag"] = "W/01" | ||
return response | ||
|
||
|
||
@app.get("/user/{username}") | ||
def get_user_profile(username): | ||
# show the user profile for that user | ||
return {"user": username} | ||
|
||
|
||
@app.get("/post/{post_id:int}") | ||
def get_post(post_id): | ||
# show the post with the given id, the id is an integer | ||
return {"post": post_id} | ||
|
||
|
||
@app.get("/post/{username}/{post_id:int}/summary") | ||
def get_user_post(username, post_id): | ||
# Show the summary of a user's post | ||
return {"user": username, "post": post_id} | ||
|
||
|
||
@app.get("/{org:int}/posts/{username}") | ||
def get_org_user_posts(org, username): | ||
return {"org": org, "username": username} | ||
|
||
|
||
@app.route("/exception") | ||
def raise_exception(): | ||
raise Exception("An exception") |
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 @@ | ||
import appmap |
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,14 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
from fastapiapp import app | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
yield TestClient(app) | ||
|
||
|
||
def test_request(client): | ||
response = client.get("/") | ||
|
||
assert response.status_code == 200 |
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
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,44 @@ | ||
import importlib.metadata | ||
|
||
import pytest | ||
from fastapi.testclient import TestClient | ||
|
||
import appmap | ||
from _appmap.env import Env | ||
from _appmap.metadata import Metadata | ||
|
||
# pylint: disable=unused-import | ||
from .web_framework import TestRequestCapture | ||
|
||
# pylint: enable=unused-import | ||
|
||
pytestmark = pytest.mark.web | ||
|
||
@pytest.fixture(name="app") | ||
def fastapi_app(data_dir, monkeypatch): | ||
monkeypatch.syspath_prepend(data_dir / "fastapi") | ||
|
||
Env.current.set("APPMAP_CONFIG", data_dir / "fastapi" / "appmap.yml") | ||
|
||
from fastapiapp import main # pyright: ignore[reportMissingImports] | ||
|
||
importlib.reload(main) | ||
|
||
# Add the FastAPI middleware to the app. This now happens automatically when a FastAPI app is | ||
# started from the command line, but must be done manually otherwise. | ||
main.app.add_middleware(appmap.fastapi.Middleware) | ||
|
||
return main.app | ||
|
||
|
||
@pytest.fixture(name="client") | ||
def fastapi_client(app): | ||
yield TestClient(app) | ||
|
||
|
||
@pytest.mark.appmap_enabled(env={"APPMAP_RECORD_REQUESTS": "false"}) | ||
def test_framework_metadata(client, events): # pylint: disable=unused-argument | ||
client.get("/") | ||
assert Metadata()["frameworks"] == [ | ||
{"name": "FastAPI", "version": importlib.metadata.version("fastapi")} | ||
] |
Oops, something went wrong.