def test_read_text_while_closed(self): obj = multipart.BodyPartReader(self.boundary, {CONTENT_TYPE: 'text/plain'}, Stream(b'')) obj._at_eof = True result = obj.text() self.assertEqual('', result)
def test_iterate(self): obj = multipart.BodyPartReader( self.boundary, {}, Stream(b'Hello,\r\n--:\r\n\r\nworld!\r\n--:--')) expected = [b'Hello,', b'world!'] for part in obj: self.assertEqual(part, expected.pop(0)) self.assertTrue(obj.at_eof())
def test_read_does_reads_boundary(self): stream = Stream(b'Hello, world!\r\n--:') obj = multipart.BodyPartReader(self.boundary, {}, stream) result = obj.read() self.assertEqual(b'Hello, world!', result) self.assertEqual(b'', (stream.read())) self.assertEqual([b'--:'], list(obj._unread))
def test_read_form_encoding(self): obj = multipart.BodyPartReader( self.boundary, {CONTENT_TYPE: 'application/x-www-form-urlencoded'}, Stream('foo=bar&foo=baz&boo=zoo\r\n--:--'.encode('cp1251'))) result = obj.form(encoding='cp1251') self.assertEqual([('foo', 'bar'), ('foo', 'baz'), ('boo', 'zoo')], result)
def test_release_respects_content_length(self): obj = multipart.BodyPartReader(self.boundary, {'CONTENT-LENGTH': 100500}, Stream(b'.' * 100500 + b'\r\n--:--')) result = obj.release() self.assertIsNone(result) self.assertTrue(obj.at_eof())
def test_release_release(self): stream = Stream(b'Hello,\r\n--:\r\n\r\nworld!\r\n--:--') obj = multipart.BodyPartReader(self.boundary, {}, stream) obj.release() obj.release() self.assertEqual(b'\r\nworld!\r\n--:--', stream.content.read()) self.assertEqual([b'--:\r\n'], list(obj._unread))
def test_read_text_compressed(self): obj = multipart.BodyPartReader( self.boundary, { CONTENT_ENCODING: 'deflate', CONTENT_TYPE: 'text/plain' }, Stream(b'\x0b\xc9\xccMU(\xc9W\x08J\xcdI\xacP\x04\x00\r\n--:--')) result = obj.text() self.assertEqual('Time to Relax!', result)
def test_read_json_while_closed(self): stream = Stream(b'') obj = multipart.BodyPartReader(self.boundary, {CONTENT_TYPE: 'application/json'}, stream) obj._at_eof = True result = obj.json() self.assertIsNone(result)
def test_read_form_while_closed(self): stream = Stream(b'') obj = multipart.BodyPartReader( self.boundary, {CONTENT_TYPE: 'application/x-www-form-urlencoded'}, stream) obj._at_eof = True result = obj.form() self.assertEqual(None, result)
def test_next_next(self): obj = multipart.BodyPartReader(self.boundary, {}, Stream(b'Hello, world!\r\n--:')) result = obj.next() self.assertEqual(b'Hello, world!', result) self.assertTrue(obj.at_eof()) with self.assertRaises(StopIteration): obj.next()
def test_read_with_content_encoding_gzip(self): obj = multipart.BodyPartReader( self.boundary, {CONTENT_ENCODING: 'gzip'}, Stream(b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x0b\xc9\xccMU' b'(\xc9W\x08J\xcdI\xacP\x04\x00$\xfb\x9eV\x0e\x00\x00\x00' b'\r\n--:--')) result = obj.read(decode=True) self.assertEqual(b'Time to Relax!', result)
def test_read_multiline(self): obj = multipart.BodyPartReader(self.boundary, {}, Stream(b'Hello\n,\r\nworld!\r\n--:--')) result = obj.read() self.assertEqual(b'Hello\n,\r\nworld!', result) result = obj.read() self.assertIsNone(result) self.assertTrue(obj.at_eof())
def test_read_json_compressed(self): obj = multipart.BodyPartReader( self.boundary, { CONTENT_ENCODING: 'deflate', CONTENT_TYPE: 'application/json' }, Stream(b'\xabV*I-.Q\xb2RP*H,.NMQ\xaa\x05\x00\r\n--:--')) result = obj.json() self.assertEqual({'test': 'passed'}, result)
def test_read_with_content_transfer_encoding_quoted_printable(self): obj = multipart.BodyPartReader( self.boundary, {CONTENT_TRANSFER_ENCODING: 'quoted-printable'}, Stream(b'=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D1=82,' b' =D0=BC=D0=B8=D1=80!\r\n--:--')) result = obj.read(decode=True) self.assertEqual( b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82,' b' \xd0\xbc\xd0\xb8\xd1\x80!', result)
def test_read_with_content_encoding_identity(self): thing = (b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x0b\xc9\xccMU' b'(\xc9W\x08J\xcdI\xacP\x04\x00$\xfb\x9eV\x0e\x00\x00\x00' b'\r\n') obj = multipart.BodyPartReader(self.boundary, {CONTENT_ENCODING: 'identity'}, Stream(thing + b'--:--')) result = obj.read(decode=True) self.assertEqual(thing[:-2], result)
def test_read_chunk_properly_counts_read_bytes(self): expected = b'.' * 10 size = len(expected) obj = multipart.BodyPartReader( self.boundary, {'CONTENT-LENGTH': size}, StreamWithShortenRead(expected + b'\r\n--:--')) result = bytearray() while True: chunk = obj.read_chunk() if not chunk: break result.extend(chunk) self.assertEqual(size, len(result)) self.assertEqual(b'.' * size, result) self.assertTrue(obj.at_eof())
def test_read_json(self): obj = multipart.BodyPartReader(self.boundary, {CONTENT_TYPE: 'application/json'}, Stream(b'{"test": "passed"}\r\n--:--')) result = obj.json() self.assertEqual({'test': 'passed'}, result)
def test_read_chunk_at_eof(self): obj = multipart.BodyPartReader(self.boundary, {}, Stream(b'--:')) obj._at_eof = True result = obj.read_chunk() self.assertIsNone(result)
def test_read_chunk_requires_content_length(self): obj = multipart.BodyPartReader(self.boundary, {}, Stream(b'Hello, world!\r\n--:')) with self.assertRaises(AssertionError): obj.read_chunk()
def test_read_with_content_encoding_deflate(self): obj = multipart.BodyPartReader( self.boundary, {CONTENT_ENCODING: 'deflate'}, Stream(b'\x0b\xc9\xccMU(\xc9W\x08J\xcdI\xacP\x04\x00\r\n--:--')) result = obj.read(decode=True) self.assertEqual(b'Time to Relax!', result)
def test_read(self): obj = multipart.BodyPartReader(self.boundary, {}, Stream(b'Hello, world!\r\n--:')) result = obj.read() self.assertEqual(b'Hello, world!', result) self.assertTrue(obj.at_eof())
def test_read_with_content_transfer_encoding_base64(self): obj = multipart.BodyPartReader( self.boundary, {CONTENT_TRANSFER_ENCODING: 'base64'}, Stream(b'VGltZSB0byBSZWxheCE=\r\n--:--')) result = obj.read(decode=True) self.assertEqual(b'Time to Relax!', result)
def test_filename(self): part = multipart.BodyPartReader( self.boundary, {CONTENT_DISPOSITION: 'attachment; filename=foo.html'}, None) self.assertEqual('foo.html', part.filename)
def test_read_with_content_transfer_encoding_unknown(self): obj = multipart.BodyPartReader(self.boundary, {CONTENT_TRANSFER_ENCODING: 'unknown'}, Stream(b'\x0e4Time to Relax!\r\n--:--')) with self.assertRaises(RuntimeError): obj.read(decode=True)
def test_read_text(self): obj = multipart.BodyPartReader(self.boundary, {}, Stream(b'Hello, world!\r\n--:--')) result = obj.text() self.assertEqual('Hello, world!', result)
def test_read_text_encoding(self): obj = multipart.BodyPartReader( self.boundary, {}, Stream(u'Привет, Мир!\r\n--:--'.encode('cp1251'))) result = obj.text(encoding='cp1251') self.assertEqual(u'Привет, Мир!', result)
def test_read_text_guess_encoding(self): obj = multipart.BodyPartReader( self.boundary, {CONTENT_TYPE: 'text/plain;charset=cp1251'}, Stream(u'Привет, Мир!\r\n--:--'.encode('cp1251'))) result = obj.text() self.assertEqual(u'Привет, Мир!', result)
def test_read_json_guess_encoding(self): obj = multipart.BodyPartReader( self.boundary, {CONTENT_TYPE: 'application/json; charset=cp1251'}, Stream(u'{"тест": "пассед"}\r\n--:--'.encode('cp1251'))) result = obj.json() self.assertEqual({u'тест': u'пассед'}, result)