From 3967faf44e9920391d625f7d52c78106784101be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Sun, 15 Dec 2019 18:40:25 +0200 Subject: [PATCH] Add html test for __html__ and _repr_html_. --- tests/test_requests.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index 2f83513c..f24fdcb5 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -11,7 +11,7 @@ import pytest from sanic import Blueprint, Sanic from sanic.exceptions import ServerError from sanic.request import DEFAULT_HTTP_CONTENT_TYPE, RequestParameters -from sanic.response import json, text +from sanic.response import html, json, text from sanic.testing import ASGI_HOST, HOST, PORT @@ -72,6 +72,41 @@ def test_text(app): assert response.text == "Hello" +def test_html(app): + class Foo: + def __html__(self): + return "

Foo

" + + def _repr_html_(self): + return "

Foo object repr

" + + class Bar: + def _repr_html_(self): + return "

Bar object repr

" + + @app.route("/") + async def handler(request): + return html("

Hello

") + + @app.route("/foo") + async def handler(request): + return html(Foo()) + + @app.route("/bar") + async def handler(request): + return html(Bar()) + + request, response = app.test_client.get("/") + assert response.content_type == "text/html; charset=utf-8" + assert response.text == "

Hello

" + + request, response = app.test_client.get("/foo") + assert response.text == "

Foo

" + + request, response = app.test_client.get("/bar") + assert response.text == "

Bar object repr

" + + @pytest.mark.asyncio async def test_text_asgi(app): @app.route("/")