-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.js
96 lines (85 loc) · 2.42 KB
/
mock.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
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
/**
* Mock module to test mail templates
* Module is required in development/test mode
* Test can be done by sending http post to the local server (port 45321)
* Post body must be application/json and variables "mailTemplate" and "mailVariables" set
*/
const http = require('http');
const port = 45321;
let senderFn;
//mock classes for testing
class _task{
constructor(){
this.variables = new _variables();
}
}
class _variables{
constructor(){
this.bKey = '';
this.formName = '';
this.outputName = '';
this.identifier = '';
this.transform = '';
}
get(name){
return this[name];
}
set(name, value){
this[name] = value;
}
getAll(){
return {
bKey: this.bKey,
formName: this.formName,
outputName: this.outputName,
identifier: this.identifier,
transform: this.transform
};
}
}
class _taskService{
constructor(){}
async complete(t, vars){
console.log(t);
console.log(vars);
}
async handleFailure(t, err){
console.error(t);
console.error(err);
}
}
function start(main){
senderFn = main;
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
return console.error('Error: ', err)
}
console.log(`mock server is listening on ${port}`)
});
}
const requestHandler = (request, response) => {
const { headers, method, url } = request;
if (request.method === 'POST') {
let body = '';
request.on('data', chunk => {
body += chunk.toString();
});
request.on('end', () => {
let content = JSON.parse(body);
console.log(content);
if(content){
let t = new _task("org.prodig.pdf");
t.variables.set('bKey', [...Array(8)].map(i=>(~~(Math.random()*36)).toString(36)).join('') + '-A');
t.variables.set('formName', content.formName);
t.variables.set('outputName', content.outputName);
t.variables.set('identifier', content.identifier);
t.variables.set('transform', content.transform);
let service = new _taskService();
senderFn({task: t, taskService: service});
}
response.end('ok');
});
}
};
module.exports = { start };