Skip to content

Commit

Permalink
added headers to ClientSession.ws_connnect aio-libs#785
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskalas committed Jun 2, 2016
1 parent 67c7bbf commit f1410df
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
19 changes: 15 additions & 4 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def ws_connect(self, url, *,
autoclose=True,
autoping=True,
auth=None,
origin=None):
origin=None,
headers=None):
"""Initiate websocket connection."""
return _WSRequestContextManager(
self._ws_connect(url,
Expand All @@ -259,7 +260,8 @@ def ws_connect(self, url, *,
autoclose=autoclose,
autoping=autoping,
auth=auth,
origin=origin))
origin=origin,
headers=headers))

@asyncio.coroutine
def _ws_connect(self, url, *,
Expand All @@ -268,16 +270,25 @@ def _ws_connect(self, url, *,
autoclose=True,
autoping=True,
auth=None,
origin=None):
origin=None,
headers=None):

sec_key = base64.b64encode(os.urandom(16))

headers = {
if headers is None:
headers = {}

default_headers = {
hdrs.UPGRADE: hdrs.WEBSOCKET,
hdrs.CONNECTION: hdrs.UPGRADE,
hdrs.SEC_WEBSOCKET_VERSION: '13',
hdrs.SEC_WEBSOCKET_KEY: sec_key.decode(),
}

for key, value in default_headers.items():
if key not in headers:
headers[key] = value

if protocols:
headers[hdrs.SEC_WEBSOCKET_PROTOCOL] = ','.join(protocols)
if origin is not None:
Expand Down
44 changes: 43 additions & 1 deletion tests/test_websocket_client_functional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import aiohttp
import asyncio
import pytest
from aiohttp import web
from aiohttp import web, hdrs


@pytest.mark.run_loop
Expand Down Expand Up @@ -283,3 +283,45 @@ def handler(request):
yield from asyncio.sleep(0.1, loop=loop)
assert resp.closed
assert resp.exception() is None


@pytest.mark.run_loop
def test_override_default_headers(create_app_and_client, loop):

@asyncio.coroutine
def handler(request):
assert request.headers[hdrs.SEC_WEBSOCKET_VERSION] == '8'
ws = web.WebSocketResponse()
yield from ws.prepare(request)

ws.send_str('answer')
yield from ws.close()
return ws

app, client = yield from create_app_and_client()
app.router.add_route('GET', '/', handler)
resp = yield from client.ws_connect('/', headers={hdrs.SEC_WEBSOCKET_VERSION: '8'})
msg = yield from resp.receive()
assert msg.data == 'answer'
yield from resp.close()


@pytest.mark.run_loop
def test_additional_headers(create_app_and_client, loop):

@asyncio.coroutine
def handler(request):
assert request.headers['x-hdr'] == 'xtra'
ws = web.WebSocketResponse()
yield from ws.prepare(request)

ws.send_str('answer')
yield from ws.close()
return ws

app, client = yield from create_app_and_client()
app.router.add_route('GET', '/', handler)
resp = yield from client.ws_connect('/', headers={'x-hdr': 'xtra'})
msg = yield from resp.receive()
assert msg.data == 'answer'
yield from resp.close()

0 comments on commit f1410df

Please sign in to comment.