-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
150 lines (130 loc) · 3.65 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var HAR = require('har');
const HAR_VERSION = 1.2;
const MIME_TYPE = 'application/json; charset=utf-8';
const executions = [];
function getMimeType(members) {
for (const m of members) {
if (m.key === 'Content-Type') {
return m.value
}
}
return MIME_TYPE;
}
function createRequest(aRequest) {
var request = new HAR.Request({
method: aRequest.method,
url: aRequest.url.toString(),
postData: new HAR.PostData({
mimeType: getMimeType(aRequest.headers.members),
text: aRequest.body === undefined ? '' : aRequest.body.toString()
})
});
aRequest.url.query.members.forEach((member) => {
request.addQuery(new HAR.Query({
name: member.key,
value: member.value
}));
});
aRequest.headers.members.forEach((member) => {
if (member.hasOwnProperty("disabled") && member.disabled) {
return;
}
request.addHeader(new HAR.Header({
name: member.key,
value: member.key === 'Content-Length' ? member.value.toString() : member.value
}));
});
return request;
}
function createResponse(aResponse) {
if (aResponse === undefined || aResponse == null || (Object.keys(aResponse).length === 0 && aResponse.constructor === Object)) {
throw new Error('Server Not Found');
}
var response = new HAR.Response({
status: aResponse.code,
statusText: aResponse.status,
content: new HAR.PostData({
mimeType: getMimeType(aResponse.headers.members),
text: aResponse.text()
})
});
aResponse.cookies.members.forEach((member) => {
response.addCookie(new HAR.Cookie({
name: member.key,
value: member.value
}));
});
aResponse.headers.members.forEach((member) => {
response.addHeader(new HAR.Header({
name: member.key,
value: member.key === 'Content-Length' ? member.value.toString() : member.value
}));
});
return response;
}
function createHar(summary) {
var log = new HAR.Log({
version: HAR_VERSION,
creator: new HAR.Creator({
name: summary.collection.name,
version: summary.collection.id
})
});
executions.forEach((execution) => {
log.addEntry(new HAR.Entry({
startedDateTime: execution.startedDateTime,
request: createRequest(execution.request),
response: createResponse(execution.response),
time: execution.response.responseTime,
timings: {
blocked : 0,
connect: 0,
dns: 0,
receive: 0,
send: 0,
wait: execution.response.responseTime
}
}));
});
return { log: log };
}
function replacer(key, value) {
return (key === '_request' || key === '_response') ? undefined : value;
}
/**
* Reporter that outputs a HAR fle (default: newman-run-har-report.har).
*
* @param {Object} newman - The collection run object, with event hooks for reporting run details.
* @param {Object} options - A set of collection run options.
* @param {String} options.export - The path to which the summary object must be written.
* @returns {*}
*/
module.exports = function (newman, options) {
newman.on('request', (err, data) => {
if (err) { return; }
try {
executions.push({
startedDateTime: new Date().toISOString(),
request: data.request,
response: data.response
})
} catch (e) {
console.error(e);
return;
}
});
newman.on('beforeDone', function (err, data) {
if (err) { return; }
try {
newman.exports.push({
name: 'newman-reporter-har',
default: 'newman-report.har',
path: options.export,
content: JSON.stringify(createHar(data.summary), replacer, 2)
});
} catch (e) {
console.error(e);
return;
}
});
};