-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #695 from emmo-repo/redirectioncheck
Added redirection checking tool
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ Pygments>=2.7.4,<3 | |
pyparsing>=2.4.7 | ||
PyYAML>=5.4.1,<7 | ||
rdflib>=4.2.1,<8 | ||
requests>=2.31,<3 | ||
semver>=2.8.1,<4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
redirections: | ||
https://example.com: https://example.com/ | ||
http://emmo.info/emmo: https://raw.githubusercontent.com/emmo-repo/EMMO/master/emmo.ttl | ||
https://w3id.org/emmo/1.0.0-beta5/: https://raw.githubusercontent.com/emmo-repo/EMMO/1.0.0-beta5/emmo.ttl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Test the `redirectioncheck` tool.""" | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from pathlib import Path | ||
from types import ModuleType | ||
from typing import Callable | ||
|
||
|
||
def test_run(get_tool: "Callable[[str], ModuleType]") -> None: | ||
"""Check that running `redirectioncheck` works. | ||
Parameters: | ||
get_tool: Local module fixture to load a named tool as a module. | ||
See the current folder's `conftest.py` file. | ||
""" | ||
from pathlib import Path | ||
|
||
yamlfile = ( | ||
Path(__file__).resolve().parent / "input" / "expected_redirections.yaml" | ||
) | ||
redirectioncheck = get_tool("redirectioncheck") | ||
|
||
# Make output more readable when running pytest with -s option | ||
print("\n\n") | ||
|
||
redirectioncheck.main([str(yamlfile), "-v"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/usr/bin/env python3 | ||
"""Script for testing URL redirections. | ||
The expected redirections are specified in a YAML file maping URLs to | ||
expected redirections: | ||
--- | ||
redirections: | ||
https://example.com: | | ||
https://example.com | ||
http://emmo.info/emmo: | | ||
https://raw.githubusercontent.com/emmo-repo/EMMO/master/emmo.ttl | ||
https://w3id.org/emmo/: | | ||
https://raw.githubusercontent.com/emmo-repo/EMMO/master/emmo.ttl | ||
""" | ||
import sys | ||
import argparse | ||
|
||
import yaml | ||
import requests | ||
|
||
|
||
def test_all(yamlfile, conf=None): | ||
"""Test all redirections specified in `yamlfile`. | ||
Arguments: | ||
yamlfile: File specifying redirections. | ||
conf: Dict with additional testing configurations. | ||
Returns: | ||
ok: Whether the redirection was successful. | ||
""" | ||
with open(yamlfile, "r") as f: | ||
specifications = yaml.safe_load(f) | ||
|
||
ok = True | ||
for url, redirected_url in specifications["redirections"].items(): | ||
ok &= test_redirection(url, redirected_url, conf=conf) | ||
|
||
return ok | ||
|
||
|
||
def test_redirection(url, redirected_url, conf=None): | ||
"""Test a redirection. | ||
Arguments: | ||
url: URL to test. | ||
redirected_url: Expected redirection. | ||
conf: Dict with additional testing configurations. | ||
Returns: | ||
ok: Whether the redirection was successful. | ||
""" | ||
verbose = conf.get("verbose") | ||
r = requests.get(url, timeout=5) | ||
ok, reason, got = r.ok, r.reason, None | ||
|
||
if r.ok and r.url != redirected_url: | ||
ok, reason, got = False, "Invalid redirection", r.url | ||
|
||
if verbose: | ||
print(f"{url:60} {reason}") | ||
if (got and verbose > 1) or verbose > 2: | ||
print(f" got: {got}") | ||
print(f" expected: {redirected_url}") | ||
print() | ||
|
||
return ok | ||
|
||
|
||
def main(argv: list = None): | ||
"""Main function for running redirection tests.""" | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
"yamlfile", | ||
nargs="?", | ||
default="redirections.yaml", | ||
help="Name of YAML file specifying redirections to test.", | ||
) | ||
parser.add_argument( | ||
"--verbose", | ||
"-v", | ||
action="count", | ||
default=1, | ||
help="Whether to print verbose output (default).", | ||
) | ||
parser.add_argument( | ||
"--silent", | ||
"--quiet", | ||
"-q", | ||
action="store_const", | ||
const=0, | ||
dest="verbose", | ||
help="Whether to be silent..", | ||
) | ||
args = parser.parse_args(args=argv) | ||
conf = {"verbose": args.verbose} | ||
|
||
return test_all(args.yamlfile, conf=conf) | ||
|
||
|
||
if __name__ == "__main__": | ||
success = main() | ||
sys.exit(0 if success else 1) |