"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const core_utils_1 = require("../../../shared/core-utils");
const requests_1 = require("../../helpers/requests");
const shared_1 = require("../shared");
describe('Request helpers', function () {
    const destPath1 = (0, path_1.join)((0, core_utils_1.root)(), 'test-output-1.txt');
    const destPath2 = (0, path_1.join)((0, core_utils_1.root)(), 'test-output-2.txt');
    it('Should throw an error when the bytes limit is exceeded for request', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                yield (0, requests_1.doRequest)(shared_1.FIXTURE_URLS.file4K, { bodyKBLimit: 3 });
            }
            catch (_a) {
                return;
            }
            throw new Error('No error thrown by do request');
        });
    });
    it('Should throw an error when the bytes limit is exceeded for request and save file', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                yield (0, requests_1.doRequestAndSaveToFile)(shared_1.FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 });
            }
            catch (_a) {
                yield (0, core_utils_1.wait)(500);
                (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)(destPath1)).to.be.false;
                return;
            }
            throw new Error('No error thrown by do request and save to file');
        });
    });
    it('Should correctly retry on 429 error', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(25000);
            const mock = new shared_1.Mock429();
            const port = yield mock.initialize();
            const before = new Date().getTime();
            yield (0, requests_1.doRequest)('http://127.0.0.1:' + port);
            (0, chai_1.expect)(new Date().getTime() - before).to.be.greaterThan(2000);
            yield mock.terminate();
        });
    });
    it('Should succeed if the file is below the limit', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, requests_1.doRequest)(shared_1.FIXTURE_URLS.file4K, { bodyKBLimit: 5 });
            yield (0, requests_1.doRequestAndSaveToFile)(shared_1.FIXTURE_URLS.file4K, destPath2, { bodyKBLimit: 5 });
            (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)(destPath2)).to.be.true;
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, fs_extra_1.remove)(destPath1);
            yield (0, fs_extra_1.remove)(destPath2);
        });
    });
});
//# sourceMappingURL=request.js.map