<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> import BaseHTTPServer <EOL> import SocketServer <EOL> import multiprocessing <EOL> import unittest <EOL> import phlurl_request <EOL> class HttpReqHandler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> BaseHTTPServer . BaseHTTPRequestHandler . __init__ ( self , * args , ** kwargs ) <EOL> self . wfile = None <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:OK>' ) <EOL> def log_message ( self , format , * args ) : <EOL> pass <EOL> class BasicAuthHttpReqHandler ( HttpReqHandler ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> HttpReqHandler . __init__ ( self , * args , ** kwargs ) <EOL> self . headers = None <EOL> self . wfile = None <EOL> def do_GET ( self ) : <EOL> auth = self . headers . getheader ( '<STR_LIT>' ) <EOL> if auth is None : <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . send_header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . send_header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( '<STR_LIT>' ) <EOL> else : <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( auth ) <EOL> def httpd_serve_forever ( parent_pipe , req_handler ) : <EOL> httpd = SocketServer . TCPServer ( ( "<STR_LIT:localhost>" , <NUM_LIT:0> ) , req_handler , False ) <EOL> httpd . allow_reuse_address = True <EOL> httpd . server_bind ( ) <EOL> httpd . server_activate ( ) <EOL> parent_pipe . send ( httpd . server_address ) <EOL> parent_pipe . close ( ) <EOL> httpd . serve_forever ( ) <EOL> def start_httpd ( request_handler ) : <EOL> parent_conn , child_conn = multiprocessing . Pipe ( False ) <EOL> httpd_process = multiprocessing . Process ( <EOL> target = httpd_serve_forever , <EOL> args = ( child_conn , request_handler , ) ) <EOL> httpd_process . start ( ) <EOL> child_conn . close ( ) <EOL> httpd_host , httpd_port = parent_conn . recv ( ) <EOL> return ( httpd_process , httpd_host , httpd_port ) <EOL> class Test ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> pass <EOL> def tearDown ( self ) : <EOL> pass <EOL> def test_join_url ( self ) : <EOL> self . assertEqual ( <EOL> phlurl_request . join_url ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> phlurl_request . join_url ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> phlurl_request . join_url ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> phlurl_request . join_url ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> def test_split_url ( self ) : <EOL> self . assertEqual ( <EOL> phlurl_request . split_url ( '<STR_LIT>' ) , <EOL> phlurl_request . SplitUrlResult ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' , None , None ) ) <EOL> self . assertEqual ( <EOL> phlurl_request . split_url ( '<STR_LIT>' ) , <EOL> phlurl_request . SplitUrlResult ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' , None , None ) ) <EOL> self . assertEqual ( <EOL> phlurl_request . split_url ( '<STR_LIT>' ) , <EOL> phlurl_request . SplitUrlResult ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , None , '<STR_LIT>' , None , None ) ) <EOL> self . assertEqual ( <EOL> phlurl_request . split_url ( <EOL> '<STR_LIT>' ) , <EOL> phlurl_request . SplitUrlResult ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> None , <EOL> '<STR_LIT>' , <EOL> None , <EOL> None ) ) <EOL> def test_group_urls ( self ) : <EOL> urls = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> expected_split_a = phlurl_request . SplitUrlResult ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:http>' , '<STR_LIT>' , None , '<STR_LIT>' , None , None ) <EOL> expected_split_b = phlurl_request . SplitUrlResult ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:http>' , '<STR_LIT>' , None , '<STR_LIT>' , None , None ) <EOL> expected_split_c = phlurl_request . SplitUrlResult ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , None , '<STR_LIT>' , None , None ) <EOL> expected_http = { <EOL> ( '<STR_LIT>' , None ) : [ expected_split_a , expected_split_b ] , <EOL> } <EOL> expected_https = { <EOL> ( '<STR_LIT>' , None ) : [ expected_split_c ] <EOL> } <EOL> result = phlurl_request . _group_urls ( urls ) <EOL> http_result = dict ( result . http ) <EOL> https_result = dict ( result . https ) <EOL> self . assertEqual ( <EOL> http_result , <EOL> expected_http ) <EOL> self . assertEqual ( <EOL> https_result , <EOL> expected_https ) <EOL> class HttpTest ( unittest . TestCase ) : <EOL> def __init__ ( self , data ) : <EOL> super ( HttpTest , self ) . __init__ ( data ) <EOL> self . httpd_process = None <EOL> self . httpd_host = None <EOL> self . httpd_port = None <EOL> def setUp ( self ) : <EOL> self . httpd_process , self . httpd_host , self . httpd_port = start_httpd ( <EOL> HttpReqHandler ) <EOL> def tearDown ( self ) : <EOL> self . httpd_process . terminate ( ) <EOL> def test_get ( self ) : <EOL> self . assertEqual ( <EOL> phlurl_request . get ( self . _url ( '<STR_LIT>' ) ) , <EOL> ( <NUM_LIT:200> , '<STR_LIT:OK>' ) ) <EOL> def test_get_many ( self ) : <EOL> expected = { <EOL> self . _url ( '<STR_LIT>' ) : ( <NUM_LIT:200> , '<STR_LIT:OK>' ) , <EOL> self . _url ( '<STR_LIT>' ) : ( <NUM_LIT:200> , '<STR_LIT:OK>' ) , <EOL> self . _url ( '<STR_LIT>' ) : ( <NUM_LIT:200> , '<STR_LIT:OK>' ) , <EOL> } <EOL> self . assertEqual ( <EOL> phlurl_request . get_many ( expected . iterkeys ( ) ) , <EOL> expected ) <EOL> def _url ( self , format_string ) : <EOL> return format_string . format ( <EOL> host = self . httpd_host , <EOL> port = self . httpd_port ) <EOL> class HttpTest_Auth ( unittest . TestCase ) : <EOL> def __init__ ( self , data ) : <EOL> super ( HttpTest_Auth , self ) . __init__ ( data ) <EOL> self . httpd_process = None <EOL> self . httpd_host = None <EOL> self . httpd_port = None <EOL> def setUp ( self ) : <EOL> self . httpd_process , self . httpd_host , self . httpd_port = start_httpd ( <EOL> BasicAuthHttpReqHandler ) <EOL> def tearDown ( self ) : <EOL> self . httpd_process . terminate ( ) <EOL> def test_get ( self ) : <EOL> self . assertEqual ( <EOL> phlurl_request . get ( self . _url ( '<STR_LIT>' ) ) , <EOL> ( <NUM_LIT> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( <EOL> phlurl_request . get ( <EOL> self . _url ( '<STR_LIT>' ) ) , <EOL> ( <NUM_LIT:200> , '<STR_LIT>' ) ) <EOL> def test_get_many ( self ) : <EOL> url_a = self . _url ( '<STR_LIT>' ) <EOL> url_b = self . _url ( '<STR_LIT>' ) <EOL> url_c = self . _url ( '<STR_LIT>' ) <EOL> expected = { <EOL> url_a : ( <NUM_LIT> , '<STR_LIT>' ) , <EOL> url_b : ( <NUM_LIT:200> , '<STR_LIT>' ) , <EOL> url_c : ( <NUM_LIT:200> , '<STR_LIT>' ) , <EOL> } <EOL> self . assertEqual ( <EOL> phlurl_request . get_many ( expected . iterkeys ( ) ) , <EOL> expected ) <EOL> def _url ( self , format_string ) : <EOL> return format_string . format ( <EOL> host = self . httpd_host , <EOL> port = self . httpd_port ) </s>
