Test WebClient asynchronous uploads cancellation.
authormarcos henrich <marcoshenrich@gmail.com>
Mon, 2 Jun 2014 18:04:43 +0000 (19:04 +0100)
committermarcos henrich <marcoshenrich@gmail.com>
Mon, 2 Jun 2014 18:04:43 +0000 (19:04 +0100)
Tests that UploadStringAsync triggers UploadStringCompleted with Cancelled == true.
Tests that UploadDataAsync triggers UploadDataCompleted with Cancelled == true.
Tests that UploadValuesAsync triggers UploadValuesCompleted with Cancelled == true.
Tests that UploadFileAsync triggers UploadFileCompleted with Cancelled == true.

mcs/class/System/Test/System.Net/WebClientTest.cs

index 3505926f41074e31c559b69c2187940f9fd475ef..e39ca906b2fe80c263b62c33bbcf770b007211e4 100644 (file)
@@ -2156,5 +2156,93 @@ namespace MonoTests.System.Net
                        // and return the same instance as WebRequest.DefaultWebProxy
                        Assert.AreSame (wc.Proxy, WebRequest.DefaultWebProxy);
                }
+               
+               [Test]
+               public void UploadStringAsyncCancelEvent ()
+               {
+                       UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+                       {
+
+                               webClient.UploadStringCompleted += (sender, args) =>
+                               {
+                                       if (args.Cancelled)
+                                               cancelEvent.Set ();
+                               };
+
+                               webClient.UploadStringAsync (uri, "PUT", "text");
+                       });
+               }
+
+               [Test]
+               public void UploadDataAsyncCancelEvent ()
+               {
+                       UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+                       {
+                               webClient.UploadDataCompleted += (sender, args) =>
+                               {
+                                       if (args.Cancelled)
+                                               cancelEvent.Set ();
+                               };
+
+                               webClient.UploadDataAsync (uri, "PUT", new byte[] { });
+                       });
+               }
+               
+               [Test]
+               public void UploadValuesAsyncCancelEvent ()
+               {
+                       UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+                       {
+                               webClient.UploadValuesCompleted += (sender, args) =>
+                               {
+                                       if (args.Cancelled)
+                                               cancelEvent.Set ();
+                               };
+
+                               webClient.UploadValuesAsync (uri, "PUT", new NameValueCollection ());
+                       });
+               }
+
+               [Test]
+               public void UploadFileAsyncCancelEvent ()
+               {
+                       UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+                       {
+                               string tempFile = Path.Combine (_tempFolder, "upload.tmp");
+                               File.Create (tempFile).Close ();
+
+                               webClient.UploadFileCompleted += (sender, args) =>
+                               {
+                                       if (args.Cancelled)
+                                               cancelEvent.Set ();
+                               };
+
+                               webClient.UploadFileAsync (uri, "PUT", tempFile);
+                       });
+               }
+
+               public void UploadAsyncCancelEventTest (Action<WebClient, Uri, EventWaitHandle> uploadAction)
+               {
+                       var ep = new IPEndPoint (IPAddress.Loopback, 8000);
+                       string url = "http://" + IPAddress.Loopback + ":8000/test/";
+
+                       using (var responder = new SocketResponder (ep, EchoRequestHandler))
+                       {
+                               responder.Start ();
+
+                               var webClient = new WebClient ();
+
+                               var cancellationTokenSource = new CancellationTokenSource ();
+                               cancellationTokenSource.Token.Register (webClient.CancelAsync);
+
+                               var cancelEvent = new ManualResetEvent (false);
+
+                               uploadAction.Invoke (webClient, new Uri (url), cancelEvent);
+
+                               cancellationTokenSource.Cancel ();
+
+                               Assert.IsTrue (cancelEvent.WaitOne (1000));
+                       }
+               }
        }
 }