@@ -55,20 +55,34 @@ async def handle(request):
55
55
})
56
56
57
57
58
- def run_rest_api_server (host : str , port : int ):
59
- app = web .Application ()
58
+ def run_rest_api_server (environment : Environment ):
59
+ app = web .Application (client_max_size = environment . HTTP_MAX_REQUEST_SIZE_MB * 1024 ** 2 )
60
60
app .router .add_route ('*' , '/{any:.*}' , handle )
61
61
try :
62
- web .run_app (app , host = host , port = port )
62
+ web .run_app (app , host = environment . TESTER_LISTEN_HOST , port = environment . TESTER_LISTEN_PORT )
63
63
except KeyboardInterrupt :
64
64
app .shutdown ()
65
65
66
66
67
67
def run_rest_api_client (tester_destination : str ):
68
68
async def fetch (session , method , url , body ):
69
- print (f'Tester fetch { method } { url } { body } ...' )
70
- async with session .request (method , url , json = body ) as response :
71
- return await response .text ()
69
+ body_str = ''
70
+ if body is str :
71
+ body_str = body
72
+ elif body is dict :
73
+ body_str = json .dumps (body )
74
+ if len (body_str ) > 128 :
75
+ body_str = body_str [:128 ] + '...'
76
+ print (f'Tester fetch { method } { url } { body_str } ...' )
77
+ if body is dict :
78
+ async with session .request (method , url , json = body ) as response :
79
+ return await response .text ()
80
+ else :
81
+ headers = {
82
+ 'content-type' : 'text/plain' ,
83
+ }
84
+ async with session .request (method , url , data = body , headers = headers ) as response :
85
+ return await response .text ()
72
86
73
87
async def main ():
74
88
async with aiohttp .ClientSession () as session :
@@ -86,6 +100,8 @@ async def main():
86
100
if random_boolean ():
87
101
query_parameters_string = '?' + '&' .join (
88
102
[f'key_{ i } ={ random .randint (0 , 100 )} ' for i in range (random .randint (1 , 8 ))])
103
+ if random .randint (0 , 15 ) < 1 :
104
+ body = '' .join (random .choices (string .ascii_lowercase , k = 2 * 1024 * 1024 ))
89
105
response = await fetch (session , method , f'{ tester_destination } /{ path } { query_parameters_string } ' ,
90
106
body = body )
91
107
if len (response ) > 150 :
@@ -101,7 +117,7 @@ async def main():
101
117
102
118
103
119
def run_rest_api_tester (environment : Environment ):
104
- Process (target = run_rest_api_server , args = (environment . TESTER_LISTEN_HOST , environment . TESTER_LISTEN_PORT ,)).start ()
120
+ Process (target = run_rest_api_server , args = (environment ,)).start ()
105
121
time .sleep (2 )
106
122
for _ in range (1 ):
107
123
Process (target = run_rest_api_client , args = (environment .TESTER_DESTINATION ,)).start ()
0 commit comments