forked from alexfernandez/loadtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseClient.js
66 lines (61 loc) · 1.68 KB
/
baseClient.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
import * as urlLib from 'url'
import {addUserAgent} from './headers.js'
export class BaseClient {
constructor(operation, params) {
this.operation = operation;
this.params = params;
this.generateMessage = undefined;
}
/**
* Get a function that finishes one request and goes for the next.
*/
getRequestFinisher(id) {
return (error, result) => {
let errorCode = null;
if (error) {
if (result) {
errorCode = result;
} else {
errorCode = '-1';
}
}
this.operation.latency.end(id, errorCode);
let callback;
if (!this.params.requestsPerSecond) {
callback = () => this.makeRequest();
}
this.operation.callback(error, result, callback);
};
}
/**
* Init options and message to send.
*/
init() {
this.options = urlLib.parse(this.params.url);
this.options.headers = {};
if (this.params.headers) {
this.options.headers = this.params.headers;
}
if (this.params.cert && this.params.key) {
this.options.cert = this.params.cert;
this.options.key = this.params.key;
}
this.options.agent = false;
if (this.params.body) {
if (typeof this.params.body == 'string') {
this.generateMessage = () => this.params.body;
} else if (typeof this.params.body == 'object') {
this.generateMessage = () => this.params.body;
} else if (typeof this.params.body == 'function') {
this.generateMessage = this.params.body;
} else {
console.error('Unrecognized body: %s', typeof this.params.body);
}
this.options.headers['Content-Type'] = this.params.contentType || 'text/plain';
}
addUserAgent(this.options.headers);
if (this.params.secureProtocol) {
this.options.secureProtocol = this.params.secureProtocol;
}
}
}