-
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
17 changed files
with
467 additions
and
106 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,57 @@ | ||
""" | ||
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 fastapi import FastAPI, 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.route("/test") | ||
# def the_test(): | ||
# response = make_response("testing") | ||
# response.add_etag() | ||
# return response | ||
|
||
|
||
# @app.route("/user/<username>") | ||
# def show_user_profile(username): | ||
# # show the user profile for that user | ||
# return "User %s" % escape(username) | ||
|
||
|
||
# @app.route("/post/<int:post_id>") | ||
# def show_post(post_id): | ||
# # show the post with the given id, the id is an integer | ||
# return "Post %d" % post_id | ||
|
||
|
||
# @app.route("/post/<username>/<int:post_id>/summary") | ||
# def show_user_post(username, post_id): | ||
# # Show the summary of a user's post | ||
# return f"User {escape(username)} post {post_id}" | ||
|
||
|
||
# @app.route("/<int:org>/posts/<username>") | ||
# def show_org_user_posts(org, username): | ||
# return f"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,50 @@ | ||
import importlib | ||
import sys | ||
|
||
import fastapi | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
|
||
import appmap | ||
from _appmap.env import Env | ||
from _appmap.metadata import Metadata | ||
|
||
# pylint: disable=unused-import,import-error,import-outside-toplevel | ||
# from .web_framework import _test_request_capture # pyright: ignore | ||
|
||
# pylint: enable=unused-import | ||
|
||
|
||
@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") | ||
|
||
sys.modules.pop("app", None) | ||
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 flask_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": fastapi.__version__}] | ||
|
||
|
||
@pytest.mark.appmap_enabled(env={"APPMAP_RECORD_REQUESTS": "false"}) | ||
def test_app_can_read_body(client): | ||
response = client.post("/echo", json={"test": "json"}) | ||
assert response.content == b'{"test": "json"}' |
Oops, something went wrong.