From a29c16b770aa0bb53c632eecc1764caf02789fe7 Mon Sep 17 00:00:00 2001 From: Muriithi Frederick Muriuki Date: Fri, 9 Mar 2018 18:15:19 +0300 Subject: Add link_checker module * Add the module that will hold the code to test the links on the system. --- test/requests/link_checker.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 test/requests/link_checker.py (limited to 'test/requests/link_checker.py') diff --git a/test/requests/link_checker.py b/test/requests/link_checker.py new file mode 100644 index 00000000..f171c12a --- /dev/null +++ b/test/requests/link_checker.py @@ -0,0 +1,9 @@ +import requests + +def check_links(args_obj, parser): + print("") + print("Checking links") + print("########################") + print("Not implemented yet.") + print("This is supposed to check all links in the system.") + print("########################") -- cgit v1.2.3 From dd5ec1d3080b74fa065620f6915eeacf2cca8a2d Mon Sep 17 00:00:00 2001 From: Muriithi Frederick Muriuki Date: Mon, 12 Mar 2018 13:00:03 +0300 Subject: Add tests to check links. --- test/requests/link_checker.py | 62 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) (limited to 'test/requests/link_checker.py') diff --git a/test/requests/link_checker.py b/test/requests/link_checker.py index f171c12a..256bf6ef 100644 --- a/test/requests/link_checker.py +++ b/test/requests/link_checker.py @@ -1,9 +1,63 @@ +from __future__ import print_function +import re import requests +from lxml.html import parse +from requests.exceptions import ConnectionError + +def is_root_link(link): + pattern = re.compile("^/$") + return pattern.match(link) + +def is_mailto_link(link): + pattern = re.compile("^mailto:.*") + return pattern.match(link) + +def is_internal_link(link): + pattern = re.compile("^/.*") + return pattern.match(link) + +def get_links(doc): + return filter( + lambda x: not ( + is_root_link(x) + or is_mailto_link(x)) + , map(lambda y: y.get("href") + , doc.cssselect("a"))) + +def verify_link(link): + try: + result = requests.get(link, timeout=20) + if result.status_code == 200: + print(link+" ==> OK") + else: + print("ERROR: link `"+link+"` failed with status " + , result.status_code) + except ConnectionError as ex: + print("ERROR: ", link, ex) + +def check_page(host, start_url): + print("") + print("Checking links in page `"+start_url+"`") + doc = parse(start_url).getroot() + links = get_links(doc) + internal_links = filter(is_internal_link, links) + external_links = filter(lambda x: not is_internal_link(x), links) + external_links.append("http://somenon-existentsite.brr") + for link in internal_links: + verify_link(host+link) + + for link in external_links: + verify_link(link) def check_links(args_obj, parser): print("") print("Checking links") - print("########################") - print("Not implemented yet.") - print("This is supposed to check all links in the system.") - print("########################") + host = args_obj.host + + # Check the home page + check_page(host, host) + + # Check traits page + check_page( + host, + host+"/show_trait?trait_id=1435395_s_at&dataset=HC_M2_0606_P") -- cgit v1.2.3