Skip to content

Commit

Permalink
add testcases (#478)
Browse files Browse the repository at this point in the history
* add failing testcases

* bump lilya to the unreleased 0.12.3 version which should fix the post issue

* Changes:

- fix tests
- add warning/explaination
  • Loading branch information
devkral authored Jan 24, 2025
1 parent 1d123c6 commit 7edc25a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
7 changes: 7 additions & 0 deletions docs/en/docs/extras/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ dataclasses.
{!> ../../../docs_src/extras/form/model.py !}
```

## Using form as non-data, non-payload field

When using Form as a non-data, non-payload field, the form receives only the data which matches the field-name.

If you use form-data requests make sure the field content is valid json.
When you use json requests only the portion which matches the field-name is passed through the model wrapped by the form.

## Notes

As you could see from the examples, it is very simple and direct to use the `Form` in Esmerald and
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies = [
"email-validator >=2.2.0,<3.0.0",
"itsdangerous>=2.1.2,<3.0.0",
"jinja2>=3.1.2,<4.0.0",
"lilya>=0.12.0",
"lilya>=0.12.3",
"loguru>=0.7.0,<0.8.0",
"pydantic>=2.10,<3.0.0",
"pydantic-settings>=2.0.0,<3.0.0",
Expand Down
6 changes: 6 additions & 0 deletions tests/forms/test_forms_route.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Optional, Union

import pytest
Expand All @@ -17,6 +18,8 @@ class Model(BaseModel):
def test_get_and_post():
@route(methods=["GET", "POST"])
async def start(request: Request, form: Union[Model, None] = Form()) -> bytes:
if request.method == "POST":
assert form.id == "733"
return b"hello world"

app = Esmerald(
Expand All @@ -27,6 +30,9 @@ async def start(request: Request, form: Union[Model, None] = Form()) -> bytes:
response = client.get("/")
assert response.status_code == 200

response = client.post("/", data={"form": json.dumps({"id": "733"})})
assert response.status_code == 200


def test_get_and_post_optional():
@route(methods=["GET", "POST"])
Expand Down
65 changes: 52 additions & 13 deletions tests/test_direct_responses.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import os
import json
from typing import Optional

from esmerald import Esmerald, Redirect, Request, Template
from pydantic import BaseModel

from esmerald import Esmerald, Form, Redirect, Request, Template
from esmerald.config.template import TemplateConfig
from esmerald.responses.base import RedirectResponse
from esmerald.routing.gateways import Gateway
from esmerald.routing.handlers import get
from esmerald.routing.handlers import get, route
from esmerald.testclient import EsmeraldTestClient


def test_return_response_container(template_dir):
path = os.path.join(template_dir, "start.html")
with open(path, "w") as file:
file.write("<html>Hello, <a href='{{ url_for('homepage') }}'>world</a></html>")
class Model(BaseModel):
id: int


def test_return_response_container(template_dir):
@get()
async def start(request: Request) -> Template:
return Redirect(path="/home", status_code=301)

app = Esmerald(
debug=True,
routes=[Gateway("/", handler=start)],
template_config=TemplateConfig(
directory=template_dir,
Expand All @@ -31,16 +33,11 @@ async def start(request: Request) -> Template:


def test_return_response(template_dir):
path = os.path.join(template_dir, "start.html")
with open(path, "w") as file:
file.write("<html>Hello, <a href='{{ url_for('homepage') }}'>world</a></html>")

@get()
async def start(request: Request) -> Template:
return RedirectResponse(url="/home", status_code=301)

app = Esmerald(
debug=True,
routes=[Gateway("/", handler=start)],
template_config=TemplateConfig(
directory=template_dir,
Expand All @@ -49,3 +46,45 @@ async def start(request: Request) -> Template:
client = EsmeraldTestClient(app)
response = client.get("/", follow_redirects=False)
assert response.status_code == 301


def test_return_response_route_form(template_dir):
@route(methods=["GET", "POST"])
async def start(request: Request, form: Optional[Model] = Form()) -> Template:
if request.method == "POST":
assert form.id == 55
return RedirectResponse(url="/home", status_code=301)

app = Esmerald(
routes=[Gateway("/", handler=start)],
template_config=TemplateConfig(
directory=template_dir,
),
)
client = EsmeraldTestClient(app)
response = client.get("/", follow_redirects=False)
assert response.status_code == 301

response = client.post("/", data={"form": json.dumps({"id": 55})}, follow_redirects=False)
assert response.status_code == 301


def test_return_response_route_data(template_dir):
@route(methods=["GET", "POST"])
async def start(request: Request, data: Optional[Model] = Form()) -> Template:
if request.method == "POST":
assert data.id == 55
return RedirectResponse(url="/home", status_code=301)

app = Esmerald(
routes=[Gateway("/", handler=start)],
template_config=TemplateConfig(
directory=template_dir,
),
)
client = EsmeraldTestClient(app)
response = client.get("/", follow_redirects=False)
assert response.status_code == 301

response = client.post("/", data={"id": 55}, follow_redirects=False)
assert response.status_code == 301

0 comments on commit 7edc25a

Please sign in to comment.