-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.js
44 lines (37 loc) · 979 Bytes
/
hooks.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
var hooks = require('hooks');
var fileName = "./secret-config.json";
var config;
hooks.beforeAll(function (transactions, done) {
console.log("Hello World");
try {
config = require(fileName);
} catch (err) {
config = {};
console.error("unable to read file '"+ fileName +"': ", err);
console.error("see secret-config-sample.json for an example");
}
done();
});
hooks.beforeEach(function (transaction) {
console.log("Bye World");
transaction.request.uri = setParameter(
transaction.request.uri,
'key',
config.API_KEY
);
transaction.fullPath = transaction.request.uri;
});
function setParameter(uri, name, value) {
var regex = new RegExp('('+name+'=).*?(&|$)');
if (uri.search(regex) > -1) {
uri = uri.replace(regex, '$1' + value + '$2');
} else {
var paramToAdd = name + '=' + value;
if(uri.indexOf('?') > -1){
uri += "&" + paramToAdd;
} else{
uri += "?" + paramToAdd;
}
}
return uri;
}