/usr/share/pyshared/zope/app/publication/tests/test_httpfactory.py is in python-zope.app.publication 3.13.2-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | ##############################################################################
#
# Copyright (c) 2003 Zope Foundation 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.
#
##############################################################################
"""Tests for the HTTP Publication Request Factory.
"""
from unittest import TestCase, TestSuite, main, makeSuite
from StringIO import StringIO
from zope import component, interface
from zope.publisher.browser import BrowserRequest
from zope.publisher.http import HTTPRequest
from zope.publisher.xmlrpc import XMLRPCRequest
from zope.component.testing import PlacelessSetup
from zope.app.publication.httpfactory import HTTPPublicationRequestFactory
from zope.app.publication.browser import BrowserPublication
from zope.app.publication.http import HTTPPublication
from zope.app.publication.xmlrpc import XMLRPCPublication
from zope.app.publication import interfaces
from zope.app.publication.requestpublicationregistry import factoryRegistry
from zope.app.publication.requestpublicationfactories import \
HTTPFactory, SOAPFactory, BrowserFactory, XMLRPCFactory
class DummyRequestFactory(object):
def __call__(self, input_stream, env):
self.input_stream = input_stream
self.env = env
return self
def setPublication(self, pub):
self.pub = pub
class Test(PlacelessSetup, TestCase):
def setUp(self):
super(Test, self).setUp()
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',
}
# Simulate standard configuration
factoryRegistry.register('GET', '*', 'browser', 0, BrowserFactory())
factoryRegistry.register('POST', '*', 'browser', 0, BrowserFactory())
factoryRegistry.register('HEAD', '*', 'browser', 0, BrowserFactory())
factoryRegistry.register('*', '*', 'http', 0, HTTPFactory())
factoryRegistry.register('POST', 'text/xml', 'xmlrpc', 20,
XMLRPCFactory())
factoryRegistry.register('POST', 'text/xml', 'soap', 30,
SOAPFactory())
def test_override(self):
# TODO: making a SOAP request without configuring a SOAP request
# currently generates an XMLRPC request. Not sure what the right thing
# is, but that doesn't seem to be the right thing.
soaprequestfactory = DummyRequestFactory()
interface.directlyProvides(
soaprequestfactory, interfaces.ISOAPRequestFactory)
component.provideUtility(soaprequestfactory)
xmlrpcrequestfactory = DummyRequestFactory()
interface.directlyProvides(
xmlrpcrequestfactory, interfaces.IXMLRPCRequestFactory)
component.provideUtility(xmlrpcrequestfactory)
httprequestfactory = DummyRequestFactory()
interface.directlyProvides(
httprequestfactory, interfaces.IHTTPRequestFactory)
component.provideUtility(httprequestfactory)
browserrequestfactory = DummyRequestFactory()
interface.directlyProvides(
browserrequestfactory, interfaces.IBrowserRequestFactory)
component.provideUtility(browserrequestfactory)
httpfactory = HTTPPublicationRequestFactory(None)
env = self.__env
env['REQUEST_METHOD'] = 'POST'
env['CONTENT_TYPE'] = 'text/xml'
input = StringIO('')
env['HTTP_SOAPACTION'] = 'foo'
self.assertEqual(httpfactory(input, env), soaprequestfactory)
del env['HTTP_SOAPACTION']
self.assertEqual(httpfactory(input, env), xmlrpcrequestfactory)
env['CONTENT_TYPE'] = 'text/foo'
self.assertEqual(
httpfactory(input, env), browserrequestfactory)
env['REQUEST_METHOD'] = 'FLOO'
self.assertEqual(httpfactory(input, env), httprequestfactory)
def test_browser(self):
r = self.__factory(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(''), self.__env)
self.assertEqual(r.__class__, BrowserRequest)
self.assertEqual(r.publication.__class__, BrowserPublication)
def test_http(self):
for method in ('PUT', 'put', 'ZZZ'):
self.__env['REQUEST_METHOD'] = method
r = self.__factory(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(''), 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(''), self.__env)
self.assertEqual(r.__class__, BrowserRequest)
self.assertEqual(r.publication.__class__, BrowserPublication)
for method in ('PUT', 'put', 'ZZZ'):
self.__env['REQUEST_METHOD'] = method
r = self.__factory(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')
|