forked from Mermade/widdershins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharGenerator.js
47 lines (40 loc) · 1.26 KB
/
harGenerator.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
'use strict';
function generate(data) {
const url = data.baseUrl && data.requiredUriExample
? `${data.baseUrl}${data.requiredUriExample}`
: data.url;
const request = {
httpVersion: 'HTTP/1.1',
method: data.methodUpper,
url,
queryString: getValues(data.queryParameters),
headers: getValues(data.allHeaders),
headersSize: -1,
cookies: []
};
if (data.bodyParameter && data.bodyParameter.present) {
request.bodySize = -1;
request.postData = {
mimeType: data.bodyParameter.contentType
};
if (request.postData.mimeType === 'multipart/form-data') {
request.postData.params = Object.keys(data.bodyParameter.exampleValues.object)
.map(field => {
return { name: field, value: data.bodyParameter.exampleValues.object[field] };
});
}
else {
request.postData.text = JSON.stringify(data.bodyParameter.exampleValues.object);
}
}
return request;
}
function getValues(items) {
return (items || [])
.map((item) => {
return { name: item.name, value: item.exampleValues.object.toString() };
});
}
module.exports = {
generate
};