Skip to content

Commit

Permalink
fix: Handle 404 errors when capturing redirected assets (#354)
Browse files Browse the repository at this point in the history
* fix: Handle 404 errors when capturing redirected assets

* Destructure the response from axios
  • Loading branch information
Robdel12 authored Sep 25, 2019
1 parent 1e24e86 commit aefcd17
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
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

0 comments on commit aefcd17

Please sign in to comment.