summaryrefslogtreecommitdiff
path: root/cherrypy/test/test_misc_tools.py
diff options
context:
space:
mode:
Diffstat (limited to 'cherrypy/test/test_misc_tools.py')
-rw-r--r--cherrypy/test/test_misc_tools.py91
1 files changed, 43 insertions, 48 deletions
diff --git a/cherrypy/test/test_misc_tools.py b/cherrypy/test/test_misc_tools.py
index df3e2e65..fb85b8f8 100644
--- a/cherrypy/test/test_misc_tools.py
+++ b/cherrypy/test/test_misc_tools.py
@@ -1,83 +1,81 @@
import os
-localDir = os.path.dirname(__file__)
-logfile = os.path.join(localDir, "test_misc_tools.log")
import cherrypy
from cherrypy import tools
+from cherrypy.test import helper
+
+
+localDir = os.path.dirname(__file__)
+logfile = os.path.join(localDir, 'test_misc_tools.log')
def setup_server():
class Root:
+ @cherrypy.expose
def index(self):
- yield "Hello, world"
- index.exposed = True
- h = [("Content-Language", "en-GB"), ('Content-Type', 'text/plain')]
+ yield 'Hello, world'
+ h = [('Content-Language', 'en-GB'), ('Content-Type', 'text/plain')]
tools.response_headers(headers=h)(index)
- def other(self):
- return "salut"
- other.exposed = True
- other._cp_config = {
+ @cherrypy.expose
+ @cherrypy.config(**{
'tools.response_headers.on': True,
- 'tools.response_headers.headers': [("Content-Language", "fr"),
- ('Content-Type', 'text/plain')],
+ 'tools.response_headers.headers': [
+ ('Content-Language', 'fr'),
+ ('Content-Type', 'text/plain'),
+ ],
'tools.log_hooks.on': True,
- }
+ })
+ def other(self):
+ return 'salut'
+ @cherrypy.config(**{'tools.accept.on': True})
class Accept:
- _cp_config = {'tools.accept.on': True}
+ @cherrypy.expose
def index(self):
return '<a href="feed">Atom feed</a>'
- index.exposed = True
- # In Python 2.4+, we could use a decorator instead:
- # @tools.accept('application/atom+xml')
+ @cherrypy.expose
+ @tools.accept(media='application/atom+xml')
def feed(self):
return """<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Unknown Blog</title>
</feed>"""
- feed.exposed = True
- feed._cp_config = {'tools.accept.media': 'application/atom+xml'}
+ @cherrypy.expose
def select(self):
# We could also write this: mtype = cherrypy.lib.accept.accept(...)
mtype = tools.accept.callable(['text/html', 'text/plain'])
if mtype == 'text/html':
- return "<h2>Page Title</h2>"
+ return '<h2>Page Title</h2>'
else:
- return "PAGE TITLE"
- select.exposed = True
+ return 'PAGE TITLE'
class Referer:
+ @cherrypy.expose
def accept(self):
- return "Accepted!"
- accept.exposed = True
+ return 'Accepted!'
reject = accept
class AutoVary:
+ @cherrypy.expose
def index(self):
# Read a header directly with 'get'
- ae = cherrypy.request.headers.get('Accept-Encoding')
+ cherrypy.request.headers.get('Accept-Encoding')
# Read a header directly with '__getitem__'
- cl = cherrypy.request.headers['Host']
+ cherrypy.request.headers['Host']
# Read a header directly with '__contains__'
- hasif = 'If-Modified-Since' in cherrypy.request.headers
- # Read a header directly with 'has_key'
- if hasattr(dict, 'has_key'):
- # Python 2
- has = cherrypy.request.headers.has_key('Range')
- else:
- # Python 3
- has = 'Range' in cherrypy.request.headers
+ 'If-Modified-Since' in cherrypy.request.headers
+ # Read a header directly
+ 'Range' in cherrypy.request.headers
# Call a lib function
- mtype = tools.accept.callable(['text/html', 'text/plain'])
- return "Hello, world!"
- index.exposed = True
+ tools.accept.callable(['text/html', 'text/plain'])
+ return 'Hello, world!'
conf = {'/referer': {'tools.referer.on': True,
'tools.referer.pattern': r'http://[^/]*example\.com',
@@ -96,20 +94,17 @@ def setup_server():
cherrypy.config.update({'log.error_file': logfile})
-from cherrypy.test import helper
-
-
class ResponseHeadersTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
def testResponseHeadersDecorator(self):
self.getPage('/')
- self.assertHeader("Content-Language", "en-GB")
+ self.assertHeader('Content-Language', 'en-GB')
self.assertHeader('Content-Type', 'text/plain;charset=utf-8')
def testResponseHeaders(self):
self.getPage('/other')
- self.assertHeader("Content-Language", "fr")
+ self.assertHeader('Content-Language', 'fr')
self.assertHeader('Content-Type', 'text/plain;charset=utf-8')
@@ -163,9 +158,9 @@ class AcceptTest(helper.CPWebCase):
# Specify unacceptable media types
self.getPage('/accept/feed', headers=[('Accept', 'text/html')])
self.assertErrorPage(406,
- "Your client sent this Accept header: text/html. "
- "But this resource only emits these media types: "
- "application/atom+xml.")
+ 'Your client sent this Accept header: text/html. '
+ 'But this resource only emits these media types: '
+ 'application/atom+xml.')
# Test resource where tool is 'on' but media is None (not set).
self.getPage('/accept/')
@@ -198,9 +193,9 @@ class AcceptTest(helper.CPWebCase):
self.getPage('/accept/select', [('Accept', 'application/xml')])
self.assertErrorPage(
406,
- "Your client sent this Accept header: application/xml. "
- "But this resource only emits these media types: "
- "text/html, text/plain.")
+ 'Your client sent this Accept header: application/xml. '
+ 'But this resource only emits these media types: '
+ 'text/html, text/plain.')
class AutoVaryTest(helper.CPWebCase):
@@ -209,7 +204,7 @@ class AutoVaryTest(helper.CPWebCase):
def testAutoVary(self):
self.getPage('/autovary/')
self.assertHeader(
- "Vary",
+ 'Vary',
'Accept, Accept-Charset, Accept-Encoding, '
'Host, If-Modified-Since, Range'
)