| 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 |
import logging |
| 30 |
import time |
| 31 |
from typing import ClassVar |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
from apprise import NotificationManager |
| 38 |
from apprise.common import ConfigFormat |
| 39 |
from apprise.config.http import ConfigHTTP |
| 40 |
from apprise.plugins import NotifyBase |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
|
| 45 |
N_MGR = NotificationManager() |
| 46 |
|
| 47 |
|
| 48 |
REQUEST_EXCEPTIONS = ( |
| 49 |
requests.ConnectionError(0, "requests.ConnectionError() not handled"), |
| 50 |
requests.RequestException(0, "requests.RequestException() not handled"), |
| 51 |
requests.HTTPError(0, "requests.HTTPError() not handled"), |
| 52 |
requests.ReadTimeout(0, "requests.ReadTimeout() not handled"), |
| 53 |
requests.TooManyRedirects(0, "requests.TooManyRedirects() not handled"), |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
@mock.patch("requests.post") |
| 58 |
def test_config_http(mock_post): |
| 59 |
""" |
| 60 |
API: ConfigHTTP() object |
| 61 |
|
| 62 |
""" |
| 63 |
|
| 64 |
|
| 65 |
class GoodNotification(NotifyBase): |
| 66 |
def __init__(self, *args, **kwargs): |
| 67 |
super().__init__(*args, **kwargs) |
| 68 |
|
| 69 |
def notify(self, *args, **kwargs): |
| 70 |
|
| 71 |
return True |
| 72 |
|
| 73 |
def url(self, **kwargs): |
| 74 |
|
| 75 |
return "" |
| 76 |
|
| 77 |
|
| 78 |
N_MGR["good"] = GoodNotification |
| 79 |
|
| 80 |
|
| 81 |
default_content = """taga,tagb=good://server01""" |
| 82 |
|
| 83 |
class DummyResponse: |
| 84 |
"""A dummy response used to manage our object.""" |
| 85 |
|
| 86 |
status_code = requests.codes.ok |
| 87 |
headers: ClassVar[dict[str, str]] = { |
| 88 |
"Content-Length": str(len(default_content)), |
| 89 |
"Content-Type": "text/plain", |
| 90 |
} |
| 91 |
|
| 92 |
text = default_content |
| 93 |
|
| 94 |
|
| 95 |
ptr = None |
| 96 |
|
| 97 |
def close(self): |
| 98 |
return |
| 99 |
|
| 100 |
def raise_for_status(self): |
| 101 |
return |
| 102 |
|
| 103 |
def __enter__(self): |
| 104 |
return self |
| 105 |
|
| 106 |
def __exit__(self, *args, **kwargs): |
| 107 |
return |
| 108 |
|
| 109 |
|
| 110 |
dummy_response = DummyResponse() |
| 111 |
mock_post.return_value = dummy_response |
| 112 |
|
| 113 |
assert ConfigHTTP.parse_url("garbage://") is None |
| 114 |
|
| 115 |
results = ConfigHTTP.parse_url("http://user:pass@localhost?+key=value") |
| 116 |
assert isinstance(results, dict) |
| 117 |
ch = ConfigHTTP(**results) |
| 118 |
assert isinstance(ch.url(), str) is True |
| 119 |
assert isinstance(ch.read(), str) is True |
| 120 |
|
| 121 |
|
| 122 |
assert len(ch) == 1 |
| 123 |
|
| 124 |
results = ConfigHTTP.parse_url("http://localhost:8080/path/") |
| 125 |
assert isinstance(results, dict) |
| 126 |
ch = ConfigHTTP(**results) |
| 127 |
assert isinstance(ch.url(), str) is True |
| 128 |
assert isinstance(ch.read(), str) is True |
| 129 |
|
| 130 |
|
| 131 |
assert len(ch) == 1 |
| 132 |
|
| 133 |
|
| 134 |
mock_post.reset_mock() |
| 135 |
|
| 136 |
|
| 137 |
results = ConfigHTTP.parse_url("http://localhost:8080/path/?cache=30") |
| 138 |
assert mock_post.call_count == 0 |
| 139 |
assert isinstance(ch.url(), str) is True |
| 140 |
|
| 141 |
assert isinstance(results, dict) |
| 142 |
ch = ConfigHTTP(**results) |
| 143 |
assert mock_post.call_count == 0 |
| 144 |
|
| 145 |
assert isinstance(ch.url(), str) is True |
| 146 |
assert mock_post.call_count == 0 |
| 147 |
|
| 148 |
assert isinstance(ch.read(), str) is True |
| 149 |
assert mock_post.call_count == 1 |
| 150 |
|
| 151 |
|
| 152 |
mock_post.reset_mock() |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
assert ch.expired() is True |
| 157 |
|
| 158 |
|
| 159 |
assert ch |
| 160 |
|
| 161 |
|
| 162 |
assert mock_post.call_count == 1 |
| 163 |
mock_post.reset_mock() |
| 164 |
|
| 165 |
|
| 166 |
assert ch.expired() is False |
| 167 |
assert len(ch) == 1 |
| 168 |
assert mock_post.call_count == 0 |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
mock_post.reset_mock() |
| 173 |
assert ch |
| 174 |
assert len(ch.servers()) == 1 |
| 175 |
assert len(ch) == 1 |
| 176 |
|
| 177 |
|
| 178 |
assert mock_post.call_count == 0 |
| 179 |
|
| 180 |
with mock.patch("time.time", return_value=time.time() + 10): |
| 181 |
|
| 182 |
assert ch.expired() is False |
| 183 |
assert ch |
| 184 |
assert len(ch.servers()) == 1 |
| 185 |
assert len(ch) == 1 |
| 186 |
|
| 187 |
|
| 188 |
assert mock_post.call_count == 0 |
| 189 |
|
| 190 |
with mock.patch("time.time", return_value=time.time() + 31): |
| 191 |
|
| 192 |
assert ch.expired() is True |
| 193 |
assert ch |
| 194 |
assert len(ch.servers()) == 1 |
| 195 |
assert len(ch) == 1 |
| 196 |
|
| 197 |
|
| 198 |
assert mock_post.call_count == 1 |
| 199 |
|
| 200 |
|
| 201 |
assert len(ch) == 1 |
| 202 |
|
| 203 |
|
| 204 |
results = ConfigHTTP.parse_url("http://localhost:8080/path/?cache=False") |
| 205 |
assert isinstance(results, dict) |
| 206 |
assert isinstance(ch.url(), str) is True |
| 207 |
|
| 208 |
results = ConfigHTTP.parse_url("http://localhost:8080/path/?cache=-10") |
| 209 |
assert isinstance(results, dict) |
| 210 |
with pytest.raises(TypeError): |
| 211 |
ch = ConfigHTTP(**results) |
| 212 |
|
| 213 |
results = ConfigHTTP.parse_url("http://user@localhost?format=text") |
| 214 |
assert isinstance(results, dict) |
| 215 |
ch = ConfigHTTP(**results) |
| 216 |
assert isinstance(ch.url(), str) is True |
| 217 |
assert isinstance(ch.read(), str) is True |
| 218 |
|
| 219 |
|
| 220 |
assert len(ch) == 1 |
| 221 |
|
| 222 |
results = ConfigHTTP.parse_url("https://localhost") |
| 223 |
assert isinstance(results, dict) |
| 224 |
ch = ConfigHTTP(**results) |
| 225 |
assert isinstance(ch.url(), str) is True |
| 226 |
assert isinstance(ch.read(), str) is True |
| 227 |
|
| 228 |
|
| 229 |
assert len(ch) == 1 |
| 230 |
|
| 231 |
|
| 232 |
ch = ConfigHTTP(**results) |
| 233 |
|
| 234 |
ref = ch[0] |
| 235 |
assert isinstance(ref, NotifyBase) is True |
| 236 |
|
| 237 |
ref_popped = ch.pop(0) |
| 238 |
assert isinstance(ref_popped, NotifyBase) is True |
| 239 |
|
| 240 |
assert ref == ref_popped |
| 241 |
|
| 242 |
assert len(ch) == 0 |
| 243 |
|
| 244 |
|
| 245 |
ch = ConfigHTTP(**results) |
| 246 |
assert isinstance(ch.pop(0), NotifyBase) is True |
| 247 |
|
| 248 |
ch = ConfigHTTP(**results) |
| 249 |
assert isinstance(ch[0], NotifyBase) is True |
| 250 |
|
| 251 |
assert isinstance(ch[0], NotifyBase) is True |
| 252 |
|
| 253 |
ch = ConfigHTTP(**results) |
| 254 |
|
| 255 |
iter(ch) |
| 256 |
|
| 257 |
iter(ch) |
| 258 |
|
| 259 |
|
| 260 |
ch.max_buffer_size = len(dummy_response.text) |
| 261 |
assert isinstance(ch.read(), str) is True |
| 262 |
|
| 263 |
|
| 264 |
yaml_supported_types = ( |
| 265 |
"text/yaml", |
| 266 |
"text/x-yaml", |
| 267 |
"application/yaml", |
| 268 |
"application/x-yaml", |
| 269 |
) |
| 270 |
|
| 271 |
for st in yaml_supported_types: |
| 272 |
dummy_response.headers["Content-Type"] = st |
| 273 |
ch.default_config_format = None |
| 274 |
assert isinstance(ch.read(), str) is True |
| 275 |
|
| 276 |
assert ch.default_config_format == ConfigFormat.YAML |
| 277 |
|
| 278 |
|
| 279 |
text_supported_types = ("text/plain", "text/html") |
| 280 |
|
| 281 |
for st in text_supported_types: |
| 282 |
dummy_response.headers["Content-Type"] = st |
| 283 |
ch.default_config_format = None |
| 284 |
assert isinstance(ch.read(), str) is True |
| 285 |
|
| 286 |
assert ch.default_config_format == ConfigFormat.TEXT |
| 287 |
|
| 288 |
|
| 289 |
ukwn_supported_types = ("text/css", "application/zip") |
| 290 |
|
| 291 |
for st in ukwn_supported_types: |
| 292 |
dummy_response.headers["Content-Type"] = st |
| 293 |
ch.default_config_format = None |
| 294 |
assert isinstance(ch.read(), str) is True |
| 295 |
|
| 296 |
assert ch.default_config_format is None |
| 297 |
|
| 298 |
|
| 299 |
del dummy_response.headers["Content-Type"] |
| 300 |
ch.default_config_format = None |
| 301 |
assert isinstance(ch.read(), str) is True |
| 302 |
|
| 303 |
assert ch.default_config_format is None |
| 304 |
|
| 305 |
|
| 306 |
dummy_response.headers["Content-Type"] = "text/plain" |
| 307 |
|
| 308 |
|
| 309 |
max_buffer_size = ch.max_buffer_size |
| 310 |
|
| 311 |
ch.max_buffer_size = len(dummy_response.text) - 1 |
| 312 |
assert ch.read() is None |
| 313 |
|
| 314 |
|
| 315 |
ch.max_buffer_size = max_buffer_size |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
dummy_response.headers["Content-Length"] = "garbage" |
| 320 |
|
| 321 |
assert isinstance(ch.read(), str) is True |
| 322 |
|
| 323 |
dummy_response.headers["Content-Length"] = "None" |
| 324 |
|
| 325 |
assert isinstance(ch.read(), str) is True |
| 326 |
|
| 327 |
|
| 328 |
dummy_response.text = "a" * ch.max_buffer_size |
| 329 |
|
| 330 |
assert isinstance(ch.read(), str) is True |
| 331 |
|
| 332 |
|
| 333 |
dummy_response.text = "b" * (ch.max_buffer_size + 1) |
| 334 |
assert ch.read() is None |
| 335 |
|
| 336 |
|
| 337 |
dummy_response.status_code = 400 |
| 338 |
assert ch.read() is None |
| 339 |
ch.max_error_buffer_size = 0 |
| 340 |
assert ch.read() is None |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
class RedirectResponse: |
| 345 |
"""Simulates a 301 response returned when allow_redirects=False.""" |
| 346 |
|
| 347 |
status_code = requests.codes.moved_permanently |
| 348 |
headers: ClassVar[dict[str, str]] = {} |
| 349 |
text = "" |
| 350 |
|
| 351 |
def raise_for_status(self): |
| 352 |
return |
| 353 |
|
| 354 |
def __enter__(self): |
| 355 |
return self |
| 356 |
|
| 357 |
def __exit__(self, *args, **kwargs): |
| 358 |
return |
| 359 |
|
| 360 |
mock_post.return_value = RedirectResponse() |
| 361 |
results = ConfigHTTP.parse_url("http://localhost/?redirect=no") |
| 362 |
assert isinstance(results, dict) |
| 363 |
ch_no_redir = ConfigHTTP(**results) |
| 364 |
assert ch_no_redir.redirects is False |
| 365 |
|
| 366 |
assert ch_no_redir.read() is None |
| 367 |
|
| 368 |
mock_post.return_value = dummy_response |
| 369 |
|
| 370 |
|
| 371 |
for exception in REQUEST_EXCEPTIONS: |
| 372 |
mock_post.side_effect = exception |
| 373 |
assert ch.read() is None |
| 374 |
|
| 375 |
|
| 376 |
ch.max_buffer_size = max_buffer_size |