File: test_network_new.py

package info (click to toggle)
simplemonitor 1.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,520 kB
  • sloc: python: 8,725; sh: 258; makefile: 74; javascript: 69
file content (87 lines) | stat: -rw-r--r-- 2,473 bytes parent folder | download
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
import unittest
from unittest.mock import Mock, patch

from requests import Response
from requests.auth import HTTPBasicAuth

from simplemonitor.Monitors import MonitorHTTP
from simplemonitor.util import MonitorState


class TestMonitorHTTP(unittest.TestCase):
    def test_get_ok_default_params(self):
        response_mock = Mock(spec=Response)
        response_mock.status_code = 200

        monitor = MonitorHTTP(
            name="test_http_monitor",
            config_options={
                "url": "http://example.com",
                "urgent": 0,
                "tolerance": 1,
                "remote_alert": 1,
            },
        )

        with patch("requests.request", return_value=response_mock) as mock:
            monitor.run_test()

        result = monitor.get_result()
        state = monitor.state()

        mock.assert_called_once_with(
            "GET",
            "http://example.com",
            headers=None,
            timeout=5,
            verify=True,
            allow_redirects=True,
            auth=None,
            cert=None,
            data=None,
            json=None,
        )
        self.assertEqual(state, MonitorState.OK)
        self.assertIn("200", result)

    def test_get_ok_non_default_params(self):
        response_mock = Mock(spec=Response)
        response_mock.status_code = 200

        monitor = MonitorHTTP(
            name="test_http_monitor",
            config_options={
                "url": "http://example.com",
                "urgent": 0,
                "tolerance": 1,
                "remote_alert": 1,
                "verify_hostname": False,
                "allow_redirects": False,
                "username": "test",
                "password": "pass",
                "headers": '{"test": "header"}',
                "timeout": 10,
            },
        )

        with patch("requests.request", return_value=response_mock) as mock:
            monitor.run_test()

        result = monitor.get_result()
        state = monitor.state()

        mock.assert_called_once_with(
            "GET",
            "http://example.com",
            headers={"test": "header"},
            timeout=10,
            verify=False,
            allow_redirects=False,
            auth=HTTPBasicAuth("test", "pass"),
            cert=None,
            data=None,
            json=None,
        )
        self.assertEqual(state, MonitorState.OK)
        self.assertIn("200", result)
        pass