예제 #1
0
    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)
예제 #2
0
파일: test_xml.py 프로젝트: zackw/mini-amf
    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))
예제 #3
0
    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')
예제 #4
0
    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')
예제 #5
0
    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)
예제 #6
0
    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')
예제 #7
0
파일: test_amf0.py 프로젝트: zackw/mini-amf
    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'}])
예제 #8
0
    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)
예제 #9
0
    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)
예제 #10
0
 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()