[Zope3-checkins] CVS: Zope3/src/zope/app/publication/tests - test_http.py:1.1 test_httpfactory.py:1.1 test_browserpublication.py:1.7

Jim Fulton jim@zope.com
Fri, 7 Feb 2003 11:00:17 -0500


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

Modified Files:
	test_browserpublication.py 
Added Files:
	test_http.py test_httpfactory.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/publication/tests/test_http.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_http.py,v 1.1 2003/02/07 15:59:43 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
import zope.app.publication.http
from zope.publisher.http import HTTPRequest
from zope.app.tests.placelesssetup import PlacelessSetup
from StringIO import StringIO
from zope.component.view import provideView
from zope.interface import Interface
from zope.publisher.interfaces.http import IHTTPPresentation

class I(Interface): pass
class C:
    spammed = 0
    __implements__ = I

class V:

    def __init__(self, context, request):
        self.context = context
    
    def SPAM(self):
        self.context.spammed += 1



class Test(PlacelessSetup, TestCase):
    # Note that zope publication tests cover all of the code but callObject

    def test_callObject(self):
        pub = zope.app.publication.http.HTTPPublication(None)
        request = HTTPRequest(StringIO(''), StringIO(), {})
        request.method = 'SPAM'
        provideView(I, 'SPAM', IHTTPPresentation, V)

        ob = C()
        pub.callObject(request, ob)
        self.assertEqual(ob.spammed, 1)

        

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

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


=== Added File Zope3/src/zope/app/publication/tests/test_httpfactory.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_httpfactory.py,v 1.1 2003/02/07 15:59:43 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.publication.httpfactory import HTTPPublicationRequestFactory
from zope.publisher.browser import BrowserRequest
from zope.app.publication.browser import BrowserPublication
from zope.publisher.http import HTTPRequest
from zope.app.publication.http import HTTPPublication
from zope.publisher.xmlrpc import XMLRPCRequest
from zope.app.publication.xmlrpc import XMLRPCPublication
from StringIO import StringIO

class Test(TestCase):

    def setUp(self):
        self.__factory = HTTPPublicationRequestFactory(None)
        self.__env =  {
            'SERVER_URL':         'http://127.0.0.1',
            'HTTP_HOST':          '127.0.0.1',
            'CONTENT_LENGTH':     '0',
            'GATEWAY_INTERFACE':  'TestFooInterface/1.0',
            }

    def test_browser(self):
        r = self.__factory(StringIO(''), StringIO(), self.__env)
        self.assertEqual(r.__class__, BrowserRequest)
        self.assertEqual(r.publication.__class__, BrowserPublication)

        for method in ('GET', 'HEAD', 'POST', 'get', 'head', 'post'):
            self.__env['REQUEST_METHOD'] = method
            r = self.__factory(StringIO(''), StringIO(), self.__env)
            self.assertEqual(r.__class__, BrowserRequest)
            self.assertEqual(r.publication.__class__, BrowserPublication)
            

    def test_http(self):

        for method in ('PUT', 'put', 'XXX'):
            self.__env['REQUEST_METHOD'] = method
            r = self.__factory(StringIO(''), StringIO(), self.__env)
            self.assertEqual(r.__class__, HTTPRequest)
            self.assertEqual(r.publication.__class__, HTTPPublication)

    def test_xmlrpc(self):
        self.__env['CONTENT_TYPE'] = 'text/xml'
        for method in ('POST', 'post'):
            self.__env['REQUEST_METHOD'] = method
            r = self.__factory(StringIO(''), StringIO(), self.__env)
            self.assertEqual(r.__class__, XMLRPCRequest)
            self.assertEqual(r.publication.__class__, XMLRPCPublication)


        # content type doesn't matter for non post
        for method in ('GET', 'HEAD', 'get', 'head'):
            self.__env['REQUEST_METHOD'] = method
            r = self.__factory(StringIO(''), StringIO(), self.__env)
            self.assertEqual(r.__class__, BrowserRequest)
            self.assertEqual(r.publication.__class__, BrowserPublication)

        for method in ('PUT', 'put', 'XXX'):
            self.__env['REQUEST_METHOD'] = method
            r = self.__factory(StringIO(''), StringIO(), self.__env)
            self.assertEqual(r.__class__, HTTPRequest)
            self.assertEqual(r.publication.__class__, HTTPPublication)

    
        

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

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


=== Zope3/src/zope/app/publication/tests/test_browserpublication.py 1.6 => 1.7 ===
--- Zope3/src/zope/app/publication/tests/test_browserpublication.py:1.6	Thu Feb  6 01:49:38 2003
+++ Zope3/src/zope/app/publication/tests/test_browserpublication.py	Fri Feb  7 10:59:43 2003
@@ -13,6 +13,8 @@
 ##############################################################################
 import unittest
 
+from StringIO import StringIO
+
 from zope.interface import Interface
 
 from zope.component import getService, getServiceManager
@@ -303,6 +305,50 @@
         app = r.publication.getApplication(r)
         self.assertEqual(app, applicationControllerRoot)
 
+
+    def testHEADFuxup(self):
+        pub = self.klass(None)
+
+        class User:
+            def getId(self):
+                return 'bob'
+
+        # With a normal request, we should get a body:
+        output = StringIO()
+        request = TestRequest(StringIO(''), output, {'PATH_INFO': '/'})
+        request.user = User()
+        request.response.setBody("spam")
+        pub.afterCall(request)
+        request.response.outputBody()
+        self.assertEqual(
+            output.getvalue(),
+            'Status: 200 Ok\r\n'
+            'Content-Length: 4\r\n'
+            'Content-Type: text/plain;charset=iso-8859-1\r\n'
+            'X-Powered-By: Zope (www.zope.org), Python (www.python.org)\r\n'
+            '\r\nspam'
+            )
+
+        # But with a HEAD request, the body should be empty
+        output = StringIO()
+        request = TestRequest(StringIO(''), output, {'PATH_INFO': '/'})
+        request.user = User()
+        request.method = 'HEAD'
+        request.response.setBody("spam")
+        pub.afterCall(request)
+        request.response.outputBody()
+        self.assertEqual(
+            output.getvalue(),
+            'Status: 200 Ok\r\n'
+            'Content-Length: 0\r\n'
+            'Content-Type: text/plain;charset=iso-8859-1\r\n'
+            'X-Powered-By: Zope (www.zope.org), Python (www.python.org)\r\n'
+            '\r\n'
+            )
+
+        
+        
+        
 
 def test_suite():
     t2 = unittest.makeSuite(BrowserPublicationTests, 'test')