Merge pull request #469 from miguelgrinberg/websocket-support

websocket support
This commit is contained in:
Eli Uriegas
2017-03-05 17:42:49 -08:00
committed by GitHub
11 changed files with 280 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
import asyncio
import inspect
from sanic import Sanic
@@ -236,6 +237,7 @@ def test_bp_static():
def test_bp_shorthand():
app = Sanic('test_shorhand_routes')
blueprint = Blueprint('test_shorhand_routes')
ev = asyncio.Event()
@blueprint.get('/get')
def handler(request):
@@ -265,6 +267,10 @@ def test_bp_shorthand():
def handler(request):
return text('OK')
@blueprint.websocket('/ws')
async def handler(request, ws):
ev.set()
app.blueprint(blueprint)
request, response = app.test_client.get('/get')
@@ -308,3 +314,11 @@ def test_bp_shorthand():
request, response = app.test_client.get('/delete')
assert response.status == 405
request, response = app.test_client.get('/ws', headers={
'Upgrade': 'websocket',
'Connection': 'upgrade',
'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',
'Sec-WebSocket-Version': '13'})
assert response.status == 101
assert ev.is_set()

View File

@@ -1,3 +1,4 @@
import asyncio
import pytest
from sanic import Sanic
@@ -234,6 +235,23 @@ def test_dynamic_route_unhashable():
assert response.status == 404
def test_websocket_route():
app = Sanic('test_websocket_route')
ev = asyncio.Event()
@app.websocket('/ws')
async def handler(request, ws):
ev.set()
request, response = app.test_client.get('/ws', headers={
'Upgrade': 'websocket',
'Connection': 'upgrade',
'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',
'Sec-WebSocket-Version': '13'})
assert response.status == 101
assert ev.is_set()
def test_route_duplicate():
app = Sanic('test_route_duplicate')