forked from aurorafox1/toproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
65 lines (51 loc) · 1.75 KB
/
test.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
#!/usr/bin/env python
import unittest
import urllib2
import subprocess
import os
import time
import tornado.ioloop
import tornado.httpclient
import sys
sys.path.append('../')
from toproxy import run_proxy
class TestStandaloneProxy(unittest.TestCase):
def setUp(self):
self.proxy = subprocess.Popen(['python', 'toproxy/proxy.py',
'8888'])
proxy_support = urllib2.ProxyHandler({
"https": "http://localhost:8888",
"http": "http://localhost:8888"
})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
time.sleep(1)
def tearDown(self):
os.kill(self.proxy.pid, 15)
time.sleep(1)
os.kill(self.proxy.pid, 9)
def test(self):
base_url = '//xiaorui.cc/'
urllib2.urlopen('https:' + base_url + 'get').read()
urllib2.urlopen('http:' + base_url + 'get').read()
urllib2.urlopen('https:' + base_url + 'post', '').read()
urllib2.urlopen('http:' + base_url + 'post', '').read()
class TestTornadoProxy(unittest.TestCase):
def setUp(self):
self.ioloop = tornado.ioloop.IOLoop.instance()
run_proxy(8889, start_ioloop=False)
def tearDown(self):
pass
def test(self):
def handle_response(resp):
self.assertIsNone(resp.error)
self.ioloop.stop()
tornado.httpclient.AsyncHTTPClient.configure(
"tornado.curl_httpclient.CurlAsyncHTTPClient")
client = tornado.httpclient.AsyncHTTPClient()
req = tornado.httpclient.HTTPRequest('http://xiaorui.cc/',
proxy_host='127.0.0.1', proxy_port=8889)
client.fetch(req, handle_response)
self.ioloop.start()
if __name__ == '__main__':
unittest.main()