Skip to content

Commit

Permalink
add validation service
Browse files Browse the repository at this point in the history
  • Loading branch information
Themanwithoutaplan committed Aug 17, 2013
1 parent f3035cd commit 08317fe
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 10 deletions.
15 changes: 8 additions & 7 deletions deformdemo/scripts/radiochoice.html
Expand Up @@ -122,7 +122,6 @@
<h1>Radio Choice Widget</h1>
<div id="form"><form
id="deform"
action=""
method="POST"
enctype="multipart/form-data"
accept-charset="utf-8" class="deform"
Expand All @@ -134,14 +133,13 @@ <h1>Radio Choice Widget</h1>

<input type="hidden" name="_charset_" />
<input type="hidden" name="__formid__" value="deform"/>
<ul>






<li
class="field"
<ul>
<li
class="field "
title="Select a Pepper"
id="item-deformField1">

Expand Down Expand Up @@ -204,8 +202,11 @@ <h1>Radio Choice Widget</h1>
<!-- /mapping_item -->

</li>
</ul>


<ul>

<li class="buttons">

<button
Expand Down
101 changes: 98 additions & 3 deletions deformdemo/validation.py
@@ -1,6 +1,91 @@
from pyramid import testing
from pyramid.paster import bootstrap
from deformdemo import DeformDemo, main
import unittest
import httplib
import os
import sys
import re
import urlparse
import string
import gzip
import StringIO
import json


def validate(data):

extPat = re.compile(r'^.*\.([A-Za-z]+)$')
extDict = {
"html" : "text/html",
"htm" : "text/html",
"xhtml" : "application/xhtml+xml",
"xht" : "application/xhtml+xml",
"xml" : "application/xml",
}


errorsOnly = 0
encoding = None
contentType = "text/html"
service = 'http://html5.validator.nu/'

buf = StringIO.StringIO()
gzipper = gzip.GzipFile(fileobj=buf, mode='wb')
gzipper.write(data)
gzipper.close()
gzippeddata = buf.getvalue()
buf.close()

connection = None
response = None
status = 302
redirectCount = 0

url = service + '?out=json'
url = service + '?out=text'
if errorsOnly:
url = url + '&level=error'

while (status == 302 or status == 301 or status == 307) and redirectCount < 10:
if redirectCount > 0:
url = response.getheader('Location')
parsed = urlparse.urlsplit(url)
if parsed[0] != 'http':
sys.stderr.write('URI scheme %s not supported.\n' % parsed[0])
sys.exit(7)
if redirectCount > 0:
connection.close() # previous connection
print 'Redirecting to %s' % url
print 'Please press enter to continue or type "stop" followed by enter to stop.'
if raw_input() != "":
sys.exit(0)
connection = httplib.HTTPConnection(parsed[1])
connection.connect()
connection.putrequest("POST", "%s?%s" % (parsed[2], parsed[3]), skip_accept_encoding=1)
connection.putheader("Accept-Encoding", 'gzip')
connection.putheader("Content-Type", contentType)
connection.putheader("Content-Encoding", 'gzip')
connection.putheader("Content-Length", len(gzippeddata))
connection.endheaders()
connection.send(gzippeddata)
response = connection.getresponse()
status = response.status
redirectCount += 1

if status != 200:
sys.stderr.write('%s %s\n' % (status, response.reason))
sys.exit(5)

if response.getheader('Content-Encoding', 'identity').lower() == 'gzip':
response = gzip.GzipFile(fileobj=StringIO.StringIO(response.read()))

connection.close()
return response.read()
result = json.loads(response.read())
return result



class FunctionalTests(unittest.TestCase):
def setUp(self):
Expand All @@ -12,11 +97,21 @@ def setUp(self):
self.demos = DeformDemo(self.request)

def test_valid_html(self):
errors = []
demos_urls = self.demos.get_demos()
for demo in demos_urls:
res = self.testapp.get(demo[1], status=200)
import pdb; pdb.set_trace() # NOQA
#self.failUnless('Pyramid' in res.body)
check = validate(res.body)
#import pdb; pdb.set_trace() # NOQA
try:
self.assertFalse(check)
print demo[0], "."
except AssertionError, e:
errors.append((demo[0], check))
print demo[0], "E"
print check
print
#self.assertFalse(errors)

if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 08317fe

Please sign in to comment.