Example #1
0
func TestEncodeToBytesDoesNotMangleOutput(t *testing.T) {
	s := amf0.NewString("hello world")

	buf := new(bytes.Buffer)

	n, e1 := amf0.Encode(s, buf)
	out, e2 := amf0.EncodeToBytes(s)

	assert.Len(t, out, n)
	assert.Equal(t, buf.Bytes(), out)
	assert.Equal(t, e1, e2)
}
Example #2
0
func TestUnsuccessfulEncodingReturnsError(t *testing.T) {
	buf := new(bytes.Buffer)

	typ := new(MockAmfType)
	typ.On("Marker").Return(byte(0x00))
	typ.On("Encode", mock.Anything).Return(0, errors.New("test"))

	n, err := amf0.Encode(typ, buf)

	assert.Equal(t, 0, n)
	assert.Equal(t, "test", err.Error())
}
Example #3
0
func TestSuccesfulEncodingWritesMarkerAndPayload(t *testing.T) {
	buf := new(bytes.Buffer)

	b := amf0.Bool(false)

	n, err := amf0.Encode(&b, buf)

	assert.Equal(t, 2, n)
	assert.Nil(t, err)

	assert.Equal(t, byte(0x1), buf.Bytes()[0],
		"amf0/encoder: did not write header byte")
	assert.Equal(t, byte(0x0), buf.Bytes()[1],
		"amf0/encoder: did not write type payload")
}
Example #4
0
// Marshal marshals some interface{} into its AMF-encoded equal. It passes
// through each field of a type one-by-one and marshals it by converting to its
// AMF type. If a field is already an AMF type, it marshals it directly. It does
// not recurse to embedded fields.
//
// If the field is nil (i.e., an uninitialized pointer), then an amf0.Null will
// be written, instead of the actual type.
func (m *Marshaler) Marshal(dest interface{}) ([]byte, error) {
	buf := new(bytes.Buffer)

	value := reflect.ValueOf(dest).Elem()
	for i := 0; i < value.NumField(); i++ {
		amf, err := m.convertToAmfType(value.Field(i))
		if err != nil {
			return nil, err
		}

		if _, err = amf0.Encode(amf, buf); err != nil {
			return nil, err
		}
	}

	return buf.Bytes(), nil
}