Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created August 25, 2014 21:30
Show Gist options
  • Save peterbe/b68fe9bfe5e356973d62 to your computer and use it in GitHub Desktop.
Save peterbe/b68fe9bfe5e356973d62 to your computer and use it in GitHub Desktop.
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)
@peterbe
Copy link
Author

peterbe commented Aug 25, 2014

Example usage:

import os
import shutil

from django.test import TestCase
from django.conf import settings
from django.contrib.auth.models import User

from .validatingclient import ValidatingClient


class DjangoTestCase(TestCase):

    def shortDescription(self):
        return None

    def setUp(self):
        super(DjangoTestCase, self).setUp()
        self.client = ValidatingClient()

    def tearDown(self):
        super(DjangoTestCase, self).tearDown()
        assert os.path.basename(settings.MEDIA_ROOT).startswith('testmedia')
        if os.path.isdir(settings.MEDIA_ROOT):
            shutil.rmtree(settings.MEDIA_ROOT)

        super(DjangoTestCase, self).tearDown()

    def _login(self, username='mary', email='mary@mozilla.com', pwd='secret'):
        user = User.objects.create_user(
            username, email, pwd
        )
        assert self.client.login(username=username, password=pwd)
        return user

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:

VALIDATINGCLIENT_DISABLED = False

And if you want it to raise an exception if there's any errors:

VALIDATINGCLIENT_HARDCORE = True

@peterbe
Copy link
Author

peterbe commented Aug 25, 2014

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