-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHARParser.js
More file actions
51 lines (44 loc) · 2.23 KB
/
HARParser.js
File metadata and controls
51 lines (44 loc) · 2.23 KB
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
define( ["handlebars", "lodash"],
function (Handlebars, _) {
return {
parse: function (testTemplate, harData) {
var compiledTemplate = Handlebars.compile(testTemplate),
isXHR = function (e) {
console.log(e.request.url + " isXHR? " + _.find(e.request.headers, function (h) {
return h.name === "X-Requested-With" && h.value === "XMLHttpRequest";
}) || e.request.url.match(/\.less$/))
return _.find(e.request.headers, function (h) {
return h.name === "X-Requested-With" && h.value === "XMLHttpRequest";
}) || e.request.url.match(/\.less$/);
},
isJSONResponse = function (e) {
var contentType = _.find(e.response.headers, function (h) {
return h.name === "Content-Type";
});
return contentType && contentType.value.match(/^application\/json/);
},
asTextContent = function (e) {
// put the response back in JSON so it will be safe to embed in the Javascript directly
e.response.content.text = JSON.stringify(e.response.content.text);
// add a flag to help with handling trailing commas
e.response.headers[e.response.headers.length-1].last = true;
e.request.url = e.request.url.replace(/https?:\/\/.*?\//i, '/');
return e;
};
harData.log.nonJSONEntries =
_(harData.log.entries)
.filter(isXHR)
.reject(isJSONResponse)
.map(asTextContent)
.value();
harData.log.JSONEntries =
_(harData.log.entries)
.filter(isXHR)
.filter(isJSONResponse)
.map(asTextContent)
.value();
return compiledTemplate(harData);
}
};
}
);