Created
August 25, 2014 21:30
-
-
Save peterbe/b68fe9bfe5e356973d62 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import inspect | |
import tempfile | |
import codecs | |
import StringIO | |
import gzip | |
import os | |
import re | |
import requests | |
from django.test.client import Client | |
from django.conf import settings | |
class ValidatorOperationalError(Exception): | |
pass | |
class ValidationError(AssertionError): | |
pass | |
class ValidatingClient(Client): | |
def get(self, *args, **kwargs): | |
response = super(ValidatingClient, self).get(*args, **kwargs) | |
disabled = getattr( | |
settings, | |
'VALIDATINGCLIENT_DISABLED', | |
True | |
) | |
if disabled: | |
return response | |
caller = inspect.stack()[1] | |
caller_file = caller[1] | |
caller_line = caller[2] | |
caller_name = caller[3] | |
if ( | |
response['Content-Type'].startswith('text/html') | |
and | |
response.status_code == 200 | |
): | |
try: | |
encoding = re.findall( | |
'charset=([\w-]+)', | |
response['Content-Type'] | |
)[0] | |
except IndexError: | |
encoding = 'utf-8' # sensible default | |
temp_dir = os.path.join( | |
tempfile.gettempdir(), 'outputtestingclient' | |
) | |
if not os.path.isdir(temp_dir): | |
os.mkdir(temp_dir) | |
temp_file = os.path.join( | |
temp_dir, | |
'%s-%s.html' % (caller_name, caller_line) | |
) | |
with codecs.open(temp_file, 'w', encoding) as f: | |
if not response.content: | |
raise ValueError('No response.content', args[0]) | |
f.write(response.content) | |
self._validate(temp_file, encoding, *args, **kwargs) | |
return response | |
def _validate(self, html_file, encoding, *args, **kwargs): | |
buf = StringIO.StringIO() | |
gzipper = gzip.GzipFile(fileobj=buf, mode='wb') | |
with codecs.open(html_file, 'r', encoding) as f: | |
gzipper.write(f.read()) | |
gzipper.close() | |
gzippeddata = buf.getvalue() | |
buf.close() | |
req = requests.post( | |
'http://html5.validator.nu/?out=text', | |
headers={ | |
'Content-Type': 'text/html', | |
'Accept-Encoding': 'gzip', | |
'Content-Encoding': 'gzip', | |
'Content-Length': len(gzippeddata), | |
}, | |
data=gzippeddata | |
) | |
if req.status_code != 200: | |
raise ValidatorOperationalError(req) | |
raise_exceptions = getattr( | |
settings, | |
'VALIDATINGCLIENT_HARDCORE', | |
False | |
) | |
messages = [] | |
for block in re.split('\n\n+', req.content): | |
messages.append(block) | |
if req.content: | |
print "VALIDATON TROUBLE" | |
print "To debug, see:" | |
print "\t", html_file | |
txt_file = re.sub('\.html$', '.txt', html_file) | |
print "\t", txt_file | |
with codecs.open(txt_file, 'w', encoding) as f: | |
f.write('Arguments to GET:\n') | |
for arg in args: | |
f.write('\t%s\n' % arg) | |
for k, w in kwargs.items(): | |
f.write('\t%s=%s\n' % (k, w)) | |
f.write('\n') | |
f.write(unicode(req.content, encoding)) | |
if raise_exceptions: | |
raise ValidationError(req.content) | |
else: | |
print "\n\n".join(messages) |
Example output can look like this:
$ t -x airmozilla/main/tests/test_views.py:TestPages.test_view_event_with_pin -x -s
VALIDATON TROUBLE
To debug, see:
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/assertRedirects-600.html
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/assertRedirects-600.txt
VALIDATON TROUBLE
To debug, see:
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/test_view_event_with_pin-195.html
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/test_view_event_with_pin-195.txt
VALIDATON TROUBLE
To debug, see:
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/test_view_event_with_pin-207.html
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/test_view_event_with_pin-207.txt
VALIDATON TROUBLE
To debug, see:
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/assertRedirects-600.html
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/assertRedirects-600.txt
VALIDATON TROUBLE
To debug, see:
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/test_view_event_with_pin-223.html
/var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/test_view_event_with_pin-223.txt
.
----------------------------------------------------------------------
Ran 1 test in 2.710s
And the output something like this:
$ cat /var/folders/l7/9ffkzwg965gf0zl261g34n_m0000gp/T/outputtestingclient/test_view_event_with_pin-223.txt
Arguments to GET:
/test-event/
Error: Element “div” not allowed as child of element “hgroup” in this context. (Suppressing further errors from this subtree.)
From line 89, column 11; to line 89, column 31
Error: Element “hgroup” is missing a required instance of one or more of the following child elements: “h1”, “h2”, “h3”, “h4”, “h5”, “h6”.
From line 102, column 15; to line 102, column 23
Error: Bad value “navigation” for attribute “role” on element “ul”.
From line 186, column 7; to line 186, column 28
Warning: Consider using the “h1” element as a top-level heading only (all “h1” elements are treated as top-level headings by many screen readers and other tools).
From line 112, column 7; to line 112, column 30
There were errors. (Tried in the text/html mode.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
By default, Django's TestCase class creates an instance of django.test.client.Client into the test class instance. This overrides that.
To really enable this you need to edit your settings:
And if you want it to raise an exception if there's any errors: