-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- fix `data` and `payload` special keyword arguments so they are allowed when another method than one without a body (HEAD, GET) is available,
- Loading branch information
Showing
4 changed files
with
92 additions
and
5 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,24 @@ | ||
from pydantic import BaseModel | ||
|
||
from esmerald import Esmerald, Form, Request | ||
from esmerald.routing.gateways import Gateway | ||
from esmerald.routing.handlers import route | ||
from esmerald.testclient import EsmeraldTestClient | ||
|
||
|
||
class Model(BaseModel): | ||
id: str | ||
|
||
|
||
def test_get_and_post(): | ||
@route(methods=["GET", "POST"]) | ||
async def start(request: Request, form: Model | None = Form()) -> bytes: | ||
return b"hello world" | ||
|
||
app = Esmerald( | ||
debug=True, | ||
routes=[Gateway("/", handler=start)], | ||
) | ||
client = EsmeraldTestClient(app) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import contextlib | ||
import uuid | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
import pytest | ||
from lilya.responses import JSONResponse, PlainText, Response as LilyaResponse | ||
|
@@ -10,13 +11,14 @@ | |
|
||
from esmerald.applications import Esmerald | ||
from esmerald.enums import MediaType | ||
from esmerald.exceptions import ImproperlyConfigured | ||
from esmerald.permissions import AllowAny, DenyAll | ||
from esmerald.requests import Request | ||
from esmerald.responses import Response | ||
from esmerald.responses.encoders import UJSONResponse | ||
from esmerald.routing.apis.views import APIView | ||
from esmerald.routing.gateways import Gateway, WebSocketGateway | ||
from esmerald.routing.handlers import get, post, put, websocket | ||
from esmerald.routing.handlers import get, post, put, route, websocket | ||
from esmerald.routing.router import Include, Router | ||
from esmerald.testclient import create_client | ||
|
||
|
@@ -1020,3 +1022,54 @@ def another_user(data: UserOut) -> UserOut: | |
|
||
assert response.status_code == 200 | ||
assert response.json() == {"name": "test", "email": "[email protected]"} | ||
|
||
|
||
def test_get_and_post_data(test_app_client_factory): | ||
@route(path="/another-user", status_code=200, methods=["GET", "POST"]) | ||
def another_user(data: Optional[UserOut]) -> Optional[UserOut]: | ||
return data | ||
|
||
data = {"name": "test", "email": "[email protected]"} | ||
app = Esmerald(routes=[Gateway(handler=another_user)]) | ||
client = test_app_client_factory(app) | ||
response = client.get("/another-user") | ||
assert response.status_code == 200 | ||
assert response.text == "" | ||
response = client.post("/another-user", json=data) | ||
|
||
assert response.status_code == 200 | ||
assert response.json() == {"name": "test", "email": "[email protected]"} | ||
|
||
|
||
def test_get_and_post_payload(test_app_client_factory): | ||
@route(path="/another-user", status_code=200, methods=["GET", "POST"]) | ||
def another_user(payload: Optional[UserOut]) -> Optional[UserOut]: | ||
return payload | ||
|
||
data = {"name": "test", "email": "[email protected]"} | ||
app = Esmerald(routes=[Gateway(handler=another_user)]) | ||
|
||
client = test_app_client_factory(app) | ||
response = client.get("/another-user") | ||
assert response.status_code == 200 | ||
assert response.text == "" | ||
response = client.post("/another-user", json=data) | ||
|
||
assert response.status_code == 200 | ||
assert response.json() == {"name": "test", "email": "[email protected]"} | ||
|
||
|
||
def test_get_and_head_data(): | ||
with pytest.raises(ImproperlyConfigured): | ||
|
||
@route(path="/another-user", status_code=200, methods=["GET", "HEAD"]) | ||
def another_user(data: UserOut) -> UserOut: | ||
return data | ||
|
||
|
||
def test_get_and_head_payload(): | ||
with pytest.raises(ImproperlyConfigured): | ||
|
||
@route(path="/another-user", status_code=200, methods=["GET", "HEAD"]) | ||
def another_user(payload: UserOut) -> UserOut: | ||
return payload |