Ejemplo n.º 1
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));
        }