MCPcopy
hub / github.com/cherrypy/cherrypy / test_urlencoded_decoding

Method test_urlencoded_decoding

cherrypy/test/test_encoding.py:146–198  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

144 "strings for this resource must be encoded with 'utf8'.")
145
146 def test_urlencoded_decoding(self):
147 # Test the decoding of an application/x-www-form-urlencoded entity.
148 europoundUtf8 = europoundUnicode.encode('utf-8')
149 body = b'param=' + europoundUtf8
150 self.getPage('/',
151 method='POST',
152 headers=[
153 ('Content-Type', 'application/x-www-form-urlencoded'),
154 ('Content-Length', str(len(body))),
155 ],
156 body=body),
157 self.assertBody(europoundUtf8)
158
159 # Encoded utf8 entities MUST be parsed and decoded correctly.
160 # Here, q is the POUND SIGN U+00A3 encoded in utf8
161 body = b'q=\xc2\xa3'
162 self.getPage('/reqparams', method='POST',
163 headers=[(
164 'Content-Type', 'application/x-www-form-urlencoded'),
165 ('Content-Length', str(len(body))),
166 ],
167 body=body),
168 self.assertBody(b'q: \xc2\xa3')
169
170 # ...and in utf16, which is not in the default attempt_charsets list:
171 body = b'\xff\xfeq\x00=\xff\xfe\xa3\x00'
172 self.getPage('/reqparams',
173 method='POST',
174 headers=[
175 ('Content-Type',
176 'application/x-www-form-urlencoded;charset=utf-16'),
177 ('Content-Length', str(len(body))),
178 ],
179 body=body),
180 self.assertBody(b'q: \xc2\xa3')
181
182 # Entities that are incorrectly encoded MUST raise 400.
183 # Here, q is the POUND SIGN U+00A3 encoded in utf16, but
184 # the Content-Type incorrectly labels it utf-8.
185 body = b'\xff\xfeq\x00=\xff\xfe\xa3\x00'
186 self.getPage('/reqparams',
187 method='POST',
188 headers=[
189 ('Content-Type',
190 'application/x-www-form-urlencoded;charset=utf-8'),
191 ('Content-Length', str(len(body))),
192 ],
193 body=body),
194 self.assertStatus(400)
195 self.assertErrorPage(
196 400,
197 'The request entity could not be decoded. The following charsets '
198 "were attempted: ['utf-8']")
199
200 def test_decode_tool(self):
201 # An extra charset should be tried first, and succeed if it matches.

Callers

nothing calls this directly

Calls 3

encodeMethod · 0.80
getPageMethod · 0.80
assertErrorPageMethod · 0.80

Tested by

no test coverage detected