[Zope3-checkins] CVS: Zope3/src/zope/app/http/tests - __init__.py:1.1 test_put.py:1.1 test_traversers.py:1.1

Jim Fulton jim@zope.com
Fri, 7 Feb 2003 10:59:39 -0500


Update of /cvs-repository/Zope3/src/zope/app/http/tests
In directory cvs.zope.org:/tmp/cvs-serv24670/src/zope/app/http/tests

Added Files:
	__init__.py test_put.py test_traversers.py 
Log Message:
Implemented HTTP PUT. Do do this, I had to:

- Implement working HTTP publication, request, response

- Change the server setup so that rather than having a Browser
  server and an XML-RPC server, there is an HTTP server that 
  uses:

  o Browser request, response, and publication for browser (GET, POST, 
    and HEAD) requests,

  o XMLRPC request, response, and publication for xml-rpc (POST 
    w content-type=='text/xml') requests,

  o HTTP request, response, and publication for all other HTTP requests.

  XML-RPC now runs on the same port, 8080, as browser requests.

- Implemented HEAD.

- Implemented some simple PUT views that use the
  file-system-reprentation adapter framework. (This is the replacement
  for VFS that is also used by FTP and may be used as part of
  file-system synchronization.) 
  



=== Added File Zope3/src/zope/app/http/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/http/tests/test_put.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""
$Id: test_put.py,v 1.1 2003/02/07 15:59:38 jim Exp $
"""
__metaclass__ = type

from unittest import TestCase, TestSuite, main, makeSuite
from StringIO import StringIO
import zope.app.http.put
from zope.publisher.browser import TestRequest
from zope.app.interfaces.file import IWriteFile, IWriteDirectory, IFileFactory
from zope.app.interfaces.container import IZopeWriteContainer
from zope.app.tests.placelesssetup import PlacelessSetup

class File:

    __implements__ = IWriteFile

    def __init__(self, name, content_type, data):
        self.name = name
        self.content_type = content_type
        self.data = data

    def write(self, data):
        self.data = data

class Container:

    __implements__ = IWriteDirectory, IZopeWriteContainer, IFileFactory

    def setObject(self, name, object):
        setattr(self, name, object)

    def __call__(self, name, content_type, data):
        return File(name, content_type, data)
        

class TestNullPUT(PlacelessSetup, TestCase):

    def test(self):
        container = Container()
        content = "some content\n for testing"
        request = TestRequest(StringIO(content), StringIO(),
                              {'CONTENT_TYPE': 'test/foo'})
        null = zope.app.http.put.NullResource(container, 'spam')
        put = zope.app.http.put.NullPUT(null, request)
        self.assertEqual(getattr(container, 'spam', None), None)
        self.assertEqual(put.PUT(), '')
        file = container.spam
        self.assertEqual(file.__class__, File)
        self.assertEqual(file.name, 'spam')
        self.assertEqual(file.content_type, 'test/foo')
        self.assertEqual(file.data, content)        

class TestFilePUT(PlacelessSetup, TestCase):

    def test(self):
        file = File("thefile", "text/x", "initial content")
        content = "some content\n for testing"
        request = TestRequest(StringIO(content), StringIO(),
                              {'CONTENT_TYPE': 'test/foo'})
        put = zope.app.http.put.FilePUT(file, request)
        self.assertEqual(put.PUT(), '')
        self.assertEqual(file.data, content)        

def test_suite():
    return TestSuite((
        makeSuite(TestFilePUT),
        makeSuite(TestNullPUT),
        ))


=== Added File Zope3/src/zope/app/http/tests/test_traversers.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""
$Id: test_traversers.py,v 1.1 2003/02/07 15:59:38 jim Exp $
"""
__metaclass__ = type

from unittest import TestCase, TestSuite, main, makeSuite
from zope.exceptions import NotFoundError
from zope.app.http.traversal import ContainerTraverser, ItemTraverser
from zope.publisher.browser import TestRequest
from zope.app.http.put import NullResource

class Items:

    def __init__(self, data):
        self.data = data

    def __getitem__(self, name):
        return self.data[name]

class Container(Items):

    def get(self, name, default=None):
        return self.data.get(name, default)
    

class TestContainer(TestCase):

    Container = Container
    Traverser = ContainerTraverser

    def testSubobject(self):
        container = self.Container({'foo': 42})
        request = TestRequest()
        traverser = self.Traverser(container, request)
        self.assertEqual(traverser.publishTraverse(request, 'foo'), 42)

    def testNotFound(self):
        container = self.Container({'foo': 42})
        request = TestRequest()
        traverser = self.Traverser(container, request)
        self.assertRaises(NotFoundError,
                          traverser.publishTraverse, request, 'bar')
    

    def testNull(self):
        container = self.Container({'foo': 42})
        request = TestRequest()
        request.method = 'PUT'
        traverser = self.Traverser(container, request)
        null = traverser.publishTraverse(request, 'bar')
        self.assertEqual(null.__class__, NullResource)
        self.assertEqual(null.container, container)
        self.assertEqual(null.name, 'bar')
        

class TestItem(TestContainer):

    Container = Items
    Traverser = ItemTraverser
    

def test_suite():
    return TestSuite((
        makeSuite(TestContainer),
        makeSuite(TestItem),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')