Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle 404 errors when capturing redirected assets #354

Merged
merged 2 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions src/services/response-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions test/integration/agent-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down