|
4 | 4 | import re |
5 | 5 | import traceback |
6 | 6 | import urllib2 as urllib |
| 7 | +import httplib |
7 | 8 | from urlparse import urljoin |
8 | 9 | from datetime import datetime |
9 | 10 |
|
|
12 | 13 |
|
13 | 14 | import django.test.simple |
14 | 15 | from django.test import TestCase |
| 16 | +from django.test.client import Client |
15 | 17 | from django.conf import settings |
16 | 18 | from django.db import connection |
17 | 19 | from django.core import management |
@@ -68,6 +70,7 @@ def update_reachability(url, code, page): |
68 | 70 | links = ( [ urljoin(url, a["href"]) for a in page.findAll("a") if a.has_key("href")] |
69 | 71 | + [ urljoin(url, img["src"]) for img in page.findAll("img") if a.has_key("src")] ) |
70 | 72 | for link in links: |
| 73 | + link = link.split("#")[0] # don't include fragment identifier |
71 | 74 | if not link in module.reachability: |
72 | 75 | module.reachability[link] = (None, url) |
73 | 76 |
|
@@ -226,7 +229,6 @@ def __init__(self, *args, **kwargs): |
226 | 229 | # Setup and tear-down |
227 | 230 |
|
228 | 231 | def setUp(self): |
229 | | - from django.test.client import Client |
230 | 232 | self.client = Client() |
231 | 233 |
|
232 | 234 | self.testdb = settings.DATABASE_NAME |
@@ -295,35 +297,39 @@ def testUrlsList(self): |
295 | 297 |
|
296 | 298 | def testUrlsReachability(self): |
297 | 299 | # This test should be sorted after the other tests which retrieve URLs |
298 | | - note("\nTesting URL reachability:") |
| 300 | + note("\nTesting URL reachability of %s URLs:" % len(module.reachability) ) |
299 | 301 | for url in module.reachability: |
300 | | - code, source = module.reachability[url] |
301 | | - if not code: |
302 | | - note(" %s" % ( url)) |
303 | | - if url.startswith("/"): |
304 | | - baseurl, args = split_url(url) |
305 | | - try: |
306 | | - code = str(self.client.get(baseurl, args).status_code) |
307 | | - except AssertionError: |
308 | | - note("Exception for URL '%s'" % url) |
309 | | - traceback.print_exc() |
310 | | - code = "500" |
311 | | - elif url.startswith("mailto:"): |
312 | | - continue |
313 | | - else: |
314 | | - try: |
315 | | - file = urllib.urlopen(url) |
316 | | - file.close() |
317 | | - code = "200" |
318 | | - except urllib.HTTPError, e: |
319 | | - code = str(e.code) |
320 | | - except urllib.InvalidURL, e: |
321 | | - note("Exception for URL '%s'" % url) |
322 | | - traceback.print_exc() |
323 | | - code = "500" |
324 | | - |
| 302 | + if url: |
| 303 | + code, source = module.reachability[url] |
| 304 | + if not code: |
| 305 | + print " %s" % ( url.strip() ) |
| 306 | + if url.startswith("/"): |
| 307 | + baseurl, args = split_url(url) |
| 308 | + try: |
| 309 | + code = str(self.client.get(baseurl, args).status_code) |
| 310 | + except AssertionError: |
| 311 | + note("Exception for URL '%s'" % url) |
| 312 | + traceback.print_exc() |
| 313 | + self.client = Client() |
| 314 | + code = "500" |
| 315 | + elif url.startswith("mailto:"): |
| 316 | + continue |
| 317 | + else: |
| 318 | + try: |
| 319 | + file = urllib.urlopen(url) |
| 320 | + file.close() |
| 321 | + code = "200" |
| 322 | + except urllib.HTTPError, e: |
| 323 | + code = str(e.code) |
| 324 | + except httplib.InvalidURL, e: |
| 325 | + note("Exception for URL '%s'" % url) |
| 326 | + traceback.print_exc() |
| 327 | + self.client = Client() |
| 328 | + code = "500" |
| 329 | + else: |
| 330 | + code = "500" |
325 | 331 | if not code in ["200"]: |
326 | | - note("Reach %3s %s (from %s)" % (code, url, source)) |
| 332 | + note("Reach %3s <%s> (from %s)\n" % (code, url, source)) |
327 | 333 |
|
328 | 334 |
|
329 | 335 | # ------------------------------------------------------------------------------ |
|
0 commit comments