[CalendarServer-changes] [4711] CalendarServer/trunk/twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Thu Nov 5 20:26:42 PST 2009


Revision: 4711
          http://trac.macosforge.org/projects/calendarserver/changeset/4711
Author:   glyph at apple.com
Date:     2009-11-05 20:26:38 -0800 (Thu, 05 Nov 2009)
Log Message:
-----------
Add some basic coverage for directory listings.

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/extensions.py

Added Paths:
-----------
    CalendarServer/trunk/twistedcaldav/test/test_extensions.py

Modified: CalendarServer/trunk/twistedcaldav/extensions.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/extensions.py	2009-11-05 23:13:07 UTC (rev 4710)
+++ CalendarServer/trunk/twistedcaldav/extensions.py	2009-11-06 04:26:38 UTC (rev 4711)
@@ -1,3 +1,4 @@
+# -*- test-case-name: twistedcaldav.test.test_extensions -*-
 ##
 # Copyright (c) 2005-2009 Apple Inc. All rights reserved.
 #

Added: CalendarServer/trunk/twistedcaldav/test/test_extensions.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_extensions.py	                        (rev 0)
+++ CalendarServer/trunk/twistedcaldav/test/test_extensions.py	2009-11-06 04:26:38 UTC (rev 4711)
@@ -0,0 +1,97 @@
+
+from xml.etree.cElementTree import XML# , tostring
+
+from twisted.trial.unittest import TestCase
+
+from twisted.internet.defer import inlineCallbacks
+from twisted.python.filepath import FilePath
+
+from twisted.web.microdom import parseString
+
+from twistedcaldav.extensions import DAVFile
+
+class SimpleFakeRequest(object):
+    """
+    Emulate a very small portion of the web2 'Request' API, just enough to
+    render a L{DAVFile}.
+
+    @ivar path: the path portion of the URL being rendered.
+    """
+
+    def __init__(self, path):
+        self.path = path
+
+
+    def urlForResource(self, resource):
+        """
+        @return: this L{SimpleFakeRequest}'s 'path' attribute, since this
+            request can render only one thing.
+        """
+        return self.path
+
+
+
+def browserHTML2ETree(htmlString):
+    """
+    Loosely interpret an HTML string as XML and return an ElementTree object for it.
+    
+    We're not promising strict XML (in fact, we're specifically saying HTML) in
+    the content-type of certain responses, but it's much easier to work with
+    the ElementTree data structures present in Python 2.5+ for testing; so
+    we'll use Twisted's built-in facilities to sanitize the inputs before
+    making any structured assertions about them.
+
+    A more precise implementation would use
+    U{HTML5Lib<http://code.google.com/p/html5lib/wiki/UserDocumentation>}'s
+    etree bindings to do the parsing, as that is more directly 'what a browser
+    would do', but Twisted's built-in stuff is a good approximation and doesn't
+    drag in another dependency.
+
+    @param htmlString: a L{str}, encoded in UTF-8, representing a pile of
+        browser-friendly HTML tag soup.
+
+    @return: an object implementing the standard library ElementTree interface.
+    """
+    return XML(parseString(htmlString, beExtremelyLenient=True).toxml().decode("utf-8"))
+
+
+
+class DirectoryListingTest(TestCase):
+    """
+    Test cases for HTML directory listing.
+    """
+
+    @inlineCallbacks
+    def doDirectoryTest(self, expectedNames):
+        """
+        Do a test of a L{DAVFile} pointed at a directory, verifying that files
+        existing with the given names will be faithfully 'played back' via HTML
+        rendering.
+        """
+        fp = FilePath(self.mktemp())
+        fp.createDirectory()
+        for sampleName in expectedNames:
+            fp.child(sampleName).touch()
+        df = DAVFile(fp)
+        responseXML = browserHTML2ETree(
+            (yield df.render(SimpleFakeRequest('/'))).stream.read()
+        )
+        names = set([element.text for element in responseXML.findall(".//a")])
+        self.assertEquals(set(expectedNames), names)
+
+
+    def test_simpleList(self):
+        """
+        Rendering a L{DAVFile} that is backed by a directory will produce an
+        HTML document including links to its contents.
+        """
+        return self.doDirectoryTest(['gamma.txt', 'beta.html', 'alpha.xml'])
+
+
+    def test_emptyList(self):
+        """
+        Listing a directory with no files in it will produce an index with no
+        links.
+        """
+        return self.doDirectoryTest([])
+        
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20091105/cda0d913/attachment.html>


More information about the calendarserver-changes mailing list