2 localDir
= os
.path
.dirname(__file__
)
3 logfile
= os
.path
.join(localDir
, "test_misc_tools.log")
6 from cherrypy
import tools
14 h
= [("Content-Language", "en-GB"), ('Content-Type', 'text/plain')]
15 tools
.response_headers(headers
=h
)(index
)
21 'tools.response_headers.on': True,
22 'tools.response_headers.headers': [("Content-Language", "fr"),
23 ('Content-Type', 'text/plain')],
24 'tools.log_hooks.on': True,
29 _cp_config
= {'tools.accept.on': True}
32 return '<a href="feed">Atom feed</a>'
35 # In Python 2.4+, we could use a decorator instead:
36 # @tools.accept('application/atom+xml')
38 return """<?xml version="1.0" encoding="utf-8"?>
39 <feed xmlns="http://www.w3.org/2005/Atom">
40 <title>Unknown Blog</title>
43 feed
._cp
_config
= {'tools.accept.media': 'application/atom+xml'}
46 # We could also write this: mtype = cherrypy.lib.accept.accept(...)
47 mtype
= tools
.accept
.callable(['text/html', 'text/plain'])
48 if mtype
== 'text/html':
49 return "<h2>Page Title</h2>"
62 # Read a header directly with 'get'
63 ae
= cherrypy
.request
.headers
.get('Accept-Encoding')
64 # Read a header directly with '__getitem__'
65 cl
= cherrypy
.request
.headers
['Host']
66 # Read a header directly with '__contains__'
67 hasif
= 'If-Modified-Since' in cherrypy
.request
.headers
68 # Read a header directly with 'has_key'
69 has
= cherrypy
.request
.headers
.has_key('Range')
71 mtype
= tools
.accept
.callable(['text/html', 'text/plain'])
72 return "Hello, world!"
75 conf
= {'/referer': {'tools.referer.on': True,
76 'tools.referer.pattern': r
'http://[^/]*example\.com',
78 '/referer/reject': {'tools.referer.accept': False,
79 'tools.referer.accept_missing': True,
81 '/autovary': {'tools.autovary.on': True},
85 root
.referer
= Referer()
86 root
.accept
= Accept()
87 root
.autovary
= AutoVary()
88 cherrypy
.tree
.mount(root
, config
=conf
)
89 cherrypy
.config
.update({'log.error_file': logfile
})
92 from cherrypy
.test
import helper
94 class ResponseHeadersTest(helper
.CPWebCase
):
95 setup_server
= staticmethod(setup_server
)
97 def testResponseHeadersDecorator(self
):
99 self
.assertHeader("Content-Language", "en-GB")
100 self
.assertHeader('Content-Type', 'text/plain;charset=utf-8')
102 def testResponseHeaders(self
):
103 self
.getPage('/other')
104 self
.assertHeader("Content-Language", "fr")
105 self
.assertHeader('Content-Type', 'text/plain;charset=utf-8')
108 class RefererTest(helper
.CPWebCase
):
109 setup_server
= staticmethod(setup_server
)
111 def testReferer(self
):
112 self
.getPage('/referer/accept')
113 self
.assertErrorPage(403, 'Forbidden Referer header.')
115 self
.getPage('/referer/accept',
116 headers
=[('Referer', 'http://www.example.com/')])
117 self
.assertStatus(200)
118 self
.assertBody('Accepted!')
121 self
.getPage('/referer/reject')
122 self
.assertStatus(200)
123 self
.assertBody('Accepted!')
125 self
.getPage('/referer/reject',
126 headers
=[('Referer', 'http://www.example.com/')])
127 self
.assertErrorPage(403, 'Forbidden Referer header.')
130 class AcceptTest(helper
.CPWebCase
):
131 setup_server
= staticmethod(setup_server
)
133 def test_Accept_Tool(self
):
134 # Test with no header provided
135 self
.getPage('/accept/feed')
136 self
.assertStatus(200)
137 self
.assertInBody('<title>Unknown Blog</title>')
139 # Specify exact media type
140 self
.getPage('/accept/feed', headers
=[('Accept', 'application/atom+xml')])
141 self
.assertStatus(200)
142 self
.assertInBody('<title>Unknown Blog</title>')
144 # Specify matching media range
145 self
.getPage('/accept/feed', headers
=[('Accept', 'application/*')])
146 self
.assertStatus(200)
147 self
.assertInBody('<title>Unknown Blog</title>')
149 # Specify all media ranges
150 self
.getPage('/accept/feed', headers
=[('Accept', '*/*')])
151 self
.assertStatus(200)
152 self
.assertInBody('<title>Unknown Blog</title>')
154 # Specify unacceptable media types
155 self
.getPage('/accept/feed', headers
=[('Accept', 'text/html')])
156 self
.assertErrorPage(406,
157 "Your client sent this Accept header: text/html. "
158 "But this resource only emits these media types: "
159 "application/atom+xml.")
161 # Test resource where tool is 'on' but media is None (not set).
162 self
.getPage('/accept/')
163 self
.assertStatus(200)
164 self
.assertBody('<a href="feed">Atom feed</a>')
166 def test_accept_selection(self
):
167 # Try both our expected media types
168 self
.getPage('/accept/select', [('Accept', 'text/html')])
169 self
.assertStatus(200)
170 self
.assertBody('<h2>Page Title</h2>')
171 self
.getPage('/accept/select', [('Accept', 'text/plain')])
172 self
.assertStatus(200)
173 self
.assertBody('PAGE TITLE')
174 self
.getPage('/accept/select', [('Accept', 'text/plain, text/*;q=0.5')])
175 self
.assertStatus(200)
176 self
.assertBody('PAGE TITLE')
178 # text/* and */* should prefer text/html since it comes first
179 # in our 'media' argument to tools.accept
180 self
.getPage('/accept/select', [('Accept', 'text/*')])
181 self
.assertStatus(200)
182 self
.assertBody('<h2>Page Title</h2>')
183 self
.getPage('/accept/select', [('Accept', '*/*')])
184 self
.assertStatus(200)
185 self
.assertBody('<h2>Page Title</h2>')
187 # Try unacceptable media types
188 self
.getPage('/accept/select', [('Accept', 'application/xml')])
189 self
.assertErrorPage(406,
190 "Your client sent this Accept header: application/xml. "
191 "But this resource only emits these media types: "
192 "text/html, text/plain.")
195 class AutoVaryTest(helper
.CPWebCase
):
196 setup_server
= staticmethod(setup_server
)
198 def testAutoVary(self
):
199 self
.getPage('/autovary/')
201 "Vary", 'Accept, Accept-Charset, Accept-Encoding, Host, If-Modified-Since, Range')