Ejemplo n.º 1
0
        public async Task DownloadFileAsync_ReportsCorrectProgressWhenContentLengthIsKnown()
        {
            byte[] byteData = new byte[4096 * 5];
            Random random   = new Random();

            random.NextBytes(byteData);

            MemoryStream ms = new MemoryStream();

            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(byteData, 0, byteData.Length)
            };
            StubHttpMessageHandler testmessage    = new StubHttpMessageHandler(httpResponseMessage);
            HttpClient             stubHttpClient = new HttpClient(testmessage);
            IHttpService           httpService    = new HttpService(stubHttpClient);

            var observable = httpService.DownloadFileAsync("http://testsite.com", "TestFile", ms);
            TaskCompletionSource <bool> updateResult = new TaskCompletionSource <bool>();
            int count = 0;

            observable.Subscribe(d =>
            {
                count++;
                if (d >= 1.0)
                {
                    updateResult.SetResult(true);
                }
            }, exception => { });

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));

            cancellationTokenSource.Token.Register(() =>
                                                   { updateResult.TrySetCanceled(); });

            var result = await updateResult.Task;

            Assert.AreEqual(7, count);
        }
Ejemplo n.º 2
0
        public async Task DownloadFileAsync_OnErrorWhenGetFails()
        {
            byte[] byteData = new byte[4096 * 5];
            Random random   = new Random();

            random.NextBytes(byteData);

            MemoryStream ms = new MemoryStream();

            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new ByteArrayContent(byteData, 0, byteData.Length)
            };
            StubHttpMessageHandler testmessage    = new StubHttpMessageHandler(httpResponseMessage);
            HttpClient             stubHttpClient = new HttpClient(testmessage);
            IHttpService           httpService    = new HttpService(stubHttpClient);

            var observable = httpService.DownloadFileAsync("http://testsite.com", "TestFile", ms);
            TaskCompletionSource <HttpStatusCodeException> errorResult = new TaskCompletionSource <HttpStatusCodeException>();

            observable.Subscribe(d =>
                                 { }, exception =>
            {
                errorResult.SetResult((HttpStatusCodeException)exception);
            });

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));

            cancellationTokenSource.Token.Register(() =>
                                                   { errorResult.TrySetCanceled(); });

            HttpStatusCodeException result = await errorResult.Task;

            Assert.NotNull(result);
            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }