diff --git a/src/services/response-service.ts b/src/services/response-service.ts index 70b3159f..63a9acd2 100644 --- a/src/services/response-service.ts +++ b/src/services/response-service.ts @@ -102,29 +102,36 @@ export default class ResponseService extends PercyClientService { async handleRedirectResouce(originalURL: string, redirectedURL: string, width: number, logger: any) { logger.debug(`Making local copy of redirected response: ${originalURL}`) - const { data, headers } = await Axios(originalURL, { responseType: 'arraybuffer' }) as any - const buffer = Buffer.from(data) - const sha = crypto.createHash('sha256').update(buffer).digest('hex') - const localCopy = path.join(os.tmpdir(), sha) - const didWriteFile = this.maybeWriteFile(localCopy, buffer) - const { fileIsTooLarge, responseBodySize } = this.checkFileSize(localCopy) + try { + const { data, headers } = await Axios(originalURL, { responseType: 'arraybuffer' }) as any + const buffer = Buffer.from(data) + const sha = crypto.createHash('sha256').update(buffer).digest('hex') + const localCopy = path.join(os.tmpdir(), sha) + const didWriteFile = this.maybeWriteFile(localCopy, buffer) + const { fileIsTooLarge, responseBodySize } = this.checkFileSize(localCopy) + + if (!didWriteFile) { + logger.debug(`Skipping file copy [already_copied]: ${originalURL}`) + } - if (!didWriteFile) { - logger.debug(`Skipping file copy [already_copied]: ${originalURL}`) - } + if (fileIsTooLarge) { + logger.debug(`Skipping [max_file_size_exceeded_${responseBodySize}] [${width} px]: ${originalURL}`) + return + } - if (fileIsTooLarge) { - logger.debug(`Skipping [max_file_size_exceeded_${responseBodySize}] [${width} px]: ${originalURL}`) - return - } + const contentType = headers['content-type'] as string + const resource = this.resourceService.createResourceFromFile(originalURL, localCopy, contentType, logger) - const contentType = headers['content-type'] as string - const resource = this.resourceService.createResourceFromFile(originalURL, localCopy, contentType, logger) + this.responsesProcessed.set(originalURL, resource) + this.responsesProcessed.set(redirectedURL, resource) - this.responsesProcessed.set(originalURL, resource) - this.responsesProcessed.set(redirectedURL, resource) + return resource + } catch (err) { + logger.debug(`${err}`) + logger.debug(`Failed to make a local copy of redirected response: ${originalURL}`) - return resource + return + } } /** diff --git a/test/integration/agent-integration.test.ts b/test/integration/agent-integration.test.ts index 855e0208..8d3fe747 100644 --- a/test/integration/agent-integration.test.ts +++ b/test/integration/agent-integration.test.ts @@ -90,10 +90,11 @@ describe('Integration test', () => { it('snapshots a site with redirected assets', async () => { await page.goto('https://sdk-test.percy.dev/redirects/') - await page.waitFor('.note') + await page.waitFor('h2') const domSnapshot = await snapshot(page, 'Redirects snapshot') - expect(domSnapshot).contains('correctly snapshotted') + // This will fail the test if the redirected JS fails to work + expect(domSnapshot).contains('Snapshot worked') }) it('falls back to default widths when nothing is passed', async () => {