Esempio n. 1
0
    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);
    }
Esempio n. 2
0
    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);
    }
Esempio n. 3
0
    public void EmptyDataStream()
    {
        var baseHttpDownloader = new BaseHttpDownloader("someurl", 10000, 64,
                                                        url => MockRequest(null, HttpStatusCode.OK));

        Assert.Catch <DownloaderException>(() => baseHttpDownloader.Download(CancellationToken.Empty));
    }
Esempio n. 4
0
    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));
    }
Esempio n. 5
0
    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));
    }
Esempio n. 6
0
    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));
    }