-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
checks.py
271 lines (218 loc) · 7.12 KB
/
checks.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import sys
from owslib.etree import etree
from GeoHealthCheck.util import CONFIG
from GeoHealthCheck.plugin import Plugin
from GeoHealthCheck.check import Check
from html import escape
""" Contains basic Check classes for a Probe object."""
class HttpStatusNoError(Check):
"""
Checks if HTTP status code is not in the 400- or 500-range.
"""
NAME = 'HTTP status should not be errored'
DESCRIPTION = 'Response should not contain a HTTP 400 or 500 range Error'
def __init__(self):
Check.__init__(self)
def perform(self):
"""Default check: Resource should at least give no error"""
status = self.probe.response.status_code
overall_status = status // 100
if overall_status in [4, 5]:
self.set_result(False, 'HTTP Error status=%d' % status)
class HttpHasHeaderValue(Check):
"""
Checks if header exists and has given header value.
See http://docs.python-requests.org/en/master/user/quickstart
"""
NAME = 'Has specific HTTP Header value'
DESCRIPTION = 'HTTP response has specific HTTP Header value'
PARAM_DEFS = {
'header_name': {
'type': 'string',
'description': 'The HTTP header name',
'default': None,
'required': True,
'range': None
},
'header_value': {
'type': 'string',
'description': 'The HTTP header value',
'default': None,
'required': True,
'range': None
}
}
"""Param defs"""
def __init__(self):
Check.__init__(self)
def perform(self):
result = True
msg = 'OK'
name = self.get_param('header_name')
value = self.get_param('header_value')
headers = self.probe.response.headers
if name not in headers:
result = False
msg = 'HTTP response has no header %s' % name
elif headers[name] != value:
result = False
msg = 'HTTP response header %s has no value %s' % (name, value)
self.set_result(result, msg)
class HttpHasContentType(HttpHasHeaderValue):
"""
Checks if HTTP response has content type.
"""
NAME = 'Has specific Content-Type'
DESCRIPTION = 'HTTP response has specific Content-Type'
PARAM_DEFS = Plugin.merge(HttpHasHeaderValue.PARAM_DEFS, {
'header_name': {
'value': 'content-type'
}
})
"""Params defs for header content type."""
def __init__(self):
HttpHasHeaderValue.__init__(self)
def perform(self):
HttpHasHeaderValue.perform(self)
class HttpHasImageContentType(Check):
"""
Checks if HTTP response has image content type.
"""
NAME = 'HTTP response is image'
DESCRIPTION = 'HTTP response has image/* Content-Type'
def __init__(self):
Check.__init__(self)
"""
Check if HTTP response has image/ ContentType header value
"""
def perform(self):
result = True
msg = 'OK'
name = 'content-type'
response = self.probe.response
headers = response.headers
if name not in headers:
result = False
msg = 'HTTP response has no header %s' % name
elif 'image/' not in headers[name]:
result = False
msg = 'HTTP response header %s is not image type' % name
if type(response.content) is str:
rsp_str = response.content
if len(rsp_str) > 256:
rsp_str = rsp_str[-256:]
msg += ' - error: ' + escape(rsp_str)
self.set_result(result, msg)
class XmlParse(Check):
"""
Checks if HTTP response is valid XML.
"""
NAME = 'Valid XML response'
DESCRIPTION = 'HTTP response contains valid XML'
def __init__(self):
Check.__init__(self)
def perform(self):
try:
etree.fromstring(
self.probe.response.content,
parser=etree.XMLParser(huge_tree=CONFIG['GHC_LARGE_XML']))
except Exception:
self.set_result(False, str(sys.exc_info()))
class JsonParse(Check):
"""
Checks if HTTP response is valid JSON.
"""
NAME = 'Valid JSON response'
DESCRIPTION = 'HTTP response contains valid JSON'
def __init__(self):
Check.__init__(self)
def perform(self):
import json
try:
json.loads(self.probe.response.content)
except Exception:
self.set_result(False, str(sys.exc_info()))
class ContainsStrings(Check):
"""
Checks if HTTP response contains given strings (keywords).
"""
NAME = 'Response contains strings'
DESCRIPTION = \
'HTTP response contains all (comma-separated) strings specified'
PARAM_DEFS = {
'strings': {
'type': 'stringlist',
'description':
'The string text(s) that should be contained \
in response (comma-separated)',
'default': None,
'required': True,
'range': None
}
}
"""Param defs"""
def __init__(self):
Check.__init__(self)
def perform(self):
result = True
msg = 'OK'
for text in self.get_param('strings'):
try:
result = text in self.probe.response.text
if result is False:
msg = '%s not in response text' % text
break
except Exception:
result = False
msg = str(sys.exc_info())
break
self.set_result(result, msg)
class NotContainsStrings(ContainsStrings):
"""
Checks if HTTP response NOT contains given strings (keywords).
"""
NAME = 'Response NOT contains strings'
DESCRIPTION = """
HTTP response does not contain any of the
(comma-separated) strings specified
"""
PARAM_DEFS = {
'strings': {
'type': 'stringlist',
'description':
"""The string text(s) that should NOT be
contained in response (comma-separated)""",
'default': None,
'required': True,
'range': None
}
}
"""Param defs"""
def __init__(self):
ContainsStrings.__init__(self)
def perform(self):
result = True
msg = 'OK'
for text in self.get_param('strings'):
try:
result = text not in self.probe.response.text
if result is False:
msg = '%s in response text' % text
break
except Exception:
result = False
msg = str(sys.exc_info())
break
self.set_result(result, msg)
class NotContainsOwsException(NotContainsStrings):
"""
Checks if HTTP response NOT contains given OWS Exceptions.
"""
NAME = 'Response NOT contains OWS Exception'
DESCRIPTION = 'HTTP response does not contain an OWS Exception'
PARAM_DEFS = Plugin.merge(ContainsStrings.PARAM_DEFS, {
'strings': {
'value': ['ExceptionReport>', 'ServiceException>']
}
})
"""Param defs"""