99
1010TIMEOUT  =  300 
1111
12+ # Global variable for test host 
13+ TEST_HOST  =  'jsonplaceholder.typicode.com' 
14+ 
1215# Accepting multiple args so we can pass something like: python elasticurl.py 
1316elasticurl_cmd_prefix  =  sys .argv [1 :]
1417if  not  elasticurl_cmd_prefix :
2528# Remove args from sys.argv so that unittest doesn't also try to parse them. 
2629sys .argv  =  sys .argv [:1 ]
2730
31+ 
2832def  run_command (args ):
2933    # gather all stderr and stdout to a single string that we print only if things go wrong 
30-     process  =  subprocess .Popen (args , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
34+     process  =  subprocess .Popen (
35+         args , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
3136    timedout  =  False 
3237    try :
3338        output  =  process .communicate (timeout = TIMEOUT )[0 ]
@@ -43,9 +48,12 @@ def run_command(args):
4348            for  line  in  output .splitlines ():
4449                print (line .decode ())
4550            if  timedout :
46-                 raise  RuntimeError ("Timeout happened after {secs} secs from: {cmd}" .format (secs = TIMEOUT , cmd = args_str ))
51+                 raise  RuntimeError ("Timeout happened after {secs} secs from: {cmd}" .format (
52+                     secs = TIMEOUT , cmd = args_str ))
4753            else :
48-                 raise  RuntimeError ("Return code {code} from: {cmd}" .format (code = process .returncode , cmd = args_str ))
54+                 raise  RuntimeError ("Return code {code} from: {cmd}" .format (
55+                     code = process .returncode , cmd = args_str ))
56+ 
4957
5058def  compare_files (filename_expected , filename_other ):
5159    if  not  filecmp .cmp (filename_expected , filename_other , shallow = False ):
@@ -63,58 +71,76 @@ def compare_files(filename_expected, filename_other):
6371                raise  RuntimeError ("Failed to open %s"  %  filename_other )
6472
6573        if  len (bytes_expected ) !=  len (bytes_other ):
66-             raise  RuntimeError ("File lengths differ. Expected %d, got %d"  %  (len (bytes_expected ), len (bytes_other )))
74+             raise  RuntimeError ("File lengths differ. Expected %d, got %d"  %  (
75+                 len (bytes_expected ), len (bytes_other )))
6776
6877        for  i  in  range (len (bytes_expected )):
6978            if  bytes_expected [i ] !=  bytes_other [i ]:
70-                 raise  RuntimeError ("Files differ at byte[%d]. Expected %d, got %d."  %  (i , bytes_expected [i ], bytes_other [i ]))
79+                 raise  RuntimeError ("Files differ at byte[%d]. Expected %d, got %d."  %  (
80+                     i , bytes_expected [i ], bytes_other [i ]))
7181
7282        print ("filecmp says these files differ, but they are identical. what the heck." )
7383
84+ 
7485class  SimpleTests (unittest .TestCase ):
7586    def  test_simple_get_amazon (self ):
7687        """make a simple GET request via alpn h2;http/1.1 to amazon and make sure it succeeds""" 
77-         simple_get_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , 'https://www.amazon.com' ]
88+         simple_get_args  =  elasticurl_cmd_prefix  +  \
89+             ['-v' , 'TRACE' , 'https://www.amazon.com' ]
7890        run_command (simple_get_args )
91+ 
7992    def  test_simple_get_google (self ):
8093        """make a simple GET request via alpn h2;http/1.1 to google and make sure it succeeds""" 
81-         simple_get_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , 'https://www.google.com' ]
94+         simple_get_args  =  elasticurl_cmd_prefix  +  \
95+             ['-v' , 'TRACE' , 'https://www.google.com' ]
8296        run_command (simple_get_args )
97+ 
8398    def  test_simple_get_h1 (self ):
8499        """make a simple GET request via HTTP/1.1 and make sure it succeeds""" 
85-         simple_get_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , '--http1_1' , 'http://postman-echo.com/get' ]
100+         simple_get_args  =  elasticurl_cmd_prefix  +  \
101+             ['-v' , 'TRACE' , '--http1_1' , f'http://{ TEST_HOST }  ]
86102        run_command (simple_get_args )
87103
88104    def  test_simple_post_h1 (self ):
89105        """make a simple POST request via HTTP/1.1 to make sure sending data succeeds""" 
90-         simple_post_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , '--http1_1' , '-P' , '-H' , 'content-type: application/json' , '-i' , '-d' , '\" {\' test\' :\' testval\' }\" ' , 'http://postman-echo.com/post' ]
106+         simple_post_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , '--http1_1' , '-P' , '-H' ,
107+                                                     'content-type: application/json' , '-i' , '-d' , '\" {\' test\' :\' testval\' }\" ' , f'http://{ TEST_HOST }  ]
91108        run_command (simple_post_args )
92109
93110    def  test_simple_download_h1 (self ):
94111        """download a large file via HTTP/1.1 and compare the results with something we assume works (e.g. urllib)""" 
95-         elasticurl_download_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , '--http1_1' , '-o' , 'elastigirl.png' , 'https://s3.amazonaws.com/code-sharing-aws-crt/elastigirl.png' ]
112+         elasticurl_download_args  =  elasticurl_cmd_prefix  +  \
113+             ['-v' , 'TRACE' , '--http1_1' , '-o' , 'elastigirl.png' ,
114+                 'https://s3.amazonaws.com/code-sharing-aws-crt/elastigirl.png' ]
96115        run_command (elasticurl_download_args )
97-         urllib .request .urlretrieve ('https://s3.amazonaws.com/code-sharing-aws-crt/elastigirl.png' , 'elastigirl_expected.png' )
116+         urllib .request .urlretrieve (
117+             'https://s3.amazonaws.com/code-sharing-aws-crt/elastigirl.png' , 'elastigirl_expected.png' )
98118
99119        compare_files ('elastigirl_expected.png' , 'elastigirl.png' )
100120
101121    def  test_simple_get_h2 (self ):
102122        """make a simple GET request via HTTP2 and make sure it succeeds""" 
103-         simple_get_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , '--http2' , 'https://postman-echo.com/get' ]
123+         simple_get_args  =  elasticurl_cmd_prefix  +  \
124+             ['-v' , 'TRACE' , '--http2' , f'https://{ TEST_HOST }  ]
104125        run_command (simple_get_args )
105126
106127    def  test_simple_post_h2 (self ):
107128        """make a simple POST request via HTTP2 to make sure sending data succeeds""" 
108-         simple_post_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , '--http2' , '-P' , '-H' , 'content-type: application/json' , '-i' , '-d' , '\" {\' test\' :\' testval\' }\" ' , 'https://postman-echo.com/post' ]
129+         simple_post_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , '--http2' , '-P' , '-H' ,
130+                                                     'content-type: application/json' , '-i' , '-d' , '\" {\' test\' :\' testval\' }\" ' , f'https://{ TEST_HOST }  ]
109131        run_command (simple_post_args )
110132
111133    def  test_simple_download_h2 (self ):
112134        """download a large file via HTTP2 and compare the results with something we assume works (e.g. urllib)""" 
113-         elasticurl_download_args  =  elasticurl_cmd_prefix  +  ['-v' , 'TRACE' , '--http2' , '-o' , 'elastigirl_h2.png' , 'https://d1cz66xoahf9cl.cloudfront.net/elastigirl.png' ]
135+         elasticurl_download_args  =  elasticurl_cmd_prefix  +  \
136+             ['-v' , 'TRACE' , '--http2' , '-o' , 'elastigirl_h2.png' ,
137+                 'https://d1cz66xoahf9cl.cloudfront.net/elastigirl.png' ]
114138        run_command (elasticurl_download_args )
115-         urllib .request .urlretrieve ('https://d1cz66xoahf9cl.cloudfront.net/elastigirl.png' , 'elastigirl_expected.png' )
139+         urllib .request .urlretrieve (
140+             'https://d1cz66xoahf9cl.cloudfront.net/elastigirl.png' , 'elastigirl_expected.png' )
116141
117142        compare_files ('elastigirl_expected.png' , 'elastigirl_h2.png' )
118143
144+ 
119145if  __name__  ==  '__main__' :
120146    unittest .main (verbosity = 2 )
0 commit comments