fix tests

This commit is contained in:
HawkOwl 2015-09-16 12:25:52 +08:00
parent 18eb52914a
commit 6b83faf5ce
8 changed files with 195 additions and 164 deletions

View File

@ -23,3 +23,18 @@
# THE SOFTWARE.
#
###############################################################################
from __future__ import absolute_import, print_function
class FakeTransport(object):
_written = b""
_open = True
def write(self, msg):
if not self._open:
raise Exception("Can't write to a closed connection")
self._written = self._written + msg
def loseConnection(self):
self._open = False

View File

@ -26,9 +26,6 @@
from __future__ import absolute_import
import os
if os.environ.get('USE_TWISTED', False):
# t.i.reactor doesn't exist until we've imported it once, but we
# need it to exist so we can @patch it out in the tests ...
from twisted.internet import reactor # noqa
@ -39,9 +36,11 @@ if os.environ.get('USE_TWISTED', False):
from autobahn.twisted.wamp import ApplicationRunner
def raise_error(*args, **kw):
raise RuntimeError("we always fail")
class TestApplicationRunner(unittest.TestCase):
@patch('twisted.internet.reactor')
def test_runner_default(self, fakereactor):

View File

@ -26,10 +26,8 @@
from __future__ import absolute_import
import os
import sys
if os.environ.get('USE_TWISTED', False):
import twisted.internet
from twisted.trial.unittest import TestCase
@ -37,6 +35,7 @@ if os.environ.get('USE_TWISTED', False):
from autobahn.twisted import choosereactor
class ChooseReactorTests(TestCase):
def patch_reactor(self, name, new_reactor):

View File

@ -0,0 +1,58 @@
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Tavendo GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
from __future__ import absolute_import, print_function
import unittest2 as unittest
from autobahn.twisted.websocket import WebSocketServerFactory
from autobahn.twisted.websocket import WebSocketServerProtocol
from autobahn.test import FakeTransport
class Hixie76RejectionTests(unittest.TestCase):
"""
Hixie-76 should not be accepted by an Autobahn server.
"""
def test_handshake_fails(self):
"""
A handshake from a client only supporting Hixie-76 will fail.
"""
t = FakeTransport()
f = WebSocketServerFactory()
p = WebSocketServerProtocol()
p.factory = f
p.transport = t
# from http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
http_request = b"GET /demo HTTP/1.1\r\nHost: example.com\r\nConnection: Upgrade\r\nSec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\nSec-WebSocket-Protocol: sample\r\nUpgrade: WebSocket\r\nSec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\nOrigin: http://example.com\r\n\r\n^n:ds[4U"
p.openHandshakeTimeout = 0
p._connectionMade()
p.data = http_request
p.processHandshake()
self.assertIn(b"HTTP/1.1 400", t._written)
self.assertIn(b"Hixie76 protocol not supported", t._written)

View File

@ -36,6 +36,9 @@ if os.environ.get('USE_TWISTED', False):
from twisted.trial import unittest
from six import PY3
from twisted.internet.base import DelayedCall
DelayedCall.debug = True
from autobahn import util
from autobahn.twisted.wamp import ApplicationSession
from autobahn.wamp import message, role, serializer, types

View File

@ -26,52 +26,11 @@
from __future__ import absolute_import, print_function
import os
import unittest2 as unittest
from autobahn.websocket.protocol import WebSocketServerProtocol
from autobahn.websocket.protocol import WebSocketServerFactory
class FakeTransport(object):
_written = b""
_open = True
def write(self, msg):
if not self._open:
raise Exception("Can't write to a closed connection")
self._written = self._written + msg
def loseConnection(self):
self._open = False
class Hixie76RejectionTests(unittest.TestCase):
"""
Hixie-76 should not be accepted by an Autobahn server.
"""
def test_handshake_fails(self):
"""
A handshake from a client only supporting Hixie-76 will fail.
"""
from autobahn.twisted.websocket import WebSocketServerFactory
from autobahn.twisted.websocket import WebSocketServerProtocol
t = FakeTransport()
f = WebSocketServerFactory()
p = WebSocketServerProtocol()
p.factory = f
p.transport = t
# from http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
http_request = b"GET /demo HTTP/1.1\r\nHost: example.com\r\nConnection: Upgrade\r\nSec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\nSec-WebSocket-Protocol: sample\r\nUpgrade: WebSocket\r\nSec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\nOrigin: http://example.com\r\n\r\n^n:ds[4U"
p._connectionMade()
p.data = http_request
p.processHandshake()
self.assertIn(b"HTTP/1.1 400", t._written)
self.assertIn(b"Hixie76 protocol not supported", t._written)
from autobahn.test import FakeTransport
class WebSocketProtocolTests(unittest.TestCase):
@ -182,7 +141,3 @@ class WebSocketProtocolTests(unittest.TestCase):
# We shouldn't have closed
self.assertEqual(self.transport._written, b"")
self.assertEqual(self.protocol.state, self.protocol.STATE_OPEN)
if not os.environ.get('USE_TWISTED', False):
del Hixie76RejectionTests

2
setup.cfg Normal file
View File

@ -0,0 +1,2 @@
[pytest]
norecursedirs = autobahn/twisted/*

View File

@ -32,7 +32,7 @@ commands =
sh -c "which python"
python -V
coverage --version
asyncio,trollius: coverage run {envbindir}/py.test autobahn
asyncio,trollius: coverage run {envbindir}/py.test autobahn/
twtrunk,twcurrent,tw121,tw132,twcurrent: coverage run {envbindir}/trial autobahn
coverage report
whitelist_externals = sh