/usr/share/pyshared/tests/test_encode.py is in python-poster 0.8.1-0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | # -*- coding: utf-8 -*-
from unittest import TestCase
import mimetypes
import poster.encode
import StringIO
import sys
def unix2dos(s):
return s.replace("\n", "\r\n")
class TestEncode_String(TestCase):
def test_simple(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
bar
""")
self.assertEqual(expected,
poster.encode.encode_string("XXXXXXXXX", "foo", "bar"))
def test_quote_name_space(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo baz"
Content-Type: text/plain; charset=utf-8
bar
""")
self.assertEqual(expected,
poster.encode.encode_string("XXXXXXXXX", "foo baz", "bar"))
def test_quote_name_phparray(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="files[]"
Content-Type: text/plain; charset=utf-8
bar
""")
self.assertEqual(expected,
poster.encode.encode_string("XXXXXXXXX", "files[]", "bar"))
def test_quote_unicode_name(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="=?utf-8?b?4piD?="
Content-Type: text/plain; charset=utf-8
bar
""")
self.assertEqual(expected,
poster.encode.encode_string("XXXXXXXXX", u"\N{SNOWMAN}", "bar"))
def test_quote_value(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
bar baz@bat
""")
self.assertEqual(expected,
poster.encode.encode_string("XXXXXXXXX", "foo", "bar baz@bat"))
def test_boundary(self):
expected = unix2dos("""--ABC+DEF
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
bar
""")
self.assertEqual(expected,
poster.encode.encode_string("ABC DEF", "foo", "bar"))
def test_unicode(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
b\xc3\xa1r
""")
self.assertEqual(expected,
poster.encode.encode_string("XXXXXXXXX", "foo", u"bár"))
class TestEncode_File(TestCase):
def test_simple(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
""")
self.assertEqual(expected,
poster.encode.encode_file_header("XXXXXXXXX", "foo", 42))
def test_content_type(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"
Content-Type: text/html
""")
self.assertEqual(expected,
poster.encode.encode_file_header("XXXXXXXXX", "foo", 42, filetype="text/html"))
def test_filename_simple(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"; filename="test.txt"
Content-Type: text/plain; charset=utf-8
""")
self.assertEqual(expected,
poster.encode.encode_file_header("XXXXXXXXX", "foo", 42,
"test.txt"))
def test_quote_filename(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"; filename="test file.txt"
Content-Type: text/plain; charset=utf-8
""")
self.assertEqual(expected,
poster.encode.encode_file_header("XXXXXXXXX", "foo", 42,
"test file.txt"))
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"; filename="test\\"file.txt"
Content-Type: text/plain; charset=utf-8
""")
self.assertEqual(expected,
poster.encode.encode_file_header("XXXXXXXXX", "foo", 42,
"test\"file.txt"))
def test_unicode_filename(self):
expected = unix2dos("""--XXXXXXXXX
Content-Disposition: form-data; name="foo"; filename="☃.txt"
Content-Type: text/plain; charset=utf-8
""")
self.assertEqual(expected,
poster.encode.encode_file_header("XXXXXXXXX", "foo", 42,
u"\N{SNOWMAN}.txt"))
class TestEncodeAndQuote(TestCase):
def test(self):
self.assertEqual("foo+bar", poster.encode.encode_and_quote("foo bar"))
self.assertEqual("foo%40bar", poster.encode.encode_and_quote("foo@bar"))
self.assertEqual("%28%C2%A9%29+2008",
poster.encode.encode_and_quote(u"(©) 2008"))
class TestMultiparam(TestCase):
def test_from_params(self):
fp = open("tests/test_encode.py")
expected = [poster.encode.MultipartParam("foo", "bar"),
poster.encode.MultipartParam("baz", fileobj=fp,
filename=fp.name,
filetype=mimetypes.guess_type(fp.name)[0])]
self.assertEqual(poster.encode.MultipartParam.from_params(
[("foo", "bar"), ("baz", fp)]), expected)
self.assertEqual(poster.encode.MultipartParam.from_params(
(("foo", "bar"), ("baz", fp))), expected)
self.assertEqual(poster.encode.MultipartParam.from_params(
{"foo": "bar", "baz": fp}), expected)
self.assertEqual(poster.encode.MultipartParam.from_params(
[expected[0], expected[1]]), expected)
def test_from_params_dict(self):
p = poster.encode.MultipartParam('file', fileobj=open("tests/test_encode.py"))
params = {"foo": "bar", "file": p}
expected = [poster.encode.MultipartParam("foo", "bar"), p]
retval = poster.encode.MultipartParam.from_params(params)
expected.sort()
retval.sort()
self.assertEqual(retval, expected)
def test_from_params_assertion(self):
p = poster.encode.MultipartParam('file', fileobj=open("tests/test_encode.py"))
params = {"foo": "bar", "baz": p}
self.assertRaises(AssertionError, poster.encode.MultipartParam.from_params,
params)
def test_simple(self):
p = poster.encode.MultipartParam("foo", "bar")
boundary = "XYZXYZXYZ"
expected = unix2dos("""--XYZXYZXYZ
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
bar
--XYZXYZXYZ--
""")
self.assertEqual(p.encode(boundary), expected[:-len(boundary)-6])
self.assertEqual(p.get_size(boundary), len(expected)-len(boundary)-6)
self.assertEqual(poster.encode.get_body_size([p], boundary),
len(expected))
self.assertEqual(poster.encode.get_headers([p], boundary),
{'Content-Length': str(len(expected)),
'Content-Type': 'multipart/form-data; boundary=%s' % boundary})
datagen, headers = poster.encode.multipart_encode([p], boundary)
self.assertEqual(headers, {'Content-Length': str(len(expected)),
'Content-Type': 'multipart/form-data; boundary=%s' % boundary})
self.assertEqual("".join(datagen), expected)
def test_multiple_keys(self):
params = poster.encode.MultipartParam.from_params(
[("key", "value1"), ("key", "value2")])
boundary = "XYZXYZXYZ"
datagen, headers = poster.encode.multipart_encode(params, boundary)
encoded = "".join(datagen)
expected = unix2dos("""--XYZXYZXYZ
Content-Disposition: form-data; name="key"
Content-Type: text/plain; charset=utf-8
value1
--XYZXYZXYZ
Content-Disposition: form-data; name="key"
Content-Type: text/plain; charset=utf-8
value2
--XYZXYZXYZ--
""")
self.assertEqual(encoded, expected)
def test_stringio(self):
fp = StringIO.StringIO("file data")
params = poster.encode.MultipartParam.from_params( [("foo", fp)] )
boundary = "XYZXYZXYZ"
datagen, headers = poster.encode.multipart_encode(params, boundary)
encoded = "".join(datagen)
expected = unix2dos("""--XYZXYZXYZ
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
file data
--XYZXYZXYZ--
""")
self.assertEqual(encoded, expected)
def test_reset_string(self):
p = poster.encode.MultipartParam("foo", "bar")
boundary = "XYZXYZXYZ"
datagen, headers = poster.encode.multipart_encode([p], boundary)
expected = unix2dos("""--XYZXYZXYZ
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
bar
--XYZXYZXYZ--
""")
self.assertEquals("".join(datagen), expected)
datagen.reset()
self.assertEquals("".join(datagen), expected)
def test_reset_multiple_keys(self):
params = poster.encode.MultipartParam.from_params(
[("key", "value1"), ("key", "value2")])
boundary = "XYZXYZXYZ"
datagen, headers = poster.encode.multipart_encode(params, boundary)
expected = unix2dos("""--XYZXYZXYZ
Content-Disposition: form-data; name="key"
Content-Type: text/plain; charset=utf-8
value1
--XYZXYZXYZ
Content-Disposition: form-data; name="key"
Content-Type: text/plain; charset=utf-8
value2
--XYZXYZXYZ--
""")
encoded = "".join(datagen)
self.assertEqual(encoded, expected)
datagen.reset()
encoded = "".join(datagen)
self.assertEqual(encoded, expected)
def test_reset_file(self):
fp = StringIO.StringIO("file data")
params = poster.encode.MultipartParam.from_params( [("foo", fp)] )
boundary = "XYZXYZXYZ"
datagen, headers = poster.encode.multipart_encode(params, boundary)
expected = unix2dos("""--XYZXYZXYZ
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8
file data
--XYZXYZXYZ--
""")
encoded = "".join(datagen)
self.assertEqual(encoded, expected)
datagen.reset()
encoded = "".join(datagen)
self.assertEqual(encoded, expected)
def test_MultipartParam_cb(self):
log = []
def cb(p, current, total):
log.append( (p, current, total) )
p = poster.encode.MultipartParam("foo", "bar", cb=cb)
boundary = "XYZXYZXYZ"
datagen, headers = poster.encode.multipart_encode([p], boundary)
"".join(datagen)
l = p.get_size(boundary)
self.assertEquals(log[-1], (p, l, l))
def test_MultipartParam_file_cb(self):
log = []
def cb(p, current, total):
log.append( (p, current, total) )
p = poster.encode.MultipartParam("foo", fileobj=open("tests/test_encode.py"),
cb=cb)
boundary = poster.encode.gen_boundary()
list(p.iter_encode(boundary))
l = p.get_size(boundary)
self.assertEquals(log[-1], (p, l, l))
def test_multipart_encode_cb(self):
log = []
def cb(p, current, total):
log.append( (p, current, total) )
p = poster.encode.MultipartParam("foo", "bar")
boundary = "XYZXYZXYZ"
datagen, headers = poster.encode.multipart_encode([p], boundary, cb=cb)
"".join(datagen)
l = int(headers['Content-Length'])
self.assertEquals(log[-1], (None, l, l))
class TestGenBoundary(TestCase):
def testGenBoundary(self):
boundary1 = poster.encode.gen_boundary()
boundary2 = poster.encode.gen_boundary()
self.assertNotEqual(boundary1, boundary2)
self.assert_(len(boundary1) > 0)
class TestBackupGenBoundary(TestGenBoundary):
_orig_import = __import__
def setUp(self):
# Make import uuid fail
def my_import(name, *args, **kwargs):
if name == 'uuid':
raise ImportError("Disabled for testing")
return self._orig_import(name, *args, **kwargs)
__builtins__['__import__'] = my_import
reload(poster.encode)
def tearDown(self):
__builtins__['__import__'] = self._orig_import
reload(poster.encode)
|