@@ -215,6 +215,56 @@ def test_ws_connect_err_challenge(loop, ws_key, key_data):
215
215
assert ctx .value .message == 'Invalid challenge response'
216
216
217
217
218
+ @asyncio .coroutine
219
+ def test_ws_connect_common_headers (ws_key , loop , key_data ):
220
+ """Emulate a headers dict being reused for a second ws_connect.
221
+
222
+ In this scenario, we need to ensure that the newly generated secret key
223
+ is sent to the server, not the stale key.
224
+ """
225
+ headers = {}
226
+
227
+ @asyncio .coroutine
228
+ def test_connection ():
229
+ @asyncio .coroutine
230
+ def mock_get (* args , ** kwargs ):
231
+ resp = mock .Mock ()
232
+ resp .status = 101
233
+ key = kwargs .get ('headers' ).get (hdrs .SEC_WEBSOCKET_KEY )
234
+ accept = base64 .b64encode (
235
+ hashlib .sha1 (base64 .b64encode (base64 .b64decode (key )) + WS_KEY )
236
+ .digest ()).decode ()
237
+ resp .headers = {
238
+ hdrs .UPGRADE : hdrs .WEBSOCKET ,
239
+ hdrs .CONNECTION : hdrs .UPGRADE ,
240
+ hdrs .SEC_WEBSOCKET_ACCEPT : accept ,
241
+ hdrs .SEC_WEBSOCKET_PROTOCOL : 'chat'
242
+ }
243
+ return resp
244
+ with mock .patch ('aiohttp.client.os' ) as m_os :
245
+ with mock .patch ('aiohttp.client.ClientSession.get' ,
246
+ side_effect = mock_get ) as m_req :
247
+ m_os .urandom .return_value = key_data
248
+ #m_req.return_value = helpers.create_future(loop)
249
+ #m_req.return_value.set_result(resp)
250
+
251
+ res = yield from aiohttp .ClientSession (loop = loop ).ws_connect (
252
+ 'http://test.org' ,
253
+ protocols = ('t1' , 't2' , 'chat' ),
254
+ headers = headers )
255
+
256
+ assert isinstance (res , ClientWebSocketResponse )
257
+ assert res .protocol == 'chat'
258
+ assert hdrs .ORIGIN not in m_req .call_args [1 ]["headers" ]
259
+
260
+ yield from test_connection ()
261
+ # Generate a new ws key
262
+ key_data = os .urandom (16 )
263
+ ws_key = base64 .b64encode (
264
+ hashlib .sha1 (base64 .b64encode (key_data ) + WS_KEY ).digest ()).decode ()
265
+ yield from test_connection ()
266
+
267
+
218
268
@asyncio .coroutine
219
269
def test_close (loop , ws_key , key_data ):
220
270
resp = mock .Mock ()
0 commit comments