Skip to content

Commit fb909cb

Browse files
committed
test: add unit tests for User-Agent header on HTTP(S) schema fetch
Cover version from env var, 'unknown' fallback, http:// parity, Accept-Encoding preservation, successful response handling, and xhr error rejection.
1 parent 86a81e1 commit fb909cb

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

test/schemaRequestHandler.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import { schemaRequestHandler } from '../src/languageservice/services/schemaRequestHandler';
77
import * as sinon from 'sinon';
8+
import * as request from 'request-light';
9+
import { XHRResponse } from 'request-light';
810
import { Connection } from 'vscode-languageserver';
911
import { URI } from 'vscode-uri';
1012
import * as chai from 'chai';
@@ -66,4 +68,85 @@ describe('Schema Request Handler Tests', () => {
6668
expect(result).to.be.equal('{some: "json"}');
6769
});
6870
});
71+
72+
describe('HTTP(S) schema requests', () => {
73+
const sandbox = sinon.createSandbox();
74+
let xhrStub: sinon.SinonStub;
75+
const connection = {} as Connection;
76+
77+
beforeEach(() => {
78+
xhrStub = sandbox.stub(request, 'xhr');
79+
xhrStub.resolves({ responseText: '{"$schema":"http://json-schema.org/draft-07/schema"}', status: 200 } as XHRResponse);
80+
});
81+
82+
afterEach(() => {
83+
sandbox.restore();
84+
delete process.env.YAML_LANGUAGE_SERVER_VERSION;
85+
});
86+
87+
it('should send correct User-Agent with version, Node runtime and platform', async () => {
88+
process.env.YAML_LANGUAGE_SERVER_VERSION = '1.0.0-test';
89+
await schemaRequestHandler(connection, 'https://example.com/schema.json', [], URI.parse(''), false, testFileSystem, false);
90+
91+
expect(xhrStub).calledOnce;
92+
const { headers } = xhrStub.firstCall.args[0];
93+
expect(headers['User-Agent']).to.equal(
94+
`yaml-language-server/1.0.0-test (RedHat) node/${process.versions.node} (${process.platform})`
95+
);
96+
});
97+
98+
it('should fall back to "unknown" version when YAML_LANGUAGE_SERVER_VERSION is not set', async () => {
99+
delete process.env.YAML_LANGUAGE_SERVER_VERSION;
100+
await schemaRequestHandler(connection, 'https://example.com/schema.json', [], URI.parse(''), false, testFileSystem, false);
101+
102+
const { headers } = xhrStub.firstCall.args[0];
103+
expect(headers['User-Agent']).to.match(/^yaml-language-server\/unknown \(RedHat\)/);
104+
});
105+
106+
it('should send User-Agent on http:// URIs as well as https://', async () => {
107+
process.env.YAML_LANGUAGE_SERVER_VERSION = '2.0.0';
108+
await schemaRequestHandler(connection, 'http://example.com/schema.json', [], URI.parse(''), false, testFileSystem, false);
109+
110+
const { headers } = xhrStub.firstCall.args[0];
111+
expect(headers['User-Agent']).to.match(/^yaml-language-server\/2\.0\.0 \(RedHat\)/);
112+
});
113+
114+
it('should preserve Accept-Encoding header alongside User-Agent', async () => {
115+
await schemaRequestHandler(connection, 'https://example.com/schema.json', [], URI.parse(''), false, testFileSystem, false);
116+
117+
const { headers } = xhrStub.firstCall.args[0];
118+
expect(headers['Accept-Encoding']).to.equal('gzip, deflate');
119+
});
120+
121+
it('should return the response text on success', async () => {
122+
const result = await schemaRequestHandler(
123+
connection,
124+
'https://example.com/schema.json',
125+
[],
126+
URI.parse(''),
127+
false,
128+
testFileSystem,
129+
false
130+
);
131+
expect(result).to.equal('{"$schema":"http://json-schema.org/draft-07/schema"}');
132+
});
133+
134+
it('should reject with responseText on xhr error', async () => {
135+
xhrStub.rejects({ responseText: 'Not Found', status: 404 } as XHRResponse);
136+
try {
137+
await schemaRequestHandler(
138+
connection,
139+
'https://example.com/schema.json',
140+
[],
141+
URI.parse(''),
142+
false,
143+
testFileSystem,
144+
false
145+
);
146+
expect.fail('Expected promise to be rejected');
147+
} catch (err) {
148+
expect(err).to.equal('Not Found');
149+
}
150+
});
151+
});
69152
});

0 commit comments

Comments
 (0)