Skip to content

Commit

Permalink
🐛 Pass auth headers when requesting assets directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Robdel12 committed Jun 30, 2022
1 parent 813db57 commit e0e8eee
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/src/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export function createRequestFinishedHandler(network, {

if (mimeType?.includes('font')) {
// font responses from the browser may not be properly encoded, so request them directly
body = await makeRequest(response.url, { buffer: true });
log.debug('Requesting asset directly');
body = await makeRequest(response.url, { buffer: true, headers: request.headers });
}

resource = createResource(url, body, mimeType, {
Expand Down
52 changes: 52 additions & 0 deletions packages/core/test/discovery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,58 @@ describe('Discovery', () => {
]));
});

it('captures fonts with valid auth credentials', async () => {
percy.loglevel('debug');

const fontAuthDOM = dedent`
<html>
<head>
<style>
@font-face { font-family: "test"; src: url("font-auth/font.woff") format("woff"); }
body { font-family: "test", "sans-serif"; }
</style>
</head>
<body>
<p>Hello Percy!<p>
${' '.repeat(1000)}
</body>
</html>
`;

server.reply('/font-auth/font.woff', ({ headers: { authorization } }) => {
if (authorization === 'Basic dGVzdDo=') {
return [200, 'font/woff', '<font>'];
} else {
return [401, {
'WWW-Authenticate': 'Basic',
'Content-Type': 'text/plain'
}, '401 Unauthorized'];
}
});

await percy.snapshot({
name: 'font auth snapshot',
url: 'http://localhost:8000/font-auth',
domSnapshot: fontAuthDOM,
discovery: {
requestHeaders: { Authorization: 'Basic dGVzdDo=' }
}
});

await percy.idle();

expect(logger.stderr).toContain(
'[percy:core:discovery] Requesting asset directly'
);
expect(captured[0]).toEqual(jasmine.arrayContaining([
jasmine.objectContaining({
attributes: jasmine.objectContaining({
'resource-url': 'http://localhost:8000/font-auth/font.woff'
})
})
]));
});

it('does not capture without auth credentials', async () => {
await percy.snapshot({
name: 'auth snapshot',
Expand Down

0 comments on commit e0e8eee

Please sign in to comment.