2016-12-26 14:41:10 +00:00
|
|
|
from sanic.server import HttpProtocol
|
|
|
|
from sanic.response import text
|
|
|
|
|
|
|
|
|
|
|
|
class CustomHttpProtocol(HttpProtocol):
|
|
|
|
|
|
|
|
def write_response(self, response):
|
|
|
|
if isinstance(response, str):
|
|
|
|
response = text(response)
|
|
|
|
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):
|
2016-12-26 14:41:10 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
@app.route('/1')
|
|
|
|
async def handler_1(request):
|
|
|
|
return 'OK'
|
2016-12-26 14:41:10 +00:00
|
|
|
|
|
|
|
server_kwargs = {
|
|
|
|
'protocol': CustomHttpProtocol
|
|
|
|
}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get(
|
|
|
|
'/1', server_kwargs=server_kwargs)
|
2016-12-26 14:41:10 +00:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|