yaml-language-server
Version:
136 lines • 7.79 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const schemaRequestHandler_1 = require("../src/languageservice/services/schemaRequestHandler");
const sinon = __importStar(require("sinon"));
const request = __importStar(require("request-light"));
const vscode_uri_1 = require("vscode-uri");
const chai = __importStar(require("chai"));
const sinon_chai_1 = __importDefault(require("sinon-chai"));
const expect = chai.expect;
chai.use(sinon_chai_1.default);
const testHelper_1 = require("./utils/testHelper");
describe('Schema Request Handler Tests', () => {
describe('schemaRequestHandler', () => {
const sandbox = sinon.createSandbox();
let readFileStub;
beforeEach(() => {
readFileStub = sandbox.stub(testHelper_1.testFileSystem, 'readFile');
readFileStub.returns(Promise.resolve('{some: "json"}'));
});
afterEach(() => {
sandbox.restore();
});
it('Should care Win URI', async () => {
const connection = {};
const resultPromise = (0, schemaRequestHandler_1.schemaRequestHandler)(connection, 'c:\\some\\window\\path\\scheme.json', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
expect(readFileStub).calledOnceWith('c:\\some\\window\\path\\scheme.json');
const result = await resultPromise;
expect(result).to.be.equal('{some: "json"}');
});
it('UNIX URI should works', async () => {
const connection = {};
const resultPromise = (0, schemaRequestHandler_1.schemaRequestHandler)(connection, '/some/unix/path/', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
const result = await resultPromise;
expect(result).to.be.equal('{some: "json"}');
});
it('should handle not valid Windows path', async () => {
const connection = {};
const resultPromise = (0, schemaRequestHandler_1.schemaRequestHandler)(connection, 'A:/some/window/path/scheme.json', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
expect(readFileStub).calledOnceWith(vscode_uri_1.URI.file('a:/some/window/path/scheme.json').fsPath);
const result = await resultPromise;
expect(result).to.be.equal('{some: "json"}');
});
});
describe('HTTP(S) schema requests', () => {
const sandbox = sinon.createSandbox();
let xhrStub;
const connection = {};
beforeEach(() => {
xhrStub = sandbox.stub(request, 'xhr');
xhrStub.resolves({ responseText: '{"$schema":"http://json-schema.org/draft-07/schema"}', status: 200 });
});
afterEach(() => {
sandbox.restore();
delete process.env.YAML_LANGUAGE_SERVER_VERSION;
});
it('should send correct User-Agent with version, Node runtime and platform', async () => {
process.env.YAML_LANGUAGE_SERVER_VERSION = '1.0.0-test';
await (0, schemaRequestHandler_1.schemaRequestHandler)(connection, 'https://example.com/schema.json', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
expect(xhrStub).calledOnce;
const { headers } = xhrStub.firstCall.args[0];
expect(headers['User-Agent']).to.equal(`yaml-language-server/1.0.0-test (RedHat) node/${process.versions.node} (${process.platform})`);
});
it('should fall back to "unknown" version when YAML_LANGUAGE_SERVER_VERSION is not set', async () => {
delete process.env.YAML_LANGUAGE_SERVER_VERSION;
await (0, schemaRequestHandler_1.schemaRequestHandler)(connection, 'https://example.com/schema.json', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
const { headers } = xhrStub.firstCall.args[0];
expect(headers['User-Agent']).to.match(/^yaml-language-server\/unknown \(RedHat\)/);
});
it('should send User-Agent on http:// URIs as well as https://', async () => {
process.env.YAML_LANGUAGE_SERVER_VERSION = '2.0.0';
await (0, schemaRequestHandler_1.schemaRequestHandler)(connection, 'http://example.com/schema.json', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
const { headers } = xhrStub.firstCall.args[0];
expect(headers['User-Agent']).to.match(/^yaml-language-server\/2\.0\.0 \(RedHat\)/);
});
it('should preserve Accept-Encoding header alongside User-Agent', async () => {
await (0, schemaRequestHandler_1.schemaRequestHandler)(connection, 'https://example.com/schema.json', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
const { headers } = xhrStub.firstCall.args[0];
expect(headers['Accept-Encoding']).to.equal('gzip, deflate');
});
it('should return the response text on success', async () => {
const result = await (0, schemaRequestHandler_1.schemaRequestHandler)(connection, 'https://example.com/schema.json', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
expect(result).to.equal('{"$schema":"http://json-schema.org/draft-07/schema"}');
});
it('should reject with responseText on xhr error', async () => {
xhrStub.rejects({ responseText: 'Not Found', status: 404 });
try {
await (0, schemaRequestHandler_1.schemaRequestHandler)(connection, 'https://example.com/schema.json', [], vscode_uri_1.URI.parse(''), false, testHelper_1.testFileSystem, false);
expect.fail('Expected promise to be rejected');
}
catch (err) {
expect(err).to.equal('Not Found');
}
});
});
});
//# sourceMappingURL=schemaRequestHandler.test.js.map