diff options
| author | Sergey Shepelev <temotor@gmail.com> | 2014-07-17 14:26:18 +0400 |
|---|---|---|
| committer | Sergey Shepelev <temotor@gmail.com> | 2014-08-26 04:08:07 +0400 |
| commit | 0fa4988c48fe7db9450254bbe9b109775a03a096 (patch) | |
| tree | adae606a737addc1a54fb6094ae450751bdc23a4 | |
| parent | e18b30b119e646eba6a75f3e19ab44673429beb0 (diff) | |
| download | eventlet-pep8.tar.gz | |
PEP-8 fixes; six.next for Python3 compatibilitypep8
| -rw-r--r-- | eventlet/support/greendns.py | 56 | ||||
| -rw-r--r-- | tests/queue_test.py | 46 | ||||
| -rw-r--r-- | tests/timeout_test.py | 42 | ||||
| -rw-r--r-- | tests/timeout_test_with_statement.py | 40 | ||||
| -rw-r--r-- | tests/websocket_new_test.py | 31 | ||||
| -rw-r--r-- | tests/websocket_test.py | 59 |
6 files changed, 149 insertions, 125 deletions
diff --git a/eventlet/support/greendns.py b/eventlet/support/greendns.py index 804b552..7e798b3 100644 --- a/eventlet/support/greendns.py +++ b/eventlet/support/greendns.py @@ -34,6 +34,8 @@ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import struct +import sys from eventlet import patcher from eventlet.green import _socket_nodns @@ -55,13 +57,17 @@ socket = _socket_nodns DNS_QUERY_TIMEOUT = 10.0 + # # Resolver instance used to perfrom DNS lookups. # class FakeAnswer(list): - expiration = 0 + expiration = 0 + + class FakeRecord(object): - pass + pass + class ResolverProxy(object): def __init__(self, *args, **kwargs): @@ -94,7 +100,7 @@ class ResolverProxy(object): def query(self, *args, **kwargs): if self._resolver is None: - self._resolver = dns.resolver.Resolver(filename = self._filename) + self._resolver = dns.resolver.Resolver(filename=self._filename) self._resolver.cache = dns.resolver.Cache() query = args[0] @@ -111,7 +117,8 @@ class ResolverProxy(object): # # cache # -resolver = ResolverProxy(dev=True) +resolver = ResolverProxy(dev=True) + def resolve(name): error = None @@ -120,9 +127,9 @@ def resolve(name): if rrset is None or time.time() > rrset.expiration: try: rrset = resolver.query(name) - except dns.exception.Timeout as e: + except dns.exception.Timeout: error = (socket.EAI_AGAIN, 'Lookup timed out') - except dns.exception.DNSException as e: + except dns.exception.DNSException: error = (socket.EAI_NODATA, 'No address associated with hostname') else: pass @@ -134,6 +141,8 @@ def resolve(name): else: sys.stderr.write('DNS error: %r %r\n' % (name, error)) return rrset + + # # methods # @@ -147,9 +156,9 @@ def getaliases(host): try: answers = dns.resolver.query(host, 'cname') - except dns.exception.Timeout as e: + except dns.exception.Timeout: error = (socket.EAI_AGAIN, 'Lookup timed out') - except dns.exception.DNSException as e: + except dns.exception.DNSException: error = (socket.EAI_NODATA, 'No address associated with hostname') else: for record in answers: @@ -160,6 +169,7 @@ def getaliases(host): return cnames + def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0): """Replacement for Python's socket.getaddrinfo. @@ -178,6 +188,7 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0): value.append((socket.AF_INET, socktype, proto, '', (rr.address, port))) return value + def gethostbyname(hostname): """Replacement for Python's socket.gethostbyname. @@ -189,6 +200,7 @@ def gethostbyname(hostname): rrset = resolve(hostname) return rrset[0].address + def gethostbyname_ex(hostname): """Replacement for Python's socket.gethostbyname_ex. @@ -204,6 +216,7 @@ def gethostbyname_ex(hostname): addrs.append(rr.address) return (hostname, [], addrs) + def getnameinfo(sockaddr, flags): """Replacement for Python's socket.getnameinfo. @@ -227,15 +240,15 @@ def getnameinfo(sockaddr, flags): if is_ipv4_addr(host): try: - rrset = resolver.query( + rrset = resolver.query( dns.reversename.from_address(host), dns.rdatatype.PTR) if len(rrset) > 1: raise socket.error('sockaddr resolved to multiple addresses') host = rrset[0].target.to_text(omit_final_dot=True) - except dns.exception.Timeout as e: + except dns.exception.Timeout: if flags & socket.NI_NAMEREQD: raise socket.gaierror((socket.EAI_AGAIN, 'Lookup timed out')) - except dns.exception.DNSException as e: + except dns.exception.DNSException: if flags & socket.NI_NAMEREQD: raise socket.gaierror( (socket.EAI_NONAME, 'Name or service not known')) @@ -246,9 +259,9 @@ def getnameinfo(sockaddr, flags): raise socket.error('sockaddr resolved to multiple addresses') if flags & socket.NI_NUMERICHOST: host = rrset[0].address - except dns.exception.Timeout as e: + except dns.exception.Timeout: raise socket.gaierror((socket.EAI_AGAIN, 'Lookup timed out')) - except dns.exception.DNSException as e: + except dns.exception.DNSException: raise socket.gaierror( (socket.EAI_NODATA, 'No address associated with hostname')) @@ -272,6 +285,7 @@ def is_ipv4_addr(host): return True return False + def _net_read(sock, count, expiration): """coro friendly replacement for dns.query._net_write Read the specified number of bytes from sock. Keep trying until we @@ -293,6 +307,7 @@ def _net_read(sock, count, expiration): s = s + n return s + def _net_write(sock, data, expiration): """coro friendly replacement for dns.query._net_write Write the specified data to the socket. @@ -309,9 +324,9 @@ def _net_write(sock, data, expiration): if expiration - time.time() <= 0.0: raise dns.exception.Timeout -def udp( - q, where, timeout=DNS_QUERY_TIMEOUT, port=53, af=None, source=None, - source_port=0, ignore_unexpected=False): + +def udp(q, where, timeout=DNS_QUERY_TIMEOUT, port=53, af=None, source=None, + source_port=0, ignore_unexpected=False): """coro friendly replacement for dns.query.udp Return the response obtained after sending a query via UDP. @@ -377,7 +392,7 @@ def udp( if not ignore_unexpected: raise dns.query.UnexpectedSource( 'got a response from %s instead of %s' - % (from_address, destination)) + % (from_address, destination)) finally: s.close() @@ -386,8 +401,9 @@ def udp( raise dns.query.BadResponse() return r + def tcp(q, where, timeout=DNS_QUERY_TIMEOUT, port=53, - af=None, source=None, source_port=0): + af=None, source=None, source_port=0): """coro friendly replacement for dns.query.tcp Return the response obtained after sending a query via TCP. @@ -454,10 +470,10 @@ def tcp(q, where, timeout=DNS_QUERY_TIMEOUT, port=53, raise dns.query.BadResponse() return r + def reset(): - resolver.clear() + resolver.clear() # Install our coro-friendly replacements for the tcp and udp query methods. dns.query.tcp = tcp dns.query.udp = udp - diff --git a/tests/queue_test.py b/tests/queue_test.py index e2b4e1a..27cf988 100644 --- a/tests/queue_test.py +++ b/tests/queue_test.py @@ -1,6 +1,7 @@ -from tests import LimitedTestCase, main import eventlet -from eventlet import event +from eventlet import event, hubs, queue +from tests import LimitedTestCase, main + def do_bail(q): eventlet.Timeout(0, RuntimeError()) @@ -10,6 +11,7 @@ def do_bail(q): except RuntimeError: return 'timed out' + class TestQueue(LimitedTestCase): def test_send_first(self): q = eventlet.Queue() @@ -18,6 +20,7 @@ class TestQueue(LimitedTestCase): def test_send_last(self): q = eventlet.Queue() + def waiter(q): self.assertEqual(q.get(), 'hi2') @@ -51,6 +54,7 @@ class TestQueue(LimitedTestCase): def test_zero_max_size(self): q = eventlet.Queue(0) + def sender(evt, q): q.put('hi') evt.send('done') @@ -64,12 +68,13 @@ class TestQueue(LimitedTestCase): eventlet.sleep(0) assert not evt.ready() gt2 = eventlet.spawn(receiver, q) - self.assertEqual(gt2.wait(),'hi') - self.assertEqual(evt.wait(),'done') + self.assertEqual(gt2.wait(), 'hi') + self.assertEqual(evt.wait(), 'done') gt.wait() def test_resize_up(self): q = eventlet.Queue(0) + def sender(evt, q): q.put('hi') evt.send('done') @@ -84,7 +89,6 @@ class TestQueue(LimitedTestCase): gt.wait() def test_resize_down(self): - size = 5 q = eventlet.Queue(5) for i in range(5): @@ -97,6 +101,7 @@ class TestQueue(LimitedTestCase): def test_resize_to_Unlimited(self): q = eventlet.Queue(0) + def sender(evt, q): q.put('hi') evt.send('done') @@ -115,10 +120,9 @@ class TestQueue(LimitedTestCase): q = eventlet.Queue() sendings = ['1', '2', '3', '4'] - gts = [eventlet.spawn(q.get) - for x in sendings] + gts = [eventlet.spawn(q.get) for x in sendings] - eventlet.sleep(0.01) # get 'em all waiting + eventlet.sleep(0.01) # get 'em all waiting q.put(sendings[0]) q.put(sendings[1]) @@ -180,11 +184,12 @@ class TestQueue(LimitedTestCase): def test_channel_send(self): channel = eventlet.Queue(0) events = [] + def another_greenlet(): events.append(channel.get()) events.append(channel.get()) - gt = eventlet.spawn(another_greenlet) + eventlet.spawn(another_greenlet) events.append('sending') channel.put('hello') @@ -194,7 +199,6 @@ class TestQueue(LimitedTestCase): self.assertEqual(['sending', 'hello', 'sent hello', 'world', 'sent world'], events) - def test_channel_wait(self): channel = eventlet.Queue(0) events = [] @@ -206,7 +210,7 @@ class TestQueue(LimitedTestCase): channel.put('world') events.append('sent world') - gt = eventlet.spawn(another_greenlet) + eventlet.spawn(another_greenlet) events.append('waiting') events.append(channel.get()) @@ -233,16 +237,14 @@ class TestQueue(LimitedTestCase): self.assertEqual(c.getting(), 0) # NOTE: we don't guarantee that waiters are served in order results = sorted([w1.wait(), w2.wait(), w3.wait()]) - self.assertEqual(results, [1,2,3]) + self.assertEqual(results, [1, 2, 3]) def test_channel_sender_timing_out(self): - from eventlet import queue c = eventlet.Queue(0) self.assertRaises(queue.Full, c.put, "hi", timeout=0.001) self.assertRaises(queue.Empty, c.get_nowait) def test_task_done(self): - from eventlet import queue, debug channel = queue.Queue(0) X = object() gt = eventlet.spawn(channel.put, X) @@ -267,7 +269,6 @@ def store_result(result, func, *args): class TestNoWait(LimitedTestCase): def test_put_nowait_simple(self): - from eventlet import hubs,queue hub = hubs.get_hub() result = [] q = eventlet.Queue(1) @@ -275,12 +276,11 @@ class TestNoWait(LimitedTestCase): hub.schedule_call_global(0, store_result, result, q.put_nowait, 3) eventlet.sleep(0) eventlet.sleep(0) - assert len(result)==2, result - assert result[0]==None, result + assert len(result) == 2, result + assert result[0] is None, result assert isinstance(result[1], queue.Full), result def test_get_nowait_simple(self): - from eventlet import hubs,queue hub = hubs.get_hub() result = [] q = queue.Queue(1) @@ -288,13 +288,12 @@ class TestNoWait(LimitedTestCase): hub.schedule_call_global(0, store_result, result, q.get_nowait) hub.schedule_call_global(0, store_result, result, q.get_nowait) eventlet.sleep(0) - assert len(result)==2, result - assert result[0]==4, result + assert len(result) == 2, result + assert result[0] == 4, result assert isinstance(result[1], queue.Empty), result # get_nowait must work from the mainloop def test_get_nowait_unlock(self): - from eventlet import hubs,queue hub = hubs.get_hub() result = [] q = queue.Queue(0) @@ -316,11 +315,10 @@ class TestNoWait(LimitedTestCase): # put_nowait must work from the mainloop def test_put_nowait_unlock(self): - from eventlet import hubs,queue hub = hubs.get_hub() result = [] q = queue.Queue(0) - p = eventlet.spawn(q.get) + eventlet.spawn(q.get) assert q.empty(), q assert q.full(), q eventlet.sleep(0) @@ -337,5 +335,5 @@ class TestNoWait(LimitedTestCase): assert q.empty(), q -if __name__=='__main__': +if __name__ == '__main__': main() diff --git a/tests/timeout_test.py b/tests/timeout_test.py index d58bf5a..46c4346 100644 --- a/tests/timeout_test.py +++ b/tests/timeout_test.py @@ -1,53 +1,55 @@ +import eventlet + from tests import LimitedTestCase -from eventlet import timeout -from eventlet import greenthread + + DELAY = 0.01 + class TestDirectRaise(LimitedTestCase): def test_direct_raise_class(self): try: - raise timeout.Timeout - except timeout.Timeout as t: + raise eventlet.Timeout + except eventlet.Timeout as t: assert not t.pending, repr(t) def test_direct_raise_instance(self): - tm = timeout.Timeout() + tm = eventlet.Timeout() try: raise tm - except timeout.Timeout as t: + except eventlet.Timeout as t: assert tm is t, (tm, t) assert not t.pending, repr(t) def test_repr(self): # just verify these don't crash - tm = timeout.Timeout(1) - greenthread.sleep(0) + tm = eventlet.Timeout(1) + eventlet.sleep(0) repr(tm) str(tm) tm.cancel() - tm = timeout.Timeout(None, RuntimeError) + tm = eventlet.Timeout(None, RuntimeError) repr(tm) str(tm) - tm = timeout.Timeout(None, False) + tm = eventlet.Timeout(None, False) repr(tm) str(tm) + class TestWithTimeout(LimitedTestCase): def test_with_timeout(self): - self.assertRaises(timeout.Timeout, timeout.with_timeout, DELAY, greenthread.sleep, DELAY*10) + self.assertRaises(eventlet.Timeout, eventlet.with_timeout, DELAY, eventlet.sleep, DELAY*10) X = object() - r = timeout.with_timeout(DELAY, greenthread.sleep, DELAY*10, timeout_value=X) + r = eventlet.with_timeout(DELAY, eventlet.sleep, DELAY * 10, timeout_value=X) assert r is X, (r, X) - r = timeout.with_timeout(DELAY*10, greenthread.sleep, - DELAY, timeout_value=X) + r = eventlet.with_timeout(DELAY * 10, eventlet.sleep, DELAY, timeout_value=X) assert r is None, r - def test_with_outer_timer(self): def longer_timeout(): # this should not catch the outer timeout's exception - return timeout.with_timeout(DELAY * 10, - greenthread.sleep, DELAY * 20, - timeout_value='b') - self.assertRaises(timeout.Timeout, - timeout.with_timeout, DELAY, longer_timeout) + return eventlet.with_timeout(DELAY * 10, eventlet.sleep, DELAY * 20, timeout_value='b') + self.assertRaises( + eventlet.Timeout, + eventlet.with_timeout, + DELAY, longer_timeout) diff --git a/tests/timeout_test_with_statement.py b/tests/timeout_test_with_statement.py index 9b6e529..b764780 100644 --- a/tests/timeout_test_with_statement.py +++ b/tests/timeout_test_with_statement.py @@ -3,7 +3,6 @@ import gc import sys import time -import unittest import weakref from eventlet import sleep @@ -21,7 +20,7 @@ class Error(Exception): class Test(LimitedTestCase): def test_cancellation(self): # Nothing happens if with-block finishes before the timeout expires - t = Timeout(DELAY*2) + t = Timeout(DELAY * 2) sleep(0) # make it pending assert t.pending, repr(t) with t: @@ -29,13 +28,13 @@ class Test(LimitedTestCase): sleep(DELAY) # check if timer was actually cancelled assert not t.pending, repr(t) - sleep(DELAY*2) + sleep(DELAY * 2) def test_raising_self(self): # An exception will be raised if it's not try: with Timeout(DELAY) as t: - sleep(DELAY*2) + sleep(DELAY * 2) except Timeout as ex: assert ex is t, (ex, t) else: @@ -45,7 +44,7 @@ class Test(LimitedTestCase): # specifying True as the exception raises self as well try: with Timeout(DELAY, True) as t: - sleep(DELAY*2) + sleep(DELAY * 2) except Timeout as ex: assert ex is t, (ex, t) else: @@ -55,25 +54,25 @@ class Test(LimitedTestCase): # You can customize the exception raised: try: with Timeout(DELAY, IOError("Operation takes way too long")): - sleep(DELAY*2) + sleep(DELAY * 2) except IOError as ex: - assert str(ex)=="Operation takes way too long", repr(ex) + assert str(ex) == "Operation takes way too long", repr(ex) def test_raising_exception_class(self): # Providing classes instead of values should be possible too: try: with Timeout(DELAY, ValueError): - sleep(DELAY*2) + sleep(DELAY * 2) except ValueError: pass def test_raising_exc_tuple(self): try: - 1//0 + 1 // 0 except: try: with Timeout(DELAY, sys.exc_info()[0]): - sleep(DELAY*2) + sleep(DELAY * 2) raise AssertionError('should not get there') raise AssertionError('should not get there') except ZeroDivisionError: @@ -90,13 +89,12 @@ class Test(LimitedTestCase): def test_silent_block(self): # To silence the exception before exiting the block, pass # False as second parameter. - XDELAY=0.1 + XDELAY = 0.1 start = time.time() with Timeout(XDELAY, False): - sleep(XDELAY*2) + sleep(XDELAY * 2) delta = (time.time()-start) - assert delta<XDELAY*2, delta - + assert delta < XDELAY * 2, delta def test_dummy_timer(self): # passing None as seconds disables the timer @@ -107,7 +105,7 @@ class Test(LimitedTestCase): def test_ref(self): err = Error() err_ref = weakref.ref(err) - with Timeout(DELAY*2, err): + with Timeout(DELAY * 2, err): sleep(DELAY) del err gc.collect() @@ -115,24 +113,24 @@ class Test(LimitedTestCase): def test_nested_timeout(self): with Timeout(DELAY, False): - with Timeout(DELAY*2, False): - sleep(DELAY*3) + with Timeout(DELAY * 2, False): + sleep(DELAY * 3) raise AssertionError('should not get there') with Timeout(DELAY) as t1: - with Timeout(DELAY*2) as t2: + with Timeout(DELAY * 2) as t2: try: - sleep(DELAY*3) + sleep(DELAY * 3) except Timeout as ex: assert ex is t1, (ex, t1) assert not t1.pending, t1 assert t2.pending, t2 assert not t2.pending, t2 - with Timeout(DELAY*2) as t1: + with Timeout(DELAY * 2) as t1: with Timeout(DELAY) as t2: try: - sleep(DELAY*3) + sleep(DELAY * 3) except Timeout as ex: assert ex is t2, (ex, t2) assert t1.pending, t1 diff --git a/tests/websocket_new_test.py b/tests/websocket_new_test.py index 4089ec1..ec29fc6 100644 --- a/tests/websocket_new_test.py +++ b/tests/websocket_new_test.py @@ -3,9 +3,9 @@ import struct import eventlet from eventlet import event +from eventlet import websocket from eventlet.green import httplib from eventlet.green import socket -from eventlet import websocket from tests.wsgi_test import _TestBase @@ -43,7 +43,8 @@ class TestWebSocket(_TestBase): # NOTE: intentionally no connection header "Host: localhost:%s" % self.port, "Origin: http://localhost:%s" % self.port, - "Sec-WebSocket-Version: 13", ]) + "Sec-WebSocket-Version: 13", + ]) http = httplib.HTTPConnection('localhost', self.port) http.request("GET", "/echo", headers=headers) resp = http.getresponse() @@ -58,7 +59,8 @@ class TestWebSocket(_TestBase): "Connection: Upgrade", "Host: localhost:%s" % self.port, "Origin: http://localhost:%s" % self.port, - "Sec-WebSocket-Version: 13", ]) + "Sec-WebSocket-Version: 13", + ]) http = httplib.HTTPConnection('localhost', self.port) http.request("GET", "/echo", headers=headers) resp = http.getresponse() @@ -72,7 +74,8 @@ class TestWebSocket(_TestBase): "Connection: Upgrade", "Host: localhost:%s" % self.port, "Origin: http://localhost:%s" % self.port, - "Sec-WebSocket-Version: 13", ]) + "Sec-WebSocket-Version: 13", + ]) http = httplib.HTTPConnection('localhost', self.port) http.request("GET", "/echo", headers=headers) resp = http.getresponse() @@ -113,12 +116,13 @@ class TestWebSocket(_TestBase): "Host: localhost:%s" % self.port, "Origin: http://localhost:%s" % self.port, "Sec-WebSocket-Version: 13", - "Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", ] + "Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", + ] sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n') - first_resp = sock.recv(1024) + sock.recv(1024) ws = websocket.RFC6455WebSocket(sock, {}, client=True) ws.send('hello') assert ws.wait() == 'hello' @@ -152,11 +156,12 @@ class TestWebSocket(_TestBase): "Host: localhost:%s" % self.port, "Origin: http://localhost:%s" % self.port, "Sec-WebSocket-Version: 13", - "Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", ] + "Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", + ] sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n') - resp = sock.recv(1024) # get the headers + sock.recv(1024) # get the headers sock.close() # close while the app is running done_with_request.wait() assert not error_detected[0] @@ -184,11 +189,12 @@ class TestWebSocket(_TestBase): "Host: localhost:%s" % self.port, "Origin: http://localhost:%s" % self.port, "Sec-WebSocket-Version: 13", - "Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", ] + "Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", + ] sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n') - resp = sock.recv(1024) # get the headers + sock.recv(1024) # get the headers closeframe = struct.pack('!BBIH', 1 << 7 | 8, 1 << 7 | 2, 0, 1000) sock.sendall(closeframe) # "Close the connection" packet. done_with_request.wait() @@ -217,11 +223,12 @@ class TestWebSocket(_TestBase): "Host: localhost:%s" % self.port, "Origin: http://localhost:%s" % self.port, "Sec-WebSocket-Version: 13", - "Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", ] + "Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", + ] sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n') - resp = sock.recv(1024) # get the headers + sock.recv(1024) # get the headers sock.sendall('\x07\xff') # Weird packet. done_with_request.wait() assert not error_detected[0] diff --git a/tests/websocket_test.py b/tests/websocket_test.py index 3c07248..bf05555 100644 --- a/tests/websocket_test.py +++ b/tests/websocket_test.py @@ -116,12 +116,13 @@ class TestWebSocket(_TestBase): sock.sendall('\r\n'.join(connect) + '\r\n\r\n') result = sock.recv(1024) # The server responds the correct Websocket handshake - self.assertEqual(result, - '\r\n'.join(['HTTP/1.1 101 Web Socket Protocol Handshake', - 'Upgrade: WebSocket', - 'Connection: Upgrade', - 'WebSocket-Origin: http://localhost:%s' % self.port, - 'WebSocket-Location: ws://localhost:%s/echo\r\n\r\n' % self.port])) + self.assertEqual(result, '\r\n'.join([ + 'HTTP/1.1 101 Web Socket Protocol Handshake', + 'Upgrade: WebSocket', + 'Connection: Upgrade', + 'WebSocket-Origin: http://localhost:%s' % self.port, + 'WebSocket-Location: ws://localhost:%s/echo\r\n\r\n' % self.port, + ])) def test_correct_upgrade_request_76(self): connect = [ @@ -140,13 +141,14 @@ class TestWebSocket(_TestBase): sock.sendall('\r\n'.join(connect) + '\r\n\r\n^n:ds[4U') result = sock.recv(1024) # The server responds the correct Websocket handshake - self.assertEqual(result, - '\r\n'.join(['HTTP/1.1 101 WebSocket Protocol Handshake', - 'Upgrade: WebSocket', - 'Connection: Upgrade', - 'Sec-WebSocket-Origin: http://localhost:%s' % self.port, - 'Sec-WebSocket-Protocol: ws', - 'Sec-WebSocket-Location: ws://localhost:%s/echo\r\n\r\n8jKS\'y:G*Co,Wxa-' % self.port])) + self.assertEqual(result, '\r\n'.join([ + 'HTTP/1.1 101 WebSocket Protocol Handshake', + 'Upgrade: WebSocket', + 'Connection: Upgrade', + 'Sec-WebSocket-Origin: http://localhost:%s' % self.port, + 'Sec-WebSocket-Protocol: ws', + 'Sec-WebSocket-Location: ws://localhost:%s/echo\r\n\r\n8jKS\'y:G*Co,Wxa-' % self.port, + ])) def test_query_string(self): # verify that the query string comes out the other side unscathed @@ -165,13 +167,14 @@ class TestWebSocket(_TestBase): sock.sendall('\r\n'.join(connect) + '\r\n\r\n^n:ds[4U') result = sock.recv(1024) - self.assertEqual(result, - '\r\n'.join(['HTTP/1.1 101 WebSocket Protocol Handshake', - 'Upgrade: WebSocket', - 'Connection: Upgrade', - 'Sec-WebSocket-Origin: http://localhost:%s' % self.port, - 'Sec-WebSocket-Protocol: ws', - 'Sec-WebSocket-Location: ws://localhost:%s/echo?query_string\r\n\r\n8jKS\'y:G*Co,Wxa-' % self.port])) + self.assertEqual(result, '\r\n'.join([ + 'HTTP/1.1 101 WebSocket Protocol Handshake', + 'Upgrade: WebSocket', + 'Connection: Upgrade', + 'Sec-WebSocket-Origin: http://localhost:%s' % self.port, + 'Sec-WebSocket-Protocol: ws', + 'Sec-WebSocket-Location: ws://localhost:%s/echo?query_string\r\n\r\n8jKS\'y:G*Co,Wxa-' % self.port, + ])) def test_empty_query_string(self): # verify that a single trailing ? doesn't get nuked @@ -212,7 +215,7 @@ class TestWebSocket(_TestBase): ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n') - first_resp = sock.recv(1024) + sock.recv(1024) sock.sendall('\x00hello\xFF') result = sock.recv(1024) self.assertEqual(result, '\x00hello\xff') @@ -240,7 +243,7 @@ class TestWebSocket(_TestBase): ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n^n:ds[4U') - first_resp = sock.recv(1024) + sock.recv(1024) sock.sendall('\x00hello\xFF') result = sock.recv(1024) self.assertEqual(result, '\x00hello\xff') @@ -328,7 +331,7 @@ class TestWebSocket(_TestBase): sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n') - resp = sock.recv(1024) # get the headers + sock.recv(1024) # get the headers sock.close() # close while the app is running done_with_request.wait() assert not error_detected[0] @@ -362,7 +365,7 @@ class TestWebSocket(_TestBase): sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n^n:ds[4U') - resp = sock.recv(1024) # get the headers + sock.recv(1024) # get the headers sock.close() # close while the app is running done_with_request.wait() assert not error_detected[0] @@ -396,7 +399,7 @@ class TestWebSocket(_TestBase): sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n^n:ds[4U') - resp = sock.recv(1024) # get the headers + sock.recv(1024) # get the headers sock.sendall('\xff\x00') # "Close the connection" packet. done_with_request.wait() assert not error_detected[0] @@ -430,7 +433,7 @@ class TestWebSocket(_TestBase): sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n^n:ds[4U') - resp = sock.recv(1024) # get the headers + sock.recv(1024) # get the headers sock.sendall('\xef\x00') # Weird packet. done_with_request.wait() assert error_detected[0] @@ -482,7 +485,7 @@ class TestWebSocket(_TestBase): sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n') - resp = sock.recv(1024) + sock.recv(1024) done_with_request.wait() assert error_detected[0] @@ -515,7 +518,7 @@ class TestWebSocket(_TestBase): sock = eventlet.connect( ('localhost', self.port)) sock.sendall('\r\n'.join(connect) + '\r\n\r\n^n:ds[4U') - resp = sock.recv(1024) + sock.recv(1024) done_with_request.wait() assert error_detected[0] |
