Пример #1
0
    def test_getChild(self):
        """
        L{NameVirtualHost.getChild} returns correct I{Resource} based off
        the header and modifies I{Request} to ensure proper prepath and
        postpath are set.
        """
        virtualHostResource = NameVirtualHost()
        leafResource = Data(b"leaf data", "")
        leafResource.isLeaf = True
        normResource = Data(b"norm data", "")
        virtualHostResource.addHost(b"leaf.example.org", leafResource)
        virtualHostResource.addHost(b"norm.example.org", normResource)

        request = DummyRequest([])
        request.requestHeaders.addRawHeader(b"host", b"norm.example.org")
        request.prepath = [b""]

        self.assertIsInstance(virtualHostResource.getChild(b"", request),
                              NoResource)
        self.assertEqual(request.prepath, [b""])
        self.assertEqual(request.postpath, [])

        request = DummyRequest([])
        request.requestHeaders.addRawHeader(b"host", b"leaf.example.org")
        request.prepath = [b""]

        self.assertIsInstance(virtualHostResource.getChild(b"", request), Data)
        self.assertEqual(request.prepath, [])
        self.assertEqual(request.postpath, [b""])
Пример #2
0
    def test_getChild(self):
        """
        L{_HostResource.getChild} returns the proper I{Resource} for the vhost
        embedded in the URL.  Verify that returning the proper I{Resource}
        required changing the I{Host} in the header.
        """
        bazroot = Data(b"root data", "")
        bazuri = Data(b"uri data", "")
        baztest = Data(b"test data", "")
        bazuri.putChild(b"test", baztest)
        bazroot.putChild(b"uri", bazuri)
        hr = _HostResource()

        root = NameVirtualHost()
        root.default = Data(b"default data", "")
        root.addHost(b"baz.com", bazroot)

        request = DummyRequest([b"uri", b"test"])
        request.prepath = [b"bar", b"http", b"baz.com"]
        request.site = Site(root)
        request.isSecure = lambda: False
        request.host = b""

        step = hr.getChild(b"baz.com", request)  # Consumes rest of path
        self.assertIsInstance(step, Data)

        request = DummyRequest([b"uri", b"test"])
        step = root.getChild(b"uri", request)
        self.assertIsInstance(step, NoResource)
Пример #3
0
    def setUp(self):
        self.agent = None # for twisted.web.client.Agent test
        self.cleanupServerConnections = 0
        r = resource.Resource()
        r.putChild(b"file", Data(b"0123456789", "text/html"))
        r.putChild(b"redirect", Redirect(b"/file"))
        self.infiniteRedirectResource = CountingRedirect(b"/infiniteRedirect")
        r.putChild(b"infiniteRedirect", self.infiniteRedirectResource)
        r.putChild(b"wait", ForeverTakingResource())
        r.putChild(b"write-then-wait", ForeverTakingResource(write=True))
        r.putChild(b"never-read", ForeverTakingNoReadingResource())
        r.putChild(b"error", ErrorResource())
        r.putChild(b"nolength", NoLengthResource())
        r.putChild(b"host", HostHeaderResource())
        r.putChild(b"payload", PayloadResource())
        r.putChild(b"broken", BrokenDownloadResource())
        r.putChild(b"cookiemirror", CookieMirrorResource())
        r.putChild(b'delay1', DelayResource(1))
        r.putChild(b'delay2', DelayResource(2))

        self.afterFoundGetCounter = CountingResource()
        r.putChild(b"afterFoundGetCounter", self.afterFoundGetCounter)
        r.putChild(b"afterFoundGetRedirect", Redirect(b"/afterFoundGetCounter"))

        miscasedHead = Data(b"miscased-head GET response content", "major/minor")
        miscasedHead.render_Head = lambda request: b"miscased-head content"
        r.putChild(b"miscased-head", miscasedHead)

        self.extendedRedirect = ExtendedRedirect(b'/extendedRedirect')
        r.putChild(b"extendedRedirect", self.extendedRedirect)
        self.site = server.Site(r, timeout=None)
        self.wrapper = WrappingFactory(self.site)
        self.port = self._listen(self.wrapper)
        self.portno = self.port.getHost().port
Пример #4
0
 def setUp(self):
     self.username = b'foo bar'
     self.password = b'bar baz'
     self.avatarContent = b"contents of the avatar resource itself"
     self.childName = b"foo-child"
     self.childContent = b"contents of the foo child of the avatar"
     self.checker = InMemoryUsernamePasswordDatabaseDontUse()
     self.checker.addUser(self.username, self.password)
     self.avatar = Data(self.avatarContent, 'text/plain')
     self.avatar.putChild(self.childName,
                          Data(self.childContent, 'text/plain'))
     self.avatars = {self.username: self.avatar}
     self.realm = Realm(self.avatars.get)
     self.portal = portal.Portal(self.realm, [self.checker])
     self.wrapper = SoledadSession(self.portal)
Пример #5
0
    def test_http(self):
        """
        When connecting to a HTTP server, a PB connection times
        out.
        """
        result = defer.Deferred()

        site = Site(Data("", "text/plain"))
        client = HangCheckFactory(
            PBClientFactory(), lambda: result.callback(None))

        self.patch(HangCheckProtocol, '_HUNG_CONNECTION_TIMEOUT', 0.1)

        d_connected = connected_server_and_client(
            self, site, client,
        )

        def cancel_all():
            result.cancel()
            d_connected.cancel()

        timer = reactor.callLater(2, cancel_all)

        try:
            yield result
        except defer.CancelledError:
            raise Exception('Timeout did not happen')
        finally:
            d_connected.cancel()
            timer.cancel()
    def test_renderWithHost(self):
        """
        L{NameVirtualHost.render} returns the result of rendering the resource
        which is the value in the instance's C{host} dictionary corresponding
        to the key indicated by the value of the I{Host} header in the request.
        """
        virtualHostResource = NameVirtualHost()
        virtualHostResource.addHost('example.org', Data("winner", ""))

        request = DummyRequest([''])
        request.headers['host'] = 'example.org'
        d = _render(virtualHostResource, request)
        def cbRendered(ignored, request):
            self.assertEqual(''.join(request.written), "winner")
        d.addCallback(cbRendered, request)

        # The port portion of the Host header should not be considered.
        requestWithPort = DummyRequest([''])
        requestWithPort.headers['host'] = 'example.org:8000'
        dWithPort = _render(virtualHostResource, requestWithPort)
        def cbRendered(ignored, requestWithPort):
            self.assertEqual(''.join(requestWithPort.written), "winner")
        dWithPort.addCallback(cbRendered, requestWithPort)

        return gatherResults([d, dWithPort])
Пример #7
0
    def test_trpProcessor(self):
        """
        The I{--path} option creates a root resource which serves the
        pickled resource out of any child with the C{".rpy"} extension.
        """
        path, root = self._pathOption()
        path.child("foo.trp").setContent(pickle.dumps(Data("foo", "bar")))
        child = root.getChild("foo.trp", None)
        self.assertIsInstance(child, Data)
        self.assertEqual(child.data, 'foo')
        self.assertEqual(child.type, 'bar')

        warnings = self.flushWarnings()

        # If the trp module hadn't been imported before this test ran, there
        # will be two deprecation warnings; one for the module, one for the
        # function.  If the module has already been imported, the
        # module-scope deprecation won't be emitted again.
        if len(warnings) == 2:
            self.assertEqual(warnings[0]['category'], DeprecationWarning)
            self.assertEqual(
                warnings[0]['message'],
                "twisted.web.trp is deprecated as of Twisted 9.0.  Resource "
                "persistence is beyond the scope of Twisted Web.")
            warning = warnings[1]
        else:
            warning = warnings[0]

        self.assertEqual(warning['category'], DeprecationWarning)
        self.assertEqual(
            warning['message'],
            "twisted.web.trp.ResourceUnpickler is deprecated as of Twisted "
            "9.0.  Resource persistence is beyond the scope of Twisted Web.")
Пример #8
0
def get_provider_factory():
    """
    Instantiates a Site that serves the resources
    that we expect from a valid provider.
    Listens on:
    * port 8000 for http connections
    * port 8443 for https connections

    :rparam: factory for a site
    :rtype: Site instance
    """
    root = Data("", "")
    root.putChild("", root)
    root.putChild("provider.json",
                  FileModified(os.path.join(_here, "test_provider.json")))
    config = Resource()
    config.putChild("eip-service.json",
                    FileModified(os.path.join(_here, "eip-service.json")))
    apiv1 = Resource()
    apiv1.putChild("config", config)
    apiv1.putChild("sessions", API_Sessions())
    apiv1.putChild("users", FakeUsers(None))
    apiv1.putChild("cert", FileModified(os.path.join(_here, 'openvpn.pem')))
    root.putChild("1", apiv1)

    factory = Site(root)
    return factory
Пример #9
0
    def test_http(self):
        """
        When connecting to a HTTP server, a PB connection times
        out.
        """
        result = Deferred()

        site = Site(Data("", "text/plain"))
        client = HangCheckFactory(PBClientFactory(),
                                  lambda: result.callback(None))

        self.patch(HangCheckProtocol, '_HUNG_CONNECTION_TIMEOUT', 0.1)

        connected_server_and_client(
            self,
            site,
            client,
        )

        timer = reactor.callLater(2, result.cancel)
        result.addCallback(lambda _: timer.cancel())

        def check_for_timeout(reason):
            reason.trap(CancelledError)
            raise Exception("Didn't not hangup")

        result.addErrback(check_for_timeout)

        return result
Пример #10
0
 def getChild(self, path, request):
     try:
         paste = Paste.findByName(self._store, path.decode('ascii'))
     except ItemNotFound:
         return NoResource('No such paste')
     else:
         return Data(paste.toJSON(), 'application/json')
Пример #11
0
    def setUp(self):
        market_factory = WebSocketServerFactory(
            'ws://localhost:8080/market_stream')
        user_factory = WebSocketServerFactory(
            'ws://localhost:8080/user_stream')
        market_factory.protocol = MarketStreamServerProtocol
        user_factory.protocol = UserStreamServerProtocol
        market_factory.startFactory()
        user_factory.startFactory()
        root = Data('', 'text/plain')
        root.putChild(b'market_stream', WebSocketResource(market_factory))
        root.putChild(b'user_stream', WebSocketResource(user_factory))
        site = Site(root)
        reactor.listenTCP(8080, site)

        def run_server():
            reactor.run(installSignalHandlers=False)

        Thread(target=run_server).start()

        def run_client():
            from examples import simple_trading

        self.client_process = Process(target=run_client)
        self.client_process.start()
Пример #12
0
 def setUp(self):
     root = Data(b'El toro!', 'text/plain')
     root.putChild(b"cookiemirror", CookieMirrorResource())
     root.putChild(b"rawcookiemirror", RawCookieMirrorResource())
     site = server.Site(root, timeout=None)
     self.port = self._listen(site)
     self.portno = self.port.getHost().port
Пример #13
0
 def start_responding(self, server_name, challenge, response):
     """
     Add the child resource.
     """
     self.resource.putChild(
         challenge.encode('token').encode('utf-8'),
         Data(response.key_authorization.encode('utf-8'), 'text/plain'))
Пример #14
0
def magic_folder_uri_hierarchy_from_magic_folder_json(
    folders,
    collective_dircap,
    collective_json,
    upload_dircap,
    upload_json,
    token,
):
    upload = Data(
        dumps(upload_json),
        b"text/plain",
    )
    collective = Data(
        dumps(collective_json),
        b"text/plain",
    )

    uri = Resource()
    uri.putChild(
        upload_dircap,
        upload,
    )
    uri.putChild(
        cap_from_string(
            upload_dircap.encode("ascii")).get_readonly().to_string(),
        upload,
    )
    uri.putChild(
        collective_dircap,
        collective,
    )

    api = MagicFolderWebApi(
        get_magic_folder=lambda name: folders[name.decode("utf-8")],
        get_auth_token=lambda: token,
    )

    root = Resource()
    # Unfortunately the following two resource hierarchies should live at
    # different servers.  However, we lack multi-server support in our web
    # testing library.  So, they live side-by-side.  I hope that if the
    # implementation mistakenly sends requests to the wrong server it will be
    # blindingly obvious and this test shortcoming will not hurt us much.
    root.putChild(b"uri", uri)
    root.putChild(b"api", api)

    return root
Пример #15
0
 def getChild(self, path, request):
     try:
         paste = Paste.findByName(self.store, path.decode('ascii'))
     except ItemNotFound:
         return NoResource('No such paste')
     else:
         return Data(paste.content.encode('utf-8'),
                     'text/plain; charset=UTF-8')
Пример #16
0
    def init_app(self, app, workerid):

        # do your imports *here*
        from twisted.web.static import Data
        from twoost.httprpc import DumbRPCResource, XMLRPCResource
        from demoapp.webres import SlowHelloWorldResource
        from demoapp.webapi import WebAPIService

        # our webapi-worker hasn't direct access to DB
        # it only validates data & send it to our amqp
        amqps = build_amqps(app, ['default'])

        webapi_service = attach_service(app, WebAPIService(
            queue_event_callback=amqps['default'].makeSender(
                # routing_key_fn=lambda msg: msg['some_field'],
                exchange='new_event',
            ),
            # we can use various callbacks here
            # `amqps.makeSender(...)` - send message via AMQP
            # `partial(proxy.remoteCall, 'method-name')` - execute RPC
            # `other_service.method_name` - direct access...
            # `partial(logger.info, "msg: %r")` -- just log
            # `lambda _: None` -- skip at all...
        ))

        # RPC fn = any function, which CAN return Deferred
        rpc_methods = {
            'version': lambda: __version__,
            'new_event': webapi_service.new_event,
        }

        restree = {
            'demoapp': {
                None: Data("twoost demoapp. <a href='/demoapp/'>Hello</a>.", 'text/html'),
                '': SlowHelloWorldResource(),
                'rpc': {
                    'dumbrpc': DumbRPCResource(rpc_methods),
                    'xmlrpc': XMLRPCResource(rpc_methods),
                    # TODO: add JSON-RPC ?
                },
                'workerid': Data(workerid, "text/plain"),
            },
        }

        build_web(app, restree)
Пример #17
0
    def test_authorized(self, auth_token, child_segments, content):
        """
        If the correct bearer token is not given in the **Authorization** header
        of the request then the response code is UNAUTHORIZED.

        :param bytes auth_token: A bearer token which, when presented, should
            authorize access to the resource.

        :param [unicode] child_segments: Additional path segments to add to the
            request path beneath **v1**.

        :param bytes content: The bytes we expect to see on a successful
            request.
        """
        def get_auth_token():
            return auth_token

        # Since we don't want to exercise any real magic-folder application
        # logic we'll just magic up the child resource being requested.
        branch = Data(
            content,
            b"application/binary",
        )
        segments_remaining = child_segments[:]
        while segments_remaining:
            name = segments_remaining.pop()
            resource = Resource()
            resource.putChild(name.encode("utf-8"), branch)
            branch = resource

        root = magic_folder_resource(
            get_auth_token,
            v1_resource=branch,
        )

        treq = StubTreq(root)
        url = DecodedURL.from_text(u"http://example.invalid./v1").child(
            *child_segments)
        encoded_url = url_to_bytes(url)

        # A request with no token at all or the wrong token should receive an
        # unauthorized response.
        headers = {
            b"Authorization": u"Bearer {}".format(auth_token).encode("ascii"),
        }

        self.assertThat(
            treq.get(
                encoded_url,
                headers=headers,
            ),
            succeeded(
                matches_response(
                    code_matcher=Equals(OK),
                    body_matcher=Equals(content),
                ), ),
        )
Пример #18
0
    def _get_index_content(self):
        ns = {}
        for attribute in [
                'codecs', 'mime-type', 'width', 'height', 'stream-url'
        ]:
            ns[attribute] = self._properties[attribute]

        content = HTML5TEMPLATE % ns
        return Data(content, 'text/html')
Пример #19
0
 def test_renderWithoutHost(self):
     """
     L{NameVirtualHost.render} returns the result of rendering the
     instance's C{default} if it is not C{None} and there is no I{Host}
     header in the request.
     """
     virtualHostResource = NameVirtualHost()
     virtualHostResource.default = Data("correct result", "")
     request = DummyRequest([''])
     self.assertEqual(virtualHostResource.render(request), "correct result")
Пример #20
0
 def setUp(self):
     """
     Create a realm, portal, and L{HTTPAuthSessionWrapper} to use in the tests.
     """
     self.username = b'foo bar'
     self.password = b'bar baz'
     self.avatarContent = b"contents of the avatar resource itself"
     self.childName = b"foo-child"
     self.childContent = b"contents of the foo child of the avatar"
     self.checker = InMemoryUsernamePasswordDatabaseDontUse()
     self.checker.addUser(self.username, self.password)
     self.avatar = Data(self.avatarContent, 'text/plain')
     self.avatar.putChild(self.childName,
                          Data(self.childContent, 'text/plain'))
     self.avatars = {self.username: self.avatar}
     self.realm = Realm(self.avatars.get)
     self.portal = portal.Portal(self.realm, [self.checker])
     self.credentialFactories = []
     self.wrapper = HTTPAuthSessionWrapper(self.portal,
                                           self.credentialFactories)
Пример #21
0
    def setUp(self):
        plainRoot = Data(b'not me', 'text/plain')
        tlsRoot = Data(b'me neither', 'text/plain')

        plainSite = server.Site(plainRoot, timeout=None)
        tlsSite = server.Site(tlsRoot, timeout=None)

        self.tlsPort = reactor.listenSSL(
            0, tlsSite,
            contextFactory=ssl.DefaultOpenSSLContextFactory(
                serverPEMPath, serverPEMPath),
            interface="127.0.0.1")
        self.plainPort = reactor.listenTCP(0, plainSite, interface="127.0.0.1")

        self.plainPortno = self.plainPort.getHost().port
        self.tlsPortno = self.tlsPort.getHost().port

        plainRoot.putChild(b'one', Redirect(self.getHTTPS('two')))
        tlsRoot.putChild(b'two', Redirect(self.getHTTP('three')))
        plainRoot.putChild(b'three', Redirect(self.getHTTPS('four')))
        tlsRoot.putChild(b'four', Data(b'FOUND IT!', 'text/plain'))
Пример #22
0
 def test_makePersonalServerFactory(self):
     """
     L{makePersonalServerFactory} returns a PB server factory which has
     as its root object a L{ResourcePublisher}.
     """
     # The fact that this pile of objects can actually be used somehow is
     # verified by twisted.web.test.test_distrib.
     site = Site(Data(b"foo bar", "text/plain"))
     serverFactory = makePersonalServerFactory(site)
     self.assertIsInstance(serverFactory, PBServerFactory)
     self.assertIsInstance(serverFactory.root, ResourcePublisher)
     self.assertIdentical(serverFactory.root.site, site)
Пример #23
0
    def setUp(self):
        self.avatar_content = b"avatar content"
        self.child_content = b"child content"
        self.grandchild_content = b"grandchild content"

        grandchild = Data(self.grandchild_content, b"text/plain")

        child = Data(self.child_content, b"text/plain")
        child.putChild(b"grandchild", grandchild)

        self.avatar = Data(self.avatar_content, b"text/plain")
        self.avatar.putChild(b"child", child)

        self.realm = OneIResourceAvatarRealm(self.avatar)
        self.portal = Portal(
            self.realm,
            [AllowAnonymousAccess()],
        )
        self.guard = HTTPAuthSessionWrapper(
            self.portal,
            [BasicCredentialFactory("example.com")],
        )
Пример #24
0
    def _get_index_content(self):
        html_template = self._get_template_filename()
        ns = {}
        ns['has-audio'] = _htmlbool(self._properties['has-audio'])
        ns['has-video'] = _htmlbool(self._properties['has-video'])
        for attribute in [
                'codebase', 'width', 'height', 'stream-url', 'buffer-size'
        ]:
            ns[attribute] = self._properties[attribute]

        data = open(html_template, 'r').read()
        content = data % ns
        return Data(content, 'text/html')
Пример #25
0
 def get_client_resource(self, configuration):
     """
     :return: A static data resource that produces the given configuration when
         rendered, as an aid to testing.
     """
     items = configuration.items(self._client_section_name, [])
     resource = Data(
         dumps(dict(items)),
         b"text/json",
     )
     # Give it some dynamic stuff too.
     resource.putChild(b"counter", GetCounter())
     return resource
Пример #26
0
def configuration(stripe_publishable_api_key):
    """
    Create a ``Resource`` which serves up simple configuration used by
    JavaScript on the website.
    """
    return Data(
        dumps({
            # Stripe publishable key identifies a Stripe account in
            # API uses.  It's safe to share and required by the
            # JavaScript Stripe client API.
            u"stripe-publishable-api-key": stripe_publishable_api_key,
        }),
        b"application/json",
    )
 def test_renderWithUnknownHost(self):
     """
     L{NameVirtualHost.render} returns the result of rendering the
     instance's C{default} if it is not C{None} and there is no host
     matching the value of the I{Host} header in the request.
     """
     virtualHostResource = NameVirtualHost()
     virtualHostResource.default = Data("correct data", "")
     request = DummyRequest([''])
     request.headers['host'] = 'example.com'
     d = _render(virtualHostResource, request)
     def cbRendered(ignored):
         self.assertEqual(''.join(request.written), "correct data")
     d.addCallback(cbRendered)
     return d
Пример #28
0
 def test_some_zones(self):
     agent = RequestTraversalAgent(
         static_resource({
             b"2013-04-01": {
                 b"hostedzone":
                 Data(
                     sample_list_hosted_zones_result.xml,
                     b"text/xml",
                 ),
             },
         }))
     aws = AWSServiceRegion(access_key="abc", secret_key="def")
     client = get_route53_client(agent, aws, uncooperator())
     zones = self.successResultOf(client.list_hosted_zones())
     expected = [HostedZone(**sample_list_hosted_zones_result.details)]
     self.assertEquals(expected, zones)
Пример #29
0
 def _client_for_rrsets(self, zone_id, rrsets_xml):
     agent = RequestTraversalAgent(
         static_resource({
             b"2013-04-01": {
                 b"hostedzone": {
                     zone_id: {
                         b"rrset": Data(
                             rrsets_xml,
                             b"text/xml",
                         )
                     }
                 }
             }
         }))
     aws = AWSServiceRegion(access_key="abc", secret_key="def")
     return get_route53_client(agent, aws, uncooperator())
Пример #30
0
    def go(self, reactor):
        data = Data("Hello world\n", "text/plain")
        data.putChild("", data)
        factory = Site(data)

        # TODO: adoptStreamConnection should really support AF_UNIX
        protocol = ConnectionFromManager(reactor, factory)
        skt = fromfd(MAGIC_FILE_DESCRIPTOR, AF_UNIX, SOCK_STREAM)
        os.close(MAGIC_FILE_DESCRIPTOR)
        serverTransport = UNIXServer(skt, protocol, None, None, 1234, reactor)
        protocol.makeConnection(serverTransport)
        serverTransport.startReading()

        globalLogBeginner.beginLoggingTo([protocol.sendLog])
        factory.doStart()

        return Deferred()