コード例 #1
0
func TestEncodeSerializableObjectValue(t *testing.T) {
	arr := php_serialize.PhpArray{
		"a": 5,
		"b": "priv",
		"c": 8,
	}
	obj := php_serialize.NewPhpObjectSerialized("TestObject")
	obj.SetValue(php_serialize.PhpValue(arr))
	data := PhpSession{
		"obj": obj,
	}

	encoder := NewPhpEncoder(data)
	encoder.SetSerializedEncodeFunc(php_serialize.SerializedEncodeFunc(php_serialize.Serialize))
	if result, err := encoder.Encode(); err != nil {
		t.Errorf("Can not encode object value %#v \n", err)
	} else {
		if !strings.Contains(result, "C:10:\"TestObject\"") {
			t.Errorf("Object value was encoded incorrectly %v\n", result)
		} else if !strings.Contains(result, "s:1:\"a\";i:5;") {
			t.Errorf("Object value was encoded incorrectly %v\n", result)
		} else if !strings.Contains(result, "s:1:\"b\";s:4:\"priv\";") {
			t.Errorf("Object value was encoded incorrectly %v\n", result)
		} else if !strings.Contains(result, "s:1:\"c\";i:8;") {
			t.Errorf("Object value was encoded incorrectly %v\n", result)
		}
	}
}
コード例 #2
0
func TestEncodeSerializableObjectValueNoFunc(t *testing.T) {
	obj := php_serialize.NewPhpObjectSerialized("TestObject")
	obj.SetData("a:3:{s:1:\"a\";i:5;s:1:\"b\";s:4:\"priv\";s:1:\"c\";i:8;}")
	data := PhpSession{
		"obj": obj,
	}

	encoder := NewPhpEncoder(data)
	if result, err := encoder.Encode(); err != nil {
		t.Errorf("Can not encode object value %#v \n", err)
	} else {
		if !strings.Contains(result, "a:3:{s:1:\"a\";i:5;s:1:\"b\";s:4:\"priv\";s:1:\"c\";i:8;}") || !strings.Contains(result, "C:10:\"TestObject\"") {
			t.Errorf("Object value was encoded incorrectly %v\n", result)
		}
	}
}
コード例 #3
0
func TestEncodeSerializableObjectValueJSON(t *testing.T) {
	var f php_serialize.SerializedEncodeFunc
	f = func(v php_serialize.PhpValue) (string, error) {
		res, err := json.Marshal(v)
		return string(res), err
	}

	obj := php_serialize.NewPhpObjectSerialized("Bar")
	obj.SetValue(map[string]string{"public": "public"})
	data := PhpSession{
		"bar": obj,
	}

	encoder := NewPhpEncoder(data)
	encoder.SetSerializedEncodeFunc(f)
	if result, err := encoder.Encode(); err != nil {
		t.Errorf("Can not encode object value %#v \n", err)
	} else {
		if result != "bar|C:3:\"Bar\":19:{{\"public\":\"public\"}}" {
			t.Errorf("Object value was encoded incorrectly %v\n", result)
		}
	}
}