-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester_server.py
123 lines (111 loc) · 4.88 KB
/
tester_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import asyncio
import json
import random
import string
import time
from multiprocessing import Process
import aiohttp
import brotli
from aiohttp import web
from environment import Environment
from random_helper import random_boolean
async def handle(request):
request_body = None
if request.can_read_body:
request_body = await request.text()
random_sleep = random.random() * 2 + 2.0
response_data = {
'\"': "\"",
"űáéúő": "\nűáéúőóüö\nűáéúőóüŰÁÉÚŐÓÜÖ",
'request': {
'method': request.method,
'url': str(request.url),
'host': request.host,
'body': request_body,
},
'multiline': "\n".join([string.ascii_lowercase for v in range(20)]),
'random': random.randint(0, 100),
'big_data': ''.join(random.choices(string.ascii_lowercase, k=1000)),
'random_string': ''.join(random.choices(string.ascii_lowercase, k=random.randint(1, 10))),
'random_dict': {random.choice(string.ascii_lowercase): random.randint(0, 100) for _ in
range(random.randint(1, 10))},
'sleep': random_sleep,
}
time.sleep(random_sleep)
status_code_rand = random.randint(0, 2)
status_code = 200
if status_code_rand == 0:
status_code = random.randint(200, 210)
elif status_code_rand == 1:
status_code = random.randint(400, 420)
elif status_code_rand == 2:
status_code = random.randint(500, 520)
if random_boolean():
return web.json_response(response_data, status=status_code)
json_bytes = json.dumps(response_data).encode('utf-8')
json_bytes_brotli = brotli.compress(json_bytes)
print('brotli compressed response size:', len(json_bytes_brotli), 'bytes')
return web.Response(body=json_bytes_brotli, status=status_code, headers={
'Content-Encoding': 'br',
'Content-Type': 'application/json; charset=utf-8',
})
def run_rest_api_server(environment: Environment):
app = web.Application(client_max_size=environment.HTTP_MAX_REQUEST_SIZE_MB * 1024 ** 2)
app.router.add_route('*', '/{any:.*}', handle)
try:
web.run_app(app, host=environment.TESTER_LISTEN_HOST, port=environment.TESTER_LISTEN_PORT)
except KeyboardInterrupt:
app.shutdown()
def run_rest_api_client(tester_destination: str):
async def fetch(session, method, url, body):
body_str = ''
if body is str:
body_str = body
elif body is dict:
body_str = json.dumps(body)
if len(body_str) > 128:
body_str = body_str[:128] + '...'
print(f'Tester fetch {method} {url} {body_str}...')
if body is dict:
async with session.request(method, url, json=body) as response:
return await response.text()
else:
headers = {
'content-type': 'text/plain',
}
async with session.request(method, url, data=body, headers=headers) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
while True:
try:
path = ''.join(random.choices(string.ascii_lowercase, k=random.randint(1, 10)))
method = random.choice(['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH'])
body = None
if method in ['POST', 'PUT', 'PATCH']:
body = {
'key': ''.join(random.choices(string.ascii_lowercase, k=random.randint(1, 10))),
'value': random.randint(0, 100),
}
query_parameters_string = ''
if random_boolean():
query_parameters_string = '?' + '&'.join(
[f'key_{i}={random.randint(0, 100)}' for i in range(random.randint(1, 8))])
if random.randint(0, 15) < 1:
body = ''.join(random.choices(string.ascii_lowercase, k=2 * 1024 * 1024))
response = await fetch(session, method, f'{tester_destination}/{path}{query_parameters_string}',
body=body)
if len(response) > 150:
response = response[:150] + '...'
print('... tester received response: ' + response)
await asyncio.sleep(random.random() * 3 + 0.1)
except KeyboardInterrupt:
break
except Exception as e:
print('Tester exception:', e)
asyncio.run(main())
def run_rest_api_tester(environment: Environment):
Process(target=run_rest_api_server, args=(environment,)).start()
time.sleep(2)
for _ in range(1):
Process(target=run_rest_api_client, args=(environment.TESTER_DESTINATION,)).start()