def testNoDefault(self): root = vhost.NameVirtualHost() # Test lack of host specified self.assertResponse( (root, 'http://frob/'), (404, {}, None))
def testNameVirtualHostWithNesting(self): """ Test that an unknown virtual host gets handled by the domain parent and passed on to the parent's resource. """ nested = vhost.NameVirtualHost() nested.addHost('is.nested', HostResource()) self.root.addHost('nested', nested) self.assertResponse( (self.root, 'http://is.nested/'), (200, {}, 'is.nested'))
class TestVhost(BaseCase): root = vhost.NameVirtualHost(default=HostResource()) def setUp(self): self.root.addHost('foo', HostResource()) def testNameVirtualHost(self): """ Test basic Name Virtual Host behavior 1) NameVirtualHost.default is defined, so an undefined NVH (localhost) gets handled by NameVirtualHost.default 2) A defined NVH gets passed the proper host header and is handled by the proper resource """ self.assertResponse( (self.root, 'http://localhost/'), (200, {}, 'localhost')) self.assertResponse( (self.root, 'http://foo/'), (200, {}, 'foo')) def testNoDefault(self): root = vhost.NameVirtualHost() # Test lack of host specified self.assertResponse( (root, 'http://frob/'), (404, {}, None)) def testNameVirtualHostWithChildren(self): """ Test that children of a defined NVH are handled appropriately """ self.assertResponse( (self.root, 'http://foo/bar/'), (200, {}, 'foo')) def testNameVirtualHostWithNesting(self): """ Test that an unknown virtual host gets handled by the domain parent and passed on to the parent's resource. """ nested = vhost.NameVirtualHost() nested.addHost('is.nested', HostResource()) self.root.addHost('nested', nested) self.assertResponse( (self.root, 'http://is.nested/'), (200, {}, 'is.nested'))
def opt_vhost_static(self, virtualHost): """Specify a virtual host in the form of domain=path to be served as raw directory or file. """ if (self['root'] and not \ isinstance(self['root'], vhost.NameVirtualHost)): raise usage.UsageError("You can only use --vhost-static alone " "or with --vhost-class and --vhost-path") domain, path = virtualHost.split('=', 1) if not self['root']: self['root'] = vhost.NameVirtualHost() self['root'].addHost(domain, static.File(os.path.abspath(path)))
def opt_vhost_dav(self, virtualHost): """Specify a virtual host in the form of domain=path, to have path served as a DAV collection at the root of domain """ if (self['root'] and not \ isinstance(self['root'], vhost.NameVirtualHost)): raise usage.UsageError("You can only use --vhost-static alone " "or with --vhost-class and --vhost-path") domain, path = virtualHost.split('=', 1) if not self['root']: self['root'] = vhost.NameVirtualHost() self['root'].addHost(domain, dav_static.DAVFile(os.path.abspath(path)))
def opt_vhost_class(self, virtualHost): """Specify a virtual host in the form of domain=class, where class can be adapted to an iweb.IResource and has a zero-argument constructor. """ if (self['root'] and not \ isinstance(self['root'], vhost.NameVirtualHost)): raise usage.UsageError("You can not use --vhost-class with " "--path or --class.") domain, className = virtualHost.split('=', 1) if not self['root']: self['root'] = vhost.NameVirtualHost() classObj = reflect.namedClass(className) self['root'].addHost(domain, iweb.IResource(classObj()))
def opt_vhost_path(self, path): """Specify a directory to use for automatic named virtual hosts. It is assumed that this directory contains a series of subdirectories each representing a virtual host domain name and containing the files to be served at that domain. """ if self['root']: if not isintance(self['root'], vhost.NameVirtualHost): raise usage.UsageError("You may only have one root resource") else: self['root'] = vhost.NameVirtualHost() path = os.path.abspath(path) for name in os.listdir(path): fullname = os.path.join(path, name) self['root'].addHost(name, static.File(fullname))