Ejemplo n.º 1
0
    def _root(self, scheme, hostname, portObj, standardPort):
        # TODO - real unicode support (but punycode is so bad)
        if portObj is None:
            return None

        portNumber = portObj.portNumber
        port = portObj.listeningPort

        if hostname is None:
            hostname = self.hostname
        else:
            hostname = hostname.split(':')[0].encode('ascii')

        if portNumber == 0:
            if port is None:
                return None
            else:
                portNumber = port.getHost().port

        # At some future point, we may want to make pathsegs persistently
        # configurable - perhaps scheme and hostname as well - in order to
        # easily support reverse proxying configurations, particularly where
        # Mantissa is being "mounted" somewhere other than /.  See also rootURL
        # which has some overlap with this method (the difference being
        # universal vs absolute URLs - rootURL may want to call cleartextRoot
        # or encryptedRoot in the future).  See #417 and #2309.
        pathsegs = ['']
        if portNumber != standardPort:
            hostname = '%s:%d' % (hostname, portNumber)
        return URL(scheme, hostname, pathsegs)
Ejemplo n.º 2
0
 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('', ''))
Ejemplo n.º 3
0
 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('', ''))
Ejemplo n.º 4
0
 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('', ''))
Ejemplo n.º 5
0
 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)))
Ejemplo n.º 6
0
 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('', ''))
Ejemplo n.º 7
0
    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)))
Ejemplo n.º 8
0
 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('', ''))
Ejemplo n.º 9
0
 def test_renderHTTPNeedsSecure(self):
     """
     L{SecuringWrapper.renderHTTP} returns a L{URL} pointing at the same
     location as the request URI but with an https scheme if the wrapped
     resource has a C{needsSecure} attribute with a true value and the
     request is over http.
     """
     SSLPort(store=self.store, factory=self.urlGenerator, portNumber=443)
     request = FakeRequest(isSecure=False,
                           uri='/bar/baz',
                           currentSegments=['bar', 'baz'])
     self.resource.needsSecure = True
     result = self.wrapper.renderHTTP(request)
     self.assertEqual(
         result, URL('https', self.urlGenerator.hostname, ['bar', 'baz']))
Ejemplo n.º 10
0
    def test_getJSModuleURL(self):
        """
        L{MantissaLivePage.getJSModuleURL} should return a child of its
        C{_moduleRoot} attribute of the form::

            _moduleRoot/<SHA1 digest of module contents>/Package.ModuleName
        """
        module = 'Mantissa'
        url = URL(scheme='https', netloc='example.com', pathsegs=['foo'])
        page = MantissaLivePage(None)
        page._moduleRoot = url
        jsDir = FilePath(__file__).parent().parent().child("js")
        modulePath = jsDir.child(module).child("__init__.js")
        moduleContents = modulePath.open().read()
        expect = sha.new(moduleContents).hexdigest()
        self.assertEqual(page.getJSModuleURL(module),
                         url.child(expect).child(module))
Ejemplo n.º 11
0
    def test_jsCaching(self):
        """
        Rendering a L{MantissaLivePage} causes each of its dependent modules to
        be loaded at most once.
        """
        _realdeps = AthenaModule.dependencies

        def dependencies(self):
            self.count = getattr(self, 'count', 0) + 1
            return _realdeps(self)

        self.patch(AthenaModule, 'dependencies', dependencies)

        module = jsDeps.getModuleForName('Mantissa.Test.Dummy.DummyWidget')
        module.count = 0

        root = URL(netloc='example.com', pathsegs=['a', 'b'])

        class FakeWebSite(object):
            def rootURL(self, request):
                return root

        def _renderPage():
            page = MantissaLivePage(FakeWebSite())
            element = LiveElement(
                stan(tags.span(render=tags.directive('liveElement'))))
            element.setFragmentParent(page)
            element.jsClass = u'Mantissa.Test.Dummy.DummyWidget'
            page.docFactory = stan(
                [tags.span(render=tags.directive('liveglue')), element])

            ctx = WovenContext()
            req = FakeRequest(headers={'host': self.hostname})
            ctx.remember(req, IRequest)
            page.beforeRender(ctx)
            page.renderHTTP(ctx)
            page._messageDeliverer.close()

        # Make sure we have a fresh dependencies memo.
        self.patch(theHashModuleProvider, 'depsMemo', {})

        _renderPage()
        self.assertEqual(module.count, 1)
        _renderPage()
        self.assertEqual(module.count, 1)
Ejemplo n.º 12
0
    def test_beforeRenderSetsModuleRoot(self):
        """
        L{MantissaLivePage.beforeRender} should set I{_moduleRoot} to the
        C{__jsmodule__} child of the URL returned by the I{rootURL}
        method of the L{WebSite} it wraps.
        """
        receivedRequests = []
        root = URL(netloc='example.com', pathsegs=['a', 'b'])

        class FakeWebSite(object):
            def rootURL(self, request):
                receivedRequests.append(request)
                return root

        request = FakeRequest()
        page = MantissaLivePage(FakeWebSite())
        page.beforeRender(request)
        self.assertEqual(receivedRequests, [request])
        self.assertEqual(page._moduleRoot, root.child('__jsmodule__'))
Ejemplo n.º 13
0
    def rootURL(self, request):
        """
        Return the URL for the root of this website which is appropriate to use
        in links generated in response to the given request.

        @type request: L{twisted.web.http.Request}
        @param request: The request which is being responded to.

        @rtype: L{URL}
        @return: The location at which the root of the resource hierarchy for
            this website is available.
        """
        host = request.getHeader('host') or self.hostname
        if ':' in host:
            host = host.split(':', 1)[0]
        if (host == self.hostname or
            host.startswith('www.') and host[len('www.'):] == self.hostname):
            return URL(scheme='', netloc='', pathsegs=[''])
        else:
            if request.isSecure():
                return self.encryptedRoot(self.hostname)
            else:
                return self.cleartextRoot(self.hostname)
Ejemplo n.º 14
0
 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)))
Ejemplo n.º 15
0
 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))
Ejemplo n.º 16
0
 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))
Ejemplo n.º 17
0
 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'))