public void DownloadStreamsCertainData_For_SpecifiedRange() { var bytesRange = new BytesRange { Start = 100, End = 200 }; var inputData = CreateRandomData(1024); var inputDataStream = new MemoryStream(inputData, false); var baseHttpDownloader = new BaseHttpDownloader("http://test_url.com", 10000, MockHttpClient(inputDataStream, HttpStatusCode.OK), Substitute.For <ILogger>()); var outputDataStream = new MemoryStream(inputData.Length); baseHttpDownloader.DataAvailable += (data, length) => { Assert.IsTrue(length > 0, "Data length passed in DataAvailable event is not more than zero."); outputDataStream.Write(data, 0, length); }; baseHttpDownloader.SetBytesRange(bytesRange); baseHttpDownloader.Download(CancellationToken.Empty); outputDataStream.Seek(0, SeekOrigin.Begin); ValidateOutput(inputData, outputDataStream, bytesRange); }
public void DownloadRange() { const int bytesStartRange = 100; const int bytesEndRange = 200; var inputData = CreateRandomData(1024); var inputDataStream = new MemoryStream(inputData, false); var baseHttpDownloader = new BaseHttpDownloader("someurl", 10000, 64, url => MockRequest(inputDataStream, HttpStatusCode.OK)); var outputDataStream = new MemoryStream(inputData.Length); baseHttpDownloader.DataAvailable += (data, length) => { Assert.IsTrue(length > 0, "Data length passed in DataAvailable event is not more than zero."); outputDataStream.Write(data, 0, length); }; baseHttpDownloader.SetBytesRange(bytesStartRange, bytesEndRange); baseHttpDownloader.Download(CancellationToken.Empty); outputDataStream.Seek(0, SeekOrigin.Begin); ValidateOutput(inputData, outputDataStream, bytesStartRange, bytesEndRange); }
public void EmptyDataStream() { var baseHttpDownloader = new BaseHttpDownloader("someurl", 10000, 64, url => MockRequest(null, HttpStatusCode.OK)); Assert.Catch <DownloaderException>(() => baseHttpDownloader.Download(CancellationToken.Empty)); }
public void DownloadThrowsException_For_Status500() { var inputData = CreateRandomData(1024); var inputDataStream = new MemoryStream(inputData, false); var baseHttpDownloader = new BaseHttpDownloader("http://test_url.com", 10000, MockHttpClient(inputDataStream, HttpStatusCode.InternalServerError), Substitute.For <ILogger>()); Assert.Catch <ServerErrorException>(() => baseHttpDownloader.Download(CancellationToken.Empty)); }
public void InvalidResponse() { var inputData = CreateRandomData(1024); var inputDataStream = new MemoryStream(inputData, false); var baseHttpDownloader = new BaseHttpDownloader("someurl", 10000, 64, url => MockRequest(inputDataStream, HttpStatusCode.BadRequest)); Assert.Catch <DownloaderException>(() => baseHttpDownloader.Download(CancellationToken.Empty)); }
public void DownloadThrowsException_For_WebException() { var httpClient = Substitute.For <IHttpClient>(); httpClient.Get(Arg.Any <HttpGetRequest>()).Returns(_ => { throw new WebException(); }); var baseHttpDownloader = new BaseHttpDownloader("http://test_url.com", 10000, httpClient, Substitute.For <ILogger>()); Assert.Catch <ConnectionFailureException>(() => baseHttpDownloader.Download(CancellationToken.Empty)); }