-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Avoid analyzing
/favicon.ico
multiple times
Fix #427
- Loading branch information
1 parent
941d439
commit fb2abc2
Showing
4 changed files
with
148 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* eslint-disable no-sync */ | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as url from 'url'; | ||
import * as sinon from 'sinon'; | ||
|
||
import test from 'ava'; | ||
|
||
import { builders } from '../../helpers/connectors'; | ||
import { createServer } from '../../helpers/test-server'; | ||
import { IConnector, IConnectorBuilder, INetworkData } from '../../../src/lib/types'; // eslint-disable-line no-unused-vars | ||
import { generateHTMLPage } from '../../helpers/misc'; | ||
import { delay } from '../../../src/lib/utils/misc'; | ||
|
||
test.beforeEach(async (t) => { | ||
const sonar = { | ||
emit() { }, | ||
emitAsync() { } | ||
}; | ||
|
||
const server = createServer(); | ||
|
||
await server.start(); | ||
|
||
sinon.spy(sonar, 'emit'); | ||
sinon.spy(sonar, 'emitAsync'); | ||
|
||
t.context = { | ||
server, | ||
sonar | ||
}; | ||
}); | ||
|
||
test.afterEach.always(async (t) => { | ||
t.context.server.stop(); | ||
await t.context.connector.close(); | ||
|
||
t.context.sonar.emit.restore(); | ||
t.context.sonar.emitAsync.restore(); | ||
|
||
// `CDP` throws ECONNREFUSED error when there is no delay. | ||
await delay(300); | ||
}); | ||
|
||
const testConnectorCollect = (connectorInfo) => { | ||
const connectorBuilder: IConnectorBuilder = connectorInfo.builder; | ||
const name: string = connectorInfo.name; | ||
|
||
test.serial(`[${name}] Favicon is present in a 'link' element with 'rel' attribute set to 'icon' `, async (t) => { | ||
const { sonar } = t.context; | ||
const connector: IConnector = await (connectorBuilder)(sonar, {}); | ||
const server = t.context.server; | ||
const faviconInRootDir = `http://localhost:${server.port}/images/favicon-32x32.png`; | ||
|
||
t.plan(2); | ||
t.context.connector = connector; | ||
|
||
server.configure({ | ||
'/': generateHTMLPage(`<link rel="icon" type="image/png" href="/images/favicon-32x32.png" sizes="32x32">`), | ||
'/images/favicon-favicon-32x32.png': fs.readFileSync(path.join(__dirname, './fixtures/common/favicon-32x32.png')) | ||
}); | ||
|
||
await connector.collect(url.parse(`http://localhost:${server.port}/`)); | ||
|
||
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').callCount, 1); | ||
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').args[0][1].request.url, faviconInRootDir); | ||
}); | ||
|
||
test.serial(`[${name}] Favicon is present in the root directory`, async (t) => { | ||
const { sonar } = t.context; | ||
const connector: IConnector = await (connectorBuilder)(sonar, {}); | ||
const server = t.context.server; | ||
const faviconInLinkElementDir = `http://localhost:${server.port}/favicon.ico`; | ||
|
||
t.plan(2); | ||
t.context.connector = connector; | ||
|
||
server.configure({ '/favicon.ico': fs.readFileSync(path.join(__dirname, './fixtures/common/favicon.ico')) }); | ||
|
||
await connector.collect(url.parse(`http://localhost:${server.port}/`)); | ||
|
||
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').callCount, 1); | ||
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').args[0][1].request.url, faviconInLinkElementDir); | ||
}); | ||
|
||
test.serial(`[${name}] Favicon is present in both the root directory and the 'link' element`, async (t) => { | ||
const { sonar } = t.context; | ||
const connector: IConnector = await (connectorBuilder)(sonar, {}); | ||
const server = t.context.server; | ||
const faviconInLinkElementDir = `http://localhost:${server.port}/images/favicon-32x32.png`; | ||
|
||
t.plan(2); | ||
t.context.connector = connector; | ||
|
||
server.configure({ | ||
'/': generateHTMLPage(`<link rel="icon" type="image/png" href="/images/favicon-32x32.png" sizes="32x32">`), | ||
'/favicon.ico': fs.readFileSync(path.join(__dirname, './fixtures/common/favicon.ico')), | ||
'/images/favicon-favicon-32x32.png': fs.readFileSync(path.join(__dirname, './fixtures/common/favicon-32x32.png')) | ||
}); | ||
|
||
await connector.collect(url.parse(`http://localhost:${server.port}/`)); | ||
|
||
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').callCount, 1); | ||
// Should load favicon from the link element if it exists | ||
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').args[0][1].request.url, faviconInLinkElementDir); | ||
}); | ||
|
||
test.serial(`[${name}] Favicon is not present in either the root directory or the 'link' element`, async (t) => { | ||
const { sonar } = t.context; | ||
const connector: IConnector = await (connectorBuilder)(sonar, {}); | ||
const server = t.context.server; | ||
const faviconInRootDir = `http://localhost:${server.port}/favicon.ico`; | ||
|
||
t.plan(2); | ||
t.context.connector = connector; | ||
|
||
await connector.collect(url.parse(`http://localhost:${server.port}/`)); | ||
|
||
// Requests to `/favicon.ico` are always sent when favicon doesn't exist as a `link` tag in the html. | ||
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').callCount, 1); | ||
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').args[0][1].request.url, faviconInRootDir); | ||
}); | ||
}; | ||
|
||
builders.forEach((connector) => { | ||
testConnectorCollect(connector); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.