Skip to content

Commit 372e00a

Browse files
committed
1 parent a4994eb commit 372e00a

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

Diff for: tests/test_tnd.py

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
import unittest
2+
import socket
3+
import six
24

35
from restless.tnd import TornadoResource, _BridgeMixin
46
from restless.utils import json
5-
from tornado import testing, web, httpserver, gen
7+
from tornado import testing, web, httpserver, gen, version_info
8+
from tornado.iostream import IOStream
69
from restless.constants import UNAUTHORIZED
710

11+
def _newer_or_equal_(v):
12+
for i in six.moves.xrange(min(len(v), len(version_info))):
13+
expected, tnd = v[i], version_info[i]
14+
if tnd > expected:
15+
return True
16+
elif tnd == expected:
17+
continue
18+
else:
19+
return False
20+
return True
21+
22+
def _equal_(v):
23+
for i in six.moves.xrange(min(len(v), len(version_info))):
24+
if v[i] != version_info[i]:
25+
return False
26+
return True
27+
28+
29+
if _newer_or_equal_((4, 0, 0, 0)):
30+
from tornado.http1connection import HTTP1Connection
831

932
class TndBaseTestResource(TornadoResource):
1033
"""
@@ -127,7 +150,7 @@ def test_not_authenticated(self):
127150

128151
class BaseTestCase(unittest.TestCase):
129152
"""
130-
test case that export the wrapped tornado.web.RequestHandler
153+
test case that export the wrapped tornado.web.RequestHandler.
131154
"""
132155
def init_request_handler(self, rh_cls, view_type):
133156
global app
@@ -136,7 +159,20 @@ def init_request_handler(self, rh_cls, view_type):
136159
elif view_type == 'detail':
137160
rq = rh_cls.as_detail()
138161

139-
fake_request = httpserver.HTTPRequest('GET', '/fake', body='test123')
162+
# compose a fake incoming request
163+
fake_connection = None
164+
165+
# after tornado 4.1, it's not allowed to build a RequestHandler without a connection.
166+
if _newer_or_equal_((4, 0, 0, 0)):
167+
ios = IOStream(socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0))
168+
context = None
169+
170+
# there is a bug in these 2 version that would fail when
171+
# context is None
172+
if _equal_((4, 0, 1)) or _equal_((4, 0, 2)):
173+
context = httpserver._HTTPRequestContext(ios, None, None)
174+
fake_connection = HTTP1Connection(ios, False, context=context)
175+
fake_request = httpserver.HTTPRequest('GET', '/fake', body='test123', connection=fake_connection)
140176
self.new_handler = rq(app, fake_request)
141177

142178

0 commit comments

Comments
 (0)