示例#1
0
 IObservable <byte[]> DownloadImage(Uri url)
 {
     return(ImageDownloader.DownloadImageBytes(url)
            .SelectMany(x => x == null
             ? Observable.Empty <byte[]>()
             : Observable.Return(x)));
 }
示例#2
0
        public void NotFoundTwiceForSameHost_CouldNotDownloadExceptionMessage()
        {
            var host          = "flaky404.githubusercontent.com";
            var url           = new Uri("https://" + host + "/u/00000000?v=4");
            var expectMessage = ImageDownloader.CouldNotDownloadExceptionMessage(url);
            var httpClient    = Substitute.For <IHttpClient>();
            var response      = Substitute.For <IResponse>();

            response.StatusCode.Returns(HttpStatusCode.NotFound);
            httpClient.Send(null, default(CancellationToken)).ReturnsForAnyArgs(Task.FromResult(response));
            var target = new ImageDownloader(new Lazy <IHttpClient>(() => httpClient));

            var ex1 = Assert.CatchAsync <HttpRequestException>(async() => await target.DownloadImageBytes(url));
            var ex2 = Assert.CatchAsync <HttpRequestException>(async() => await target.DownloadImageBytes(url));

            Assert.That(ex1?.Message, Is.EqualTo(expectMessage));
            Assert.That(ex2?.Message, Is.EqualTo(expectMessage));
        }
示例#3
0
        public void NonImageContentForSameHost_ThrowsCachedHttpRequestException()
        {
            var host           = "host";
            var url            = new Uri("https://" + host + "/image");
            var contentType    = "text/html";
            var expectMessage1 = ImageDownloader.NonImageContentExceptionMessage(contentType);
            var expectMessage2 = ImageDownloader.CachedExceptionMessage(host);
            var httpClient     = Substitute.For <IHttpClient>();
            var response       = Substitute.For <IResponse>();

            response.StatusCode.Returns(HttpStatusCode.OK);
            response.ContentType.Returns(contentType);
            httpClient.Send(null, default(CancellationToken)).ReturnsForAnyArgs(Task.FromResult(response));
            var target = new ImageDownloader(new Lazy <IHttpClient>(() => httpClient));

            var ex1 = Assert.CatchAsync <HttpRequestException>(async() => await target.DownloadImageBytes(url));
            var ex2 = Assert.CatchAsync <HttpRequestException>(async() => await target.DownloadImageBytes(url));

            Assert.That(ex1?.Message, Is.EqualTo(expectMessage1));
            Assert.That(ex2?.Message, Is.EqualTo(expectMessage2));
        }
示例#4
0
        public void HttpStatusCode_NotFound404_ThrowsHttpRequestException()
        {
            var url        = new Uri("http://foo.bar");
            var httpClient = Substitute.For <IHttpClient>();
            var response   = Substitute.For <IResponse>();

            response.StatusCode.Returns(HttpStatusCode.NotFound);
            httpClient.Send(null, default(CancellationToken)).ReturnsForAnyArgs(Task.FromResult(response));
            var target = new ImageDownloader(new Lazy <IHttpClient>(() => httpClient));

            Assert.ThrowsAsync <HttpRequestException>(async() => await target.DownloadImageBytes(url));
        }
示例#5
0
        public void ContentTypeText_ThrowsHttpRequestException()
        {
            var url        = new Uri("http://foo.bar");
            var httpClient = Substitute.For <IHttpClient>();
            var response   = Substitute.For <IResponse>();

            response.StatusCode.Returns(HttpStatusCode.OK);
            response.ContentType.Returns("text/plain");
            response.Body.Returns(Array.Empty <byte>());
            httpClient.Send(null, default(CancellationToken)).ReturnsForAnyArgs(Task.FromResult(response));
            var target = new ImageDownloader(new Lazy <IHttpClient>(() => httpClient));

            Assert.ThrowsAsync <NonImageContentException>(async() => await target.DownloadImageBytes(url));
        }
示例#6
0
        public async Task HttpStatusCode_OK_Async()
        {
            var url        = new Uri("http://foo.bar");
            var httpClient = Substitute.For <IHttpClient>();
            var response   = Substitute.For <IResponse>();

            response.StatusCode.Returns(HttpStatusCode.OK);
            response.ContentType.Returns("image/xxx");
            response.Body.Returns(Array.Empty <byte>());
            httpClient.Send(null, default(CancellationToken)).ReturnsForAnyArgs(Task.FromResult(response));
            var target = new ImageDownloader(new Lazy <IHttpClient>(() => httpClient));

            var bytes = await target.DownloadImageBytes(url);

            Assert.IsEmpty(bytes);
        }