def setUp(self): # Start the proxy server create_temp_dir() self._proxy = LocalProxy(self.IP, 0) self._proxy.start() self._proxy.wait_for_start() port = self._proxy.get_port() # Build the proxy opener proxy_handler = urllib2.ProxyHandler( {"http": "http://%s:%s" % (self.IP, port)}) self.proxy_opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler)
def setUp(self): # Start the proxy server create_temp_dir() self._proxy = LocalProxy(self.IP, 0) self._proxy.start() self._proxy.wait_for_start() port = self._proxy.get_port() # Build the proxy opener proxy_handler = urllib2.ProxyHandler({"http": "http://%s:%s" % (self.IP, port)}) self.proxy_opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler)
class TestLocalProxy(unittest.TestCase): IP = '127.0.0.1' def setUp(self): # Start the proxy server create_temp_dir() self._proxy = LocalProxy(self.IP, 0) self._proxy.start() self._proxy.wait_for_start() port = self._proxy.get_port() # Build the proxy opener proxy_handler = urllib2.ProxyHandler( {"http": "http://%s:%s" % (self.IP, port)}) self.proxy_opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler) def tearDown(self): self._proxy.stop() self.assertNotIn(self._proxy, threading.enumerate()) def test_get_thread_name(self): self.assertEqual(self._proxy.name, 'LocalProxyThread') def test_no_request(self): self.assertEqual(self._proxy.get_trapped_request(), None) def test_no_trap(self): self._proxy.set_trap(False) response = self.proxy_opener.open('http://moth/') self.assertEqual(response.code, 200) def test_request_trapped_drop(self): def send_request(proxy_opener, result_queue): try: proxy_opener.open('http://moth/') except urllib2.HTTPError, he: # Catch the 403 from the local proxy when the user # drops the HTTP request. result_queue.put(he) self._proxy.set_trap(True) result_queue = Queue.Queue() send_thread = threading.Thread(target=send_request, args=(self.proxy_opener, result_queue)) send_thread.start() time.sleep(0.5) request = self._proxy.get_trapped_request() self.assertEqual(request.get_url().url_string, 'http://moth/') self.assertEqual(request.get_method(), 'GET') self._proxy.drop_request(request) response = result_queue.get() self.assertEqual(response.code, 403) self.assertIn('dropped by the user', response.read())
class TestLocalProxy(unittest.TestCase): IP = '127.0.0.1' def setUp(self): # Start the proxy server create_temp_dir() self._proxy = LocalProxy(self.IP, 0) self._proxy.start() self._proxy.wait_for_start() port = self._proxy.get_port() # Build the proxy opener proxy_handler = urllib2.ProxyHandler({"http": "http://%s:%s" % (self.IP, port)}) self.proxy_opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler) def tearDown(self): self._proxy.stop() self.assertNotIn(self._proxy, threading.enumerate()) def test_get_thread_name(self): self.assertEqual(self._proxy.name, 'LocalProxyThread') def test_no_request(self): self.assertEqual(self._proxy.get_trapped_request(), None) def test_no_trap(self): self._proxy.set_trap(False) response = self.proxy_opener.open('http://moth/') self.assertEqual(response.code, 200) def test_request_trapped_drop(self): def send_request(proxy_opener, result_queue): try: proxy_opener.open('http://moth/') except urllib2.HTTPError, he: # Catch the 403 from the local proxy when the user # drops the HTTP request. result_queue.put(he) self._proxy.set_trap(True) result_queue = Queue.Queue() send_thread = threading.Thread(target=send_request, args=(self.proxy_opener, result_queue)) send_thread.start() time.sleep(0.5) request = self._proxy.get_trapped_request() self.assertEqual(request.get_url().url_string, 'http://moth/') self.assertEqual(request.get_method(), 'GET') self._proxy.drop_request(request) response = result_queue.get() self.assertEqual(response.code, 403) self.assertIn('dropped by the user', response.read())