2016-12-26 23:41:10 +09:00
|
|
|
from sanic.response import text
|
2019-04-24 00:44:42 +03:00
|
|
|
from sanic.server import HttpProtocol
|
2016-12-26 23:41:10 +09:00
|
|
|
|
|
|
|
|
|
|
|
class CustomHttpProtocol(HttpProtocol):
|
|
|
|
def write_response(self, response):
|
|
|
|
if isinstance(response, str):
|
|
|
|
response = text(response)
|
2018-12-30 13:18:06 +02:00
|
|
|
self.transport.write(response.output(self.request.version))
|
2016-12-26 23:41:10 +09:00
|
|
|
self.transport.close()
|
|
|
|
|
|
|
|
|
2018-08-26 16:43:14 +02:00
|
|
|
def test_use_custom_protocol(app):
|
2018-12-30 13:18:06 +02:00
|
|
|
@app.route("/1")
|
2018-08-26 16:43:14 +02:00
|
|
|
async def handler_1(request):
|
2018-12-30 13:18:06 +02:00
|
|
|
return "OK"
|
2016-12-26 23:41:10 +09:00
|
|
|
|
2018-12-30 13:18:06 +02:00
|
|
|
server_kwargs = {"protocol": CustomHttpProtocol}
|
|
|
|
request, response = app.test_client.get("/1", server_kwargs=server_kwargs)
|
2016-12-26 23:41:10 +09:00
|
|
|
assert response.status == 200
|
2018-12-30 13:18:06 +02:00
|
|
|
assert response.text == "OK"
|