forked from coldnight/tornadohttpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
209 lines (163 loc) · 6.36 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Author : cold
# E-mail : wh_linux@126.com
# Date : 13/08/05 16:37:37
# Desc :
#
from __future__ import absolute_import, print_function, division, with_statement
import time
import unittest
from tornado import testing
from tornadohttpclient import TornadoHTTPClient
class TornadoHTTPClientTest(unittest.TestCase):
def setUp(self):
self.http = TornadoHTTPClient()
self.http.debug = True
def _callback(self, response):
print(response.code)
print("当前链接地址: ")
print(response.effective_url)
self.http.stop()
def test_get(self):
self.http.get("http://www.linuxzen.com", callback=self._callback)
self.http.start()
def test_get_args(self):
self.http.get("http://www.baidu.com/s", (("wd", "tornado"),),
callback=self._callback)
def test_post(self):
params = [("vimcn", u"# 这是TornadoHTTPClient单元测试提交的".encode("utf-8"))]
url = "http://p.vim-cn.com"
def callback(response):
print("打开此链接:", end=" ")
print(response.effective_url)
self.http.stop()
self.http.post(url, params, callback=callback)
self.http.start()
def test_head(self):
def callback(response):
pass
self.http.head("http://linuxzen.com", callback=callback)
def test_callback_args(self):
def callback(times, response):
print(response.code)
print("当前链接地址: ")
print(response.effective_url)
print("当前请求次数", end=" ")
print(times)
if times == 9:
self.http.stop()
for i in range(10):
self.http.get("http://www.linuxzen.com", callback=callback,
args=(i,))
self.http.start()
def test_user_agent(self):
user_agent = "Mozilla/5.0 (X11; Linux x86_64)"\
" AppleWebKit/537.11 (KHTML, like Gecko)"\
" Chrome/23.0.1271.97 Safari/537.11"
self.http.set_user_agent(user_agent)
def callback(response):
#self.assertEqual(response.request.headers["User-Agent"], user_agent)
self.http.stop()
self.http.get("http://www.linuxzen.com", callback=callback)
self.http.start()
def test_header(self):
headers = {"Origin":"http://www.linuxzen.com"}
def callback(response):
self.assertEqual(response.request.headers["Origin"],
headers.get("Origin"))
self.http.stop()
self.http.get("http://www.linuxzen.com", callback=callback)
self.http.start()
def test_cookie(self):
def callback(response):
print("cookie >>>>>>>>>>>>>>>>>>", end=" ")
print(self.http.cookie)
self.http.stop()
self.http.get("http://www.baidu.com", callback=callback)
self.http.start()
def test_cookie_jar(self):
def callback(response):
print("cookie jar>>>>>>>>>>>>>>>>>>", end=" ")
print(self.http.cookiejar)
self.http.stop()
self.http.get("http://www.baidu.com", callback=callback)
self.http.start()
def test_upload_img(self):
def callback(response):
print("打开图片链接", end = " ")
print(response.effective_url)
self.http.stop()
self.http.upload("http://dimg.vim-cn.com", "name", "img_test.png",
callback=callback)
self.http.start()
class TornadoHTTPClientCoroutineTest(testing.AsyncTestCase):
def setUp(self):
super(TornadoHTTPClientCoroutineTest, self).setUp()
self.http = TornadoHTTPClient()
self.http.debug = True
def _callback(self, response):
print(response.code)
print("当前链接地址: ")
print(response.effective_url)
self.http.stop()
@testing.gen_test
def test_get(self):
resp = yield self.http.get("http://www.linuxzen.com")
self._callback(resp)
@testing.gen_test
def test_get_args(self):
yield self.http.get("http://www.baidu.com/s", (("wd", "tornado"),),
callback=self._callback)
@testing.gen_test
def test_post(self):
params = [("vimcn", u"# 这是TornadoHTTPClient单元测试提交的".encode("utf-8"))]
url = "http://p.vim-cn.com"
def callback(response):
print("打开此链接:", end=" ")
print(response.effective_url)
resp = yield self.http.post(url, params)
callback(resp)
@testing.gen_test
def test_head(self):
resp = yield self.http.head("http://linuxzen.com")
self.assertEqual(resp.code, 200)
@testing.gen_test
def test_user_agent(self):
user_agent = "Mozilla/5.0 (X11; Linux x86_64)"\
" AppleWebKit/537.11 (KHTML, like Gecko)"\
" Chrome/23.0.1271.97 Safari/537.11"
self.http.set_user_agent(user_agent)
resp = yield self.http.get("http://www.linuxzen.com")
self.assertEqual(resp.request.user_agent, user_agent)
@testing.gen_test
def test_header(self):
headers = {"Origin":"http://www.linuxzen.com"}
resp = yield self.http.get("http://www.linuxzen.com", headers=headers)
self.assertEqual(resp.request.headers["Origin"], headers["Origin"])
@testing.gen_test
def test_cookie(self):
yield self.http.get("http://www.baidu.com")
print(self.http.cookie)
@testing.gen_test
def test_cookie_jar(self):
yield self.http.get("http://www.baidu.com")
print("cookie jar>>>>>>>>>>>>>>>>>>", end=" ")
print(self.http.cookiejar)
@testing.gen_test
def test_upload_img(self):
def callback(response):
self.http.stop()
resp = yield self.http.upload("http://dimg.vim-cn.com", "name",
"img_test.png")
print("打开图片链接", end = " ")
print(resp.effective_url)
def main():
loader = unittest.TestLoader()
suite = loader.loadTestsFromNames(["tests.TornadoHTTPClientTest",
"tests.TornadoHTTPClientCoroutineTest"])
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)
if __name__ == "__main__":
main()