<s> import threading <EOL> import logging <EOL> import BaseHTTPServer <EOL> import StringIO <EOL> from nose . tools import assert_true , assert_false <EOL> from django . test . client import Client <EOL> from desktop . lib . django_test_util import make_logged_in_client <EOL> from proxy . views import _rewrite_links <EOL> import proxy . conf <EOL> class Handler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def do_GET ( self ) : <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( "<STR_LIT>" ) <EOL> self . wfile . write ( "<STR_LIT>" + self . path + "<STR_LIT:.>" ) <EOL> self . wfile . write ( "<STR_LIT>" ) <EOL> self . wfile . write ( "<STR_LIT>" ) <EOL> def do_POST ( self ) : <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( "<STR_LIT>" ) <EOL> self . wfile . write ( "<STR_LIT>" + self . path + "<STR_LIT:.>" ) <EOL> self . wfile . write ( "<STR_LIT>" + self . rfile . read ( <NUM_LIT:16> ) ) <EOL> def log_message ( self , fmt , * args ) : <EOL> logging . debug ( "<STR_LIT>" % <EOL> ( self . address_string ( ) , <EOL> self . log_date_time_string ( ) , <EOL> fmt % args ) ) <EOL> def run_test_server ( ) : <EOL> """<STR_LIT>""" <EOL> httpd = BaseHTTPServer . HTTPServer ( ( "<STR_LIT:127.0.0.1>" , <NUM_LIT:0> ) , Handler ) <EOL> thread = threading . Thread ( target = httpd . handle_request ) <EOL> thread . daemon = True <EOL> thread . start ( ) <EOL> def finish ( ) : <EOL> print "<STR_LIT>" + str ( thread ) <EOL> thread . join ( <NUM_LIT> ) <EOL> assert_false ( thread . isAlive ( ) ) <EOL> return httpd , finish <EOL> run_test_server . __test__ = False <EOL> def test_proxy_get ( ) : <EOL> """<STR_LIT>""" <EOL> client = Client ( ) <EOL> client . login ( username = "<STR_LIT:test>" , password = "<STR_LIT:test>" ) <EOL> httpd , finish = run_test_server ( ) <EOL> try : <EOL> finish_conf = proxy . conf . WHITELIST . set_for_testing ( r"<STR_LIT>" ) <EOL> try : <EOL> response_get = client . get ( '<STR_LIT>' % httpd . server_port , dict ( foo = "<STR_LIT:bar>" ) ) <EOL> finally : <EOL> finish_conf ( ) <EOL> assert_true ( "<STR_LIT>" in response_get . content ) <EOL> assert_true ( "<STR_LIT>" in response_get . content ) <EOL> assert_true ( "<STR_LIT>" % httpd . server_port in response_get . content ) <EOL> assert_true ( "<STR_LIT>" % httpd . server_port in response_get . content ) <EOL> finally : <EOL> finish ( ) <EOL> def test_proxy_post ( ) : <EOL> """<STR_LIT>""" <EOL> client = Client ( ) <EOL> client . login ( username = "<STR_LIT:test>" , password = "<STR_LIT:test>" ) <EOL> httpd , finish = run_test_server ( ) <EOL> try : <EOL> finish_conf = proxy . conf . WHITELIST . set_for_testing ( r"<STR_LIT>" ) <EOL> try : <EOL> response_post = client . post ( '<STR_LIT>' % httpd . server_port , dict ( foo = "<STR_LIT:bar>" , foo2 = "<STR_LIT:bar>" ) ) <EOL> finally : <EOL> finish_conf ( ) <EOL> assert_true ( "<STR_LIT>" in response_post . content ) <EOL> assert_true ( "<STR_LIT>" in response_post . content ) <EOL> assert_true ( "<STR_LIT>" in response_post . content ) <EOL> assert_true ( "<STR_LIT>" in response_post . content ) <EOL> finally : <EOL> finish ( ) <EOL> def test_blacklist ( ) : <EOL> client = make_logged_in_client ( '<STR_LIT:test>' ) <EOL> finish_confs = [ <EOL> proxy . conf . WHITELIST . set_for_testing ( r"<STR_LIT>" ) , <EOL> proxy . conf . BLACKLIST . set_for_testing ( r"<STR_LIT>" ) , <EOL> ] <EOL> try : <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> assert_true ( "<STR_LIT>" in resp . content ) <EOL> httpd , finish = run_test_server ( ) <EOL> try : <EOL> resp = client . get ( '<STR_LIT>' % ( httpd . server_port , ) ) <EOL> assert_true ( "<STR_LIT>" in resp . content ) <EOL> finally : <EOL> finish ( ) <EOL> finally : <EOL> for fin in finish_confs : <EOL> fin ( ) <EOL> class UrlLibFileWrapper ( StringIO . StringIO ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , buf , url ) : <EOL> StringIO . StringIO . __init__ ( self , buf ) <EOL> self . url = url <EOL> def geturl ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . url <EOL> def test_rewriting ( ) : <EOL> """<STR_LIT>""" <EOL> html = "<STR_LIT>" <EOL> assert_true ( '<STR_LIT>' in _rewrite_links ( UrlLibFileWrapper ( html , "<STR_LIT>" ) ) , <EOL> msg = "<STR_LIT>" ) <EOL> assert_true ( '<STR_LIT>' in _rewrite_links ( UrlLibFileWrapper ( html , "<STR_LIT>" ) ) , <EOL> msg = "<STR_LIT>" ) <EOL> html = "<STR_LIT>" <EOL> assert_true ( '<STR_LIT>' in <EOL> _rewrite_links ( UrlLibFileWrapper ( html , "<STR_LIT>" ) ) , <EOL> msg = "<STR_LIT>" ) <EOL> html = """<STR_LIT>""" <EOL> rewritten = _rewrite_links ( UrlLibFileWrapper ( html , "<STR_LIT>" ) ) <EOL> assert_true ( '<STR_LIT>' in <EOL> rewritten , <EOL> msg = "<STR_LIT>" ) </s>
