我正在寻找一些示例代码来测试由Twisted管理的http/REST-resource。从Twisted - Network Programming Essentials这本书中,我找到了以下代码:
服务器:
# 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()单元测试:
# 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")这段代码一切正常,但它只是一个简单的基于行的协议的一个小示例。
我的问题是:你能在不启动服务器和使用真正的localhost连接的情况下,用Twisted测试一个普通的http/REST资源吗?
我正在寻找代码来测试Twisted中的http/REST资源,在脱机模式下使用Twisted本身。有人能为此提供一些示例代码吗?
发布于 2015-08-03 12:57:23
将其作为对Treq的测试支持的工作正在进行中。You can find the ticket here, with references to relevant pull requests。
https://stackoverflow.com/questions/31773391
复制相似问题