[Zope3-checkins] SVN: Zope3/branches/3.2/src/zope/app/http/ backporting fix for issue 125 from trunk@70412

Wolfgang Schnerring wosc at wosc.de
Thu Sep 28 04:54:26 EDT 2006


Log message for revision 70416:
  backporting fix for issue 125 from trunk at 70412

Changed:
  A   Zope3/branches/3.2/src/zope/app/http/ftests/
  A   Zope3/branches/3.2/src/zope/app/http/ftests/__init__.py
  A   Zope3/branches/3.2/src/zope/app/http/ftests/test_put.py
  U   Zope3/branches/3.2/src/zope/app/http/put.py
  U   Zope3/branches/3.2/src/zope/app/http/tests/test_put.py

-=-
Added: Zope3/branches/3.2/src/zope/app/http/ftests/__init__.py
===================================================================
--- Zope3/branches/3.2/src/zope/app/http/ftests/__init__.py	2006-09-28 08:46:45 UTC (rev 70415)
+++ Zope3/branches/3.2/src/zope/app/http/ftests/__init__.py	2006-09-28 08:54:26 UTC (rev 70416)
@@ -0,0 +1 @@
+# python package

Added: Zope3/branches/3.2/src/zope/app/http/ftests/test_put.py
===================================================================
--- Zope3/branches/3.2/src/zope/app/http/ftests/test_put.py	2006-09-28 08:46:45 UTC (rev 70415)
+++ Zope3/branches/3.2/src/zope/app/http/ftests/test_put.py	2006-09-28 08:54:26 UTC (rev 70416)
@@ -0,0 +1,58 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Test HTTP PUT verb
+
+$Id: test_put.py 67630 2006-04-27 00:54:03Z jim $
+"""
+
+from unittest import TestSuite, makeSuite
+
+from zope.app.testing.functional import FunctionalTestCase, HTTPCaller
+
+class TestPUT(FunctionalTestCase):
+    def test_put(self):
+        # PUT something for the first time
+        response = HTTPCaller()(r"""PUT /testfile.txt HTTP/1.1
+Authorization: Basic bWdyOm1ncnB3
+Content-Length: 20
+Content-Type: text/plain
+
+This is just a test.""")
+        self.assertEquals(response._response.getStatus(), 201)
+        self.assertEquals(response._response.getHeader("Location"),
+                          "http://localhost/testfile.txt")
+
+        response = HTTPCaller()(r"""GET /testfile.txt HTTP/1.1
+Authorization: Basic bWdyOm1ncnB3""")
+        self.assertEquals(response.getBody(), "This is just a test.")
+
+        # now modify it
+        response = HTTPCaller()(r"""PUT /testfile.txt HTTP/1.1
+Authorization: Basic bWdyOm1ncnB3
+Content-Length: 22
+Content-Type: text/plain
+
+And now it is modified.""")
+        self.assertEquals(response._response.getStatus(), 200)
+        self.assertEquals(response.getBody(), "")
+
+        response = HTTPCaller()(r"""GET /testfile.txt HTTP/1.1
+Authorization: Basic bWdyOm1ncnB3""")
+        self.assertEquals(response.getBody(), "And now it is modified.")
+        
+        
+def test_suite():
+    return TestSuite((
+        makeSuite(TestPUT),
+        ))

Modified: Zope3/branches/3.2/src/zope/app/http/put.py
===================================================================
--- Zope3/branches/3.2/src/zope/app/http/put.py	2006-09-28 08:46:45 UTC (rev 70415)
+++ Zope3/branches/3.2/src/zope/app/http/put.py	2006-09-28 08:54:26 UTC (rev 70416)
@@ -18,10 +18,12 @@
 from zope.component import queryAdapter
 from zope.app.http.interfaces import INullResource
 from zope.app.filerepresentation.interfaces import IWriteFile
-from zope.app.filerepresentation.interfaces import IWriteDirectory, IFileFactory
+from zope.app.filerepresentation.interfaces import \
+    IWriteDirectory, IReadDirectory, IFileFactory
 from zope.event import notify
 from zope.app.event.objectevent import ObjectCreatedEvent
 from zope.interface import implements
+from zope.app import zapi
 
 class NullResource(object):
     """Object representing objects to be created by a `PUT`.
@@ -38,6 +40,7 @@
     """Put handler for null resources (new file-like things)
 
     This view creates new objects in containers.
+
     """
 
     def __init__(self, context, request):
@@ -59,7 +62,9 @@
             ext = "."
 
         # Get a "directory" surrogate for the container
-        dir = IWriteDirectory(container, None)
+        # TODO: Argh. Why don't we have a unioned Interface for that?!?
+        dir_write = IWriteDirectory(container)
+        dir_read = IReadDirectory(container)
 
         # Now try to get a custom factory for he container
         factory = queryAdapter(container, IFileFactory, ext)
@@ -74,13 +79,20 @@
         newfile = factory(name, request.getHeader('content-type', ''), data)
         notify(ObjectCreatedEvent(newfile))
 
-        dir[name] = newfile
+        dir_write[name] = newfile
+        # Ickyness with non-predictable support for containment: 
+        #   make sure we get a containment proxy
+        newfile = dir_read[name]
 
         request.response.setStatus(201)
+        request.response.setHeader(
+            'Location', zapi.absoluteURL(newfile, request))
         return ''
 
+
 class FilePUT(object):
     """Put handler for existing file-like things
+
     """
 
     def __init__(self, context, request):
@@ -94,11 +106,9 @@
         file = self.context
         adapter = IWriteFile(file)
 
-        # TODO: Need to add support for large files
-        data = body.read()
+        chunk = body.read(2**6)
+        while chunk:
+            adapter.write(chunk)
+            chunk = body.read(2**6)
 
-        adapter.write(data)
-
         return ''
-
-

Modified: Zope3/branches/3.2/src/zope/app/http/tests/test_put.py
===================================================================
--- Zope3/branches/3.2/src/zope/app/http/tests/test_put.py	2006-09-28 08:46:45 UTC (rev 70415)
+++ Zope3/branches/3.2/src/zope/app/http/tests/test_put.py	2006-09-28 08:54:26 UTC (rev 70416)
@@ -20,8 +20,10 @@
 import zope.app.http.put
 from zope.publisher.browser import TestRequest
 from zope.app.filerepresentation.interfaces import IWriteFile
-from zope.app.filerepresentation.interfaces import IWriteDirectory, IFileFactory
+from zope.app.filerepresentation.interfaces import IWriteDirectory, IReadDirectory, IFileFactory
 from zope.app.testing.placelesssetup import PlacelessSetup
+from zope.app.component.testing import PlacefulSetup, Place
+from zope.app.location.interfaces import ILocation
 from zope.interface import implements
 
 class File(object):
@@ -36,21 +38,30 @@
     def write(self, data):
         self.data = data
 
-class Container(object):
+class Container(Place):
 
-    implements(IWriteDirectory, IFileFactory)
+    implements(IWriteDirectory, IReadDirectory, IFileFactory, ILocation)
 
+    __name__ = None
+    __parent__ = None
+
     def __setitem__(self, name, object):
+        object.__name__ = name
+        object.__parent__ = self
         setattr(self, name, object)
 
+    def __getitem__(self, name):
+        return getattr(self, name)
+
     def __call__(self, name, content_type, data):
         return File(name, content_type, data)
 
 
-class TestNullPUT(PlacelessSetup, TestCase):
+class TestNullPUT(PlacefulSetup, TestCase):
 
     def test(self):
-        container = Container()
+        container = Container("put")
+        self.rootFolder["put"] = container
         content = "some content\n for testing"
         request = TestRequest(StringIO(content),
                               {'CONTENT_TYPE': 'test/foo',
@@ -69,13 +80,16 @@
 
         # Check HTTP Response
         self.assertEqual(request.response.getStatus(), 201)
+        self.assertEqual(request.response.getHeader("Location"),
+                         "http://127.0.0.1/put/spam")
 
     def test_bad_content_header(self):
         ## The previous behavour of the PUT method was to fail if the request
         ## object had a key beginning with 'HTTP_CONTENT_' with a status of 501.
         ## This was breaking the new Twisted server, so I am now allowing this
         ## this type of request to be valid.
-        container = Container()
+        container = Container("/put")
+        self.rootFolder["put"] = container
         content = "some content\n for testing"
         request = TestRequest(StringIO(content),
                               {'CONTENT_TYPE': 'test/foo',



More information about the Zope3-Checkins mailing list