simple-http-checker-d-superteach 1.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simple_http_checker_d_superteach-1.2.0/LICENSE +21 -0
- simple_http_checker_d_superteach-1.2.0/PKG-INFO +31 -0
- simple_http_checker_d_superteach-1.2.0/README.md +13 -0
- simple_http_checker_d_superteach-1.2.0/pyproject.toml +66 -0
- simple_http_checker_d_superteach-1.2.0/setup.cfg +4 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker/__init__.py +0 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker/checker.py +53 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker/cli.py +50 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker_d_superteach.egg-info/PKG-INFO +31 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker_d_superteach.egg-info/SOURCES.txt +14 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker_d_superteach.egg-info/dependency_links.txt +1 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker_d_superteach.egg-info/entry_points.txt +2 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker_d_superteach.egg-info/requires.txt +10 -0
- simple_http_checker_d_superteach-1.2.0/src/simple_http_checker_d_superteach.egg-info/top_level.txt +1 -0
- simple_http_checker_d_superteach-1.2.0/tests/test_checker.py +127 -0
- simple_http_checker_d_superteach-1.2.0/tests/test_cli.py +76 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mvpris
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple-http-checker-d-superteach
|
|
3
|
+
Version: 1.2.0
|
|
4
|
+
Summary: A simple CLI tool to check the status of URLs.
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: requests<3.0,>=2.34
|
|
9
|
+
Requires-Dist: click<9.0,>=8.0
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: pytest; extra == "dev"
|
|
12
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
13
|
+
Requires-Dist: ruff; extra == "dev"
|
|
14
|
+
Requires-Dist: black; extra == "dev"
|
|
15
|
+
Requires-Dist: mypy; extra == "dev"
|
|
16
|
+
Requires-Dist: bandit; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Python for DevOps: CI/CD for Python Projects
|
|
20
|
+
|
|
21
|
+
This repo contains the code for the CI/CD section of my Python for DevOps course.
|
|
22
|
+
|
|
23
|
+
## What we implement in this project
|
|
24
|
+
|
|
25
|
+
- [x] Implement the project (code files)
|
|
26
|
+
- [x] Add a simple GHA workflow and make sure it runs until completion
|
|
27
|
+
- [x] Add lint (`ruff`) and format (`black`) checks
|
|
28
|
+
- [x] Add type (`mypy`) and security (`bandit`) checks
|
|
29
|
+
- [x] Add test automation
|
|
30
|
+
- [x] Build the project
|
|
31
|
+
- [ ] Publish the project to both `TestPyPI` and `PyPI` when a new tag is pushed
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Python for DevOps: CI/CD for Python Projects
|
|
2
|
+
|
|
3
|
+
This repo contains the code for the CI/CD section of my Python for DevOps course.
|
|
4
|
+
|
|
5
|
+
## What we implement in this project
|
|
6
|
+
|
|
7
|
+
- [x] Implement the project (code files)
|
|
8
|
+
- [x] Add a simple GHA workflow and make sure it runs until completion
|
|
9
|
+
- [x] Add lint (`ruff`) and format (`black`) checks
|
|
10
|
+
- [x] Add type (`mypy`) and security (`bandit`) checks
|
|
11
|
+
- [x] Add test automation
|
|
12
|
+
- [x] Build the project
|
|
13
|
+
- [ ] Publish the project to both `TestPyPI` and `PyPI` when a new tag is pushed
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "simple-http-checker-d-superteach"
|
|
7
|
+
version = "1.2.0"
|
|
8
|
+
description = "A simple CLI tool to check the status of URLs."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = ["requests>=2.34,<3.0", "click>=8.0,<9.0"]
|
|
12
|
+
|
|
13
|
+
[project.optional-dependencies]
|
|
14
|
+
dev = ["pytest", "pytest-mock", "ruff", "black", "mypy", "bandit"]
|
|
15
|
+
|
|
16
|
+
[project.scripts]
|
|
17
|
+
check-urls = "simple_http_checker.cli:main"
|
|
18
|
+
|
|
19
|
+
[tool.ruff]
|
|
20
|
+
line-length = 88
|
|
21
|
+
|
|
22
|
+
[tool.black]
|
|
23
|
+
line-length = 88
|
|
24
|
+
|
|
25
|
+
[tool.mypy]
|
|
26
|
+
warn_return_any = true
|
|
27
|
+
warn_unused_configs = true
|
|
28
|
+
|
|
29
|
+
[tool.bandit]
|
|
30
|
+
exclude_dirs = [".venv", "__pycache__", "build", "dirs"]
|
|
31
|
+
skips = ["B101"]
|
|
32
|
+
|
|
33
|
+
[tool.pytest.ini_options]
|
|
34
|
+
addopts = "-rA -s -v"
|
|
35
|
+
|
|
36
|
+
[tool.semantic_release]
|
|
37
|
+
version_toml = ["pyproject.toml:project.version"]
|
|
38
|
+
build_command = "pip install build && python -m build"
|
|
39
|
+
dist_path = "dist/"
|
|
40
|
+
upload_to_pypi = false
|
|
41
|
+
upload_to_release = true
|
|
42
|
+
hvcs = "github"
|
|
43
|
+
commit_message = "chore(release): {version} [skip ci]"
|
|
44
|
+
|
|
45
|
+
[tool.semantic_release.branches.main]
|
|
46
|
+
match = "main"
|
|
47
|
+
prerelease = false
|
|
48
|
+
|
|
49
|
+
[tool.semantic_release.changelog.default_templates]
|
|
50
|
+
changelog_file = "CHANGELOG.md"
|
|
51
|
+
|
|
52
|
+
[tool.semantic_release.commit_parser_options]
|
|
53
|
+
allowed = [
|
|
54
|
+
"build",
|
|
55
|
+
"chore",
|
|
56
|
+
"ci",
|
|
57
|
+
"docs",
|
|
58
|
+
"feat",
|
|
59
|
+
"fix",
|
|
60
|
+
"perf",
|
|
61
|
+
"refactor",
|
|
62
|
+
"style",
|
|
63
|
+
"test",
|
|
64
|
+
]
|
|
65
|
+
minor_tags = ["feat"]
|
|
66
|
+
patch_tags = ["fix", "perf"]
|
|
File without changes
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from collections.abc import Collection
|
|
3
|
+
|
|
4
|
+
import requests
|
|
5
|
+
|
|
6
|
+
logger = logging.getLogger(__name__)
|
|
7
|
+
|
|
8
|
+
# Uncomment line below to showcase workflow fail -> security check error from bandit
|
|
9
|
+
# API_TOKEN = "ghp_ThisLooksLikeAPersonalGitHubAccessToken"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def check_urls(urls: Collection[str], timeout: int = 5) -> dict[str, str]:
|
|
13
|
+
"""
|
|
14
|
+
Checks a list of URLs and returns their status.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
urls: A list of URLs to check.
|
|
18
|
+
timeout: Maximum time in seconds to wait for each request. Defaults to 5.
|
|
19
|
+
|
|
20
|
+
Return:
|
|
21
|
+
A dictionary mapping each URL to its status string.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
logger.info(f"Starting check for {len(urls)} URLs with a timeout of {timeout} s.")
|
|
25
|
+
results: dict[str, str] = {}
|
|
26
|
+
|
|
27
|
+
for url in urls:
|
|
28
|
+
status = "UNKNOWN"
|
|
29
|
+
|
|
30
|
+
try:
|
|
31
|
+
logger.debug(f"Checking URL: {url}")
|
|
32
|
+
response = requests.get(url, timeout=timeout)
|
|
33
|
+
|
|
34
|
+
if response.ok:
|
|
35
|
+
status = f"{response.status_code} OK"
|
|
36
|
+
else:
|
|
37
|
+
status = f"{response.status_code} {response.reason}"
|
|
38
|
+
|
|
39
|
+
except requests.exceptions.Timeout:
|
|
40
|
+
status = "TIMEOUT"
|
|
41
|
+
logger.warning(f"Request to {url} timed out.")
|
|
42
|
+
except requests.exceptions.ConnectionError:
|
|
43
|
+
status = "CONNECTION_ERROR"
|
|
44
|
+
logger.warning(f"Connection error for {url}")
|
|
45
|
+
except requests.exceptions.RequestException as e:
|
|
46
|
+
status = f"REQUEST_ERROR: {type(e).__name__}"
|
|
47
|
+
# logger.error(f"An unexpected request error occured for {url}: {e}", exc_info=True)
|
|
48
|
+
logger.exception(f"An unexpected request error occured for {url}")
|
|
49
|
+
results[url] = status
|
|
50
|
+
logger.debug(f"Checked {url:<40} -> {status}")
|
|
51
|
+
|
|
52
|
+
logger.info("URL check finished.")
|
|
53
|
+
return results
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from collections.abc import Collection
|
|
3
|
+
|
|
4
|
+
import click
|
|
5
|
+
|
|
6
|
+
from .checker import check_urls
|
|
7
|
+
|
|
8
|
+
logging.basicConfig(
|
|
9
|
+
level=logging.INFO,
|
|
10
|
+
format="[%(asctime)s] %(levelname)-8s %(name)s: %(message)s",
|
|
11
|
+
datefmt="%Y-%m-%d %H:%M:%S",
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@click.command()
|
|
18
|
+
@click.argument("urls", nargs=-1)
|
|
19
|
+
@click.option("--timeout", "-t", default=5, help="Timeout in seconds for each request.")
|
|
20
|
+
@click.option("--verbose", "-v", is_flag=True, help="Enable debug logging.")
|
|
21
|
+
def main(urls: Collection[str], timeout: int, verbose: bool):
|
|
22
|
+
if verbose:
|
|
23
|
+
logging.getLogger().setLevel(logging.DEBUG)
|
|
24
|
+
logger.debug("Verbose logging enabled.")
|
|
25
|
+
|
|
26
|
+
logger.debug(f"Received urls: {urls}")
|
|
27
|
+
logger.debug(f"Received timeout: {timeout}")
|
|
28
|
+
logger.debug(f"Received verbose: {verbose}")
|
|
29
|
+
|
|
30
|
+
if not urls:
|
|
31
|
+
logger.warning("No URLs provided to check.")
|
|
32
|
+
click.echo("Usage: check-urls <URL1> <URL2> ...")
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
logger.info(f"Starting check for {len(urls)} URLs.")
|
|
36
|
+
|
|
37
|
+
results = check_urls(urls, timeout=timeout)
|
|
38
|
+
|
|
39
|
+
click.echo("\n--- RESULTS ---")
|
|
40
|
+
for url, status in results.items():
|
|
41
|
+
if "OK" in status:
|
|
42
|
+
fg_color = "green"
|
|
43
|
+
else:
|
|
44
|
+
fg_color = "red"
|
|
45
|
+
click.secho(f"{url:<40} -> {status}", fg=fg_color)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# Example usage (in-terminal command)
|
|
49
|
+
# pip install -e .
|
|
50
|
+
# check-urls -vt10 https://www.google.com https://www.github.com https://www.aajdfhhjsdbgsjbndfjok.com http://httpbin.org/status/404
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple-http-checker-d-superteach
|
|
3
|
+
Version: 1.2.0
|
|
4
|
+
Summary: A simple CLI tool to check the status of URLs.
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: requests<3.0,>=2.34
|
|
9
|
+
Requires-Dist: click<9.0,>=8.0
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: pytest; extra == "dev"
|
|
12
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
13
|
+
Requires-Dist: ruff; extra == "dev"
|
|
14
|
+
Requires-Dist: black; extra == "dev"
|
|
15
|
+
Requires-Dist: mypy; extra == "dev"
|
|
16
|
+
Requires-Dist: bandit; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Python for DevOps: CI/CD for Python Projects
|
|
20
|
+
|
|
21
|
+
This repo contains the code for the CI/CD section of my Python for DevOps course.
|
|
22
|
+
|
|
23
|
+
## What we implement in this project
|
|
24
|
+
|
|
25
|
+
- [x] Implement the project (code files)
|
|
26
|
+
- [x] Add a simple GHA workflow and make sure it runs until completion
|
|
27
|
+
- [x] Add lint (`ruff`) and format (`black`) checks
|
|
28
|
+
- [x] Add type (`mypy`) and security (`bandit`) checks
|
|
29
|
+
- [x] Add test automation
|
|
30
|
+
- [x] Build the project
|
|
31
|
+
- [ ] Publish the project to both `TestPyPI` and `PyPI` when a new tag is pushed
|
simple_http_checker_d_superteach-1.2.0/src/simple_http_checker_d_superteach.egg-info/SOURCES.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/simple_http_checker/__init__.py
|
|
5
|
+
src/simple_http_checker/checker.py
|
|
6
|
+
src/simple_http_checker/cli.py
|
|
7
|
+
src/simple_http_checker_d_superteach.egg-info/PKG-INFO
|
|
8
|
+
src/simple_http_checker_d_superteach.egg-info/SOURCES.txt
|
|
9
|
+
src/simple_http_checker_d_superteach.egg-info/dependency_links.txt
|
|
10
|
+
src/simple_http_checker_d_superteach.egg-info/entry_points.txt
|
|
11
|
+
src/simple_http_checker_d_superteach.egg-info/requires.txt
|
|
12
|
+
src/simple_http_checker_d_superteach.egg-info/top_level.txt
|
|
13
|
+
tests/test_checker.py
|
|
14
|
+
tests/test_cli.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
simple_http_checker_d_superteach-1.2.0/src/simple_http_checker_d_superteach.egg-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
simple_http_checker
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
import requests
|
|
3
|
+
from pytest_mock import MockerFixture
|
|
4
|
+
|
|
5
|
+
from simple_http_checker.checker import check_urls
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_check_urls_success(mocker: MockerFixture) -> None:
|
|
9
|
+
mock_requests_get = mocker.patch("simple_http_checker.checker.requests.get")
|
|
10
|
+
|
|
11
|
+
mock_response = mocker.MagicMock(spec=requests.Response)
|
|
12
|
+
mock_response.status_code = 200
|
|
13
|
+
mock_response.reason = "OK"
|
|
14
|
+
mock_response.ok = True
|
|
15
|
+
|
|
16
|
+
mock_requests_get.return_value = mock_response
|
|
17
|
+
|
|
18
|
+
urls = ["https://www.example.com"]
|
|
19
|
+
results = check_urls(urls)
|
|
20
|
+
|
|
21
|
+
mock_requests_get.assert_called_once_with(urls[0], timeout=5)
|
|
22
|
+
assert results[urls[0]] == "200 OK"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_check_urls_client_error(mocker: MockerFixture) -> None:
|
|
26
|
+
mock_requests_get = mocker.patch("simple_http_checker.checker.requests.get")
|
|
27
|
+
|
|
28
|
+
mock_response = mocker.MagicMock(spec=requests.Response)
|
|
29
|
+
mock_response.status_code = 404
|
|
30
|
+
mock_response.reason = "Not Found"
|
|
31
|
+
mock_response.ok = False
|
|
32
|
+
|
|
33
|
+
mock_requests_get.return_value = mock_response
|
|
34
|
+
|
|
35
|
+
urls = ["https://www.example.com/nonexistent"]
|
|
36
|
+
results = check_urls(urls)
|
|
37
|
+
|
|
38
|
+
mock_requests_get.assert_called_once_with(urls[0], timeout=5)
|
|
39
|
+
assert results[urls[0]] == "404 Not Found"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@pytest.mark.parametrize(
|
|
43
|
+
"error_exception, expected_status",
|
|
44
|
+
[
|
|
45
|
+
(requests.exceptions.Timeout, "TIMEOUT"),
|
|
46
|
+
(requests.exceptions.ConnectionError, "CONNECTION_ERROR"),
|
|
47
|
+
(requests.exceptions.RequestException, "REQUEST_ERROR: RequestException"),
|
|
48
|
+
],
|
|
49
|
+
# ids=["Timeout", "ConnectionError", "RequestException"],
|
|
50
|
+
)
|
|
51
|
+
def test_check_urls_requests_exceptions(
|
|
52
|
+
mocker: MockerFixture,
|
|
53
|
+
error_exception: type[requests.exceptions.RequestException],
|
|
54
|
+
expected_status: str,
|
|
55
|
+
) -> None:
|
|
56
|
+
mock_requests_get = mocker.patch("simple_http_checker.checker.requests.get")
|
|
57
|
+
|
|
58
|
+
mock_requests_get.side_effect = error_exception(f"Simulated {expected_status}")
|
|
59
|
+
|
|
60
|
+
urls = ["https://www.problem.com"]
|
|
61
|
+
results = check_urls(urls)
|
|
62
|
+
|
|
63
|
+
mock_requests_get.assert_called_once_with(urls[0], timeout=5)
|
|
64
|
+
assert results[urls[0]] == expected_status
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_check_urls_with_multiple_urls(mocker: MockerFixture) -> None:
|
|
68
|
+
mock_requests_get = mocker.patch("simple_http_checker.checker.requests.get")
|
|
69
|
+
|
|
70
|
+
# First call: OK
|
|
71
|
+
mock_response_ok = mocker.MagicMock(spec=requests.Response)
|
|
72
|
+
mock_response_ok.status_code = 200
|
|
73
|
+
mock_response_ok.reason = "OK"
|
|
74
|
+
mock_response_ok.ok = True
|
|
75
|
+
|
|
76
|
+
# Second call: Timeout
|
|
77
|
+
timeout_exception = requests.exceptions.Timeout("Simulated timeout")
|
|
78
|
+
|
|
79
|
+
# Third call: 500 Server Error
|
|
80
|
+
mock_response_fail = mocker.MagicMock(spec=requests.Response)
|
|
81
|
+
mock_response_fail.status_code = 500
|
|
82
|
+
mock_response_fail.reason = "Server Error"
|
|
83
|
+
mock_response_fail.ok = False
|
|
84
|
+
|
|
85
|
+
# Set (return_value, actually side_effect) to a list of the 3 calls
|
|
86
|
+
# i.e., call the mock 3 times by passing 3 URLs
|
|
87
|
+
mock_requests_get.side_effect = [
|
|
88
|
+
mock_response_ok,
|
|
89
|
+
timeout_exception,
|
|
90
|
+
mock_response_fail,
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
urls = [
|
|
94
|
+
"https://www.success.com",
|
|
95
|
+
"https://www.timeout.com",
|
|
96
|
+
"https://www.servererror.com",
|
|
97
|
+
]
|
|
98
|
+
results = check_urls(urls)
|
|
99
|
+
|
|
100
|
+
assert len(results) == 3
|
|
101
|
+
assert mock_requests_get.call_count == 3
|
|
102
|
+
assert results["https://www.success.com"] == "200 OK"
|
|
103
|
+
assert results["https://www.timeout.com"] == "TIMEOUT"
|
|
104
|
+
assert results["https://www.servererror.com"] == "500 Server Error"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_check_urls_empty_list() -> None:
|
|
108
|
+
results = check_urls([])
|
|
109
|
+
assert results == {}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_check_urls_custom_timeout(mocker: MockerFixture) -> None:
|
|
113
|
+
mock_requests_get = mocker.patch("simple_http_checker.checker.requests.get")
|
|
114
|
+
|
|
115
|
+
mock_response = mocker.MagicMock(spec=requests.Response)
|
|
116
|
+
mock_response.status_code = 200
|
|
117
|
+
mock_response.reason = "OK"
|
|
118
|
+
mock_response.ok = True
|
|
119
|
+
|
|
120
|
+
mock_requests_get.return_value = mock_response
|
|
121
|
+
|
|
122
|
+
urls = ["https://www.example.com"]
|
|
123
|
+
custom_timeout = 10
|
|
124
|
+
results = check_urls(urls, timeout=custom_timeout)
|
|
125
|
+
|
|
126
|
+
mock_requests_get.assert_called_once_with(urls[0], timeout=custom_timeout)
|
|
127
|
+
assert results[urls[0]] == "200 OK"
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from click.testing import CliRunner
|
|
2
|
+
from pytest_mock import MockerFixture
|
|
3
|
+
|
|
4
|
+
from simple_http_checker.cli import main
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_main_no_url() -> None:
|
|
8
|
+
runner = CliRunner()
|
|
9
|
+
result = runner.invoke(main, [])
|
|
10
|
+
|
|
11
|
+
assert result.exit_code == 0
|
|
12
|
+
assert "Usage: check-urls" in result.output
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_main_single_url_success(mocker: MockerFixture) -> None:
|
|
16
|
+
url = "https://www.example.com"
|
|
17
|
+
mock_check_urls = mocker.patch("simple_http_checker.cli.check_urls")
|
|
18
|
+
mock_check_urls.return_value = {url: "200 OK"}
|
|
19
|
+
|
|
20
|
+
runner = CliRunner()
|
|
21
|
+
result = runner.invoke(main, [url])
|
|
22
|
+
|
|
23
|
+
# For line below: click collects pos-args into a tuple (url,), not a list [url]
|
|
24
|
+
mock_check_urls.assert_called_once_with((url,), timeout=5)
|
|
25
|
+
assert result.exit_code == 0
|
|
26
|
+
|
|
27
|
+
assert "--- RESULTS ---" in result.output
|
|
28
|
+
assert url in result.output
|
|
29
|
+
assert "-> 200 OK" in result.output
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_main_custom_timeout(mocker: MockerFixture) -> None:
|
|
33
|
+
url = "https://www.timeout.com"
|
|
34
|
+
mock_check_urls = mocker.patch("simple_http_checker.cli.check_urls")
|
|
35
|
+
mock_check_urls.return_value = {url: "TIMEOUT"}
|
|
36
|
+
|
|
37
|
+
runner = CliRunner()
|
|
38
|
+
result = runner.invoke(main, [url, "--timeout", "10"])
|
|
39
|
+
|
|
40
|
+
mock_check_urls.assert_called_once_with((url,), timeout=10)
|
|
41
|
+
assert result.exit_code == 0
|
|
42
|
+
|
|
43
|
+
assert "--- RESULTS ---" in result.output
|
|
44
|
+
assert url in result.output
|
|
45
|
+
assert "-> TIMEOUT" in result.output
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_main_multiple_urls(mocker: MockerFixture) -> None:
|
|
49
|
+
urls = (
|
|
50
|
+
"https://www.example1.com",
|
|
51
|
+
"https://www.example2.com",
|
|
52
|
+
"https://www.clienterror.com",
|
|
53
|
+
"https://www.servererror.com",
|
|
54
|
+
)
|
|
55
|
+
mock_check_urls = mocker.patch("simple_http_checker.cli.check_urls")
|
|
56
|
+
mock_check_urls.return_value = {
|
|
57
|
+
urls[0]: "200 OK",
|
|
58
|
+
urls[1]: "200 OK",
|
|
59
|
+
urls[2]: "404 Client Error",
|
|
60
|
+
urls[3]: "504 Server Error",
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
runner = CliRunner()
|
|
64
|
+
result = runner.invoke(main, urls)
|
|
65
|
+
|
|
66
|
+
mock_check_urls.assert_called_once_with(urls, timeout=5)
|
|
67
|
+
assert result.exit_code == 0
|
|
68
|
+
|
|
69
|
+
assert "--- RESULTS ---" in result.output
|
|
70
|
+
assert urls[0] in result.output
|
|
71
|
+
assert urls[1] in result.output
|
|
72
|
+
assert urls[2] in result.output
|
|
73
|
+
assert urls[3] in result.output
|
|
74
|
+
assert "-> 200 OK" in result.output
|
|
75
|
+
assert "-> 404 Client Error" in result.output
|
|
76
|
+
assert "-> 504 Server Error" in result.output
|