public void CallByName_CorrectQueryString_SendsRpcRequest_ReturnsResult() { //setup var descriptor = new ControllerActionDescriptor() { ActionName = "getblockheader", MethodInfo = typeof(FullNodeController).GetMethod("GetBlockHeader"), Parameters = new List <ParameterDescriptor>() }; foreach (var parameter in typeof(FullNodeController).GetMethod("GetBlockHeader").GetParameters()) { descriptor.Parameters.Add(new ControllerParameterDescriptor() { Name = parameter.Name, ParameterType = parameter.ParameterType, ParameterInfo = parameter }); } this.descriptors.Add(descriptor); var values = new Dictionary <string, StringValues>(); values.Add("hash", new StringValues(new uint256(1000).ToString())); values.Add("isjsonformat", new StringValues("true")); this.controller.ControllerContext = new ControllerContext(); this.controller.ControllerContext.HttpContext = new DefaultHttpContext(); this.controller.ControllerContext.HttpContext.Request.Query = new QueryCollection(values); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new RPCResponseObject())))) { var rpcResponse = RPCResponse.Load(stream); this.rpcClient.Setup(c => c.SendCommand(It.Is <RPCRequest>(r => r.Method == "getblockheader" && ((string)r.Params[0]) == new uint256(1000).ToString() && ((string)r.Params[1]) == "true"), true)) .Returns(rpcResponse) .Verifiable(); // call var controllerResult = this.controller.CallByName("getblockheader"); //verify this.rpcClient.Verify(); var jsonResult = Assert.IsType <JsonResult>(controllerResult); var result = jsonResult.Value as JToken; Assert.Equal(Network.TestNet.GenesisHash.ToString(), result["hashPrevBlock"].ToString()); } }
public static RPCException BuildException(RPCErrorCode code, string message, object response) { var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } }; var rawMessage = JsonConvert.SerializeObject(response, jsonSerializerSettings); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(rawMessage))) { return(new RPCException(code, message, RPCResponse.Load(stream))); }; }
public async Task <RPCResponse> SendCommandAsync(RPCRequest request, bool throwIfRPCError = true) { RPCResponse response = null; HttpWebRequest webRequest = response == null?CreateWebRequest() : null; if (response == null) { var writer = new StringWriter(); request.WriteJSON(writer); writer.Flush(); var json = writer.ToString(); var bytes = Encoding.UTF8.GetBytes(json); #if !(PORTABLE || NETCORE) webRequest.ContentLength = bytes.Length; #endif var dataStream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false); await dataStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); await dataStream.FlushAsync().ConfigureAwait(false); dataStream.Dispose(); } WebResponse webResponse = null; WebResponse errorResponse = null; try { webResponse = response == null ? await webRequest.GetResponseAsync().ConfigureAwait(false) : null; response = response ?? RPCResponse.Load(await ToMemoryStreamAsync(webResponse.GetResponseStream()).ConfigureAwait(false)); if (throwIfRPCError) { response.ThrowIfError(); } } catch (WebException ex) { if (ex.Response == null || ex.Response.ContentLength == 0 || !ex.Response.ContentType.Equals("application/json", StringComparison.Ordinal)) { throw; } errorResponse = ex.Response; response = RPCResponse.Load(await ToMemoryStreamAsync(errorResponse.GetResponseStream()).ConfigureAwait(false)); if (throwIfRPCError) { response.ThrowIfError(); } } finally { if (errorResponse != null) { errorResponse.Dispose(); errorResponse = null; } if (webResponse != null) { webResponse.Dispose(); webResponse = null; } } return(response); }