本文整理汇总了Python中twisted.web.server.Request方法的典型用法代码示例。如果您正苦于以下问题:Python server.Request方法的具体用法?Python server.Request怎么用?Python server.Request使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.server
的用法示例。
在下文中一共展示了server.Request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_headersAndCode
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_headersAndCode(self):
"""
L{redirectTo} will set the C{Location} and C{Content-Type} headers on
its request, and set the response code to C{FOUND}, so the browser will
be redirected.
"""
request = Request(DummyChannel(), True)
request.method = b'GET'
targetURL = b"http://target.example.com/4321"
redirectTo(targetURL, request)
self.assertEqual(request.code, FOUND)
self.assertEqual(
request.responseHeaders.getRawHeaders(b'location'), [targetURL])
self.assertEqual(
request.responseHeaders.getRawHeaders(b'content-type'),
[b'text/html; charset=utf-8'])
示例2: test_processingFailedNoTraceback
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_processingFailedNoTraceback(self):
"""
L{Request.processingFailed} when the site has C{displayTracebacks} set
to C{False} does not write out the failure, but give a generic error
message.
"""
d = DummyChannel()
request = server.Request(d, 1)
request.site = server.Site(resource.Resource())
request.site.displayTracebacks = False
fail = failure.Failure(Exception("Oh no!"))
request.processingFailed(fail)
self.assertNotIn(b"Oh no!", request.transport.written.getvalue())
self.assertIn(
b"Processing Failed", request.transport.written.getvalue()
)
# Since we didn't "handle" the exception, flush it to prevent a test
# failure
self.assertEqual(1, len(self.flushLoggedErrors()))
示例3: test_processingFailedDisplayTraceback
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_processingFailedDisplayTraceback(self):
"""
L{Request.processingFailed} when the site has C{displayTracebacks} set
to C{True} writes out the failure.
"""
d = DummyChannel()
request = server.Request(d, 1)
request.site = server.Site(resource.Resource())
request.site.displayTracebacks = True
fail = failure.Failure(Exception("Oh no!"))
request.processingFailed(fail)
self.assertIn(b"Oh no!", request.transport.written.getvalue())
# Since we didn't "handle" the exception, flush it to prevent a test
# failure
self.assertEqual(1, len(self.flushLoggedErrors()))
示例4: test_processingFailedDisplayTracebackHandlesUnicode
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_processingFailedDisplayTracebackHandlesUnicode(self):
"""
L{Request.processingFailed} when the site has C{displayTracebacks} set
to C{True} writes out the failure, making UTF-8 items into HTML
entities.
"""
d = DummyChannel()
request = server.Request(d, 1)
request.site = server.Site(resource.Resource())
request.site.displayTracebacks = True
fail = failure.Failure(Exception(u"\u2603"))
request.processingFailed(fail)
self.assertIn(b"☃", request.transport.written.getvalue())
# On some platforms, we get a UnicodeError when trying to
# display the Failure with twisted.python.log because
# the default encoding cannot display u"\u2603". Windows for example
# uses a default encodig of cp437 which does not support u"\u2603".
self.flushLoggedErrors(UnicodeError)
# Since we didn't "handle" the exception, flush it to prevent a test
# failure
self.assertEqual(1, len(self.flushLoggedErrors()))
示例5: test_sessionAttribute
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_sessionAttribute(self):
"""
On a L{Request}, the C{session} attribute retrieves the associated
L{Session} only if it has been initialized. If the request is secure,
it retrieves the secure session.
"""
site = server.Site(resource.Resource())
d = DummyChannel()
d.transport = DummyChannel.SSL()
request = server.Request(d, 1)
request.site = site
request.sitepath = []
self.assertIs(request.session, None)
insecureSession = request.getSession(forceNotSecure=True)
self.addCleanup(insecureSession.expire)
self.assertIs(request.session, None)
secureSession = request.getSession()
self.addCleanup(secureSession.expire)
self.assertIsNot(secureSession, None)
self.assertIsNot(secureSession, insecureSession)
self.assertIs(request.session, secureSession)
示例6: test_retrieveNonExistentSession
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_retrieveNonExistentSession(self):
"""
L{Request.getSession} generates a new session if the relevant cookie is
set in the incoming request.
"""
site = server.Site(resource.Resource())
d = DummyChannel()
request = server.Request(d, 1)
request.site = site
request.sitepath = []
request.received_cookies[b'TWISTED_SESSION'] = b"does-not-exist"
session = request.getSession()
self.assertIsNotNone(session)
self.addCleanup(session.expire)
self.assertTrue(request.cookies[0].startswith(b'TWISTED_SESSION='))
# It should be a new session ID.
self.assertNotIn(b"does-not-exist", request.cookies[0])
示例7: test_interfaces
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_interfaces(self):
"""
L{server.GzipEncoderFactory} implements the
L{iweb._IRequestEncoderFactory} and its C{encoderForRequest} returns an
instance of L{server._GzipEncoder} which implements
L{iweb._IRequestEncoder}.
"""
request = server.Request(self.channel, False)
request.gotLength(0)
request.requestHeaders.setRawHeaders(b"Accept-Encoding",
[b"gzip,deflate"])
factory = server.GzipEncoderFactory()
self.assertTrue(verifyObject(iweb._IRequestEncoderFactory, factory))
encoder = factory.encoderForRequest(request)
self.assertTrue(verifyObject(iweb._IRequestEncoder, encoder))
示例8: test_multipleAccept
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_multipleAccept(self):
"""
If there are multiple I{Accept-Encoding} header,
L{server.GzipEncoderFactory} reads them properly to detect if gzip is
supported.
"""
request = server.Request(self.channel, False)
request.gotLength(0)
request.requestHeaders.setRawHeaders(b"Accept-Encoding",
[b"deflate", b"gzip"])
request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
data = self.channel.transport.written.getvalue()
self.assertNotIn(b"Content-Length", data)
self.assertIn(b"Content-Encoding: gzip\r\n", data)
body = data[data.find(b"\r\n\r\n") + 4:]
self.assertEqual(b"Some data",
zlib.decompress(body, 16 + zlib.MAX_WBITS))
示例9: test_alreadyEncoded
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_alreadyEncoded(self):
"""
If the content is already encoded and the I{Content-Encoding} header is
set, L{server.GzipEncoderFactory} properly appends gzip to it.
"""
request = server.Request(self.channel, False)
request.gotLength(0)
request.requestHeaders.setRawHeaders(b"Accept-Encoding",
[b"deflate", b"gzip"])
request.responseHeaders.setRawHeaders(b"Content-Encoding",
[b"deflate"])
request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
data = self.channel.transport.written.getvalue()
self.assertNotIn(b"Content-Length", data)
self.assertIn(b"Content-Encoding: deflate,gzip\r\n", data)
body = data[data.find(b"\r\n\r\n") + 4:]
self.assertEqual(b"Some data",
zlib.decompress(body, 16 + zlib.MAX_WBITS))
示例10: test_multipleEncodingLines
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_multipleEncodingLines(self):
"""
If there are several I{Content-Encoding} headers,
L{server.GzipEncoderFactory} normalizes it and appends gzip to the
field value.
"""
request = server.Request(self.channel, False)
request.gotLength(0)
request.requestHeaders.setRawHeaders(b"Accept-Encoding",
[b"deflate", b"gzip"])
request.responseHeaders.setRawHeaders(b"Content-Encoding",
[b"foo", b"bar"])
request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
data = self.channel.transport.written.getvalue()
self.assertNotIn(b"Content-Length", data)
self.assertIn(b"Content-Encoding: foo,bar,gzip\r\n", data)
body = data[data.find(b"\r\n\r\n") + 4:]
self.assertEqual(b"Some data",
zlib.decompress(body, 16 + zlib.MAX_WBITS))
示例11: test_sessionDifferentFromSecureSession
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_sessionDifferentFromSecureSession(self):
"""
L{Request.session} and L{Request.secure_session} should be two separate
sessions with unique ids and different cookies.
"""
d = DummyChannel()
d.transport = DummyChannel.SSL()
request = server.Request(d, 1)
request.site = server.Site(resource.Resource())
request.sitepath = []
secureSession = request.getSession()
self.assertIsNotNone(secureSession)
self.addCleanup(secureSession.expire)
self.assertEqual(request.cookies[0].split(b"=")[0],
b"TWISTED_SECURE_SESSION")
session = request.getSession(forceNotSecure=True)
self.assertIsNotNone(session)
self.assertEqual(request.cookies[1].split(b"=")[0], b"TWISTED_SESSION")
self.addCleanup(session.expire)
self.assertNotEqual(session.uid, secureSession.uid)
示例12: test_rejectNonOPTIONSStar
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_rejectNonOPTIONSStar(self):
"""
L{Request} handles any non-OPTIONS verb requesting the * path by doing
a fast-return 405 Method Not Allowed, indicating only the support for
OPTIONS.
"""
d = DummyChannel()
request = server.Request(d, 1)
request.setHost(b'example.com', 80)
request.gotLength(0)
request.requestReceived(b'GET', b'*', b'HTTP/1.1')
response = d.transport.written.getvalue()
self.assertTrue(
response.startswith(b'HTTP/1.1 405 Method Not Allowed')
)
self.assertIn(b'Content-Length: 0\r\n', response)
self.assertIn(b'Allow: OPTIONS\r\n', response)
示例13: test_noDefaultContentTypeOnZeroLengthResponse
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_noDefaultContentTypeOnZeroLengthResponse(self):
"""
Responses with no length do not have a default content-type applied.
"""
resrc = ZeroLengthResource()
resrc.putChild(b'', resrc)
site = server.Site(resrc)
d = DummyChannel()
d.site = site
request = server.Request(d, 1)
request.site = site
request.setHost(b'example.com', 80)
request.gotLength(0)
request.requestReceived(b'GET', b'/', b'HTTP/1.1')
self.assertNotIn(
b'content-type', request.transport.written.getvalue().lower()
)
示例14: test_noDefaultContentTypeOn204Response
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_noDefaultContentTypeOn204Response(self):
"""
Responses with a 204 status code have no default content-type applied.
"""
resrc = NoContentResource()
resrc.putChild(b'', resrc)
site = server.Site(resrc)
d = DummyChannel()
d.site = site
request = server.Request(d, 1)
request.site = site
request.setHost(b'example.com', 80)
request.gotLength(0)
request.requestReceived(b'GET', b'/', b'HTTP/1.1')
response = request.transport.written.getvalue()
self.assertTrue(response.startswith(b'HTTP/1.1 204 No Content\r\n'))
self.assertNotIn(b'content-type', response.lower())
示例15: test_encoding
# 需要导入模块: from twisted.web import server [as 别名]
# 或者: from twisted.web.server import Request [as 别名]
def test_encoding(self):
"""
If the client request passes a I{Accept-Encoding} header which mentions
gzip, L{server._GzipEncoder} automatically compresses the data.
"""
request = server.Request(self.channel, False)
request.gotLength(0)
request.requestHeaders.setRawHeaders(b"Accept-Encoding",
[b"gzip,deflate"])
request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
data = self.channel.transport.written.getvalue()
self.assertNotIn(b"Content-Length", data)
self.assertIn(b"Content-Encoding: gzip\r\n", data)
body = data[data.find(b"\r\n\r\n") + 4:]
self.assertEqual(b"Some data",
zlib.decompress(body, 16 + zlib.MAX_WBITS))