"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const CdmHttpClient_1 = require("../../Utilities/Network/CdmHttpClient");
const CdmHttpRequest_1 = require("../../Utilities/Network/CdmHttpRequest");
describe('Network/CdmHttpClientTest', () => {
    /**
     * Testing for a successful return of a result from Cdm Http Client.
     */
    it('Cdm Http client returns the result.', () => __awaiter(void 0, void 0, void 0, function* () {
        const githubUrl = 'https://raw.githubusercontent.com/';
        const corpusPath = '/Microsoft/CDM/master/schemaDocuments/foundations.cdm.json';
        const client = new CdmHttpClient_1.CdmHttpClient(githubUrl);
        const request = new CdmHttpRequest_1.CdmHttpRequest(corpusPath, 1, 'GET');
        request.timeout = 5000;
        request.maximumTimeout = 10000;
        const dict = new Map();
        // tslint:disable-next-line: no-backbone-get-set-outside-model
        dict.set('User-Agent', 'CDM');
        request.headers = dict;
        const response = yield client.SendAsync(request, callback);
        expect(response.content)
            .toBeDefined();
    }));
    /**
     * Testing for a failed return of a result back from Cdm Http Client.
     */
    it('Cdm Http client returns the error back.', () => __awaiter(void 0, void 0, void 0, function* () {
        const githubUrl = 'https://raw.githubusercontent2.com/';
        const corpusPath = '/Microsoft/CDM/master/schemaDocuments/foundations.cdm.json';
        const client = new CdmHttpClient_1.CdmHttpClient(githubUrl);
        const request = new CdmHttpRequest_1.CdmHttpRequest(corpusPath, 1, 'GET');
        request.timeout = 5000;
        request.maximumTimeout = 10000;
        const dict = new Map();
        // tslint:disable-next-line: no-backbone-get-set-outside-model
        dict.set('User-Agent', 'CDM');
        request.headers = dict;
        try {
            yield client.SendAsync(request, callback);
        }
        catch (err) {
            expect(err)
                .toBe('The number of retries has exceeded the maximum number allowed by the client.');
        }
    }));
});
function callback(response, hasFailed, retryNumber) {
    if (response !== undefined && response.isSuccessful && !hasFailed) {
        return undefined;
    }
    else {
        const upperBound = 1 << retryNumber;
        // Default wait time is calculated using exponential backoff with with random jitter value to avoid 'waves'.
        return Math.floor(Math.random() * upperBound) * 500;
    }
}
//# sourceMappingURL=CdmHttpClient.test.js.map