コード例 #1
0
        public void ShouldBeAbleToCancelPostRequest()
        {
            try
            {
                var mockResponse = new HttpResponseMessage(HttpStatusCode.OK);
                mockResponse.Content = new StringContent(String.Empty);

                var    mockHttpMessageHandler = new MockHttpMessageHandler(mockResponse, TimeSpan.FromSeconds(5));
                string mockAddress            = "http://127.0.0.1:5001";

                var cts = new CancellationTokenSource();
                using (var client = new IpfsClient(new Uri(mockAddress), new HttpClient(mockHttpMessageHandler)))
                {
                    var task = client.ConfigCommand("test", "test", true, cts.Token);
                    //var task = client.Object.Put(new MerkleNode(), cts.Token);
                    cts.Cancel();
                    task.Wait();
                    throw new Exception("The operation was not cancelled");
                }
            }
            catch (AggregateException ex) when(ex.InnerException is TaskCanceledException)
            {
                Console.WriteLine("The operation has been canceled properly");
            }
        }