Skip to content

Commit 59e30eb

Browse files
committed
Fixes to ietf/tests.py:
* Create new test client after an exception (the old one sticks in exception state * Don't try to retrieve empty URLs * Show how many URLS to check for reachability (currently 21594) - Legacy-Id: 856
1 parent 965563e commit 59e30eb

1 file changed

Lines changed: 34 additions & 28 deletions

File tree

ietf/tests.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
import traceback
66
import urllib2 as urllib
7+
import httplib
78
from urlparse import urljoin
89
from datetime import datetime
910

@@ -12,6 +13,7 @@
1213

1314
import django.test.simple
1415
from django.test import TestCase
16+
from django.test.client import Client
1517
from django.conf import settings
1618
from django.db import connection
1719
from django.core import management
@@ -68,6 +70,7 @@ def update_reachability(url, code, page):
6870
links = ( [ urljoin(url, a["href"]) for a in page.findAll("a") if a.has_key("href")]
6971
+ [ urljoin(url, img["src"]) for img in page.findAll("img") if a.has_key("src")] )
7072
for link in links:
73+
link = link.split("#")[0] # don't include fragment identifier
7174
if not link in module.reachability:
7275
module.reachability[link] = (None, url)
7376

@@ -226,7 +229,6 @@ def __init__(self, *args, **kwargs):
226229
# Setup and tear-down
227230

228231
def setUp(self):
229-
from django.test.client import Client
230232
self.client = Client()
231233

232234
self.testdb = settings.DATABASE_NAME
@@ -295,35 +297,39 @@ def testUrlsList(self):
295297

296298
def testUrlsReachability(self):
297299
# 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) )
299301
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"
325331
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))
327333

328334

329335
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)