-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmockproxy.js
49 lines (41 loc) · 1.27 KB
/
mockproxy.js
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
'use strict';
const { write, listen, shutdown } = require("./proxyfunctions");
const CHALLENGE_LENGTH = 512;
class MockProxy {
constructor(mockConfig) {
if (!mockConfig) {
throw new Error("Missing mock config");
}
this.mockConfig = mockConfig;
this.dataSentToRemote = [];
}
async start(listenPort, tlsOptions = undefined) {
await listen(this, listenPort, async data => {
console.info(`received from client: ${data}`);
if (this.mockConfig.assertions) {
this.dataSentToRemote.push(data.toString());
}
if (this.mockConfig.auth && !this.hasSentChallenge) {
await write(this.client, mockChallenge());
this.hasSentChallenge = true;
}
}, tlsOptions);
}
async stop() {
await shutdown(this);
}
getDataSentToRemote() {
if (!this.mockConfig.assertions) {
throw new Error("Should be called only when assertions switched on");
}
return this.dataSentToRemote;
}
}
function mockChallenge() {
let challenge = "";
for (let i = 0; i < CHALLENGE_LENGTH - 1; i++) {
challenge += 'a';
}
return challenge + '\n';
}
exports.MockProxy = MockProxy;