[py-svn] r36546 - py/dist/py/test/web

guido at codespeak.net guido at codespeak.net
Fri Jan 12 11:58:58 CET 2007


Author: guido
Date: Fri Jan 12 11:58:56 2007
New Revision: 36546

Added:
   py/dist/py/test/web/
   py/dist/py/test/web/__init__.py
   py/dist/py/test/web/exception.py
   py/dist/py/test/web/post_multipart.py
   py/dist/py/test/web/webcheck.py
Log:
Test XHTML wellformedness and validity (using validator.w3.org).


Added: py/dist/py/test/web/__init__.py
==============================================================================

Added: py/dist/py/test/web/exception.py
==============================================================================
--- (empty file)
+++ py/dist/py/test/web/exception.py	Fri Jan 12 11:58:56 2007
@@ -0,0 +1,6 @@
+class CSSError(Exception):
+    """raised when there's a problem with the CSS"""
+
+class HTMLError(Exception):
+    """raised when there's a problem with the HTML"""
+

Added: py/dist/py/test/web/post_multipart.py
==============================================================================
--- (empty file)
+++ py/dist/py/test/web/post_multipart.py	Fri Jan 12 11:58:56 2007
@@ -0,0 +1,58 @@
+import httplib, mimetypes
+
+"""Copied from the cookbook 
+
+    see ActiveState's ASPN 
+    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
+"""
+
+def post_multipart(host, selector, fields, files):
+    """
+    Post fields and files to an http host as multipart/form-data.
+    fields is a sequence of (name, value) elements for regular form fields.
+    files is a sequence of (name, filename, value) elements for data to be 
+    uploaded as files
+
+    Return the server's response page.
+    """
+    content_type, body = encode_multipart_formdata(fields, files)
+    h = httplib.HTTP(host)
+    h.putrequest('POST', selector)
+    h.putheader('content-type', content_type)
+    h.putheader('content-length', str(len(body)))
+    h.endheaders()
+    h.send(body)
+    errcode, errmsg, headers = h.getreply()
+    return h.file.read()
+
+def encode_multipart_formdata(fields, files):
+    """
+    fields is a sequence of (name, value) elements for regular form fields.
+    files is a sequence of (name, filename, value) elements for data to be 
+    uploaded as files
+    
+    Return (content_type, body) ready for httplib.HTTP instance
+    """
+    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
+    CRLF = '\r\n'
+    L = []
+    for (key, value) in fields:
+        L.append('--' + BOUNDARY)
+        L.append('Content-Disposition: form-data; name="%s"' % key)
+        L.append('')
+        L.append(value)
+    for (key, filename, value) in files:
+        L.append('--' + BOUNDARY)
+        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % 
+                        (key, filename))
+        L.append('Content-Type: %s' % get_content_type(filename))
+        L.append('')
+        L.append(value)
+    L.append('--' + BOUNDARY + '--')
+    L.append('')
+    body = CRLF.join(L)
+    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
+    return content_type, body
+
+def get_content_type(filename):
+    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'

Added: py/dist/py/test/web/webcheck.py
==============================================================================
--- (empty file)
+++ py/dist/py/test/web/webcheck.py	Fri Jan 12 11:58:56 2007
@@ -0,0 +1,41 @@
+import py
+import re
+from exception import *
+from post_multipart import post_multipart
+#import css_checker
+
+def check_html(string):
+    """check an HTML string for wellformedness and validity"""
+    tempdir = py.test.ensuretemp('check_html')
+    filename = 'temp%s.html' % (hash(string), )
+    tempfile = tempdir.join(filename)
+    tempfile.write(string)
+    ret = post_multipart('validator.w3.org', '/check', [], 
+                [('uploaded_file', 'somehtml.html', string)])
+    is_valid = get_validation_result_from_w3_html(ret)
+    return is_valid
+
+reg_validation_result = re.compile('<td[^>]*class="(in)?valid"[^>]*>([^<]*)<',
+                                    re.M | re.S)
+def get_validation_result_from_w3_html(html):
+    match = reg_validation_result.search(html)
+    valid = match.group(1) is None
+    text = match.group(2).strip()
+    if not valid:
+        temp = py.test.ensuretemp('/w3_results_%s.html' % hash(html), dir=0)
+        temp.write(html)
+        raise HTMLError(
+            "The html is not valid. See the report file at '%s'" % temp)
+    return valid
+
+#def check_css(string, basepath, htmlpath='/'):
+#    """check the CSS of an HTML string
+#    
+#        check whether an HTML string contains CSS rels, and if so check whether
+#        any classes defined in the HTML actually have a matching CSS selector 
+#    """
+#    c = css_checker.css_checker(string, basepath, htmlpath)
+#    # raises a CSSError when failing, this is done from the tester class to
+#    # allow being more verbose than just 'something went wrong'
+#    return c.check()
+



More information about the pytest-commit mailing list