-
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.
Breaking: Make
rawResponse
a Promise<Buffer>
With `debugging protocol` connectors we need to do another request to get the raw bytes of the response. With this change `sonar` will only do the extra request when is needed, caching the result for later use. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Fix #164 Close #534
- Loading branch information
Showing
8 changed files
with
227 additions
and
23 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
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
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
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,162 @@ | ||
/** | ||
* @fileoverview Minimum event functionality a connector must implement | ||
* in order to be valid. | ||
*/ | ||
|
||
/* eslint-disable no-sync */ | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as url from 'url'; | ||
import * as zlib from 'zlib'; | ||
|
||
import * as _ from 'lodash'; | ||
import * as sinon from 'sinon'; | ||
import test from 'ava'; | ||
|
||
import { builders } from '../../helpers/connectors'; | ||
import { createServer } from '../../helpers/test-server'; | ||
import { IConnector, IConnectorBuilder } from '../../../src/lib/types'; | ||
|
||
const sourceHtml = fs.readFileSync(path.join(__dirname, './fixtures/common/index.html'), 'utf8'); | ||
|
||
/** | ||
* Updates all references to localhost to use the right port for the current instance. | ||
* | ||
* This does a deep search in all the object properties. | ||
*/ | ||
const updateLocalhost = (content, port) => { | ||
if (typeof content === 'string') { | ||
return content.replace(/localhost\//g, `localhost:${port}/`); | ||
} | ||
|
||
if (typeof content === 'number' || !content) { | ||
return content; | ||
} | ||
|
||
if (Array.isArray(content)) { | ||
const transformed = _.map(content, (value) => { | ||
return updateLocalhost(value, port); | ||
}); | ||
|
||
return transformed; | ||
} | ||
|
||
const transformed = _.reduce(content, (obj, value, key) => { | ||
obj[key] = updateLocalhost(value, port); | ||
|
||
return obj; | ||
}, {}); | ||
|
||
return transformed; | ||
}; | ||
|
||
|
||
test.beforeEach(async (t) => { | ||
const sonar = { | ||
emit() { }, | ||
emitAsync() { } | ||
}; | ||
|
||
sinon.spy(sonar, 'emitAsync'); | ||
sinon.spy(sonar, 'emit'); | ||
|
||
const server = createServer(); | ||
|
||
await server.start(); | ||
|
||
const html = updateLocalhost(sourceHtml, server.port); | ||
const gzipHtml = zlib.gzipSync(Buffer.from(html)); | ||
|
||
t.context = { | ||
gzipHtml, | ||
html, | ||
server, | ||
sonar | ||
}; | ||
}); | ||
|
||
test.afterEach.always(async (t) => { | ||
t.context.sonar.emitAsync.restore(); | ||
t.context.sonar.emit.restore(); | ||
t.context.server.stop(); | ||
await t.context.connector.close(); | ||
}); | ||
|
||
const findEvent = (func, eventName) => { | ||
for (let i = 0; i < func.callCount; i++) { | ||
const args = func.getCall(i).args; | ||
|
||
if (args[0] === eventName) { | ||
return args[1]; | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const testRequestResponse = (connectorInfo) => { | ||
const connectorBuilder: IConnectorBuilder = connectorInfo.builder; | ||
const name: string = connectorInfo.name; | ||
|
||
test(`[${name}] requestResponse`, async (t) => { | ||
const { sonar } = t.context; | ||
const { emit, emitAsync } = sonar; | ||
const connector: IConnector = await (connectorBuilder)(sonar, {}); | ||
const server = t.context.server; | ||
|
||
t.context.connector = connector; | ||
|
||
server.configure({ | ||
'/': { | ||
content: t.context.gzipHtml, | ||
headers: { | ||
'content-encoding': 'gzip', | ||
'content-type': 'text/html' | ||
} | ||
} | ||
}); | ||
|
||
await connector.collect(url.parse(`http://localhost:${server.port}/`)); | ||
|
||
const invokedTargetFetchEnd = findEvent(emitAsync, 'targetfetch::end') || findEvent(emit, 'targetfetch::end'); | ||
/* eslint-disable sort-keys */ | ||
const expectedTargetFetchEnd = { | ||
resource: `http://localhost:${server.port}/`, | ||
request: { url: `http://localhost:${server.port}/` }, | ||
response: { | ||
body: { | ||
content: t.context.html, | ||
contentEncoding: 'utf-8', | ||
rawContent: Buffer.from(t.context.html), | ||
rawResponse() { | ||
return Promise.resolve(t.context.gzipHtml); | ||
} | ||
}, | ||
hops: [], | ||
statusCode: 200, | ||
url: 'http://localhost/' | ||
} | ||
}; | ||
/* eslint-enable sort-keys */ | ||
|
||
if (!invokedTargetFetchEnd) { | ||
t.fail(`targetfetch::end' event not found`); | ||
|
||
return; | ||
} | ||
|
||
const { body: invokedBody } = invokedTargetFetchEnd.response; | ||
const { body: expectedBody } = expectedTargetFetchEnd.response; | ||
const [invokedRawResponse, expectedRawResponse] = await Promise.all([invokedBody.rawResponse(), expectedBody.rawResponse()]); | ||
|
||
t.true(expectedRawResponse.equals(invokedRawResponse), 'rawResponses are different'); | ||
t.true(expectedBody.content === invokedBody.content, 'content is different'); | ||
t.true(expectedBody.contentEncoding === invokedBody.contentEncoding, 'content-encoding is different'); | ||
t.true(expectedBody.rawContent.equals(invokedBody.rawContent), 'rawContent is different'); | ||
}); | ||
}; | ||
|
||
builders.forEach((connector) => { | ||
testRequestResponse(connector); | ||
}); |
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