本文整理汇总了Python中twisted.web.test.test_web.DummyRequest类的典型用法代码示例。如果您正苦于以下问题:Python DummyRequest类的具体用法?Python DummyRequest怎么用?Python DummyRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DummyRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mk_request
def mk_request(self, user=None, password=None):
request = DummyRequest([''])
if user is not None:
request.headers["authorization"] = (
"Basic %s" % base64.b64encode("%s:%s" % (user, password))
)
return request
示例2: test_getChild
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''])
示例3: test_renderWithHost
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])
示例4: test_receipt_processing
def test_receipt_processing(self):
"""it should be able to process an incoming XML receipt via HTTP"""
publisher = TestPublisher()
resource = transport.OperaReceiptResource(publisher)
request = DummyRequest('/api/v1/sms/opera/receipt.xml')
request.content = StringIO("""
<?xml version="1.0"?>
<!DOCTYPE receipts>
<receipts>
<receipt>
<msgid>26567958</msgid>
<reference>001efc31</reference>
<msisdn>+27123456789</msisdn>
<status>D</status>
<timestamp>20080831T15:59:24</timestamp>
<billed>NO</billed>
</receipt>
</receipts>
""".strip())
resource.render_POST(request)
self.assertEquals(publisher.queue.pop(), (Message(**{
'transport_name': 'Opera',
'transport_msg_id': '001efc31',
'transport_status': 'D', # OK / delivered, opera specific
'transport_delivered_at': datetime(2008, 8, 31, 15, 59, 24),
}), {
'routing_key': 'sms.receipt.opera'
})
)
示例5: test_renderSecureRequest
def test_renderSecureRequest(self):
"""
When the rendered request is over HTTPS, L{WebSocketsResource} wraps
the protocol of the C{TLSMemoryBIOProtocol} instance.
"""
request = DummyRequest("/")
request.requestHeaders = Headers()
transport = StringTransportWithDisconnection()
secureProtocol = TLSMemoryBIOProtocol(Factory(), Protocol())
transport.protocol = secureProtocol
request.transport = transport
request.headers.update({
"upgrade": "Websocket",
"connection": "Upgrade",
"sec-websocket-key": "secure",
"sec-websocket-version": "13"})
result = self.resource.render(request)
self.assertEqual(NOT_DONE_YET, result)
self.assertEqual(
{"connection": "Upgrade",
"upgrade": "WebSocket",
"sec-websocket-accept": "oYBv54i42V5dw6KnZqOFroecUTc="},
request.outgoingHeaders)
self.assertEqual([""], request.written)
self.assertEqual(101, request.responseCode)
self.assertIdentical(None, request.transport)
self.assertIsInstance(
transport.protocol.wrappedProtocol, WebSocketsProtocol)
self.assertIsInstance(
transport.protocol.wrappedProtocol._receiver,
SavingEchoReceiver)
示例6: test_renderIProtocol
def test_renderIProtocol(self):
"""
If the protocol returned by C{lookupProtocol} isn't a
C{WebSocketsProtocol}, L{WebSocketsResource} wraps it automatically
with L{WebSocketsProtocolWrapper}.
"""
def lookupProtocol(names, otherRequest):
return AccumulatingProtocol(), None
self.resource = WebSocketsResource(lookupProtocol)
request = DummyRequest("/")
request.requestHeaders = Headers()
transport = StringTransportWithDisconnection()
transport.protocol = Protocol()
request.transport = transport
request.headers.update({
"upgrade": "Websocket",
"connection": "Upgrade",
"sec-websocket-key": "secure",
"sec-websocket-version": "13"})
result = self.resource.render(request)
self.assertEqual(NOT_DONE_YET, result)
self.assertIsInstance(transport.protocol, WebSocketsProtocolWrapper)
self.assertIsInstance(transport.protocol.wrappedProtocol,
AccumulatingProtocol)
示例7: test_render
def test_render(self):
"""
When rendering a request, L{WebSocketsResource} uses the
C{Sec-WebSocket-Key} header to generate a C{Sec-WebSocket-Accept}
value. It creates a L{WebSocketsProtocol} instance connected to the
protocol provided by the user factory.
"""
request = DummyRequest("/")
request.requestHeaders = Headers()
transport = StringTransportWithDisconnection()
transport.protocol = Protocol()
request.transport = transport
request.headers.update({
"upgrade": "Websocket",
"connection": "Upgrade",
"sec-websocket-key": "secure",
"sec-websocket-version": "13"})
result = self.resource.render(request)
self.assertEqual(NOT_DONE_YET, result)
self.assertEqual(
{"connection": "Upgrade",
"upgrade": "WebSocket",
"sec-websocket-accept": "oYBv54i42V5dw6KnZqOFroecUTc="},
request.outgoingHeaders)
self.assertEqual([""], request.written)
self.assertEqual(101, request.responseCode)
self.assertIdentical(None, request.transport)
self.assertIsInstance(transport.protocol._receiver,
SavingEchoReceiver)
示例8: test_renderProtocol
def test_renderProtocol(self):
"""
If protocols are specified via the C{Sec-WebSocket-Protocol} header,
L{WebSocketsResource} passes them to its C{lookupProtocol} argument,
which can decide which protocol to return, and which is accepted.
"""
def lookupProtocol(names, otherRequest):
self.assertEqual(["foo", "bar"], names)
self.assertIdentical(request, otherRequest)
return self.echoProtocol, "bar"
self.resource = WebSocketsResource(lookupProtocol)
request = DummyRequest("/")
request.requestHeaders = Headers(
{"sec-websocket-protocol": ["foo", "bar"]})
transport = StringTransportWithDisconnection()
transport.protocol = Protocol()
request.transport = transport
request.headers.update({
"upgrade": "Websocket",
"connection": "Upgrade",
"sec-websocket-key": "secure",
"sec-websocket-version": "13"})
result = self.resource.render(request)
self.assertEqual(NOT_DONE_YET, result)
self.assertEqual(
{"connection": "Upgrade",
"upgrade": "WebSocket",
"sec-websocket-protocol": "bar",
"sec-websocket-accept": "oYBv54i42V5dw6KnZqOFroecUTc="},
request.outgoingHeaders)
self.assertEqual([""], request.written)
self.assertEqual(101, request.responseCode)
示例9: __init__
def __init__(self, postpath, params, session=None):
self.content = StringIO()
self.content.write(urllib.urlencode(params))
self.content.seek(0, 0)
self.requestHeaders = Headers()
DummyRequest.__init__(self, postpath, session)
示例10: test_invalidMethod
def test_invalidMethod(self):
"""
L{UserDirectory.render} raises L{UnsupportedMethod} in response to a
non-I{GET} request.
"""
request = DummyRequest([""])
request.method = "POST"
self.assertRaises(server.UnsupportedMethod, self.directory.render, request)
示例11: __init__
def __init__(self, method, url, args=None):
DummyRequest.__init__(self, url.split('/'))
self.method = method
# set args
args = args or {}
for k, v in args.items():
self.addArg(k, v)
示例12: __init__
def __init__(self, postpath, params):
DummyRequest.__init__(self, postpath, None)
self.content = StringIO()
self.content.write(json.dumps(params))
self.content.seek(0, 0)
self.requestHeaders = Headers()
self.method = 'POST'
self.requestHeaders.addRawHeader('content-type', 'application/json')
示例13: create_request
def create_request(params={}, path='/', method='POST'):
"""
Creates a dummy Vas2Nets request for testing our resources with
"""
request = DummyRequest(path)
request.method = method
request.args = params
return request
示例14: test_song_id_does_not_exist
def test_song_id_does_not_exist(self):
raise SkipTest("Twisted using deferToThread seems to break the test db connection that django establishes. However, with the real django db connection this works fine...")
r = web.Song(1)
request = DummyRequest([""])
d = request.notifyFinish()
d.addCallback(self._assert_song_does_not_exist, request)
result = r.render_GET(request)
self.assertEqual(result, server.NOT_DONE_YET)
return d
示例15: request_generator
def request_generator(url, method='GET', content=True, headers=True):
request = DummyRequest(url)
request.method = method
if content:
request.content = StringIO()
if headers:
request.requestHeaders = Headers()
return request