-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathschemaRequestHandler.test.ts
More file actions
152 lines (132 loc) · 5.53 KB
/
schemaRequestHandler.test.ts
File metadata and controls
152 lines (132 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { schemaRequestHandler } from '../src/languageservice/services/schemaRequestHandler';
import * as sinon from 'sinon';
import * as request from 'request-light';
import { XHRResponse } from 'request-light';
import { Connection } from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';
const expect = chai.expect;
chai.use(sinonChai);
import { testFileSystem } from './utils/testHelper';
describe('Schema Request Handler Tests', () => {
describe('schemaRequestHandler', () => {
const sandbox = sinon.createSandbox();
let readFileStub: sinon.SinonStub;
beforeEach(() => {
readFileStub = sandbox.stub(testFileSystem, 'readFile');
readFileStub.returns(Promise.resolve('{some: "json"}'));
});
afterEach(() => {
sandbox.restore();
});
it('Should care Win URI', async () => {
const connection = {} as Connection;
const resultPromise = schemaRequestHandler(
connection,
'c:\\some\\window\\path\\scheme.json',
[],
URI.parse(''),
false,
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 = {} as Connection;
const resultPromise = schemaRequestHandler(connection, '/some/unix/path/', [], URI.parse(''), false, testFileSystem, false);
const result = await resultPromise;
expect(result).to.be.equal('{some: "json"}');
});
it('should handle not valid Windows path', async () => {
const connection = {} as Connection;
const resultPromise = schemaRequestHandler(
connection,
'A:/some/window/path/scheme.json',
[],
URI.parse(''),
false,
testFileSystem,
false
);
expect(readFileStub).calledOnceWith(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: sinon.SinonStub;
const connection = {} as Connection;
beforeEach(() => {
xhrStub = sandbox.stub(request, 'xhr');
xhrStub.resolves({ responseText: '{"$schema":"http://json-schema.org/draft-07/schema"}', status: 200 } as XHRResponse);
});
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 schemaRequestHandler(connection, 'https://example.com/schema.json', [], URI.parse(''), false, 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 schemaRequestHandler(connection, 'https://example.com/schema.json', [], URI.parse(''), false, 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 schemaRequestHandler(connection, 'http://example.com/schema.json', [], URI.parse(''), false, 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 schemaRequestHandler(connection, 'https://example.com/schema.json', [], URI.parse(''), false, 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 schemaRequestHandler(
connection,
'https://example.com/schema.json',
[],
URI.parse(''),
false,
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 } as XHRResponse);
try {
await schemaRequestHandler(
connection,
'https://example.com/schema.json',
[],
URI.parse(''),
false,
testFileSystem,
false
);
expect.fail('Expected promise to be rejected');
} catch (err) {
expect(err).to.equal('Not Found');
}
});
});
});