Skip to content

Commit b2170a2

Browse files
author
Michael Dijkstra
authored
Capture redirect before page (#89)
* Capture redirect before page * Lint fix * Use vercel for test
1 parent 551d6c0 commit b2170a2

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

index.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ function addFromFirstRequest(page, params) {
3434
}
3535
}
3636

37+
function populateRedirectResponse(page, params, entries, options) {
38+
const previousEntry = entries.find(
39+
entry => entry._requestId === params.requestId
40+
);
41+
if (previousEntry) {
42+
previousEntry._requestId += 'r';
43+
populateEntryFromResponse(
44+
previousEntry,
45+
params.redirectResponse,
46+
page,
47+
options
48+
);
49+
} else {
50+
debug(
51+
`Couldn't find original request for redirect response: ${
52+
params.requestId
53+
}`
54+
);
55+
}
56+
}
57+
3758
module.exports = {
3859
harFromMessages(messages, options) {
3960
options = Object.assign({}, defaultOptions, options);
@@ -86,7 +107,15 @@ module.exports = {
86107
}
87108
entries = entries.concat(entriesWithoutPage);
88109
addFromFirstRequest(page, paramsWithoutPage[0]);
110+
111+
// Add unmapped redirects
112+
for (let params of paramsWithoutPage) {
113+
if (params.redirectResponse) {
114+
populateRedirectResponse(page, params, entries, options);
115+
}
116+
}
89117
}
118+
90119
if (responsesWithoutPage.length > 0) {
91120
for (let params of responsesWithoutPage) {
92121
let entry = entries.find(
@@ -188,24 +217,7 @@ module.exports = {
188217
}
189218

190219
if (params.redirectResponse) {
191-
const previousEntry = entries.find(
192-
entry => entry._requestId === params.requestId
193-
);
194-
if (previousEntry) {
195-
previousEntry._requestId += 'r';
196-
populateEntryFromResponse(
197-
previousEntry,
198-
params.redirectResponse,
199-
page,
200-
options
201-
);
202-
} else {
203-
debug(
204-
`Couldn't find original request for redirect response: ${
205-
params.requestId
206-
}`
207-
);
208-
}
220+
populateRedirectResponse(page, params, entries, options);
209221
}
210222

211223
if (!page) {

test/perflogs/www.vercel.com.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

test/tests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,12 @@ test('Excludes response blocked cookies', t => {
252252
t.is(request.response.cookies.length, 1);
253253
});
254254
});
255+
256+
test('Includes initial redirect', t => {
257+
const perflogPath = perflog('www.vercel.com.json');
258+
return parsePerflog(perflogPath)
259+
.then(har => har.log)
260+
.tap(log => t.is(log.pages.length, 1))
261+
.tap(log => t.is(log.entries.length, 99))
262+
.tap(log => t.is(log.entries[0].response.status, 308));
263+
});

0 commit comments

Comments
 (0)