Exemple #1
0
        public async Task When_downloading_fails()
        {
            var fileName     = RandomString();
            var fileUrl      = RandomString();
            var httpResponse = new HttpStreamResponse(500, new HttpHeaders(), new MemoryStream());

            GetMock <IHttpClient>().Setup(x => x.ExecuteAsStreamAsync(IsAny <IHttpRequest>()))
            .ReturnsAsync(httpResponse);

            var exception = await BecauseThrowsAsync <Exception>(() => ClassUnderTest.DownloadAsync(fileUrl, fileName));

            It("throws an informative exception", () =>
            {
                exception.ShouldNotBeNull()
                .Message.ShouldBe($"Failed with status code {httpResponse.StatusCode} to download {fileName}");
            });
        }
Exemple #2
0
        internal static async Task <HttpStreamResponse> LoadHttpStreamAsync(Uri uri)
        {
            HttpStreamResponse response = null;

            try
            {
                using (var responseMessage = await HttpClient.GetAsync(uri).ConfigureAwait(false))
                {
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        MemoryStream stream = null;
                        TimeSpan?    maxAge = null;

                        if (ImageAvailable(responseMessage.Headers))
                        {
                            stream = new MemoryStream();
                            await responseMessage.Content.CopyToAsync(stream).ConfigureAwait(false);

                            stream.Seek(0, SeekOrigin.Begin);

                            maxAge = responseMessage.Headers.CacheControl?.MaxAge;
                        }

                        response = new HttpStreamResponse(stream, maxAge);
                    }
                    else
                    {
                        Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
            }

            return(response);
        }
Exemple #3
0
        public async Task When_downloading()
        {
            var          downloadsDir        = RandomString();
            var          fileName            = RandomString();
            var          fileUrl             = RandomString();
            var          httpResponse        = new HttpStreamResponse(200, new HttpHeaders(), new MemoryStream());
            IHttpRequest?capturedHttpRequest = null;
            string?      capturedPath        = null;
            var          expectedFilePath    = $"{downloadsDir}\\{fileName}";

            GetMock <IArguments>().SetupGet(x => x.DownloadsDir).Returns(downloadsDir);

            GetMock <IHttpClient>().Setup(x => x.ExecuteAsStreamAsync(IsAny <IHttpRequest>()))
            .Callback <IHttpRequest>(httpRequest => capturedHttpRequest = httpRequest)
            .ReturnsAsync(httpResponse);

            GetMock <IFileSystem>().Setup(x => x.WriteStreamAsync(IsAny <string>(), httpResponse.Stream))
            .Callback <string, Stream>((path, _) => capturedPath = path);

            var downloadedFilePath = await BecauseAsync(() => ClassUnderTest.DownloadAsync(fileUrl, fileName));

            It("downloads the required file", () =>
            {
                capturedHttpRequest.ShouldNotBeNull().ShouldSatisfyAllConditions(x =>
                {
                    x.Url.ShouldBe(fileUrl);
                    x.Method.ShouldBe(HttpMethod.GET);
                });

                capturedPath.ShouldBe(expectedFilePath);
            });

            It("returns the path to the downloaded file", () =>
            {
                downloadedFilePath.ShouldBe(expectedFilePath);
            });
        }