Skip to content

Commit

Permalink
Bugfix: do not use json.loads() for response.json (#13)
Browse files Browse the repository at this point in the history
* Bugfix: do not use json.loads() for response.json

  - response.json() is already a json object, not a string
  - json.loads() causes error trying to load json object

---------

Co-authored-by: Juri Zirnsak <[email protected]>
  • Loading branch information
fhsctv and Juri Zirnsak authored Dec 13, 2023
1 parent a79253f commit 6a07fab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
5 changes: 2 additions & 3 deletions check_smseagle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from argparse import ArgumentParser
from urllib.parse import urljoin
import json
import os
import sys
import urllib3
Expand Down Expand Up @@ -150,7 +149,7 @@ def get_strength(response):
"""
Parse response and returns signal_strength of given modem
"""
data = json.loads(response.json())
data = response.json()
return data["signal_strength"]


Expand All @@ -166,7 +165,7 @@ def main(args):

try:
strength = get_strength(response)
except ValueError as parse_exc:
except Exception as parse_exc: # pylint: disable=broad-except
print("[UNKNOWN] - Couldn't evaluate the GSM signal strength", parse_exc)
return UNKNOWN

Expand Down
17 changes: 8 additions & 9 deletions test_check_smseagle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest.mock as mock
import sys
import os
import json

import check_smseagle

Expand Down Expand Up @@ -120,9 +121,9 @@ def test_main_ok(self, mock_req):
# Mock object for HTTP Response
r = mock.MagicMock()
# Mocking the JSON method on the response
r.json.return_value = """
r.json.return_value = json.loads("""
{"modem_no":1,"signal_strength":66}
"""
""")
# Set mock HTTP Reposonse as mock_request return value
mock_req.return_value = r

Expand All @@ -133,9 +134,9 @@ def test_main_ok(self, mock_req):
@mock.patch('check_smseagle.make_request')
def test_main_warn(self, mock_req):
r = mock.MagicMock()
r.json.return_value = """
r.json.return_value = json.loads("""
{"modem_no":1,"signal_strength":9}
"""
""")
mock_req.return_value = r

args = commandline(['-u', 'http://localhost', '-t', 'token', '-M', '1'])
Expand All @@ -145,9 +146,9 @@ def test_main_warn(self, mock_req):
@mock.patch('check_smseagle.make_request')
def test_main_critical(self, mock_req):
r = mock.MagicMock()
r.json.return_value = """
r.json.return_value = json.loads("""
{"modem_no":1,"signal_strength":1}
"""
""")
mock_req.return_value = r

args = commandline(['-u', 'http://localhost', '-t', 'token', '-M', '1'])
Expand All @@ -158,9 +159,7 @@ def test_main_critical(self, mock_req):
@mock.patch('check_smseagle.make_request')
def test_main_unknown(self, mock_req):
r = mock.MagicMock()
r.json.return_value = """
¯\_ (ツ)_/¯
"""
r.json.side_effect=KeyError('OHNO')
mock_req.return_value = r

args = commandline(['-u', 'http://localhost', '-t', 'token', '-M', '1'])
Expand Down

0 comments on commit 6a07fab

Please sign in to comment.