simple-http-checker-r-version 1.3.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_r_version-1.3.0/LICENSE +21 -0
- simple_http_checker_r_version-1.3.0/PKG-INFO +50 -0
- simple_http_checker_r_version-1.3.0/README.md +31 -0
- simple_http_checker_r_version-1.3.0/pyproject.toml +79 -0
- simple_http_checker_r_version-1.3.0/setup.cfg +4 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker/__init__.py +0 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker/checker.py +53 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker/cli.py +49 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker_r_version.egg-info/PKG-INFO +50 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker_r_version.egg-info/SOURCES.txt +14 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker_r_version.egg-info/dependency_links.txt +1 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker_r_version.egg-info/entry_points.txt +2 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker_r_version.egg-info/requires.txt +11 -0
- simple_http_checker_r_version-1.3.0/src/simple_http_checker_r_version.egg-info/top_level.txt +1 -0
- simple_http_checker_r_version-1.3.0/tests/test_check.py +135 -0
- simple_http_checker_r_version-1.3.0/tests/test_cli.py +42 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mohamedrabie92
|
|
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,50 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple-http-checker-r-version
|
|
3
|
+
Version: 1.3.0
|
|
4
|
+
Summary: A simple CLI tool to check the status of URLs.
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: requests<3.9,>=2.28
|
|
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
|
+
Requires-Dist: types-requests; extra == "dev"
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# Python for DevOps: CI/CD for Python Projects
|
|
21
|
+
|
|
22
|
+
This repository contains the complete CI/CD implementation for the CI/CD section of my **Python for DevOps: Mastering Real-World Automation** course. Here, you'll learn how to build a robust, automated pipeline for Python projects using GitHub Actions. Make sure to join the course to learn comprehensive Python automation skills for DevOps and infrastructure management!
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
**Note:** Here is the link to the main code repository for the project: [https://github.com/mohamedrabie92/python-devops-cicd-project](https://github.com/mohamedrabie92/python-devops-cicd-project)
|
|
26
|
+
|
|
27
|
+
## 🎯 Project Overview
|
|
28
|
+
|
|
29
|
+
This project demonstrates a complete CI/CD pipeline for a Python package called **Simple HTTP Checker** - a command-line tool for checking HTTP endpoint availability. Through this hands-on example, you'll learn modern Python development practices and automated deployment workflows.
|
|
30
|
+
|
|
31
|
+
## ✅ What we implement in this repository
|
|
32
|
+
|
|
33
|
+
- [X] **Implement the project (code files)** - Complete Python package with proper structure
|
|
34
|
+
- [x] **Add a simple GHA workflow** - Basic GitHub Actions setup and execution
|
|
35
|
+
- [x] **Add linting (ruff) and format checks (black)** - Code quality automation
|
|
36
|
+
- [x] **Add typing (mypy) and security checks (bandit)** - Static analysis and security scanning
|
|
37
|
+
- [x] **Add test automation** - Comprehensive test suite with pytest
|
|
38
|
+
- [X] **Build our Python project** - Automated package building
|
|
39
|
+
- [X] **Publish the project to both TestPyPI and PyPI** - Automated package publishing on releases
|
|
40
|
+
|
|
41
|
+
## 📚 Learning Objectives
|
|
42
|
+
|
|
43
|
+
Through this project, you'll master:
|
|
44
|
+
|
|
45
|
+
- **Modern Python packaging** with `pyproject.toml`
|
|
46
|
+
- **GitHub Actions** for CI/CD automation
|
|
47
|
+
- **Code quality tools** (Black, Ruff, MyPy, Bandit)
|
|
48
|
+
- **Automated testing** with pytest
|
|
49
|
+
- **Package publishing** to PyPI repositories
|
|
50
|
+
- **Release management** with Git tags and GitHub releases
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Python for DevOps: CI/CD for Python Projects
|
|
2
|
+
|
|
3
|
+
This repository contains the complete CI/CD implementation for the CI/CD section of my **Python for DevOps: Mastering Real-World Automation** course. Here, you'll learn how to build a robust, automated pipeline for Python projects using GitHub Actions. Make sure to join the course to learn comprehensive Python automation skills for DevOps and infrastructure management!
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
**Note:** Here is the link to the main code repository for the project: [https://github.com/mohamedrabie92/python-devops-cicd-project](https://github.com/mohamedrabie92/python-devops-cicd-project)
|
|
7
|
+
|
|
8
|
+
## 🎯 Project Overview
|
|
9
|
+
|
|
10
|
+
This project demonstrates a complete CI/CD pipeline for a Python package called **Simple HTTP Checker** - a command-line tool for checking HTTP endpoint availability. Through this hands-on example, you'll learn modern Python development practices and automated deployment workflows.
|
|
11
|
+
|
|
12
|
+
## ✅ What we implement in this repository
|
|
13
|
+
|
|
14
|
+
- [X] **Implement the project (code files)** - Complete Python package with proper structure
|
|
15
|
+
- [x] **Add a simple GHA workflow** - Basic GitHub Actions setup and execution
|
|
16
|
+
- [x] **Add linting (ruff) and format checks (black)** - Code quality automation
|
|
17
|
+
- [x] **Add typing (mypy) and security checks (bandit)** - Static analysis and security scanning
|
|
18
|
+
- [x] **Add test automation** - Comprehensive test suite with pytest
|
|
19
|
+
- [X] **Build our Python project** - Automated package building
|
|
20
|
+
- [X] **Publish the project to both TestPyPI and PyPI** - Automated package publishing on releases
|
|
21
|
+
|
|
22
|
+
## 📚 Learning Objectives
|
|
23
|
+
|
|
24
|
+
Through this project, you'll master:
|
|
25
|
+
|
|
26
|
+
- **Modern Python packaging** with `pyproject.toml`
|
|
27
|
+
- **GitHub Actions** for CI/CD automation
|
|
28
|
+
- **Code quality tools** (Black, Ruff, MyPy, Bandit)
|
|
29
|
+
- **Automated testing** with pytest
|
|
30
|
+
- **Package publishing** to PyPI repositories
|
|
31
|
+
- **Release management** with Git tags and GitHub releases
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>77.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "simple-http-checker-r-version"
|
|
7
|
+
version = "1.3.0"
|
|
8
|
+
description = "A simple CLI tool to check the status of URLs."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"requests>=2.28,<3.9",
|
|
13
|
+
"click>=8.0,<9.0"
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.optional-dependencies]
|
|
17
|
+
dev =[
|
|
18
|
+
"pytest",
|
|
19
|
+
"pytest-mock",
|
|
20
|
+
"ruff",
|
|
21
|
+
"black",
|
|
22
|
+
"mypy",
|
|
23
|
+
"bandit",
|
|
24
|
+
"types-requests"
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.scripts]
|
|
28
|
+
check-urls = "simple_http_checker.cli:main"
|
|
29
|
+
|
|
30
|
+
[tool.ruff]
|
|
31
|
+
line-length = 65
|
|
32
|
+
|
|
33
|
+
[tool.black]
|
|
34
|
+
|
|
35
|
+
line-length = 65
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
[tool.mypy]
|
|
39
|
+
warn_return_any = true
|
|
40
|
+
warn_unused_configs = true
|
|
41
|
+
ignore_missing_imports = true
|
|
42
|
+
|
|
43
|
+
[tool.bandit]
|
|
44
|
+
exclude_dirs = [".venv", "__pycache__", "build", "dist"]
|
|
45
|
+
skips = ["B101"]
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
addopts = "-rA -s -v"
|
|
49
|
+
|
|
50
|
+
[tool.semantic_release]
|
|
51
|
+
version_toml = ["pyproject.toml:project.version"]
|
|
52
|
+
build_command = "pip install build && python -m build"
|
|
53
|
+
dist_path = "dist/"
|
|
54
|
+
upload_to_pypi = false
|
|
55
|
+
upload_to_release = true
|
|
56
|
+
hvcs = "github"
|
|
57
|
+
commit_message = "chore(release):{version} [skip ci]"
|
|
58
|
+
|
|
59
|
+
[tool.semantic_release.branchs.main]
|
|
60
|
+
match = "main"
|
|
61
|
+
prerelease = false
|
|
62
|
+
|
|
63
|
+
[tool.semantic_release.changelog.default_template]
|
|
64
|
+
changelog_template = "CHANGELOG.md"
|
|
65
|
+
|
|
66
|
+
[tool.semantic_release.commit_parser_options]
|
|
67
|
+
allowed_tages = [
|
|
68
|
+
"build",
|
|
69
|
+
"chore",
|
|
70
|
+
"ci",
|
|
71
|
+
"docs",
|
|
72
|
+
"feat",
|
|
73
|
+
"fix",
|
|
74
|
+
"perf",
|
|
75
|
+
"refactor",
|
|
76
|
+
"test",
|
|
77
|
+
]
|
|
78
|
+
minor_tags = ["feat"]
|
|
79
|
+
patch_tags = ["fix", "perf"]
|
|
File without changes
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import requests
|
|
3
|
+
from typing import Collection
|
|
4
|
+
|
|
5
|
+
logger = logging.getLogger(__name__)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def check_urls(
|
|
9
|
+
urls: Collection[str], timeout: int = 5
|
|
10
|
+
) -> dict[str, str]:
|
|
11
|
+
"""
|
|
12
|
+
Checks a list of URLs and returns their status.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
urls: A list of URLs strings to check.
|
|
16
|
+
timeout: Maximum time in seconds to wait for each request. Defaults to 5.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
A dictionary mapping each URL to its status string.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
logger.info(
|
|
23
|
+
f"Starting check for {len(urls)} URLs with a timeout of {timeout}"
|
|
24
|
+
)
|
|
25
|
+
results: dict[str, str] = {}
|
|
26
|
+
for url in urls:
|
|
27
|
+
status = "UNKNOWN"
|
|
28
|
+
try:
|
|
29
|
+
logger.debug(f"Checking URL: {url}")
|
|
30
|
+
response = requests.get(url, timeout=timeout)
|
|
31
|
+
if response.ok:
|
|
32
|
+
status = f"{response.status_code} OK"
|
|
33
|
+
else:
|
|
34
|
+
status = (
|
|
35
|
+
f"{response.status_code} {response.reason}"
|
|
36
|
+
)
|
|
37
|
+
except requests.exceptions.Timeout:
|
|
38
|
+
status = "TIMEOUT"
|
|
39
|
+
logger.warning(f"Request to {url} timed out.")
|
|
40
|
+
except requests.exceptions.ConnectionError:
|
|
41
|
+
status = "CONNECTION_ERROR"
|
|
42
|
+
logger.warning(f"Connection error for {url}.")
|
|
43
|
+
except requests.exceptions.RequestException as e:
|
|
44
|
+
status = f"REQUEST_ERROR: {type(e).__name__}"
|
|
45
|
+
logger.error(
|
|
46
|
+
f"An unexpected error occurred for {url}: {e}",
|
|
47
|
+
exc_info=True,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
results[url] = status
|
|
51
|
+
logger.info(f"Checked {url:<40} -> {status}")
|
|
52
|
+
logger.info(" URL check finished.")
|
|
53
|
+
return results
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import click
|
|
3
|
+
from typing import Collection
|
|
4
|
+
from .checker import check_urls
|
|
5
|
+
|
|
6
|
+
logging.basicConfig(
|
|
7
|
+
level=logging.INFO,
|
|
8
|
+
format="[%(asctime)s] %(levelname)-8s %(name)s:%(message)s",
|
|
9
|
+
datefmt="%Y-%m-%d %H:%M:%S",
|
|
10
|
+
)
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@click.command()
|
|
15
|
+
@click.argument("urls", nargs=-1)
|
|
16
|
+
@click.option(
|
|
17
|
+
"--timeout",
|
|
18
|
+
default=5,
|
|
19
|
+
help="Timeout in seconds for each request.",
|
|
20
|
+
)
|
|
21
|
+
@click.option(
|
|
22
|
+
"--verbose", "-v", is_flag=True, help="Enable debug logging."
|
|
23
|
+
)
|
|
24
|
+
def main(urls: Collection[str], timeout: int, verbose: bool):
|
|
25
|
+
if verbose:
|
|
26
|
+
logging.getLogger().setLevel(logging.DEBUG)
|
|
27
|
+
logger.debug("Verbose logging enabled.")
|
|
28
|
+
|
|
29
|
+
logger.debug(f"Received urls: {urls}")
|
|
30
|
+
logger.debug(f"Received timeout: {timeout}")
|
|
31
|
+
logger.debug(f"Received verbose: {verbose}")
|
|
32
|
+
|
|
33
|
+
if not urls:
|
|
34
|
+
logger.warning("No URLs provided to check.")
|
|
35
|
+
click.echo("Usage: check-urls <URL1> <URL2> ...")
|
|
36
|
+
return
|
|
37
|
+
|
|
38
|
+
logger.info(f"Starting check for {len(urls)} URLs.")
|
|
39
|
+
|
|
40
|
+
results = check_urls(urls, timeout)
|
|
41
|
+
|
|
42
|
+
click.echo("\n--- Results ---")
|
|
43
|
+
for url, status in results.items():
|
|
44
|
+
if "OK" in status:
|
|
45
|
+
fg_color = "green"
|
|
46
|
+
|
|
47
|
+
else:
|
|
48
|
+
fg_color = "red"
|
|
49
|
+
click.secho(f"{url:<40} -> {status}", fg=fg_color)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple-http-checker-r-version
|
|
3
|
+
Version: 1.3.0
|
|
4
|
+
Summary: A simple CLI tool to check the status of URLs.
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: requests<3.9,>=2.28
|
|
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
|
+
Requires-Dist: types-requests; extra == "dev"
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# Python for DevOps: CI/CD for Python Projects
|
|
21
|
+
|
|
22
|
+
This repository contains the complete CI/CD implementation for the CI/CD section of my **Python for DevOps: Mastering Real-World Automation** course. Here, you'll learn how to build a robust, automated pipeline for Python projects using GitHub Actions. Make sure to join the course to learn comprehensive Python automation skills for DevOps and infrastructure management!
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
**Note:** Here is the link to the main code repository for the project: [https://github.com/mohamedrabie92/python-devops-cicd-project](https://github.com/mohamedrabie92/python-devops-cicd-project)
|
|
26
|
+
|
|
27
|
+
## 🎯 Project Overview
|
|
28
|
+
|
|
29
|
+
This project demonstrates a complete CI/CD pipeline for a Python package called **Simple HTTP Checker** - a command-line tool for checking HTTP endpoint availability. Through this hands-on example, you'll learn modern Python development practices and automated deployment workflows.
|
|
30
|
+
|
|
31
|
+
## ✅ What we implement in this repository
|
|
32
|
+
|
|
33
|
+
- [X] **Implement the project (code files)** - Complete Python package with proper structure
|
|
34
|
+
- [x] **Add a simple GHA workflow** - Basic GitHub Actions setup and execution
|
|
35
|
+
- [x] **Add linting (ruff) and format checks (black)** - Code quality automation
|
|
36
|
+
- [x] **Add typing (mypy) and security checks (bandit)** - Static analysis and security scanning
|
|
37
|
+
- [x] **Add test automation** - Comprehensive test suite with pytest
|
|
38
|
+
- [X] **Build our Python project** - Automated package building
|
|
39
|
+
- [X] **Publish the project to both TestPyPI and PyPI** - Automated package publishing on releases
|
|
40
|
+
|
|
41
|
+
## 📚 Learning Objectives
|
|
42
|
+
|
|
43
|
+
Through this project, you'll master:
|
|
44
|
+
|
|
45
|
+
- **Modern Python packaging** with `pyproject.toml`
|
|
46
|
+
- **GitHub Actions** for CI/CD automation
|
|
47
|
+
- **Code quality tools** (Black, Ruff, MyPy, Bandit)
|
|
48
|
+
- **Automated testing** with pytest
|
|
49
|
+
- **Package publishing** to PyPI repositories
|
|
50
|
+
- **Release management** with Git tags and GitHub releases
|
|
@@ -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_r_version.egg-info/PKG-INFO
|
|
8
|
+
src/simple_http_checker_r_version.egg-info/SOURCES.txt
|
|
9
|
+
src/simple_http_checker_r_version.egg-info/dependency_links.txt
|
|
10
|
+
src/simple_http_checker_r_version.egg-info/entry_points.txt
|
|
11
|
+
src/simple_http_checker_r_version.egg-info/requires.txt
|
|
12
|
+
src/simple_http_checker_r_version.egg-info/top_level.txt
|
|
13
|
+
tests/test_check.py
|
|
14
|
+
tests/test_cli.py
|
simple_http_checker_r_version-1.3.0/src/simple_http_checker_r_version.egg-info/dependency_links.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
simple_http_checker
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
import requests
|
|
3
|
+
from pytest_mock import MockerFixture
|
|
4
|
+
from simple_http_checker.checker import check_urls
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_check_urls_success(mocker: MockerFixture):
|
|
8
|
+
mock_requests_get = mocker.patch(
|
|
9
|
+
"simple_http_checker.checker.requests.get"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
mock_response = mocker.MagicMock(spec=requests.Response)
|
|
13
|
+
mock_response.status_code = 200
|
|
14
|
+
mock_response.text = "OK"
|
|
15
|
+
mock_response.ok = True
|
|
16
|
+
mock_requests_get.return_value = mock_response
|
|
17
|
+
urls = ["http://example.com"]
|
|
18
|
+
results = check_urls(urls)
|
|
19
|
+
mock_requests_get.assert_called_once_with(urls[0], timeout=5)
|
|
20
|
+
assert results[urls[0]] == "200 OK"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_check_urls_client_error(mocker: MockerFixture):
|
|
24
|
+
mock_requests_get = mocker.patch(
|
|
25
|
+
"simple_http_checker.checker.requests.get"
|
|
26
|
+
)
|
|
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
|
+
mock_requests_get.return_value = mock_response
|
|
33
|
+
urls = ["http://example.com/nonexistent"]
|
|
34
|
+
results = check_urls(urls)
|
|
35
|
+
mock_requests_get.assert_called_once_with(urls[0], timeout=5)
|
|
36
|
+
assert results[urls[0]] == "404 Not Found"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@pytest.mark.parametrize(
|
|
40
|
+
"error_exception, expected_status",
|
|
41
|
+
[
|
|
42
|
+
(requests.exceptions.Timeout, "TIMEOUT"),
|
|
43
|
+
(
|
|
44
|
+
requests.exceptions.ConnectionError,
|
|
45
|
+
"CONNECTION_ERROR",
|
|
46
|
+
),
|
|
47
|
+
(
|
|
48
|
+
requests.exceptions.RequestException,
|
|
49
|
+
"REQUEST_ERROR: RequestException",
|
|
50
|
+
),
|
|
51
|
+
],
|
|
52
|
+
)
|
|
53
|
+
def test_check_urls_request_exceptions(
|
|
54
|
+
mocker: MockerFixture,
|
|
55
|
+
error_exception: type[requests.exceptions.RequestException],
|
|
56
|
+
expected_status: str,
|
|
57
|
+
):
|
|
58
|
+
mock_requests_get = mocker.patch(
|
|
59
|
+
"simple_http_checker.checker.requests.get"
|
|
60
|
+
)
|
|
61
|
+
mock_requests_get.side_effect = error_exception(
|
|
62
|
+
f"Simulated {expected_status}"
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
urls = ["https://www.problem.com"]
|
|
66
|
+
results = check_urls(urls)
|
|
67
|
+
|
|
68
|
+
mock_requests_get.assert_called_once_with(urls[0], timeout=5)
|
|
69
|
+
assert results[urls[0]] == expected_status
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_check_urls_multiple(mocker: MockerFixture):
|
|
73
|
+
mock_requests_get = mocker.patch(
|
|
74
|
+
"simple_http_checker.checker.requests.get"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# First call: OK
|
|
78
|
+
mock_response_ok = mocker.MagicMock(spec=requests.Response)
|
|
79
|
+
mock_response_ok.status_code = 200
|
|
80
|
+
mock_response_ok.text = "OK"
|
|
81
|
+
mock_response_ok.ok = True
|
|
82
|
+
|
|
83
|
+
# Second call: Timeout
|
|
84
|
+
timeout_exception = requests.exceptions.Timeout(
|
|
85
|
+
"Simulated timeout"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
# Third call: 500 Server Error
|
|
89
|
+
mock_response_fail = mocker.MagicMock(spec=requests.Response)
|
|
90
|
+
mock_response_fail.status_code = 500
|
|
91
|
+
mock_response_fail.reason = "Server Error"
|
|
92
|
+
mock_response_fail.ok = False
|
|
93
|
+
|
|
94
|
+
mock_requests_get.side_effect = [
|
|
95
|
+
mock_response_ok,
|
|
96
|
+
timeout_exception,
|
|
97
|
+
mock_response_fail,
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
urls = [
|
|
101
|
+
"http://success.com",
|
|
102
|
+
"http://timeout.com",
|
|
103
|
+
"http://servererror.com",
|
|
104
|
+
]
|
|
105
|
+
results = check_urls(urls)
|
|
106
|
+
|
|
107
|
+
assert len(results) == 3
|
|
108
|
+
assert mock_requests_get.call_count == 3
|
|
109
|
+
assert results[urls[0]] == "200 OK"
|
|
110
|
+
assert results[urls[1]] == "TIMEOUT"
|
|
111
|
+
assert results[urls[2]] == "500 Server Error"
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_check_urls_empty_list():
|
|
115
|
+
results = check_urls([])
|
|
116
|
+
assert results == {}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def test_check_urls_custom_timeout(mocker: MockerFixture):
|
|
120
|
+
mock_requests_get = mocker.patch(
|
|
121
|
+
"simple_http_checker.checker.requests.get"
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
mock_response = mocker.MagicMock(spec=requests.Response)
|
|
125
|
+
mock_response.status_code = 200
|
|
126
|
+
mock_response.text = "OK"
|
|
127
|
+
mock_response.ok = True
|
|
128
|
+
mock_requests_get.return_value = mock_response
|
|
129
|
+
urls = ["http://example.com"]
|
|
130
|
+
custom_timeout = 10
|
|
131
|
+
results = check_urls(urls, timeout=custom_timeout)
|
|
132
|
+
mock_requests_get.assert_called_once_with(
|
|
133
|
+
urls[0], timeout=custom_timeout
|
|
134
|
+
)
|
|
135
|
+
assert results[urls[0]] == "200 OK"
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from click.testing import CliRunner
|
|
2
|
+
from pytest_mock import MockerFixture
|
|
3
|
+
from simple_http_checker.cli import main
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_no_urls():
|
|
7
|
+
runner = CliRunner()
|
|
8
|
+
result = runner.invoke(main, [])
|
|
9
|
+
assert result.exit_code == 0
|
|
10
|
+
assert "Usage: check-urls" in result.output
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_main_single_url(mocker: MockerFixture):
|
|
14
|
+
url = "http://example.com"
|
|
15
|
+
mock_check = mocker.patch(
|
|
16
|
+
"simple_http_checker.cli.check_urls"
|
|
17
|
+
)
|
|
18
|
+
mock_check.return_value = {url: "200 OK"}
|
|
19
|
+
|
|
20
|
+
runner = CliRunner()
|
|
21
|
+
result = runner.invoke(main, [url])
|
|
22
|
+
assert result.exit_code == 0
|
|
23
|
+
mock_check.assert_called_once_with((url,), 5)
|
|
24
|
+
assert "--- Results ---" in result.output
|
|
25
|
+
assert url in result.output
|
|
26
|
+
assert "-> 200 OK" in result.output
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_main_timeout_option(mocker: MockerFixture):
|
|
30
|
+
url = "http://example.com"
|
|
31
|
+
mock_check = mocker.patch(
|
|
32
|
+
"simple_http_checker.cli.check_urls"
|
|
33
|
+
)
|
|
34
|
+
mock_check.return_value = {url: "TIMEOUT"}
|
|
35
|
+
|
|
36
|
+
runner = CliRunner()
|
|
37
|
+
result = runner.invoke(main, [url, "--timeout", "10"])
|
|
38
|
+
assert result.exit_code == 0
|
|
39
|
+
mock_check.assert_called_once_with((url,), 10)
|
|
40
|
+
assert "--- Results ---" in result.output
|
|
41
|
+
assert url in result.output
|
|
42
|
+
assert "-> TIMEOUT" in result.output
|