<commit_before>import fs from 'fs';
import path from 'path';
import zlib from 'zlib';
import assert from 'assert';
import nock from 'nock';
import lolex from 'lolex';
import streamEqual from 'stream-equal';
import miniget from '../dist';
import { Transform } from 'stream';
import { IncomingMessage, ClientRequest } from 'http';
import 'longjohn';

nock.disableNetConnect();

describe('Make a request', () => {
  afterEach(() => { nock.cleanAll(); });
  let clock: lolex.InstalledClock;
  beforeEach(() => clock = lolex.install());
  afterEach(() => clock.uninstall());

  describe('with callback', () => {
    it('Gives contents of page', (done) => {
      const scope = nock('http://website.com')
        .get('/path')
        .replyWithFile(200, __filename);
      miniget('http://website.com/path', (err, res, body) => {
        assert.ifError(err);
        scope.done();
        assert.equal(res.statusCode, 200);
        assert.ok(body.length > 100);
        done();
      });
    });

    describe('with options', () => {
      it('Makes request with options', (done) => {
        const scope = nock('http://website.com', {
          reqheaders: { 'User-Agent': 'miniget' },
        })
          .get('/path')
          .replyWithFile(200, __filename);
        miniget('http://website.com/path', {
          headers: { 'User-Agent': 'miniget' },
        }, (err, res, body) => {
          assert.ifError(err);
          scope.done();
          assert.equal(res.statusCode, 200);
          assert.ok(body.length > 100);
          done();
        });
      });
    });

    describe('that errors', () => {
      it('Calls callback with error', (done) => {
        const scope = nock('https://mysite.com')
          .get('/path')
          .replyWithError('ENOTFOUND');
        miniget('https://mysite.com/path', { maxRetries: 0 }, (err) => {
          assert.ok(err);
          assert.equal(err.message, 'ENOTFOUND');
          scope.done();
          done();
        });
      });
    });

    describe('with bad path', () => {
      it('Calls callback with error', (done) => {
        const scope = nock('https://mysite.com')
          .get('/badpath')
          .reply(404, 'not exists');
        miniget('https://mysite.com/badpath', (err) => {
          assert.ok(err);
          scope.done();
          done();
        });
      });
    });
  });

  describe('with a promise', () => {
    it('Gives contents of page', async () => {
      const scope = nock('http://webby.com')
        .get('/pathos')
        .replyWithFile(200, __filename);
      let [res, body] = await miniget.promise('http://webby.com/pathos');
      scope.done();
      assert.equal(res.statusCode, 200);
      assert.ok(body.length > 100);
    });

    describe('that errors', () => {
      it('error is caught', async () => {
        const scope = nock('http://something.com')
          .get('/one/two/three')
          .replyWithError('NONONO');
        await assert.rejects(
          miniget.promise('http://something.com/one/two/three', { maxRetries: 0 }),
          null, 'NONONO'
        );
        scope.done();
      });
    });
  });

  describe('that errors', () => {
    it('Emits error event', (done) => {
      const scope = nock('https://mysite.com')
        .get('/path')
        .replyWithError('ENOTFOUND')
        .get('/path')
        .replyWithError('ENOTFOUND')
        .get('/path')
        .reply(500, 'oh no 3');
      const stream = miniget('https://mysite.com/path');
      stream.on('request', () => { clock.tick(1); });
      stream.on('retry', (retryCount) => {
        clock.tick(retryCount * 100);
      });
      stream.on('error', (err) => {
        assert.equal(err.message, 'Status code: 500');
        scope.done();
        done();
      });
    });

    describe('without retries', () => {
      it('Emits error event', (done) => {
        const scope = nock('https://mysite.com')
          .get('/path')
          .replyWithError('oh no 1');
        const stream = miniget('https://mysite.com/path', { maxRetries: 0 });
        stream.on('error', (err) => {
          assert.equal(err.message, 'oh no 1');
          scope.done();
          done();
        });
      });
    });
  });

  describe('using https protocol', () => {
    it('Uses the https module', (done) => {
      const scope = nock('https://secureplace.net')
        .get('/')
        .reply(200);
      const stream = miniget('https://secureplace.net');
      stream.on('error', done);
      stream.on('end', () => {
        scope.done();
        done();
      });
      stream.resume();
    });
  });

  describe('without callback', () => {
    it('Returns a stream', (done) => {
      const scope = nock('http://website.com')
        .get('/path')
        .replyWithFile(200, __filename);
      const stream = miniget('http://website.com/path');
      stream.on('error', done);
      stream.on('response', (res) => {
        res.on('error', done);
        res.on('end', () => {
          scope.done();
          done();
        });
        res.resume();
      });
    });
  });

  describe('with an incorrect URL', () => {
    describe('with callback', () => {
      it('Called with error', (done) => {
        miniget('file:///Users/roly/', (err) => {
          assert.ok(err);
          done();
        });
      });
    });

    describe('without callback', () => {
      it('Throws error', (done) => {
        miniget('file:///Users/roly/').on('error', (err) => {
          assert.ok(err);
          done();
        });
      });
    });
  });

  describe('that redirects', () => {
    it('Should download file after redirect', (done) => {
      const scope = nock('http://mysite.com')
        .get('/pathy')<commit_msg>test: reorder tests<commit_after>import fs from 'fs';
import path from 'path';
import zlib from 'zlib';
import assert from 'assert';
import nock from 'nock';
import lolex from 'lolex';
import streamEqual from 'stream-equal';
import miniget from '../dist';
import { Transform } from 'stream';
import { IncomingMessage, ClientRequest } from 'http';
import 'longjohn';

nock.disableNetConnect();

describe('Make a request', () => {
  afterEach(() => { nock.cleanAll(); });
  let clock: lolex.InstalledClock;
  beforeEach(() => clock = lolex.install());
  afterEach(() => clock.uninstall());

  describe('with callback', () => {
    it('Gives contents of page', (done) => {
      const scope = nock('http://website.com')
        .get('/path')
        .replyWithFile(200, __filename);
      miniget('http://website.com/path', (err, res, body) => {
        assert.ifError(err);
        scope.done();
        assert.equal(res.statusCode, 200);
        assert.ok(body.length > 100);
        done();
      });
    });

    describe('with options', () => {
      it('Makes request with options', (done) => {
        const scope = nock('http://website.com', {
          reqheaders: { 'User-Agent': 'miniget' },
        })
          .get('/path')
          .replyWithFile(200, __filename);
        miniget('http://website.com/path', {
          headers: { 'User-Agent': 'miniget' },
        }, (err, res, body) => {
          assert.ifError(err);
          scope.done();
          assert.equal(res.statusCode, 200);
          assert.ok(body.length > 100);
          done();
        });
      });
    });

    describe('that errors', () => {
      it('Calls callback with error', (done) => {
        const scope = nock('https://mysite.com')
          .get('/path')
          .replyWithError('ENOTFOUND');
        miniget('https://mysite.com/path', { maxRetries: 0 }, (err) => {
          assert.ok(err);
          assert.equal(err.message, 'ENOTFOUND');
          scope.done();
          done();
        });
      });
    });

    describe('with bad path', () => {
      it('Calls callback with error', (done) => {
        const scope = nock('https://mysite.com')
          .get('/badpath')
          .reply(404, 'not exists');
        miniget('https://mysite.com/badpath', (err) => {
          assert.ok(err);
          scope.done();
          done();
        });
      });
    });
  });

  describe('with a promise', () => {
    it('Gives contents of page', async () => {
      const scope = nock('http://webby.com')
        .get('/pathos')
        .replyWithFile(200, __filename);
      let [res, body] = await miniget.promise('http://webby.com/pathos');
      scope.done();
      assert.equal(res.statusCode, 200);
      assert.ok(body.length > 100);
    });

    describe('that errors', () => {
      it('error is caught', async () => {
        const scope = nock('http://something.com')
          .get('/one/two/three')
          .replyWithError('NONONO');
        await assert.rejects(
          miniget.promise('http://something.com/one/two/three', { maxRetries: 0 }),
          null, 'NONONO'
        );
        scope.done();
      });
    });
  });

  describe('without callback', () => {
    it('Returns a stream', (done) => {
      const scope = nock('http://website.com')
        .get('/path')
        .replyWithFile(200, __filename);
      const stream = miniget('http://website.com/path');
      stream.on('error', done);
      stream.on('response', (res) => {
        res.on('error', done);
        res.on('end', () => {
          scope.done();
          done();
        });
        res.resume();
      });
    });
  });

  describe('that errors', () => {
    it('Emits error event', (done) => {
      const scope = nock('https://mysite.com')
        .get('/path')
        .replyWithError('ENOTFOUND')
        .get('/path')
        .replyWithError('ENOTFOUND')
        .get('/path')
        .reply(500, 'oh no 3');
      const stream = miniget('https://mysite.com/path');
      stream.on('request', () => { clock.tick(1); });
      stream.on('retry', (retryCount) => {
        clock.tick(retryCount * 100);
      });
      stream.on('error', (err) => {
        assert.equal(err.message, 'Status code: 500');
        scope.done();
        done();
      });
    });

    describe('without retries', () => {
      it('Emits error event', (done) => {
        const scope = nock('https://mysite.com')
          .get('/path')
          .replyWithError('oh no 1');
        const stream = miniget('https://mysite.com/path', { maxRetries: 0 });
        stream.on('error', (err) => {
          assert.equal(err.message, 'oh no 1');
          scope.done();
          done();
        });
      });
    });
  });

  describe('using https protocol', () => {
    it('Uses the https module', (done) => {
      const scope = nock('https://secureplace.net')
        .get('/')
        .reply(200);
      const stream = miniget('https://secureplace.net');
      stream.on('error', done);
      stream.on('end', () => {
        scope.done();
        done();
      });
      stream.resume();
    });
  });

  describe('with an incorrect URL', () => {
    it('Throws error', (done) => {
      miniget('file:///path/to/file/').on('error', (err) => {
        assert.ok(err);
        assert.equal(err.message, 'Invalid URL: file:///path/to/file/');
        done();
      });
    });
  });

  describe('that redirects', () => {
    it('Should download file after redirect', (done) => {
      const scope = nock('http://mysite.com')
        .get('/pathy')
{'fraction_non_alphanumeric': 0.20444642063139173, 'fraction_numerical': 0.009248554913294798, 'mean_word_length': 2.063470444020703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 93, 'https://': 20, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2445417', 'n_tokens_mistral': 3600, 'n_tokens_neox': 3483, 'n_words': 669}