|
5 | 5 |
|
6 | 6 | import { schemaRequestHandler } from '../src/languageservice/services/schemaRequestHandler'; |
7 | 7 | import * as sinon from 'sinon'; |
| 8 | +import * as request from 'request-light'; |
| 9 | +import { XHRResponse } from 'request-light'; |
8 | 10 | import { Connection } from 'vscode-languageserver'; |
9 | 11 | import { URI } from 'vscode-uri'; |
10 | 12 | import * as chai from 'chai'; |
@@ -66,4 +68,85 @@ describe('Schema Request Handler Tests', () => { |
66 | 68 | expect(result).to.be.equal('{some: "json"}'); |
67 | 69 | }); |
68 | 70 | }); |
| 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 | + }); |
69 | 152 | }); |
0 commit comments