dimanche 2 août 2015

Testing http/REST resources with Twisted in Twisted

I looking for some example code to test a http/REST-resource managed by Twisted. From the book Twisted - Network Programming Essentials I've found the following code:

Server:

# echo.py
from twisted.internet import reactor, protocol

class EchoClient(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.ClientFactory):
    def buildProtocol(self, addr):
        return EchoClient()

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed."
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost."
        reactor.stop()

Unittest:

# echoserver-test.py
from twisted.test import proto_helpers
from twisted.trial import unittest

from echo import EchoFactory

class EchoServerTestCase(unittest.TestCase):
    def test_echo(self):
        factory = EchoFactory()

        self.proto = factory.buildProtocol(("localhost", 0))
        self.transport = proto_helpers.StringTransport()
        self.proto.makeConnection(self.transport)
        self.proto.dataReceived("test\r\n")
        self.assertEqual(self.transport.value(), "test\r\n")

Everything is fine with that code but it's only a small example for a simple line based protocol.

My question is: can you test a normal http/REST resource with Twisted without firing up the server and using a real localhost connection.

I'm looking for code to test http/REST resources within Twisted with Twisted itself in an offline mode. Could anyone provide some example code for this?

Aucun commentaire:

Enregistrer un commentaire