-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add render and render_str method for Template
- Loading branch information
1 parent
11d7dbf
commit 3d20183
Showing
7 changed files
with
204 additions
and
14 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,57 @@ | ||
from typing import Annotated | ||
|
||
from selva.di import Inject | ||
from selva.web import controller, get | ||
from selva.web.templates import Template | ||
|
||
|
||
@controller | ||
class Controller: | ||
template: Annotated[Template, Inject] | ||
|
||
@get("/render") | ||
async def render(self, request): | ||
await self.template.respond( | ||
request.response, | ||
"template.html", | ||
{"variable": "Jinja"}, | ||
) | ||
|
||
@get("/stream") | ||
async def stream(self, request): | ||
await self.template.respond( | ||
request.response, | ||
"template.html", | ||
{"variable": "Jinja"}, | ||
stream=True, | ||
) | ||
|
||
@get("/define_content_type") | ||
async def define_content_type(self, request): | ||
await self.template.respond( | ||
request.response, | ||
"template.html", | ||
{"variable": "Jinja"}, | ||
content_type="text/defined", | ||
) | ||
|
||
@get("/override_content_type") | ||
async def override_content_type(self, request): | ||
request.response.content_type = "text/plain" | ||
|
||
await self.template.respond( | ||
request.response, | ||
"template.html", | ||
{"variable": "Jinja"}, | ||
content_type="text/overriden", | ||
) | ||
|
||
@get("/content_type_from_response") | ||
async def content_type_from_response(self, request): | ||
request.response.content_type = "text/from_response" | ||
|
||
await self.template.respond( | ||
request.response, | ||
"template.html", | ||
{"variable": "Jinja"}, | ||
) |
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 @@ | ||
{{ variable }} |
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,23 @@ | ||
from pathlib import Path | ||
|
||
from selva.configuration.defaults import default_settings | ||
from selva.configuration.settings import Settings | ||
from selva.web.templates.jinja import JinjaTemplate | ||
|
||
|
||
async def test_render_template(): | ||
path = str(Path(__file__).parent.absolute()) | ||
settings = Settings(default_settings | {"templates": {"jinja": {"path": path}}}) | ||
|
||
template = JinjaTemplate(settings) | ||
template.initialize() | ||
result = await template.render("template.html", {"variable": "Jinja"}) | ||
assert result == "Jinja" | ||
|
||
|
||
async def test_render_str(): | ||
settings = Settings(default_settings) | ||
template = JinjaTemplate(settings) | ||
template.initialize() | ||
result = await template.render_str("{{ variable }}", {"variable": "Jinja"}) | ||
assert result == "Jinja" |
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,74 @@ | ||
from pathlib import Path | ||
|
||
from httpx import AsyncClient | ||
|
||
from selva.configuration.defaults import default_settings | ||
from selva.configuration.settings import Settings | ||
from selva.web.application import Selva | ||
|
||
path = str(Path(__file__).parent.absolute()) | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": "tests.web.templates.application", | ||
"templates": {"jinja": {"path": path}}, | ||
} | ||
) | ||
|
||
|
||
async def test_render(): | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(app=app) | ||
response = await client.get("http://localhost:8000/render") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "Jinja" | ||
assert response.headers["Content-Length"] == str(len("Jinja")) | ||
assert "text/html" in response.headers["Content-Type"] | ||
|
||
|
||
async def test_stream(): | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(app=app) | ||
response = await client.get("http://localhost:8000/stream") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "Jinja" | ||
assert "Content-Length" not in response.headers | ||
|
||
|
||
async def test_define_content_type(): | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(app=app) | ||
response = await client.get("http://localhost:8000/define_content_type") | ||
|
||
assert response.status_code == 200 | ||
assert "text/defined" in response.headers["Content-Type"] | ||
|
||
|
||
async def test_override_content_type(): | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(app=app) | ||
response = await client.get("http://localhost:8000/override_content_type") | ||
|
||
assert response.status_code == 200 | ||
assert "text/overriden" in response.headers["Content-Type"] | ||
|
||
|
||
async def test_content_type_from_response(): | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(app=app) | ||
response = await client.get("http://localhost:8000/content_type_from_response") | ||
|
||
assert response.status_code == 200 | ||
assert "text/from_response" in response.headers["Content-Type"] |