public async Task ShouldUploadImageMultipartBinary() { var client = _fixture.CreateClient(); if (File.Exists("Image2.png")) { File.Delete("Image2.png"); } using var fs = File.OpenRead("cybtan.png"); var content = new MultipartFormDataContent("----WebKitFormBoundarymx2fSWqWSd0OxQq1"); var bytes = BinaryConvert.Serialize(new { Size = fs.Length, Name = "Image2.png" }); var byteArrayContent = new ByteArrayContent(bytes); byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse($"{BinarySerializer.MEDIA_TYPE}; charset={BinarySerializer.DefaultEncoding.WebName}"); content.Add(byteArrayContent); content.Add(new StreamContent(fs, (int)fs.Length), "Image", "Image.png"); var response = await client.PostAsync("/api/order/upload", content); Assert.NotNull(response); Assert.True(response.IsSuccessStatusCode); }
public async Task <T> GetOrSet <T>(string key, Func <Task <T> > func, TimeSpan?expire = null) where T : class { T cacheEntry; RedisValue entry = await _db.StringGetAsync(key).ConfigureAwait(false); if (!entry.HasValue) { cacheEntry = await func(); if (cacheEntry == null) { throw new InvalidOperationException($"Cache entry can not be null"); } var bytes = BinaryConvert.Serialize(cacheEntry); var result = await _db.StringSetAsync(key, bytes, expiry : expire).ConfigureAwait(false); if (!result) { throw new InvalidOperationException("cache error"); } return(cacheEntry); } return(BinaryConvert.Deserialize <T>(entry)); }
public async Task ShouldUploadImageMultipartJson() { var client = _fixture.CreateClient(); if (File.Exists("Image2.png")) { File.Delete("Image2.png"); } using var fs = File.OpenRead("cybtan.png"); var content = new MultipartFormDataContent("----WebKitFormBoundarymx2fSWqWSd0OxQq1"); var json = JsonConvert.SerializeObject(new { Size = fs.Length, Name = "Image2.png" }); content.Add(new StringContent(json, Encoding.UTF8, "application/json"), "content"); content.Add(new StreamContent(fs, (int)fs.Length), "Image", "Image.png"); var response = await client.PostAsync("/api/order/upload", content); Assert.NotNull(response); Assert.True(response.IsSuccessStatusCode); var bytes = await response.Content.ReadAsByteArrayAsync(); UploadImageResponse obj = BinaryConvert.Deserialize <UploadImageResponse>(bytes); fs.Seek(0, SeekOrigin.Begin); var hash = CryptoService.ToStringX2(new SymetricCryptoService().ComputeHash(fs)); Assert.Equal(hash, obj.M5checksum); }
public void BinarySerializeWithMetadataProvider() { var buffer = BinaryConvert.Serialize(_products); var result = BinaryConvert.Deserialize <List <Product> >(buffer); AssertProducts(result); }
public void BinarySerialize() { var buffer = BinaryConvert.Serialize(_modelA); var result = BinaryConvert.Deserialize <ModelA>(buffer); Assert(result); }
public override bool Equals(object other) { var bufThis = BinaryConvert.SerializeObject(this); var bufOhter = BinaryConvert.SerializeObject(other as PocoWithAllPrimitives); return(ArrayEqualityComparer <byte> .Equals(bufThis, bufOhter)); }
public void Test_DictionaryOfObjects() { var val = new Dictionary <string, PocoSimple>() { { "1", new PocoSimple() { Int = 1, Str = "1" } }, { "2", new PocoSimple() { Int = 2, Str = "2" } }, { "3", new PocoSimple() { Int = 3, Str = "3" } }, { "4", null } }; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <Dictionary <string, PocoSimple> >(buf); CollectionAssert.AreEqual( val.OrderBy(x => x.Key).ToList(), cloned.OrderBy(x => x.Key).ToList()); }
public void SerializeStream() { const int size = 10 * 1024 * 1024; var dataBuffer = CreateRandomBuffer(size); var data = new TestModel { Name = "Test", Stream = new MemoryStream(dataBuffer) }; var buffer = BinaryConvert.Serialize(data); Assert.NotNull(buffer); var result = BinaryConvert.Deserialize <TestModel>(buffer); Assert.Equal(result.Name, data.Name); Assert.NotNull(result.Stream); Assert.Equal(result.Stream.Length, size); var memory = result.Stream as MemoryStream; var memoryBuffer = memory.ToArray(); for (int i = 0; i < size; i++) { Assert.Equal(dataBuffer[i], memoryBuffer[i]); } }
public void Method_call_load_two_numbers_then_add() { // Arrange var builder = GetBuilder(); var pointer = 0; // Act _ = builder.GetIlProgram(); // Test reproducable property of CodeBuilder var program = builder.GetBinaryProgram(); // Assert // Entry Point Assert.Equal(0, pointer); Assert.Equal(11, BinaryConvert.GetInt32(ref pointer, program)); // two var method_two = pointer; Assert.Equal(5, ++pointer); Assert.Equal(InstructionCode.Ldc_i4, BinaryConvert.GetInstructionCode(ref pointer, program)); Assert.Equal(2, BinaryConvert.GetInt32(ref pointer, program)); Assert.Equal(InstructionCode.Ret, BinaryConvert.GetInstructionCode(ref pointer, program)); // main Assert.Equal(12, ++pointer); Assert.Equal(InstructionCode.Ldc_i4, BinaryConvert.GetInstructionCode(ref pointer, program)); Assert.Equal(1, BinaryConvert.GetInt32(ref pointer, program)); Assert.Equal(InstructionCode.Call, BinaryConvert.GetInstructionCode(ref pointer, program)); Assert.Equal(method_two, BinaryConvert.GetInt32(ref pointer, program)); Assert.Equal(InstructionCode.Add, BinaryConvert.GetInstructionCode(ref pointer, program)); Assert.Equal(InstructionCode.Ret, BinaryConvert.GetInstructionCode(ref pointer, program));
public void Test_StringNull() { string val = null; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <string>(buf); Assert.AreEqual(val, cloned); }
public void Test_DoubleNegative() { double val = -1234.1f; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <double>(buf); Assert.AreEqual(val, cloned); }
public void Test_BooleanTrue() { Boolean val = true; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <Boolean>(buf); Assert.AreEqual(val, cloned); }
public void Test_UInt32() { uint val = 1234; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <uint>(buf); Assert.AreEqual(val, cloned); }
public void Test_Int32Negative() { int val = -1234; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <int>(buf); Assert.AreEqual(val, cloned); }
public void Test_UInt64Max() { UInt64 val = UInt64.MaxValue; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <UInt64>(buf); Assert.AreEqual(val, cloned); }
public void Test_SByte() { SByte val = -12; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <SByte>(buf); Assert.AreEqual(val, cloned); }
public void Test_StringNonGeneric() { var val = "Hello World"; var buf = BinaryConvert.SerializeObject(typeof(string), val); var cloned = BinaryConvert.DeserializeObject(typeof(string), buf); Assert.AreEqual(val, cloned); }
public void Test_Int32NullableWithNull() { int?val = null; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <int?>(buf); Assert.AreEqual(val, cloned); }
public void Test_Empty() //same as default... { var val = Guid.Empty; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <Guid>(buf); Assert.AreEqual(val, cloned); }
public void Test_Int16() { Int16 val = 1234; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <Int16>(buf); Assert.AreEqual(val, cloned); }
public void Test_RandomNewGuid() { var val = Guid.NewGuid(); var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <Guid>(buf); Assert.AreEqual(val, cloned); }
public void Test_Char() { char val = 'x'; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <char>(buf); Assert.AreEqual(val, cloned); }
public void Test_Constants() { var val = new Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4"); var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <Guid>(buf); Assert.AreEqual(val, cloned); }
public void Test_Int32Min() { int val = Int32.MinValue; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <int>(buf); Assert.AreEqual(val, cloned); }
public void Test_Default() { var val = default(DateTime); var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <DateTime>(buf); Assert.AreEqual(val, cloned); }
public void Test_Int64Min() { Int64 val = Int64.MinValue; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <Int64>(buf); Assert.AreEqual(val, cloned); }
public void Test_String() { var val = "Hello World"; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <string>(buf); Assert.AreEqual(val, cloned); }
public void Test_SingleNegative() { float val = -1234.1f; var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <float>(buf); Assert.AreEqual(val, cloned); }
private void GetOpCodeArg(ReadOnlySpan <byte> program, out string value) { if (_pointer >= program.Length) { Throw.MissingInstructionArgument(_pointer); } value = BinaryConvert.GetString(ref _pointer, program); }
public void Test_Tuple_N8Rest() { var val = new Tuple <int, string, decimal, double, string, byte, DateTime, Tuple <int, string> >(1, "1", 1.23m, 3.3, null, 7, new DateTime(2010, 10, 10, 10, 10, 10), new Tuple <int, string>(2, "2")); var buf = BinaryConvert.SerializeObject(val); var cloned = BinaryConvert.DeserializeObject <Tuple <int, string, decimal, double, string, byte, DateTime, Tuple <int, string> > >(buf); Assert.AreEqual(val, cloned); }