class SiteConfigurationTests(TestCase): """ L{xmantissa.web.Site} defines how to create an HTTP server. """ def setUp(self): self.domain = u"example.com" self.store = Store() self.site = SiteConfiguration(store=self.store, hostname=self.domain) def test_interfaces(self): """ L{SiteConfiguration} implements L{IProtocolFactoryFactory} and L{ISiteURLGenerator}. """ self.assertTrue(verifyObject(ISiteURLGenerator, self.site)) self.assertTrue(verifyObject(IProtocolFactoryFactory, self.site)) def _baseTest(self, portType, scheme, portNumber, method): portType(store=self.store, portNumber=portNumber, factory=self.site) self.assertEquals( getattr(self.site, method)(), URL(scheme, self.domain)) def test_cleartextRoot(self): """ L{SiteConfiguration.cleartextRoot} method returns the proper URL for HTTP communication with this site. """ self._baseTest(TCPPort, 'http', 80, 'cleartextRoot') def test_encryptedRoot(self): """ L{SiteConfiguration.encryptedRoot} method returns the proper URL for HTTPS communication with this site. """ self._baseTest(SSLPort, 'https', 443, 'encryptedRoot') def _nonstandardPortTest(self, portType, scheme, portNumber, method): portType(store=self.store, portNumber=portNumber, factory=self.site) self.assertEquals( getattr(self.site, method)(), URL(scheme, '%s:%s' % (self.domain, portNumber))) def test_cleartextRootNonstandardPort(self): """ L{SiteConfiguration.cleartextRoot} method returns the proper URL for HTTP communication with this site even if the server is listening on a non-standard port number. """ self._nonstandardPortTest(TCPPort, 'http', 8000, 'cleartextRoot') def test_encryptedRootNonstandardPort(self): """ L{SiteConfiguration.encryptedRoot} method returns the proper URL for HTTPS communication with this site even if the server is listening on a non-standard port number. """ self._nonstandardPortTest(SSLPort, 'https', 8443, 'encryptedRoot') def _unavailableTest(self, method): self.assertEquals(getattr(self.site, method)(), None) def test_cleartextRootUnavailable(self): """ L{SiteConfiguration.cleartextRoot} method returns None if there is no HTTP server listening. """ self._unavailableTest('cleartextRoot') def test_encryptedRootUnavailable(self): """ L{SiteConfiguration.encryptedRoot} method returns None if there is no HTTPS server listening. """ self._unavailableTest('encryptedRoot') def _hostOverrideTest(self, portType, scheme, portNumber, method): portType(store=self.store, portNumber=portNumber, factory=self.site) self.assertEquals( getattr(self.site, method)(u'example.net'), URL(scheme, 'example.net')) def test_cleartextRootHostOverride(self): """ A hostname passed to L{SiteConfiguration.cleartextRoot} overrides the configured hostname in the result. """ self._hostOverrideTest(TCPPort, 'http', 80, 'cleartextRoot') def test_encryptedRootHostOverride(self): """ A hostname passed to L{SiteConfiguration.encryptedRoot} overrides the configured hostname in the result. """ self._hostOverrideTest(SSLPort, 'https', 443, 'encryptedRoot') def _portZero(self, portType, scheme, method): randomPort = 7777 class FakePort(object): def getHost(self): return IPv4Address('TCP', u'example.com', randomPort) port = portType(store=self.store, portNumber=0, factory=self.site) port.listeningPort = FakePort() self.assertEquals( getattr(self.site, method)(), URL(scheme, '%s:%s' % (self.domain, randomPort))) def test_cleartextRootPortZero(self): """ When the C{portNumber} of a started L{TCPPort} which refers to the C{SiteConfiguration} is C{0}, L{SiteConfiguration.cleartextRoot} returns an URL with the port number which was actually bound in the netloc. """ self._portZero(TCPPort, 'http', 'cleartextRoot') def test_encryptedRootPortZero(self): """ When the C{portNumber} of a started L{SSLPort} which refers to the C{SiteConfiguration} is C{0}, L{SiteConfiguration.encryptedRoot} returns an URL with the port number which was actually bound in the netloc. """ self._portZero(SSLPort, 'https', 'encryptedRoot') def _portZeroDisconnected(self, portType, method): portType(store=self.store, portNumber=0, factory=self.site) self.assertEquals(None, getattr(self.site, method)()) def test_cleartextRootPortZeroDisconnected(self): """ When the C{portNumber} of an unstarted L{TCPPort} which refers to the C{SiteConfiguration} is C{0}, L{SiteConfiguration.cleartextRoot} returns C{None}. """ self._portZeroDisconnected(TCPPort, 'cleartextRoot') def test_encryptedRootPortZeroDisconnected(self): """ When the C{portNumber} of an unstarted L{SSLPort} which refers to the C{SiteConfiguration} is C{0}, L{SiteConfiguration.encryptedRoot} returns C{None}. """ self._portZeroDisconnected(SSLPort, 'encryptedRoot') def test_rootURL(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made onto the hostname with which the L{SiteConfiguration} is configured. """ request = FakeRequest(headers={'host': self.domain.encode('ascii')}) self.assertEqual(self.site.rootURL(request), URL('', '')) def test_rootURLWithoutHost(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made without a I{Host} header. """ request = FakeRequest() self.assertEqual(self.site.rootURL(request), URL('', '')) def test_rootURLWWWSubdomain(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made onto the I{www} subdomain of the hostname of the L{SiteConfiguration}. """ request = FakeRequest( headers={'host': 'www.' + self.domain.encode('ascii')}) self.assertEqual(self.site.rootURL(request), URL('', '')) def _differentHostnameTest(self, portType, portNumber, isSecure, scheme): request = FakeRequest( isSecure=isSecure, headers={'host': 'alice.' + self.domain.encode('ascii')}) portType(store=self.store, factory=self.site, portNumber=portNumber) self.assertEqual(self.site.rootURL(request), URL(scheme, self.domain)) def test_cleartextRootURLDifferentHostname(self): """ L{SiteConfiguration.rootURL} returns an absolute URL with the HTTP scheme and its hostname as the netloc and with a path of C{/} for a request made over HTTP onto a hostname different from the hostname of the L{SiteConfiguration}. """ self._differentHostnameTest(TCPPort, 80, False, 'http') def test_encryptedRootURLDifferentHostname(self): """ L{SiteConfiguration.rootURL} returns an absolute URL with its hostname as the netloc and with a path of C{/} for a request made over HTTPS onto a hostname different from the hostname of the L{SiteConfiguration}. """ self._differentHostnameTest(SSLPort, 443, True, 'https') def _differentHostnameNonstandardPort(self, portType, isSecure, scheme): portNumber = 12345 request = FakeRequest( isSecure=isSecure, headers={'host': 'alice.' + self.domain.encode('ascii')}) portType(store=self.store, factory=self.site, portNumber=portNumber) self.assertEqual( self.site.rootURL(request), URL(scheme, '%s:%s' % (self.domain.encode('ascii'), portNumber))) def test_cleartextRootURLDifferentHostnameNonstandardPort(self): """ L{SiteConfiguration.rootURL} returns an absolute URL with an HTTP scheme and an explicit port number in the netloc for a request made over HTTP onto a hostname different from the hostname of the L{SiteConfiguration} if the L{SiteConfiguration} has an HTTP server on a non-standard port. """ self._differentHostnameNonstandardPort(TCPPort, False, 'http') def test_encryptedRootURLDifferentHostnameNonstandardPort(self): """ L{SiteConfiguration.rootURL} returns an absolute URL with an HTTPS scheme and an explicit port number in the netloc for a request made over HTTPS onto a hostname different from the hostname of the L{SiteConfiguration} if the L{SiteConfiguration} has an HTTPS server on a non-standard port. """ self._differentHostnameNonstandardPort(SSLPort, True, 'https') def test_rootURLNonstandardRequestPort(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made onto a non-standard port which is one on which the L{SiteConfiguration} is configured to listen. """ request = FakeRequest( headers={'host': '%s:%s' % (self.domain.encode('ascii'), 54321)}) TCPPort(store=self.store, factory=self.site, portNumber=54321) self.assertEqual(self.site.rootURL(request), URL('', ''))
class SiteConfigurationTests(TestCase): """ L{xmantissa.web.Site} defines how to create an HTTP server. """ def setUp(self): self.domain = u"example.com" self.store = Store() self.site = SiteConfiguration(store=self.store, hostname=self.domain) def test_interfaces(self): """ L{SiteConfiguration} implements L{IProtocolFactoryFactory} and L{ISiteURLGenerator}. """ self.assertTrue(verifyObject(ISiteURLGenerator, self.site)) self.assertTrue(verifyObject(IProtocolFactoryFactory, self.site)) def _baseTest(self, portType, scheme, portNumber, method): portType(store=self.store, portNumber=portNumber, factory=self.site) self.assertEquals( getattr(self.site, method)(), URL(scheme, self.domain)) def test_cleartextRoot(self): """ L{SiteConfiguration.cleartextRoot} method returns the proper URL for HTTP communication with this site. """ self._baseTest(TCPPort, 'http', 80, 'cleartextRoot') def test_encryptedRoot(self): """ L{SiteConfiguration.encryptedRoot} method returns the proper URL for HTTPS communication with this site. """ self._baseTest(SSLPort, 'https', 443, 'encryptedRoot') def _nonstandardPortTest(self, portType, scheme, portNumber, method): portType(store=self.store, portNumber=portNumber, factory=self.site) self.assertEquals( getattr(self.site, method)(), URL(scheme, '%s:%s' % (self.domain, portNumber))) def test_cleartextRootNonstandardPort(self): """ L{SiteConfiguration.cleartextRoot} method returns the proper URL for HTTP communication with this site even if the server is listening on a non-standard port number. """ self._nonstandardPortTest(TCPPort, 'http', 8000, 'cleartextRoot') def test_encryptedRootNonstandardPort(self): """ L{SiteConfiguration.encryptedRoot} method returns the proper URL for HTTPS communication with this site even if the server is listening on a non-standard port number. """ self._nonstandardPortTest(SSLPort, 'https', 8443, 'encryptedRoot') def _unavailableTest(self, method): self.assertEquals(getattr(self.site, method)(), None) def test_cleartextRootUnavailable(self): """ L{SiteConfiguration.cleartextRoot} method returns None if there is no HTTP server listening. """ self._unavailableTest('cleartextRoot') def test_encryptedRootUnavailable(self): """ L{SiteConfiguration.encryptedRoot} method returns None if there is no HTTPS server listening. """ self._unavailableTest('encryptedRoot') def _hostOverrideTest(self, portType, scheme, portNumber, method): portType(store=self.store, portNumber=portNumber, factory=self.site) self.assertEquals( getattr(self.site, method)(u'example.net'), URL(scheme, 'example.net')) def test_cleartextRootHostOverride(self): """ A hostname passed to L{SiteConfiguration.cleartextRoot} overrides the configured hostname in the result. """ self._hostOverrideTest(TCPPort, 'http', 80, 'cleartextRoot') def test_encryptedRootHostOverride(self): """ A hostname passed to L{SiteConfiguration.encryptedRoot} overrides the configured hostname in the result. """ self._hostOverrideTest(SSLPort, 'https', 443, 'encryptedRoot') def _portZero(self, portType, scheme, method): randomPort = 7777 class FakePort(object): def getHost(self): return IPv4Address('TCP', u'example.com', randomPort) port = portType(store=self.store, portNumber=0, factory=self.site) port.listeningPort = FakePort() self.assertEquals( getattr(self.site, method)(), URL(scheme, '%s:%s' % (self.domain, randomPort))) def test_cleartextRootPortZero(self): """ When the C{portNumber} of a started L{TCPPort} which refers to the C{SiteConfiguration} is C{0}, L{SiteConfiguration.cleartextRoot} returns an URL with the port number which was actually bound in the netloc. """ self._portZero(TCPPort, 'http', 'cleartextRoot') def test_encryptedRootPortZero(self): """ When the C{portNumber} of a started L{SSLPort} which refers to the C{SiteConfiguration} is C{0}, L{SiteConfiguration.encryptedRoot} returns an URL with the port number which was actually bound in the netloc. """ self._portZero(SSLPort, 'https', 'encryptedRoot') def _portZeroDisconnected(self, portType, method): portType(store=self.store, portNumber=0, factory=self.site) self.assertEquals(None, getattr(self.site, method)()) def test_cleartextRootPortZeroDisconnected(self): """ When the C{portNumber} of an unstarted L{TCPPort} which refers to the C{SiteConfiguration} is C{0}, L{SiteConfiguration.cleartextRoot} returns C{None}. """ self._portZeroDisconnected(TCPPort, 'cleartextRoot') def test_encryptedRootPortZeroDisconnected(self): """ When the C{portNumber} of an unstarted L{SSLPort} which refers to the C{SiteConfiguration} is C{0}, L{SiteConfiguration.encryptedRoot} returns C{None}. """ self._portZeroDisconnected(SSLPort, 'encryptedRoot') def test_rootURL(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made onto the hostname with which the L{SiteConfiguration} is configured. """ request = FakeRequest(headers={ 'host': self.domain.encode('ascii')}) self.assertEqual(self.site.rootURL(request), URL('', '')) def test_rootURLWithoutHost(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made without a I{Host} header. """ request = FakeRequest() self.assertEqual(self.site.rootURL(request), URL('', '')) def test_rootURLWWWSubdomain(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made onto the I{www} subdomain of the hostname of the L{SiteConfiguration}. """ request = FakeRequest(headers={ 'host': 'www.' + self.domain.encode('ascii')}) self.assertEqual(self.site.rootURL(request), URL('', '')) def test_rootURLAlternateSubdomain(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made onto a subdomain known as an internal domain. """ userbase.LoginMethod( store=self.store, localpart=u'username', domain=u'example.org', internal=True, protocol=u'*', verified=True, account=self.store) request = FakeRequest(headers={ 'host': 'example.org'}) self.assertEqual(self.site.rootURL(request), URL('', '')) def _differentHostnameTest(self, portType, portNumber, isSecure, scheme): request = FakeRequest(isSecure=isSecure, headers={ 'host': 'alice.' + self.domain.encode('ascii')}) portType(store=self.store, factory=self.site, portNumber=portNumber) self.assertEqual(self.site.rootURL(request), URL(scheme, self.domain)) def test_cleartextRootURLDifferentHostname(self): """ L{SiteConfiguration.rootURL} returns an absolute URL with the HTTP scheme and its hostname as the netloc and with a path of C{/} for a request made over HTTP onto a hostname different from the hostname of the L{SiteConfiguration}. """ self._differentHostnameTest(TCPPort, 80, False, 'http') def test_encryptedRootURLDifferentHostname(self): """ L{SiteConfiguration.rootURL} returns an absolute URL with its hostname as the netloc and with a path of C{/} for a request made over HTTPS onto a hostname different from the hostname of the L{SiteConfiguration}. """ self._differentHostnameTest(SSLPort, 443, True, 'https') def _differentHostnameNonstandardPort(self, portType, isSecure, scheme): portNumber = 12345 request = FakeRequest(isSecure=isSecure, headers={ 'host': 'alice.' + self.domain.encode('ascii')}) portType(store=self.store, factory=self.site, portNumber=portNumber) self.assertEqual( self.site.rootURL(request), URL(scheme, '%s:%s' % (self.domain.encode('ascii'), portNumber))) def test_cleartextRootURLDifferentHostnameNonstandardPort(self): """ L{SiteConfiguration.rootURL} returns an absolute URL with an HTTP scheme and an explicit port number in the netloc for a request made over HTTP onto a hostname different from the hostname of the L{SiteConfiguration} if the L{SiteConfiguration} has an HTTP server on a non-standard port. """ self._differentHostnameNonstandardPort(TCPPort, False, 'http') def test_encryptedRootURLDifferentHostnameNonstandardPort(self): """ L{SiteConfiguration.rootURL} returns an absolute URL with an HTTPS scheme and an explicit port number in the netloc for a request made over HTTPS onto a hostname different from the hostname of the L{SiteConfiguration} if the L{SiteConfiguration} has an HTTPS server on a non-standard port. """ self._differentHostnameNonstandardPort(SSLPort, True, 'https') def test_rootURLNonstandardRequestPort(self): """ L{SiteConfiguration.rootURL} returns C{/} for a request made onto a non-standard port which is one on which the L{SiteConfiguration} is configured to listen. """ request = FakeRequest(headers={ 'host': '%s:%s' % (self.domain.encode('ascii'), 54321)}) TCPPort(store=self.store, factory=self.site, portNumber=54321) self.assertEqual(self.site.rootURL(request), URL('', ''))