21 changed files with 14 additions and 119 deletions
@ -1,39 +0,0 @@ |
|||||
from typing import Union |
|
||||
|
|
||||
from fastapi import FastAPI, Header, HTTPException |
|
||||
from pydantic import BaseModel |
|
||||
from typing_extensions import Annotated |
|
||||
|
|
||||
fake_secret_token = "coneofsilence" |
|
||||
|
|
||||
fake_db = { |
|
||||
"foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"}, |
|
||||
"bar": {"id": "bar", "title": "Bar", "description": "The bartenders"}, |
|
||||
} |
|
||||
|
|
||||
app = FastAPI() |
|
||||
|
|
||||
|
|
||||
class Item(BaseModel): |
|
||||
id: str |
|
||||
title: str |
|
||||
description: Union[str, None] = None |
|
||||
|
|
||||
|
|
||||
@app.get("/items/{item_id}", response_model=Item) |
|
||||
async def read_main(item_id: str, x_token: Annotated[str, Header()]): |
|
||||
if x_token != fake_secret_token: |
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header") |
|
||||
if item_id not in fake_db: |
|
||||
raise HTTPException(status_code=404, detail="Item not found") |
|
||||
return fake_db[item_id] |
|
||||
|
|
||||
|
|
||||
@app.post("/items/", response_model=Item) |
|
||||
async def create_item(item: Item, x_token: Annotated[str, Header()]): |
|
||||
if x_token != fake_secret_token: |
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header") |
|
||||
if item.id in fake_db: |
|
||||
raise HTTPException(status_code=409, detail="Item already exists") |
|
||||
fake_db[item.id] = item |
|
||||
return item |
|
||||
@ -1,65 +0,0 @@ |
|||||
from fastapi.testclient import TestClient |
|
||||
|
|
||||
from .main import app |
|
||||
|
|
||||
client = TestClient(app) |
|
||||
|
|
||||
|
|
||||
def test_read_item(): |
|
||||
response = client.get("/items/foo", headers={"X-Token": "coneofsilence"}) |
|
||||
assert response.status_code == 200 |
|
||||
assert response.json() == { |
|
||||
"id": "foo", |
|
||||
"title": "Foo", |
|
||||
"description": "There goes my hero", |
|
||||
} |
|
||||
|
|
||||
|
|
||||
def test_read_item_bad_token(): |
|
||||
response = client.get("/items/foo", headers={"X-Token": "hailhydra"}) |
|
||||
assert response.status_code == 400 |
|
||||
assert response.json() == {"detail": "Invalid X-Token header"} |
|
||||
|
|
||||
|
|
||||
def test_read_nonexistent_item(): |
|
||||
response = client.get("/items/baz", headers={"X-Token": "coneofsilence"}) |
|
||||
assert response.status_code == 404 |
|
||||
assert response.json() == {"detail": "Item not found"} |
|
||||
|
|
||||
|
|
||||
def test_create_item(): |
|
||||
response = client.post( |
|
||||
"/items/", |
|
||||
headers={"X-Token": "coneofsilence"}, |
|
||||
json={"id": "foobar", "title": "Foo Bar", "description": "The Foo Barters"}, |
|
||||
) |
|
||||
assert response.status_code == 200 |
|
||||
assert response.json() == { |
|
||||
"id": "foobar", |
|
||||
"title": "Foo Bar", |
|
||||
"description": "The Foo Barters", |
|
||||
} |
|
||||
|
|
||||
|
|
||||
def test_create_item_bad_token(): |
|
||||
response = client.post( |
|
||||
"/items/", |
|
||||
headers={"X-Token": "hailhydra"}, |
|
||||
json={"id": "bazz", "title": "Bazz", "description": "Drop the bazz"}, |
|
||||
) |
|
||||
assert response.status_code == 400 |
|
||||
assert response.json() == {"detail": "Invalid X-Token header"} |
|
||||
|
|
||||
|
|
||||
def test_create_existing_item(): |
|
||||
response = client.post( |
|
||||
"/items/", |
|
||||
headers={"X-Token": "coneofsilence"}, |
|
||||
json={ |
|
||||
"id": "foo", |
|
||||
"title": "The Foo ID Stealers", |
|
||||
"description": "There goes my stealer", |
|
||||
}, |
|
||||
) |
|
||||
assert response.status_code == 409 |
|
||||
assert response.json() == {"detail": "Item already exists"} |
|
||||
@ -1,4 +1,4 @@ |
|||||
from docs_src.app_testing.test_main import client, test_read_main |
from docs_src.app_testing.app_a_py39.test_main import client, test_read_main |
||||
|
|
||||
|
|
||||
def test_main(): |
def test_main(): |
||||
Loading…
Reference in new issue