@@ -2634,23 +2634,43 @@ def assert_link_status_code_is_not_404(self, link):
2634
2634
bad_link_str = 'Error: "%s" returned a 404!' % link
2635
2635
self .assertNotEqual (status_code , "404" , bad_link_str )
2636
2636
2637
+ def __get_link_if_404_error (self , link ):
2638
+ status_code = str (self .get_link_status_code (link ))
2639
+ if status_code == "404" :
2640
+ return link
2641
+ else :
2642
+ return None
2643
+
2637
2644
def assert_no_404_errors (self , multithreaded = True ):
2638
2645
""" Assert no 404 errors from page links obtained from:
2639
- "a"->"href", "img"->"src", "link"->"href", and "script"->"src". """
2646
+ "a"->"href", "img"->"src", "link"->"href", and "script"->"src".
2647
+ (A 404 error represents a broken link on a web page.) """
2640
2648
all_links = self .get_unique_links ()
2641
2649
links = []
2642
2650
for link in all_links :
2643
2651
if "javascript:" not in link and "mailto:" not in link :
2644
2652
links .append (link )
2653
+ broken_links = []
2645
2654
if multithreaded :
2646
2655
from multiprocessing .dummy import Pool as ThreadPool
2647
2656
pool = ThreadPool (10 )
2648
- pool .map (self .assert_link_status_code_is_not_404 , links )
2657
+ results = pool .map (self .__get_link_if_404_error , links )
2649
2658
pool .close ()
2650
2659
pool .join ()
2660
+ for result in results :
2661
+ if result :
2662
+ broken_links .append (result )
2651
2663
else :
2664
+ broken_links = []
2652
2665
for link in links :
2653
- self .assert_link_status_code_is_not_404 (link )
2666
+ if self .__get_link_if_404_error (link ):
2667
+ broken_links .append (link )
2668
+ if len (broken_links ) > 0 :
2669
+ bad_links_str = "\n " .join (broken_links )
2670
+ if len (broken_links ) == 1 :
2671
+ self .fail ("Broken link detected:\n %s" % bad_links_str )
2672
+ elif len (broken_links ) > 1 :
2673
+ self .fail ("Broken links detected:\n %s" % bad_links_str )
2654
2674
if self .demo_mode :
2655
2675
a_t = "ASSERT NO 404 ERRORS"
2656
2676
if self ._language != "English" :
0 commit comments