-
Notifications
You must be signed in to change notification settings - Fork 12
/
trequests_tests.py
59 lines (42 loc) · 1.47 KB
/
trequests_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import requests
from tornalet import tornalet
from trequests import setup_session
from tornado.ioloop import IOLoop
from tornado.escape import json_decode
from tornado.testing import AsyncHTTPTestCase
from tornado.web import Application, RequestHandler
setup_session()
class TestUtil(object):
def send(self, data):
return requests.post('http://httpbin.org/post', data=data).json()
class TestHandler(RequestHandler):
@tornalet
def get(self):
util = TestUtil()
data = {'foo': 'bar'}
response = util.send(data)
self.write(response)
class TestCase(AsyncHTTPTestCase):
def setUp(self):
self.application = None
super(TestCase, self).setUp()
self.get_app().callback_called = False
def get_new_ioloop(self):
return IOLoop().instance()
def get_app(self):
if not self.application:
self.application = Application(
[(r'/', TestHandler)]
)
return self.application
def _test_callback(self):
self.get_app().callback_called = True
def test_post(self):
"""Test using a library that POSTs to requestbin using requests.
"""
self.get_new_ioloop().add_callback(self._test_callback)
response = self.fetch('/')
data = json_decode(response.body)
self.assertEqual(response.code, 200)
self.assertEqual(data['form']['foo'], 'bar')
self.assertTrue(self.get_app().callback_called)