forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebPanel.unit.test.ts
More file actions
149 lines (139 loc) · 6.38 KB
/
webPanel.unit.test.ts
File metadata and controls
149 lines (139 loc) · 6.38 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as chai from 'chai';
import * as http from 'http';
import * as path from 'path';
import * as portfinder from 'portfinder';
import * as uuid from 'uuid/v4';
// tslint:disable-next-line: no-var-requires no-require-imports
import chaiHttp = require('chai-http');
chai.use(chaiHttp);
import { WebPanelServer } from '../../../client/common/application/webPanels/webPanelServer';
import { FileSystem } from '../../../client/common/platform/fileSystem';
import { EXTENSION_ROOT_DIR } from '../../../client/constants';
// tslint:disable:no-any
// tslint:disable-next-line: max-func-body-length
suite('WebPanelServer', () => {
let host: WebPanelServer | undefined;
let server: http.Server | undefined;
const token = uuid();
const historyBundle = path.join(EXTENSION_ROOT_DIR, 'out', 'datascience-ui', 'history-react', 'index_bundle.js');
setup(async () => {
// So these are effectively functional tests rather than unit tests...
const fs = new FileSystem();
host = new WebPanelServer(await portfinder.getPortPromise(), token, fs);
server = host.start();
});
teardown(() => {
host?.dispose();
});
test('Server responds with html when given valid input', (done) => {
chai.request(server)
.get(
`/${uuid()}?token=${token}&scripts=${encodeURIComponent(
path.basename(historyBundle)
)}&rootPath=${encodeURIComponent(path.dirname(historyBundle))}`
)
.end((e, r) => {
// tslint:disable-next-line: no-unused-expression
chai.expect(e, 'Error is not null').to.be.null;
chai.expect(r, 'Response status is not 200').to.have.status(200);
chai.expect(r.text, 'Response does not have the script').to.include('index_bundle.js');
done();
});
});
test('Server responds with 404 when given invalid input', (done) => {
chai.request(server)
.get(
`/${uuid()}?scripts=${encodeURIComponent(path.basename(historyBundle))}&rootPath=${encodeURIComponent(
path.dirname(historyBundle)
)}`
)
.end((_e, r) => {
// tslint:disable-next-line: no-unused-expression
chai.expect(r, 'Response status is not 404').to.have.status(404);
done();
});
});
test('Server responds with 404 when given file not found', (done) => {
const agent = chai.request(server).keepOpen();
agent
.get(
`/${uuid()}?token=${token}&scripts=${encodeURIComponent(
path.basename(historyBundle)
)}&rootPath=${encodeURIComponent(path.dirname(historyBundle))}`
)
.end((_e, r) => {
// tslint:disable-next-line: no-unused-expression
chai.expect(r, 'Response status is not 200 on first request').to.have.status(200);
agent.get('/foobar.png').end((_e2, r2) => {
chai.expect(r2, 'Response status is not 404').to.have.status(404);
agent.close();
done();
});
});
});
test('Server can find the index_bundle', (done) => {
// See here for where the code for this comes from (you might think keepOpen is required, but instead request.agent is used for multiple requests)
// https://www.chaijs.com/plugins/chai-http/ and search 'Retaining cookies with each request'
const agent = chai.request.agent(server);
agent
.get(
`/${uuid()}?token=${token}&scripts=${encodeURIComponent(
path.basename(historyBundle)
)}&rootPath=${encodeURIComponent(path.dirname(historyBundle))}`
)
.end((_e, r) => {
// tslint:disable-next-line: no-unused-expression
chai.expect(r, 'Response status is not 200 on first request').to.have.status(200);
chai.expect(r, 'Response does not have a cookie').to.have.cookie('id');
return agent.get('/index_bundle.js').end((_e2, r2) => {
chai.expect(r2, 'Response status is not 200').to.have.status(200);
agent.close();
done();
});
});
});
test('Server can find the a file in a cwd', (done) => {
const agent = chai.request.agent(server);
agent
.get(
`/${uuid()}?token=${token}&scripts=${encodeURIComponent(
path.basename(historyBundle)
)}&rootPath=${encodeURIComponent(path.dirname(historyBundle))}&cwd=${encodeURIComponent(
EXTENSION_ROOT_DIR
)}`
)
.end((_e, r) => {
// tslint:disable-next-line: no-unused-expression
chai.expect(r, 'Response status is not 200 on first request').to.have.status(200);
chai.expect(r, 'Response does not have a cookie').to.have.cookie('id');
return agent.get('/package.json').end((_e2, r2) => {
chai.expect(r2, 'Response status is not 200').to.have.status(200);
agent.close();
done();
});
});
});
test('Server will skip a file not in the cwd', (done) => {
const agent = chai.request.agent(server);
agent
.get(
`/${uuid()}?token=${token}&scripts=${encodeURIComponent(
path.basename(historyBundle)
)}&rootPath=${encodeURIComponent(path.dirname(historyBundle))}&cwd=${encodeURIComponent(
EXTENSION_ROOT_DIR
)}`
)
.end((_e, r) => {
// tslint:disable-next-line: no-unused-expression
chai.expect(r, 'Response status is not 200 on first request').to.have.status(200);
chai.expect(r, 'Response does not have a cookie').to.have.cookie('id');
return agent.get('/package_missing.json').end((_e2, r2) => {
chai.expect(r2, 'Response status is not 404').to.have.status(404);
agent.close();
done();
});
});
});
});