import unittest
import zope.component.testing

from zope.component import provideUtility, getUtility
from zope.component import provideAdapter

from zojax.cachepurging.interfaces import ICachePurgingConfiglet
from zojax.cachepurging.rewrite import DefaultRewriter

class FauxRequest(dict):
    pass

class TestRewrite(unittest.TestCase):
    
    def setUp(self):
        self.request = FauxRequest()
        self.rewriter = DefaultRewriter(self.request)

        
    def tearDown(self):
        zope.component.testing.tearDown()
    
    def _prepareVHMRequest(self, path, domain='example.com', root='/plone', prefix='', protocol='http'):
        translatedPrefix = '/'.join(['_vh_%s' % p for p in prefix.split('/')])
        
        self.request['URL'] = '%s://%s%s%s' % (protocol, domain, prefix, path,)
        self.request['ACTUAL_URL'] = '%s://%s%s%s' % (protocol, domain, prefix, path,)
        self.request['SERVER_URL'] = '%s://%s' % (protocol, domain,)
        self.request['PATH_INFO'] = '/VirtualHostBase/%s/%s:80%s/VirtualHostRoot%s%s' % (protocol, domain, root, translatedPrefix, path,)
        self.request['VIRTUAL_URL'] = '%s://%s%s' % (protocol, domain, path)
        
        if prefix:
            self.request['VIRTUAL_URL_PARTS'] = ('%s://%s' % (protocol, domain,), prefix[1:], path[1:])
        else:
            self.request['VIRTUAL_URL_PARTS'] = ('%s://%s' % (protocol, domain,), path[1:])
        
        self.request['VirtualRootPhysicalPath'] = tuple(root.split('/'))
    
    def test_no_registry(self):
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_no_settings(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_virtual_hosting_disabled(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = False
        
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_empty_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self.request.clear()
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL_PARTS']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_root_physical_path(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        del self.request['VirtualRootPhysicalPath']
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_malformed_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        
        self.request['VIRTUAL_URL_PARTS'] = ('foo',)
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        
        self.request['VIRTUAL_URL_PARTS'] = ()
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        
        self.request['VIRTUAL_URL_PARTS'] = ('http://example.com', '', '/foo', 'x')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        
        self.request['VIRTUAL_URL_PARTS'] = 'foo'
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_standard_vhm(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
    def test_virtual_root_is_app_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', root='/')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
    def test_virtual_root_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', root='/bar/plone')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))

    def test_inside_out_hosting(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', root='/bar/plone', prefix='/foo/bar')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/_vh_foo/_vh_bar/foo'],
                          self.rewriter('/foo'))
    
    def test_virtual_path_is_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/', root='/plone')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
                          self.rewriter('/'))

    def test_virtual_path_is_empty(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('', root='/plone')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
                          self.rewriter(''))

    def test_virtual_path_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo/bar', root='/plone')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo/bar'],
                          self.rewriter('/foo/bar'))
    
    def test_nonstandard_port(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', domain='example.com:81')
        self.assertEquals(['/VirtualHostBase/http/example.com:81/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
    def test_https(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol='https')
        self.assertEquals(['/VirtualHostBase/https/example.com:81/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
    def test_domains(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = ('http://example.org:81', 'https://example.com:82')
        
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol='https')
        self.assertEquals(['/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
                           '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))

    def test_domains_w_different_path_in_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = ('http://example.org:81', 'https://example.com:82')
        
        self._prepareVHMRequest('/bar', domain='example.com:81', protocol='https')
        self.assertEquals(['/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
                           '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
def test_suite():
    return unittest.defaultTestLoader.loadTestsFromName(__name__)

62513f61c530497c4b79e2f89335a9991b769527
<mask token>


class TestRewrite(unittest.TestCase):
    <mask token>

    def tearDown(self):
        zope.component.testing.tearDown()
    <mask token>

    def test_no_registry(self):
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_settings(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    <mask token>
    <mask token>

    def test_no_virtual_url(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL']
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    <mask token>
    <mask token>

    def test_malformed_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        self.request['VIRTUAL_URL_PARTS'] = 'foo',
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'] = ()
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'
            ] = 'http://example.com', '', '/foo', 'x'
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'] = 'foo'
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_standard_vhm(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo'],
            self.rewriter('/foo'))
    <mask token>
    <mask token>
    <mask token>

    def test_virtual_path_is_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
            self.rewriter('/'))

    def test_virtual_path_is_empty(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
            self.rewriter(''))
    <mask token>
    <mask token>

    def test_https(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/https/example.com:81/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))
    <mask token>

    def test_domains_w_different_path_in_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = 'http://example.org:81', 'https://example.com:82'
        self._prepareVHMRequest('/bar', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
            '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))


<mask token>

<mask token>


class TestRewrite(unittest.TestCase):
    <mask token>

    def tearDown(self):
        zope.component.testing.tearDown()

    def _prepareVHMRequest(self, path, domain='example.com', root='/plone',
        prefix='', protocol='http'):
        translatedPrefix = '/'.join([('_vh_%s' % p) for p in prefix.split('/')]
            )
        self.request['URL'] = '%s://%s%s%s' % (protocol, domain, prefix, path)
        self.request['ACTUAL_URL'] = '%s://%s%s%s' % (protocol, domain,
            prefix, path)
        self.request['SERVER_URL'] = '%s://%s' % (protocol, domain)
        self.request['PATH_INFO'
            ] = '/VirtualHostBase/%s/%s:80%s/VirtualHostRoot%s%s' % (protocol,
            domain, root, translatedPrefix, path)
        self.request['VIRTUAL_URL'] = '%s://%s%s' % (protocol, domain, path)
        if prefix:
            self.request['VIRTUAL_URL_PARTS'] = '%s://%s' % (protocol, domain
                ), prefix[1:], path[1:]
        else:
            self.request['VIRTUAL_URL_PARTS'] = '%s://%s' % (protocol, domain
                ), path[1:]
        self.request['VirtualRootPhysicalPath'] = tuple(root.split('/'))

    def test_no_registry(self):
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_settings(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    <mask token>

    def test_empty_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self.request.clear()
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL_PARTS']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_root_physical_path(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VirtualRootPhysicalPath']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_malformed_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        self.request['VIRTUAL_URL_PARTS'] = 'foo',
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'] = ()
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'
            ] = 'http://example.com', '', '/foo', 'x'
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'] = 'foo'
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_standard_vhm(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo'],
            self.rewriter('/foo'))
    <mask token>

    def test_virtual_root_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', root='/bar/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))
    <mask token>

    def test_virtual_path_is_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
            self.rewriter('/'))

    def test_virtual_path_is_empty(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
            self.rewriter(''))
    <mask token>

    def test_nonstandard_port(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', domain='example.com:81')
        self.assertEquals([
            '/VirtualHostBase/http/example.com:81/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_https(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/https/example.com:81/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))
    <mask token>

    def test_domains_w_different_path_in_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = 'http://example.org:81', 'https://example.com:82'
        self._prepareVHMRequest('/bar', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
            '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))


<mask token>

<mask token>


class TestRewrite(unittest.TestCase):

    def setUp(self):
        self.request = FauxRequest()
        self.rewriter = DefaultRewriter(self.request)

    def tearDown(self):
        zope.component.testing.tearDown()

    def _prepareVHMRequest(self, path, domain='example.com', root='/plone',
        prefix='', protocol='http'):
        translatedPrefix = '/'.join([('_vh_%s' % p) for p in prefix.split('/')]
            )
        self.request['URL'] = '%s://%s%s%s' % (protocol, domain, prefix, path)
        self.request['ACTUAL_URL'] = '%s://%s%s%s' % (protocol, domain,
            prefix, path)
        self.request['SERVER_URL'] = '%s://%s' % (protocol, domain)
        self.request['PATH_INFO'
            ] = '/VirtualHostBase/%s/%s:80%s/VirtualHostRoot%s%s' % (protocol,
            domain, root, translatedPrefix, path)
        self.request['VIRTUAL_URL'] = '%s://%s%s' % (protocol, domain, path)
        if prefix:
            self.request['VIRTUAL_URL_PARTS'] = '%s://%s' % (protocol, domain
                ), prefix[1:], path[1:]
        else:
            self.request['VIRTUAL_URL_PARTS'] = '%s://%s' % (protocol, domain
                ), path[1:]
        self.request['VirtualRootPhysicalPath'] = tuple(root.split('/'))

    def test_no_registry(self):
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_settings(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_virtual_hosting_disabled(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = False
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_empty_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self.request.clear()
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL_PARTS']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_root_physical_path(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VirtualRootPhysicalPath']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_malformed_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        self.request['VIRTUAL_URL_PARTS'] = 'foo',
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'] = ()
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'
            ] = 'http://example.com', '', '/foo', 'x'
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'] = 'foo'
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_standard_vhm(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo'],
            self.rewriter('/foo'))

    def test_virtual_root_is_app_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', root='/')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/VirtualHostRoot/foo'], self.
            rewriter('/foo'))

    def test_virtual_root_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', root='/bar/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_inside_out_hosting(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', root='/bar/plone', prefix='/foo/bar')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/_vh_foo/_vh_bar/foo'
            ], self.rewriter('/foo'))

    def test_virtual_path_is_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
            self.rewriter('/'))

    def test_virtual_path_is_empty(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
            self.rewriter(''))

    def test_virtual_path_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo/bar', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo/bar'
            ], self.rewriter('/foo/bar'))

    def test_nonstandard_port(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', domain='example.com:81')
        self.assertEquals([
            '/VirtualHostBase/http/example.com:81/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_https(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/https/example.com:81/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_domains(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = 'http://example.org:81', 'https://example.com:82'
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
            '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_domains_w_different_path_in_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = 'http://example.org:81', 'https://example.com:82'
        self._prepareVHMRequest('/bar', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
            '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))


<mask token>

<mask token>


class FauxRequest(dict):
    pass


class TestRewrite(unittest.TestCase):

    def setUp(self):
        self.request = FauxRequest()
        self.rewriter = DefaultRewriter(self.request)

    def tearDown(self):
        zope.component.testing.tearDown()

    def _prepareVHMRequest(self, path, domain='example.com', root='/plone',
        prefix='', protocol='http'):
        translatedPrefix = '/'.join([('_vh_%s' % p) for p in prefix.split('/')]
            )
        self.request['URL'] = '%s://%s%s%s' % (protocol, domain, prefix, path)
        self.request['ACTUAL_URL'] = '%s://%s%s%s' % (protocol, domain,
            prefix, path)
        self.request['SERVER_URL'] = '%s://%s' % (protocol, domain)
        self.request['PATH_INFO'
            ] = '/VirtualHostBase/%s/%s:80%s/VirtualHostRoot%s%s' % (protocol,
            domain, root, translatedPrefix, path)
        self.request['VIRTUAL_URL'] = '%s://%s%s' % (protocol, domain, path)
        if prefix:
            self.request['VIRTUAL_URL_PARTS'] = '%s://%s' % (protocol, domain
                ), prefix[1:], path[1:]
        else:
            self.request['VIRTUAL_URL_PARTS'] = '%s://%s' % (protocol, domain
                ), path[1:]
        self.request['VirtualRootPhysicalPath'] = tuple(root.split('/'))

    def test_no_registry(self):
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_settings(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_virtual_hosting_disabled(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = False
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_empty_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self.request.clear()
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL_PARTS']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_root_physical_path(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        del self.request['VirtualRootPhysicalPath']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_malformed_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        self.request['VIRTUAL_URL_PARTS'] = 'foo',
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'] = ()
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'
            ] = 'http://example.com', '', '/foo', 'x'
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        self.request['VIRTUAL_URL_PARTS'] = 'foo'
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_standard_vhm(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo'],
            self.rewriter('/foo'))

    def test_virtual_root_is_app_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', root='/')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/VirtualHostRoot/foo'], self.
            rewriter('/foo'))

    def test_virtual_root_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', root='/bar/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_inside_out_hosting(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', root='/bar/plone', prefix='/foo/bar')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/_vh_foo/_vh_bar/foo'
            ], self.rewriter('/foo'))

    def test_virtual_path_is_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
            self.rewriter('/'))

    def test_virtual_path_is_empty(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
            self.rewriter(''))

    def test_virtual_path_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo/bar', root='/plone')
        self.assertEquals([
            '/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo/bar'
            ], self.rewriter('/foo/bar'))

    def test_nonstandard_port(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', domain='example.com:81')
        self.assertEquals([
            '/VirtualHostBase/http/example.com:81/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_https(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/https/example.com:81/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_domains(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = 'http://example.org:81', 'https://example.com:82'
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
            '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))

    def test_domains_w_different_path_in_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = 'http://example.org:81', 'https://example.com:82'
        self._prepareVHMRequest('/bar', domain='example.com:81', protocol=
            'https')
        self.assertEquals([
            '/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
            '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'
            ], self.rewriter('/foo'))


<mask token>

import unittest
import zope.component.testing

from zope.component import provideUtility, getUtility
from zope.component import provideAdapter

from zojax.cachepurging.interfaces import ICachePurgingConfiglet
from zojax.cachepurging.rewrite import DefaultRewriter

class FauxRequest(dict):
    pass

class TestRewrite(unittest.TestCase):
    
    def setUp(self):
        self.request = FauxRequest()
        self.rewriter = DefaultRewriter(self.request)

        
    def tearDown(self):
        zope.component.testing.tearDown()
    
    def _prepareVHMRequest(self, path, domain='example.com', root='/plone', prefix='', protocol='http'):
        translatedPrefix = '/'.join(['_vh_%s' % p for p in prefix.split('/')])
        
        self.request['URL'] = '%s://%s%s%s' % (protocol, domain, prefix, path,)
        self.request['ACTUAL_URL'] = '%s://%s%s%s' % (protocol, domain, prefix, path,)
        self.request['SERVER_URL'] = '%s://%s' % (protocol, domain,)
        self.request['PATH_INFO'] = '/VirtualHostBase/%s/%s:80%s/VirtualHostRoot%s%s' % (protocol, domain, root, translatedPrefix, path,)
        self.request['VIRTUAL_URL'] = '%s://%s%s' % (protocol, domain, path)
        
        if prefix:
            self.request['VIRTUAL_URL_PARTS'] = ('%s://%s' % (protocol, domain,), prefix[1:], path[1:])
        else:
            self.request['VIRTUAL_URL_PARTS'] = ('%s://%s' % (protocol, domain,), path[1:])
        
        self.request['VirtualRootPhysicalPath'] = tuple(root.split('/'))
    
    def test_no_registry(self):
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_no_settings(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_virtual_hosting_disabled(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = False
        
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_empty_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self.request.clear()
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        del self.request['VIRTUAL_URL_PARTS']
        self.assertEquals(['/foo'], self.rewriter('/foo'))

    def test_no_virtual_root_physical_path(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        del self.request['VirtualRootPhysicalPath']
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_malformed_virtual_url_parts(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        
        self.request['VIRTUAL_URL_PARTS'] = ('foo',)
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        
        self.request['VIRTUAL_URL_PARTS'] = ()
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        
        self.request['VIRTUAL_URL_PARTS'] = ('http://example.com', '', '/foo', 'x')
        self.assertEquals(['/foo'], self.rewriter('/foo'))
        
        self.request['VIRTUAL_URL_PARTS'] = 'foo'
        self.assertEquals(['/foo'], self.rewriter('/foo'))
    
    def test_standard_vhm(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo')
        self.assertEquals(['/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
    def test_virtual_root_is_app_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', root='/')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
    def test_virtual_root_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', root='/bar/plone')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))

    def test_inside_out_hosting(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', root='/bar/plone', prefix='/foo/bar')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/bar/plone/VirtualHostRoot/_vh_foo/_vh_bar/foo'],
                          self.rewriter('/foo'))
    
    def test_virtual_path_is_root(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/', root='/plone')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
                          self.rewriter('/'))

    def test_virtual_path_is_empty(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('', root='/plone')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/plone/VirtualHostRoot/'],
                          self.rewriter(''))

    def test_virtual_path_is_deep(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo/bar', root='/plone')
        
        self.assertEquals(['/VirtualHostBase/http/example.com/plone/VirtualHostRoot/foo/bar'],
                          self.rewriter('/foo/bar'))
    
    def test_nonstandard_port(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', domain='example.com:81')
        self.assertEquals(['/VirtualHostBase/http/example.com:81/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
    def test_https(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol='https')
        self.assertEquals(['/VirtualHostBase/https/example.com:81/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
    def test_domains(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = ('http://example.org:81', 'https://example.com:82')
        
        self._prepareVHMRequest('/foo', domain='example.com:81', protocol='https')
        self.assertEquals(['/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
                           '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))

    def test_domains_w_different_path_in_request(self):
        registry = Registry()
        provideUtility(registry, IRegistry)
        registry.registerInterface(ICachePurgingConfiglet)
        settings = registry.forInterface(ICachePurgingConfiglet)
        settings.virtualHosting = True
        settings.domains = ('http://example.org:81', 'https://example.com:82')
        
        self._prepareVHMRequest('/bar', domain='example.com:81', protocol='https')
        self.assertEquals(['/VirtualHostBase/http/example.org:81/plone/VirtualHostRoot/foo',
                           '/VirtualHostBase/https/example.com:82/plone/VirtualHostRoot/foo'],
                          self.rewriter('/foo'))
    
def test_suite():
    return unittest.defaultTestLoader.loadTestsFromName(__name__)
