def test_get_encoder(self): from pyamf import amf0, amf3 self.assertEquals(pyamf._get_encoder_class(pyamf.AMF0), amf0.Encoder) self.assertEquals(pyamf._get_encoder_class(pyamf.AMF3), amf3.Encoder) self.assertRaises(ValueError, pyamf._get_encoder_class, 'spam') self.assertTrue(isinstance(pyamf.get_encoder(pyamf.AMF0), amf0.Encoder)) self.assertTrue(isinstance(pyamf.get_encoder(pyamf.AMF3), amf3.Encoder)) self.assertRaises(ValueError, pyamf.get_encoder, 'spam') context = amf0.Context() encoder = pyamf.get_encoder(pyamf.AMF0, data='spam', context=context) self.assertEquals(encoder.stream.getvalue(), 'spam') self.assertEquals(encoder.context, context) self.assertFalse(encoder.strict) context = amf3.Context() encoder = pyamf.get_encoder(pyamf.AMF3, data='eggs', context=context) self.assertFalse(encoder.strict) encoder = pyamf.get_encoder(pyamf.AMF0, strict=True) self.assertTrue(encoder.strict) encoder = pyamf.get_encoder(pyamf.AMF3, strict=True) self.assertTrue(encoder.strict)
def test_get_encoder(self): from pyamf import amf0, amf3 self.assertEquals(pyamf._get_encoder_class(pyamf.AMF0), amf0.Encoder) self.assertEquals(pyamf._get_encoder_class(pyamf.AMF3), amf3.Encoder) self.assertRaises(ValueError, pyamf._get_encoder_class, "spam") self.assertTrue(isinstance(pyamf.get_encoder(pyamf.AMF0), amf0.Encoder)) self.assertTrue(isinstance(pyamf.get_encoder(pyamf.AMF3), amf3.Encoder)) self.assertRaises(ValueError, pyamf.get_encoder, "spam") context = amf0.Context() encoder = pyamf.get_encoder(pyamf.AMF0, stream="spam", context=context) self.assertEquals(encoder.stream.getvalue(), "spam") self.assertEquals(encoder.context, context) self.assertFalse(encoder.strict) context = amf3.Context() encoder = pyamf.get_encoder(pyamf.AMF3, stream="eggs", context=context) self.assertFalse(encoder.strict) encoder = pyamf.get_encoder(pyamf.AMF0, strict=True) self.assertTrue(encoder.strict) encoder = pyamf.get_encoder(pyamf.AMF3, strict=True) self.assertTrue(encoder.strict)
def test_deep(self): class A(object): class __amf__: static = ('a') class B(A): class __amf__: static = ('b') class C(B): class __amf__: static = ('c') pyamf.register_class(A, 'A') pyamf.register_class(B, 'B') pyamf.register_class(C, 'C') x = C() x.a = 'spam' x.b = 'eggs' x.c = 'foo' stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF0)(stream) encoder.writeElement(x) self.assertTrue( check_buffer( stream.getvalue(), ('\x10\x00\x01C', ('\x00\x01a\x02\x00\x04spam', '\x00\x01c\x02\x00\x03foo', '\x00\x01b\x02\x00\x04eggs'), '\x00\x00\t')))
def test_deep(self): class A(object): pass pyamf.register_class(A, 'A', attrs=['a']) class B(A): pass pyamf.register_class(B, 'B', attrs=['b']) class C(B): pass pyamf.register_class(C, 'C', attrs=['c']) x = C() x.a = 'spam' x.b = 'eggs' x.c = 'foo' stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF3)(stream) encoder.writeElement(x) self.assertEquals(stream.getvalue(), '\n\x1b\x03C\x03c\x06\x07foo\x03a' '\x06\tspam\x03b\x06\teggs\x01')
def test_deep(self): class A(object): class __amf__: static = ('a') class B(A): class __amf__: static = ('b') class C(B): class __amf__: static = ('c') pyamf.register_class(A, 'A') pyamf.register_class(B, 'B') pyamf.register_class(C, 'C') x = C() x.a = 'spam' x.b = 'eggs' x.c = 'foo' stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF0)(stream) encoder.writeElement(x) self.assertTrue(check_buffer(stream.getvalue(), ('\x10\x00\x01C', ( '\x00\x01a\x02\x00\x04spam', '\x00\x01c\x02\x00\x03foo', '\x00\x01b\x02\x00\x04eggs' ), '\x00\x00\t')))
def test_deep(self): class A(object): pass pyamf.register_class(A, 'A', attrs=['a']) class B(A): pass pyamf.register_class(B, 'B', attrs=['b']) class C(B): pass pyamf.register_class(C, 'C', attrs=['c']) x = C() x.a = 'spam' x.b = 'eggs' x.c = 'foo' stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF0)(stream) encoder.writeElement(x) self.assertEquals( stream.getvalue(), '\x10\x00\x01C\x00\x01a\x02\x00' '\x04spam\x00\x01c\x02\x00\x03foo\x00\x01b\x02\x00\x04eggs\x00' '\x00\t') pyamf.unregister_class(A) pyamf.unregister_class(B) pyamf.unregister_class(C)
def test_deep(self): class A(object): pass pyamf.register_class(A, 'A', attrs=['a']) class B(A): pass pyamf.register_class(B, 'B', attrs=['b']) class C(B): pass pyamf.register_class(C, 'C', attrs=['c']) x = C() x.a = 'spam' x.b = 'eggs' x.c = 'foo' stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF0)(stream) encoder.writeElement(x) self.assertEquals(stream.getvalue(), '\x10\x00\x01C\x00\x01c\x02\x00' '\x03foo\x00\x01b\x02\x00\x04eggs\x00\x01a\x02\x00\x04spam\x00\x00\t') pyamf.unregister_class(A) pyamf.unregister_class(B) pyamf.unregister_class(C)
def test_get_encoder(self): from pyamf import amf0, amf3 self.assertEquals(pyamf._get_encoder_class(pyamf.AMF0), amf0.Encoder) self.assertEquals(pyamf._get_encoder_class(pyamf.AMF3), amf3.Encoder) self.assertRaises(ValueError, pyamf._get_encoder_class, 'spam') self.assertTrue(isinstance(pyamf.get_encoder(pyamf.AMF0), amf0.Encoder)) self.assertTrue(isinstance(pyamf.get_encoder(pyamf.AMF3), amf3.Encoder)) self.assertRaises(ValueError, pyamf.get_encoder, 'spam') context = amf0.Context() encoder = pyamf.get_encoder(pyamf.AMF0, data='spam', context=context) self.assertEquals(encoder.stream.getvalue(), 'spam') self.assertEquals(encoder.context, context) context = amf3.Context() encoder = pyamf.get_encoder(pyamf.AMF3, data='eggs', context=context) self.assertEquals(encoder.stream.getvalue(), 'eggs') self.assertEquals(encoder.context, context)
def writeAMF3(self, data): """ Writes an element to the datastream in L{AMF3<pyamf.amf3>} format. @type data: C{mixed} @param data: The data to be encoded. """ encoder = pyamf._get_encoder_class(pyamf.AMF3)(self.stream) self.writeType(ASTypes.AMF3) encoder.writeElement(data)
def encode(msg, context=None, strict=False): """ Encodes AMF stream and returns file object. @type msg: L{Envelope} @param msg: The message to encode. @type context: L{amf0.Context<pyamf.amf0.Context>} or L{amf3.Context<pyamf.amf3.Context>} @param context: Context. @type strict: C{bool} @param strict: Determines whether encoding should be strict. Specifically header/body lengths will be written correctly, instead of the default 0. Default is C{False}. Introduced in 0.4. @rtype: C{StringIO} @return: File object. """ def getNewContext(): if context: new_context = copy.copy(context) new_context.reset() return new_context else: return pyamf.get_context(pyamf.AMF0) stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF0)(stream, strict=strict) if msg.clientType == pyamf.ClientTypes.Flash9: encoder.use_amf3 = True stream.write_uchar(msg.amfVersion) stream.write_uchar(msg.clientType) stream.write_short(len(msg.headers)) for name, header in msg.headers.iteritems(): _write_header( name, header, msg.headers.is_required(name), stream, encoder, strict) stream.write_short(len(msg)) for name, message in msg.iteritems(): encoder.context = getNewContext() _write_body(name, message, stream, encoder, strict) return stream
def writeAMF3(self, data): """ Writes an element to the datastream in L{AMF3<pyamf.amf3>} format. @type data: C{mixed} @param data: The data to be encoded to the AMF0 data stream. """ if not hasattr(self.context, 'amf3_context'): from pyamf import amf3 self.context.amf3_context = amf3.Context() self.context.addAMF3Object(data) encoder = pyamf._get_encoder_class(pyamf.AMF3)(self.stream, self.context.amf3_context) self.writeType(ASTypes.AMF3) encoder.writeElement(data)
def encode(msg, old_context=None, strict=False): """ Encodes AMF stream and returns file object. @type msg: L{Envelope} @param msg: The message to encode. @type old_context: L{amf0.Context<pyamf.amf0.Context>} or L{amf3.Context<pyamf.amf3.Context>} @param old_context: Context. @type strict: C{bool} @param strict: Determines whether encoding should be strict. Specifically header/body lengths will be written correctly, instead of the default 0. @rtype: C{StringIO} @return: File object. """ def getNewContext(): if old_context: import copy return copy.copy(old_context) else: return pyamf.get_context(pyamf.AMF0) stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(msg.amfVersion)(stream) stream.write_uchar(msg.amfVersion) stream.write_uchar(msg.clientType) stream.write_short(len(msg.headers)) for name, header in msg.headers.iteritems(): _write_header( name, header, msg.headers.is_required(name), stream, encoder, strict) stream.write_short(len(msg)) for name, message in msg.iteritems(): # Each body requires a new context encoder.context = getNewContext() _write_body(name, message, stream, encoder, strict) return stream
def encode(msg, old_context=None, strict=False): """ Encodes AMF stream and returns file object. @type msg: L{Envelope} @param msg: The message to encode. @type old_context: L{amf0.Context<pyamf.amf0.Context>} or L{amf3.Context<pyamf.amf3.Context>} @param old_context: Context. @type strict: C{bool} @param strict: Determines whether encoding should be strict. Specifically header/body lengths will be written correctly, instead of the default 0. @rtype: C{StringIO} @return: File object. """ def getNewContext(): if old_context: import copy return copy.copy(old_context) else: return pyamf.get_context(pyamf.AMF0) stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(msg.amfVersion)(stream) stream.write_uchar(msg.amfVersion) stream.write_uchar(msg.clientType) stream.write_short(len(msg.headers)) for name, header in msg.headers.iteritems(): _write_header(name, header, msg.headers.is_required(name), stream, encoder, strict) stream.write_short(len(msg)) for name, message in msg.iteritems(): # Each body requires a new context encoder.context = getNewContext() _write_body(name, message, stream, encoder, strict) return stream
def test_encode(self): stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF0)(stream) x = amf0.RecordSet(columns=['a', 'b', 'c'], items=[ [1, 2, 3], [4, 5, 6], [7, 8, 9]]) encoder.writeElement(x) stream.write('\x10\x00\tRecordSet\x00\nserverInfo\x03\x00\x06cursor' '\x00?\xf0\x00\x00\x00\x00\x00\x00\x00\x0bcolumnNames\n\x00\x00' '\x00\x03\x02\x00\x01a\x02\x00\x01b\x02\x00\x01c\x00\x0binitial' 'Data\n\x00\x00\x00\x03\n\x00\x00\x00\x03\x00?\xf0\x00\x00\x00' '\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00@\x08\x00\x00' '\x00\x00\x00\x00\n\x00\x00\x00\x03\x00@\x10\x00\x00\x00\x00\x00' '\x00\x00@\x14\x00\x00\x00\x00\x00\x00\x00@\x18\x00\x00\x00\x00' '\x00\x00\n\x00\x00\x00\x03\x00@\x1c\x00\x00\x00\x00\x00\x00\x00' '@ \x00\x00\x00\x00\x00\x00\x00@"\x00\x00\x00\x00\x00\x00\x00\x07' 'version\x00?\xf0\x00\x00\x00\x00\x00\x00\x00\ntotalCount\x00@' '\x08\x00\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\t')
def test_simple(self): class A(object): pass pyamf.register_class(A, 'A', attrs=['a']) class B(A): pass pyamf.register_class(B, 'B', attrs=['b']) x = B() x.a = 'spam' x.b = 'eggs' stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF3)(stream) encoder.writeElement(x) self.assertEquals(stream.getvalue(), '\n\x1b\x03B\x03b\x06\teggs\x03a' '\x06\tspam\x01')
def test_simple(self): class A(object): pass pyamf.register_class(A, 'A', attrs=['a']) class B(A): pass pyamf.register_class(B, 'B', attrs=['b']) x = B() x.a = 'spam' x.b = 'eggs' stream = util.BufferedByteStream() encoder = pyamf._get_encoder_class(pyamf.AMF0)(stream) encoder.writeElement(x) self.assertTrue(check_buffer(stream.getvalue(), ('\x10\x00\x01B', ( '\x00\x01a\x02\x00\x04spam', '\x00\x01b\x02\x00\x04eggs' ), '\x00\x00\t')))