Skip to content

Commit

Permalink
Backported
Browse files Browse the repository at this point in the history
r26524 | srichter | 2004-07-14 03:45:38 -0400 (Wed, 14 Jul 2004) | 6 lines
r26551 | srichter | 2004-07-15 03:06:37 -0400 (Thu, 15 Jul 2004) | 6 lines
r26522 | pruggera | 2004-07-14 01:42:06 -0400 (Wed, 14 Jul 2004) | 1 line
r26531 | pruggera | 2004-07-14 13:00:15 -0400 (Wed, 14 Jul 2004) | 1 line
r26534 | pruggera | 2004-07-14 15:55:34 -0400 (Wed, 14 Jul 2004) | 1 line
r26540 | pruggera | 2004-07-14 18:14:25 -0400 (Wed, 14 Jul 2004) | 1 line
  • Loading branch information
strichter committed Aug 12, 2004
1 parent c131319 commit c61c14d
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 36 deletions.
57 changes: 57 additions & 0 deletions httpfactory.py
@@ -0,0 +1,57 @@
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""HTTP Factory
$Id$
"""
from zope.interface import implements
from zope.publisher.http import HTTPRequest
from zope.publisher.browser import BrowserRequest
from zope.publisher.xmlrpc import XMLRPCRequest

from zope.app.publication.interfaces import IPublicationRequestFactory

from zope.app.publication.http import HTTPPublication
from zope.app.publication.browser import BrowserPublication
from zope.app.publication.xmlrpc import XMLRPCPublication

_browser_methods = 'GET', 'POST', 'HEAD'

class HTTPPublicationRequestFactory(object):
implements(IPublicationRequestFactory)

def __init__(self, db):
"""See zope.app.publication.interfaces.IPublicationRequestFactory"""
self._http = HTTPPublication(db)
self._brower = BrowserPublication(db)
self._xmlrpc = XMLRPCPublication(db)

def __call__(self, input_stream, output_steam, env):
"""See zope.app.publication.interfaces.IPublicationRequestFactory"""
method = env.get('REQUEST_METHOD', 'GET').upper()

if method in _browser_methods:
if (method == 'POST' and
env.get('CONTENT_TYPE', '').startswith('text/xml')
):
request = XMLRPCRequest(input_stream, output_steam, env)
request.setPublication(self._xmlrpc)
else:
request = BrowserRequest(input_stream, output_steam, env)
request.setPublication(self._brower)
else:
request = HTTPRequest(input_stream, output_steam, env)
request.setPublication(self._http)

return request
39 changes: 9 additions & 30 deletions tests/test_browserpublication.py
Expand Up @@ -45,7 +45,7 @@ def foo():
"I am an otherwise empty docstring."
return '<html><body>hello base fans</body></html>'

class DummyPublished:
class DummyPublished(object):
implements(IBrowserPublisher)

def publishTraverse(self, request, name):
Expand All @@ -70,7 +70,7 @@ def _createRequest(self, path, publication, **kw):
request.setPublication(publication)
return request

class SimpleObject:
class SimpleObject(object):
def __init__(self, v):
self.v = v

Expand Down Expand Up @@ -160,7 +160,7 @@ class BrowserPublicationTests(BasePublicationTests):

def testAdaptedTraverseNameWrapping(self):

class Adapter:
class Adapter(object):
" "
implements(IBrowserPublisher)
def __init__(self, context, request):
Expand All @@ -182,7 +182,7 @@ def publishTraverse(self, request, name):

def testAdaptedTraverseDefaultWrapping(self):
# Test default content and make sure that it's wrapped.
class Adapter:
class Adapter(object):
implements(IBrowserPublisher)
def __init__(self, context, request):
self.context = context
Expand All @@ -200,30 +200,9 @@ def browserDefault(self, request):
self.assertRaises(ForbiddenAttribute, getattr, ob2, 'v')
self.assertEqual(removeAllProxies(ob2).v, 'bruce')

# XXX we no longer support path parameters! (At least for now)
def XXXtestTraverseSkinExtraction(self):
class I1(Interface): pass
class C:
implements(I1)
class BobView(DummyView): pass

pub = self.klass(self.db)
ob = C()
ztapi.browserView(I1, 'edit', BobView)

r = self._createRequest('/@@edit;skin=zmi',pub)
ob2 = pub.traverseName(r , ob, '@@edit;skin=zmi')
self.assertEqual(r.getPresentationSkin(), 'zmi')
self.assertEqual(ob2.__class__ , BobView)

r = self._createRequest('/@@edit;skin=zmi',pub)
ob2 = pub.traverseName(r , ob, '@@edit;skin=zmi')
self.assertEqual(r.getPresentationSkin(), 'zmi')
self.assertEqual(ob2.__class__ , BobView)

def testTraverseName(self):
pub = self.klass(self.db)
class C:
class C(object):
x = SimpleObject(1)
ob = C()
r = self._createRequest('/x',pub)
Expand All @@ -235,10 +214,10 @@ class C:
def testTraverseNameView(self):
pub = self.klass(self.db)
class I(Interface): pass
class C:
class C(object):
implements(I)
ob = C()
class V:
class V(object):
def __init__(self, context, request): pass
r = self._createRequest('/@@spam',pub)
ztapi.browserView(I, 'spam', V)
Expand All @@ -247,7 +226,7 @@ def __init__(self, context, request): pass

def testTraverseNameServices(self):
pub = self.klass(self.db)
class C:
class C(object):
def getSiteManager(self):
return SimpleObject(1)
ob = C()
Expand All @@ -272,7 +251,7 @@ def testTraverseNameApplicationControl(self):
def testHEADFuxup(self):
pub = self.klass(None)

class User:
class User(object):
id = 'bob'

# With a normal request, we should get a body:
Expand Down
63 changes: 63 additions & 0 deletions tests/test_http.py
@@ -0,0 +1,63 @@
##############################################################################
#
# 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 Publication
$Id$
"""
from unittest import TestCase, TestSuite, main, makeSuite
from zope.app import zapi
import zope.app.publication.http
from zope.publisher.http import HTTPRequest
from zope.app.tests.placelesssetup import PlacelessSetup
from StringIO import StringIO
from zope.interface import Interface, implements
from zope.publisher.interfaces.http import IHTTPRequest

class I(Interface): pass
class C(object):
spammed = 0
implements(I)

class V(object):

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'

s = zapi.getGlobalService(zapi.servicenames.Presentation)
s.provideView(I, 'SPAM', IHTTPRequest, 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')
83 changes: 83 additions & 0 deletions tests/test_simplecomponenttraverser.py
@@ -0,0 +1,83 @@
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""Sample Component Traverser Test
$Id$
"""
import unittest
from zope.component.tests.request import Request
from zope.app.publication.traversers import SimpleComponentTraverser
from zope.component import getService
from zope.app.servicenames import Presentation
from zope.interface import Interface
from zope.exceptions import NotFoundError
from zope.app.tests.placelesssetup import PlacelessSetup

class I(Interface):
pass


class Container(object):
def __init__(self, **kw):
for k in kw:
setattr(self, k , kw[k])

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


class Request(Request):
def getEffectiveURL(self):
return ''


class View(object):
def __init__(self, comp, request):
self._comp = comp


class Test(PlacelessSetup, unittest.TestCase):
def testAttr(self):
# test container traver
foo = Container()
c = Container(foo=foo)
req = Request(I, '')

T = SimpleComponentTraverser(c, req)

self.assertRaises(NotFoundError , T.publishTraverse, req ,'foo')


def testView(self):
# test getting a view
foo = Container()
c = Container(foo=foo)
req = Request(I, '')

T = SimpleComponentTraverser(c, req)
getService(Presentation).provideView(None , 'foo', I, View)

self.failUnless(T.publishTraverse(req,'foo').__class__ is View )

self.assertRaises(NotFoundError , T.publishTraverse, req ,'morebar')



def test_suite():
loader = unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)


if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())
10 changes: 4 additions & 6 deletions traversers.py
Expand Up @@ -11,19 +11,17 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
"""Generic object traversers
$Id$
"""

__metaclass__ = type

from zope.interface import providedBy, implements
from zope.publisher.interfaces import Unauthorized, NotFound
from zope.publisher.interfaces.browser import IBrowserPublisher
from zope.publisher.interfaces.xmlrpc import IXMLRPCPublisher
from zope.component import queryView, getView, getDefaultViewName

class SimpleComponentTraverser:
class SimpleComponentTraverser(object):
"""Browser traverser for simple components that can only traverse to views
"""
implements(IBrowserPublisher, IXMLRPCPublisher)
Expand Down Expand Up @@ -67,7 +65,7 @@ def browserDefault(self, request):

return view, path

class TestTraverser:
class TestTraverser(object):
"""Bobo-style traverser, mostly useful for testing"""
implements(IBrowserPublisher)

Expand Down

0 comments on commit c61c14d

Please sign in to comment.