def test_amf3_encode(self): x = decimal.Decimal("1.23456463452345") self.assertEqual( miniamf.encode(x, encoding=miniamf.AMF3, strict=False).getvalue(), b'\x05?\xf3\xc0\xc6\xd8\xa18\xfa') with self.assertRaises(miniamf.EncodeError): miniamf.encode(x, encoding=miniamf.AMF3, strict=True)
def test_elementtree(self): element = self.fromstring(self.xml) xml = self.tostring(element) bytes = miniamf.encode(element, encoding=miniamf.AMF0).getvalue() self.check_amf0(bytes, xml) new_element = next(miniamf.decode(bytes, encoding=miniamf.AMF0)) self.assertIdentical(type(element), type(new_element)) bytes = miniamf.encode(element, encoding=miniamf.AMF3).getvalue() self.check_amf3(bytes, xml) new_element = next(miniamf.decode(bytes, encoding=miniamf.AMF3)) self.assertIdentical(type(element), type(new_element))
def test_post_process(self): """ Ensure that postprocessing happens when data has been decoded. """ self.executed = False post_procs = miniamf.POST_DECODE_PROCESSORS[:] def restore_post_procs(): miniamf.POST_DECODE_PROCESSORS = post_procs self.addCleanup(restore_post_procs) miniamf.POST_DECODE_PROCESSORS = [] def postprocess(payload, context): self.assertEqual(payload, u'foo') self.assertEqual(context, {}) self.executed = True return payload miniamf.add_post_decode_processor(postprocess) # setup complete bytes = miniamf.encode(u'foo', encoding=miniamf.AMF3).getvalue() self.decoder.send(bytes) ret = next(self.decoder) self.assertTrue(self.executed) self.assertEqual(ret, u'foo')
def amf3_encode_test(self, ty): x = ty(['1', '2', '3']) self.assertEqual( miniamf.encode(x, encoding=miniamf.AMF3).getvalue(), b'\t\x07\x01' b'\x06\x031' b'\x06\x032' b'\x06\x033') y = ty(['z', 'x', 'c', 'v']) self.assertEqual( miniamf.encode(y, encoding=miniamf.AMF3).getvalue(), b'\t\t\x01' b'\x06\x03c' b'\x06\x03v' b'\x06\x03x' b'\x06\x03z')
def assertEncodes(self, obj, buffer, encoding=miniamf.AMF3): bytes = miniamf.encode(obj, encoding=encoding).getvalue() if isinstance(buffer, six.string_types): self.assertEqual(bytes, buffer) return self.assertBuffer(bytes, buffer)
def amf0_encode_test(self, ty): x = ty(['1', '2', '3']) self.assertEqual( miniamf.encode(x, encoding=miniamf.AMF0).getvalue(), b'\n\x00\x00\x00\x03' b'\x02\x00\x011' b'\x02\x00\x012' b'\x02\x00\x013') y = ty(['z', 'x', 'c', 'v']) self.assertEqual( miniamf.encode(y, encoding=miniamf.AMF0).getvalue(), b'\n\x00\x00\x00\x04' b'\x02\x00\x01c' b'\x02\x00\x01v' b'\x02\x00\x01x' b'\x02\x00\x01z')
def test_numerical_keys_mixed_array(self): """ Numerical keys in L{miniamf.MixedArray} must not cause a KeyError on decode. @see: #843 """ x = miniamf.MixedArray({'10': u'foobar'}) bytes = miniamf.encode(x, encoding=miniamf.AMF0) d = list(miniamf.decode(bytes, encoding=miniamf.AMF0)) self.assertEqual(d, [{10: u'foobar'}])
def test_ioerror_buffer_position(self): """ Test to ensure that if an IOError is raised by `readElement` that the original position of the stream is restored. """ bytes = miniamf.encode(u'foo', [1, 2, 3], encoding=miniamf.AMF3).getvalue() self.buf.append(bytes[:-1]) self.decoder.readElement() self.assertEqual(self.buf.tell(), 5) self.assertRaises(IOError, self.decoder.readElement) self.assertEqual(self.buf.tell(), 5)
def _assertEncoding(self, encoding, obj, ref): obj_bytes = miniamf.encode(obj, encoding=encoding).getvalue() ref_bytes = miniamf.encode(ref, encoding=encoding).getvalue() self.assertEqual(obj_bytes, ref_bytes)
def encdec(self, encoding): return next( miniamf.decode(miniamf.encode(self.obj, encoding=encoding), encoding=encoding))
def encode(self,): self.buffer += encode(self.data)._buf.getbuffer().tobytes() self._write_length()