mirror of
https://github.com/drewkerrigan/nagios-http-json.git
synced 2024-11-22 02:13:47 +01:00
Merge pull request #53 from drewkerrigan/fix-issue-34
Add boundary check for SubArrayElement function
This commit is contained in:
commit
5c416cd0c0
@ -108,6 +108,7 @@ class JsonHelper:
|
||||
index = int(key[key.find(self.arrayOpener) +
|
||||
1:key.find(self.arrayCloser)])
|
||||
remainingKey = key[key.find(self.arrayCloser + self.separator) + 2:]
|
||||
|
||||
if key.find(self.arrayCloser + self.separator) == -1:
|
||||
remainingKey = key[key.find(self.arrayCloser) + 1:]
|
||||
if subElemKey in data:
|
||||
@ -115,6 +116,8 @@ class JsonHelper:
|
||||
return self.get(remainingKey, data[subElemKey][index])
|
||||
else:
|
||||
return (None, 'not_found')
|
||||
if index >= len(data):
|
||||
return (None, 'not_found')
|
||||
else:
|
||||
if not subElemKey:
|
||||
return self.get(remainingKey, data[index])
|
||||
|
@ -220,3 +220,26 @@ class UtilTest(unittest.TestCase):
|
||||
{ "gauges": { "jvm.buffers.direct.capacity": [
|
||||
{"value": 215415},{"value": 1235}]}}]''',
|
||||
WARNING_CODE)
|
||||
|
||||
def test_array_with_missing_element(self):
|
||||
"""
|
||||
See https://github.com/drewkerrigan/nagios-http-json/issues/34
|
||||
"""
|
||||
rules = RulesHelper()
|
||||
|
||||
# This should simply work
|
||||
data = '[{"Node": "there"}]'
|
||||
self.check_data(rules.dash_q(['(0).Node,there']), data, OK_CODE)
|
||||
|
||||
# This should warn us
|
||||
data = '[{"Node": "othervalue"}]'
|
||||
self.check_data(rules.dash_q(['(0).Node,there']), data, WARNING_CODE)
|
||||
|
||||
# # This should not throw an IndexError
|
||||
data = '[{"Node": "foobar"}]'
|
||||
self.check_data(rules.dash_q(['(0).Node,foobar', '(1).Node,missing']), data, WARNING_CODE)
|
||||
self.check_data(rules.dash_q(['(0).Node,foobar', '(1).Node,missing', '(2).Node,alsomissing']), data, WARNING_CODE)
|
||||
|
||||
# This should not throw a KeyError
|
||||
data = '{}'
|
||||
self.check_data(rules.dash_q(['(0).Node,foobar', '(1).Node,missing']), data, WARNING_CODE)
|
||||
|
Loading…
Reference in New Issue
Block a user