<s> from __future__ import absolute_import , print_function <EOL> import unittest2 as unittest <EOL> from mock import Mock <EOL> from autobahn . twisted . websocket import WebSocketServerFactory <EOL> from autobahn . twisted . websocket import WebSocketServerProtocol <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet . error import ConnectionDone , ConnectionAborted , ConnectionLost <EOL> from autobahn . test import FakeTransport <EOL> class ExceptionHandlingTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . factory = WebSocketServerFactory ( ) <EOL> self . proto = WebSocketServerProtocol ( ) <EOL> self . proto . factory = self . factory <EOL> self . proto . log = Mock ( ) <EOL> def tearDown ( self ) : <EOL> for call in [ <EOL> self . proto . autoPingPendingCall , <EOL> self . proto . autoPingTimeoutCall , <EOL> self . proto . openHandshakeTimeoutCall , <EOL> self . proto . closeHandshakeTimeoutCall , <EOL> ] : <EOL> if call is not None : <EOL> call . cancel ( ) <EOL> def test_connection_done ( self ) : <EOL> self . proto . _connectionMade ( ) <EOL> self . proto . connectionLost ( Failure ( ConnectionDone ( ) ) ) <EOL> messages = '<STR_LIT:U+0020>' . join ( [ str ( x [ <NUM_LIT:1> ] ) for x in self . proto . log . mock_calls ] ) <EOL> self . assertTrue ( '<STR_LIT>' in messages ) <EOL> def test_connection_aborted ( self ) : <EOL> self . proto . _connectionMade ( ) <EOL> self . proto . connectionLost ( Failure ( ConnectionAborted ( ) ) ) <EOL> messages = '<STR_LIT:U+0020>' . join ( [ str ( x [ <NUM_LIT:1> ] ) for x in self . proto . log . mock_calls ] ) <EOL> self . assertTrue ( '<STR_LIT>' in messages ) <EOL> def test_connection_lost ( self ) : <EOL> self . proto . _connectionMade ( ) <EOL> self . proto . connectionLost ( Failure ( ConnectionLost ( ) ) ) <EOL> messages = '<STR_LIT:U+0020>' . join ( [ str ( x [ <NUM_LIT:1> ] ) for x in self . proto . log . mock_calls ] ) <EOL> self . assertTrue ( '<STR_LIT>' in messages ) <EOL> def test_connection_lost_arg ( self ) : <EOL> self . proto . _connectionMade ( ) <EOL> self . proto . connectionLost ( Failure ( ConnectionLost ( "<STR_LIT>" ) ) ) <EOL> messages = '<STR_LIT:U+0020>' . join ( [ str ( x [ <NUM_LIT:1> ] ) + str ( x [ <NUM_LIT:2> ] ) for x in self . proto . log . mock_calls ] ) <EOL> self . assertTrue ( '<STR_LIT>' in messages ) <EOL> self . assertTrue ( '<STR_LIT>' in messages ) <EOL> class Hixie76RejectionTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_handshake_fails ( self ) : <EOL> """<STR_LIT>""" <EOL> t = FakeTransport ( ) <EOL> f = WebSocketServerFactory ( ) <EOL> p = WebSocketServerProtocol ( ) <EOL> p . factory = f <EOL> p . transport = t <EOL> http_request = b"<STR_LIT>" <EOL> p . openHandshakeTimeout = <NUM_LIT:0> <EOL> p . _connectionMade ( ) <EOL> p . data = http_request <EOL> p . processHandshake ( ) <EOL> self . assertIn ( b"<STR_LIT>" , t . _written ) <EOL> self . assertIn ( b"<STR_LIT>" , t . _written ) </s>
