Exemplo n.º 1
0
        public void V2CoreDeserializeResponseDataWhenHasDataIsFalse()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_core_error_has_data_false.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.ResponseContracts["m"]     = new JsonRpcResponseContract(typeof(string));
            jsonRpcSerializer.StaticResponseBindings[1L] = "m";

            var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.False(jsonRpcMessage.Success);

            var jsonRpcError = jsonRpcMessage.Error;

            Assert.False(jsonRpcError.HasData);
            Assert.Null(jsonRpcError.Data);
        }
Exemplo n.º 2
0
        public void CoreDeserializeResponseDataWhenJsonStringIsNull()
        {
            var jsonRpcSerializer = new JsonRpcSerializer();

            Assert.Throws <ArgumentNullException>(() =>
                                                  jsonRpcSerializer.DeserializeResponseData(null));
        }
Exemplo n.º 3
0
        public void V1SpecT010DeserializeResponse()
        {
            var jsonSample = EmbeddedResourceManager.GetString("Assets.v1_spec_01.0_res.json");

            var jsonRpcSerializer = new JsonRpcSerializer
            {
                CompatibilityLevel = JsonRpcCompatibilityLevel.Level1
            };

            jsonRpcSerializer.ResponseContracts["echo"]  = new JsonRpcResponseContract(typeof(string));
            jsonRpcSerializer.StaticResponseBindings[1L] = "echo";

            var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal(1L, jsonRpcMessage.Id);
            Assert.IsType <string>(jsonRpcMessage.Result);
            Assert.Equal("Hello JSON-RPC", jsonRpcMessage.Result);
        }
Exemplo n.º 4
0
        public void V1BitcoinT01DeserializeResponse()
        {
            var jsonSample = EmbeddedResourceManager.GetString("Assets.v1_btc_01_res.json");

            var jsonRpcSerializer = new JsonRpcSerializer
            {
                CompatibilityLevel = JsonRpcCompatibilityLevel.Level1
            };

            jsonRpcSerializer.ResponseContracts["getblockhash"] = new JsonRpcResponseContract(typeof(string));
            jsonRpcSerializer.StaticResponseBindings["foo"]     = "getblockhash";

            var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal("foo", jsonRpcMessage.Id);
            Assert.IsType <string>(jsonRpcMessage.Result);
            Assert.Equal("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", jsonRpcMessage.Result);
        }
Exemplo n.º 5
0
        public void V1CoreDeserializeResponseWhenErrorTypeIsInvali()
        {
            var jsonSample = EmbeddedResourceManager.GetString("Assets.v1_core_error_type_invalid.json");

            var jsonRpcSerializer = new JsonRpcSerializer
            {
                CompatibilityLevel = JsonRpcCompatibilityLevel.Level1
            };

            jsonRpcSerializer.ResponseContracts["m"]     = new JsonRpcResponseContract(typeof(string));
            jsonRpcSerializer.StaticResponseBindings[1L] = "m";

            var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.False(jsonRpcMessage.Success);
            Assert.Equal(1L, jsonRpcMessage.Id);
            Assert.Null(jsonRpcMessage.Result);
            Assert.NotNull(jsonRpcMessage.Error);

            var jsonRpcError = jsonRpcMessage.Error;

            Assert.Equal(default, jsonRpcError.Code);
Exemplo n.º 6
0
        public void V1BitcoinT02DeserializeResponse()
        {
            var jsonSample = EmbeddedResourceManager.GetString("Assets.v1_btc_02_res.json");

            var jsonRpcSerializer = new JsonRpcSerializer
            {
                CompatibilityLevel = JsonRpcCompatibilityLevel.Level1
            };

            jsonRpcSerializer.ResponseContracts["getblockhash"] = new JsonRpcResponseContract(typeof(string));
            jsonRpcSerializer.StaticResponseBindings["foo"]     = "getblockhash";

            var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal("foo", jsonRpcMessage.Id);
            Assert.False(jsonRpcMessage.Success);

            var jsonRpcError = jsonRpcMessage.Error;

            Assert.Equal(-8L, jsonRpcError.Code);
            Assert.Equal("Block height out of range", jsonRpcError.Message);
        }
Exemplo n.º 7
0
        public void CoreDeserializeResponseDataWhenJsonStringIsEmpty()
        {
            var jsonRpcSerializer = new JsonRpcSerializer();

            var exception = Assert.Throws <JsonRpcException>(() =>
                                                             jsonRpcSerializer.DeserializeResponseData(string.Empty));

            Assert.Equal(JsonRpcErrorCodes.InvalidJson, exception.ErrorCode);
        }
Exemplo n.º 8
0
        public void V2CoreDeserializeResponseDataWhenErrorMessageIsNull()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_core_error_message_null.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.ResponseContracts["m"]     = new JsonRpcResponseContract(typeof(string));
            jsonRpcSerializer.StaticResponseBindings[1L] = "m";

            var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.False(jsonRpcItem.IsValid);

            var jsonRpcException = jsonRpcItem.Exception;

            Assert.Equal(JsonRpcErrorCodes.InvalidMessage, jsonRpcException.ErrorCode);
        }
Exemplo n.º 9
0
        public void V2CoreDeserializeResponseDataWhenIdIsFloat()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_core_id_float_res.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.ResponseContracts["m"]     = new JsonRpcResponseContract(typeof(string));
            jsonRpcSerializer.StaticResponseBindings[1D] = "m";

            var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;
            var jsonRpcId      = jsonRpcMessage.Id;

            Assert.Equal(JsonRpcIdType.Float, jsonRpcId.Type);
            Assert.Equal(1D, (double)jsonRpcId);
        }
Exemplo n.º 10
0
        public void V2SpecT010DeserializeResponse()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_spec_01.0_res.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.ResponseContracts["subtract"] = new JsonRpcResponseContract(typeof(long));
            jsonRpcSerializer.StaticResponseBindings[1L]    = "subtract";

            var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal(1L, jsonRpcMessage.Id);
            Assert.IsType <long>(jsonRpcMessage.Result);
            Assert.Equal(19L, jsonRpcMessage.Result);
        }
Exemplo n.º 11
0
        protected JsonRpcSerializerBenchmarks()
        {
            _serializer.RequestContracts["mn"] = new JsonRpcRequestContract(new Dictionary <string, Type> {
                ["p1"] = typeof(long), ["p2"] = typeof(long)
            });
            _serializer.RequestContracts["mp"] = new JsonRpcRequestContract(new[] { typeof(long), typeof(long) });

            foreach (var identifier in new JsonRpcId[] { "1", "2", 1L, 2L, 1D, 2D })
            {
                _serializer.DynamicResponseBindings[identifier] = new JsonRpcResponseContract(typeof(long), typeof(long));
            }

            foreach (var name in new[] { "req_nul_nam", "req_nul_pos", "req_str_nam", "req_str_pos", "req_int_nam", "req_int_pos", "req_flt_nam", "req_flt_pos" })
            {
                _resources[name] = EmbeddedResourceManager.GetString($"Assets.{name}.json");
                _requests[name]  = _serializer.DeserializeRequestData(_resources[name]).Items.Select(i => i.Message).ToArray();
            }

            foreach (var name in new[] { "res_nul_err", "res_str_err", "res_str_scs", "res_int_err", "res_int_scs", "res_flt_err", "res_flt_scs" })
            {
                _resources[name] = EmbeddedResourceManager.GetString($"Assets.{name}.json");
                _responses[name] = _serializer.DeserializeResponseData(_resources[name]).Items.Select(i => i.Message).ToArray();
            }
        }
Exemplo n.º 12
0
        public void DeserializeResponseDataT010()
        {
            var jsont = EmbeddedResourceManager.GetString("Assets.v2_spec_01.0_res.json");
            var jrcr  = new JsonRpcContractResolver();
            var jrs   = new JsonRpcSerializer(jrcr);

            jrcr.AddResponseContract("subtract", new JsonRpcResponseContract(typeof(long)));
            jrcr.AddResponseBinding(1L, "subtract");

            var jrd = jrs.DeserializeResponseData(jsont);

            Assert.IsFalse(jrd.IsBatch);

            var jrmi = jrd.Item;

            Assert.IsTrue(jrmi.IsValid);

            var jrm = jrmi.Message;

            Assert.AreEqual(1L, jrm.Id);
            Assert.IsInstanceOfType(jrm.Result, typeof(long));
            Assert.AreEqual(19L, jrm.Result);
        }
        public void DeserializeResponseDataT010()
        {
            var jsont = EmbeddedResourceManager.GetString("Assets.v1_spec_01.0_res.json");
            var jrcr  = new JsonRpcContractResolver();
            var jrs   = new JsonRpcSerializer(jrcr, compatibilityLevel: JsonRpcCompatibilityLevel.Level1);

            jrcr.AddResponseContract("echo", new JsonRpcResponseContract(typeof(string)));
            jrcr.AddResponseBinding(1L, "echo");

            var jrd = jrs.DeserializeResponseData(jsont);

            Assert.IsFalse(jrd.IsBatch);

            var jrmi = jrd.Item;

            Assert.IsTrue(jrmi.IsValid);

            var jrm = jrmi.Message;

            Assert.AreEqual(1L, jrm.Id);
            Assert.IsInstanceOfType(jrm.Result, typeof(string));
            Assert.AreEqual("Hello JSON-RPC", jrm.Result);
        }
Exemplo n.º 14
0
        public void CoreDeserializeResponseDatatFromStream()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_core_res.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.DynamicResponseBindings[0L] = new JsonRpcResponseContract(typeof(long));

            using (var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonSample)))
            {
                var jsonRpcData = jsonRpcSerializer.DeserializeResponseData(jsonStream);

                Assert.False(jsonRpcData.IsBatch);

                var jsonRpcItem = jsonRpcData.Item;

                Assert.True(jsonRpcItem.IsValid);

                var jsonRpcMessage = jsonRpcItem.Message;

                Assert.Equal(0L, jsonRpcMessage.Id);
                Assert.True(jsonRpcMessage.Success);
                Assert.Equal(0L, jsonRpcMessage.Result);
            }
        }