Repository navigation
Expand file tree
/
Copy pathhttpClient.unit.test.ts
More file actions
122 lines (116 loc) · 5.34 KB
/
Copy pathhttpClient.unit.test.ts
File metadata and controls
122 lines (116 loc) · 5.34 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:no-any
import * as assert from 'assert';
import { expect } from 'chai';
// tslint:disable-next-line: match-default-export-name
import rewiremock from 'rewiremock';
import * as TypeMoq from 'typemoq';
import { WorkspaceConfiguration } from 'vscode';
import { IWorkspaceService } from '../../../client/common/application/types';
import { HttpClient } from '../../../client/common/net/httpClient';
import { IServiceContainer } from '../../../client/ioc/types';
// tslint:disable-next-line: max-func-body-length
suite('Http Client', () => {
const proxy = 'https://myproxy.net:4242';
let config: TypeMoq.IMock<WorkspaceConfiguration>;
let workSpaceService: TypeMoq.IMock<IWorkspaceService>;
let container: TypeMoq.IMock<IServiceContainer>;
let httpClient: HttpClient;
setup(() => {
container = TypeMoq.Mock.ofType<IServiceContainer>();
workSpaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
config = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
config
.setup(c => c.get(TypeMoq.It.isValue('proxy'), TypeMoq.It.isValue('')))
.returns(() => proxy)
.verifiable(TypeMoq.Times.once());
workSpaceService
.setup(w => w.getConfiguration(TypeMoq.It.isValue('http')))
.returns(() => config.object)
.verifiable(TypeMoq.Times.once());
container.setup(a => a.get(TypeMoq.It.isValue(IWorkspaceService))).returns(() => workSpaceService.object);
httpClient = new HttpClient(container.object);
});
test('Get proxy info', async () => {
expect(httpClient.requestOptions).to.deep.equal({ proxy: proxy });
config.verifyAll();
workSpaceService.verifyAll();
});
suite('Test getJSON()', async () => {
teardown(() => {
rewiremock.disable();
});
[
{
name: 'Throw error if request returns with download error',
returnedArgs: ['downloadError', { statusCode: 201 }, undefined],
expectedErrorMessage: 'downloadError'
},
{
name: 'Throw error if request does not return with status code 200',
returnedArgs: [undefined, { statusCode: 201, statusMessage: 'wrongStatus' }, undefined],
expectedErrorMessage: 'Failed with status 201, wrongStatus, Uri downloadUri'
},
{
name: 'If strict is set to true, and parsing fails, throw error',
returnedArgs: [undefined, { statusCode: 200 }, '[{ "strictJSON" : true,, }]'],
strict: true
}
].forEach(async testParams => {
test(testParams.name, async () => {
const requestMock = (_uri: any, _requestOptions: any, callBackFn: Function) => callBackFn(...testParams.returnedArgs);
rewiremock.enable();
rewiremock('request').with(requestMock);
let rejected = true;
try {
await httpClient.getJSON('downloadUri', testParams.strict);
rejected = false;
} catch (ex) {
if (testParams.expectedErrorMessage) {
// Compare error messages
if (ex.message) {
ex = ex.message;
}
expect(ex).to.equal(testParams.expectedErrorMessage, 'Promise rejected with the wrong error message');
}
}
assert(rejected === true, 'Promise should be rejected');
});
});
[
{
name: 'If strict is set to false, and jsonc parsing returns error codes, then log errors and don\'t throw, return json',
returnedArgs: [undefined, { statusCode: 200 }, '[{ "strictJSON" : false,, }]'],
strict: false,
expectedJSON: [{ strictJSON: false }]
},
{
name: 'Return expected json if strict is set to true and parsing is successful',
returnedArgs: [undefined, { statusCode: 200 }, '[{ "strictJSON" : true }]'],
strict: true,
expectedJSON: [{ strictJSON: true }]
},
{
name: 'Return expected json if strict is set to false and parsing is successful',
returnedArgs: [undefined, { statusCode: 200 }, '[{ //Comment \n "strictJSON" : false }]'],
strict: false,
expectedJSON: [{ strictJSON: false }]
}
].forEach(async testParams => {
test(testParams.name, async () => {
const requestMock = (_uri: any, _requestOptions: any, callBackFn: Function) => callBackFn(...testParams.returnedArgs);
rewiremock.enable();
rewiremock('request').with(requestMock);
let json;
try {
json = await httpClient.getJSON('downloadUri', testParams.strict);
} catch (ex) {
assert(false, 'Promise should not be rejected');
}
assert.deepEqual(json, testParams.expectedJSON, 'Unexpected JSON returned');
});
});
});
});