-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestsbase.py
More file actions
58 lines (44 loc) · 1.53 KB
/
Copy pathtestsbase.py
File metadata and controls
58 lines (44 loc) · 1.53 KB
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
import json
from numpy import mean
from hashlib import sha256
import requests
class testsbase():
"""
Range header tests
"""
def __init__(self, config):
with open(config, 'r') as f:
self.config = json.load(f)
def run(self, tests=None, vh=None, testfile='index.html'):
if vh is None:
vh = self.config['server'][0]
self.domain = vh['vhost']
self.ip = vh['ip']
self.port = vh['port']
self.docroot = vh['documentroot']
self.url = "http://" + self.domain + ':' + str(self.port) + '/' + testfile
self.testfile = self.docroot + '/' + testfile
try:
self.get = requests.get(self.url)
self.head = requests.head(self.url)
except Exception as err:
print("could not GET/HEAD file {} error: {}".format(self.url, err))
score = []
for t in tests:
try:
print("{0:12} {1:5} {2:40}: ".format(type(self).__name__, t.__name__, t.__doc__),end='')
result = t()
score.append(result)
print(result)
except Exception as err:
print("test crashed: {}".format(err))
return mean(score)
def check_byhash(self, response, offset=0, length=-1):
h = sha256()
m = sha256()
with open(self.testfile, "rb") as f:
f.seek(offset, 0)
data = f.read(length)
h.update(data)
m.update(response.content)
return (m.digest() == h.digest())