sanic/tests/test_custom_protocol.py

22 lines
639 B
Python
Raw Normal View History

from sanic.response import text
from sanic.server import HttpProtocol
class CustomHttpProtocol(HttpProtocol):
def write_response(self, response):
if isinstance(response, str):
response = text(response)
2018-12-30 11:18:06 +00:00
self.transport.write(response.output(self.request.version))
self.transport.close()
2018-08-26 15:43:14 +01:00
def test_use_custom_protocol(app):
2018-12-30 11:18:06 +00:00
@app.route("/1")
2018-08-26 15:43:14 +01:00
async def handler_1(request):
2018-12-30 11:18:06 +00:00
return "OK"
2018-12-30 11:18:06 +00:00
server_kwargs = {"protocol": CustomHttpProtocol}
request, response = app.test_client.get("/1", server_kwargs=server_kwargs)
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "OK"