Skip to content

Commit

Permalink
should log concurrent requests (closes #7977)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKamaev committed Sep 4, 2023
1 parent 2058322 commit c816af2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/api/request-hooks/request-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class RequestLoggerImplementation extends RequestHook {
throw new APIError('RequestLogger', RUNTIME_ERRORS.requestHookConfigureAPIError, 'RequestLogger', 'Cannot stringify the response body because it is not logged. Specify { logResponseBody: true } in log options.');
}

private static _getInternalRequestKey ({ requestId, sessionId }: any): string {
return `${sessionId}:${requestId}`;
}

public async onRequest (event: RequestEvent): Promise<void> {
const loggedReq: LoggedRequest = {
id: event._requestInfo.requestId,
Expand All @@ -98,11 +102,11 @@ class RequestLoggerImplementation extends RequestHook {
if (this._options.logRequestBody)
loggedReq.request.body = this._options.stringifyRequestBody ? event._requestInfo.body.toString() : event._requestInfo.body;

this._internalRequests[loggedReq.id] = loggedReq;
this._internalRequests[RequestLoggerImplementation._getInternalRequestKey(event._requestInfo)] = loggedReq;
}

public async onResponse (event: ResponseEvent): Promise<void> {
const loggedReq = this._internalRequests[event.requestId];
const loggedReq = this._internalRequests[RequestLoggerImplementation._getInternalRequestKey(event)];

// NOTE: If the 'clear' method is called during a long running request,
// we should not save a response part - request part has been already removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class RequestPausedEventBasedEventFactory extends BaseRequestHook
statusCode: this._event.responseStatusCode || StatusCodes.OK,
headers: convertToOutgoingHttpHeaders(this._event.responseHeaders),
body: this._responseBody,
sessionId: '',
sessionId: this._sessionId,
requestId: this._event.requestId,
isSameOriginPolicyFailed: false,
});
Expand Down
19 changes: 19 additions & 0 deletions test/functional/fixtures/regression/gh-7977/pages/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>gh-7977</title>
</head>
<body>
<button>click me</button>
<script>
function handler () {
for (let i = 0; i < 100; i++) {
fetch('http://localhost:3000/api/data');
}
}

document.querySelector('button').addEventListener('click', handler);
</script>
</body>
</html>
30 changes: 30 additions & 0 deletions test/functional/fixtures/regression/gh-7977/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const createTestCafe = require('../../../../../lib');
const { onlyInNativeAutomation } = require('../../../utils/skip-in');
const path = require('path');
const expect = require('chai').expect;

const EXPECTED_REQUEST_COUNT = 300;

let testcafe = null;

describe('[Regression](GH-7977)', function () {
onlyInNativeAutomation('Should log all concurrent requests', function () {
return createTestCafe('127.0.0.1', 1335, 1336)
.then(tc => {
testcafe = tc;
})
.then(() => {
return testcafe.createRunner()
.browsers(`chrome:headless`)
.concurrency(3)
.src(path.join(__dirname, './testcafe-fixtures/index.js'))
.run();
})
.then(() => {
return testcafe.close();
})
.then(() => {
expect(require('./testcafe-fixtures/requestCounter').get()).eql(EXPECTED_REQUEST_COUNT);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { RequestLogger } from 'testcafe';

const logger = new RequestLogger(/api\/data/, {
logResponseBody: true,
logRequestHeaders: true,
stringifyResponseBody: true,
});

fixture `Concurrent request loggers`
.page `http://localhost:3000/fixtures/regression/gh-7977/pages/index.html`
.requestHooks(logger);

for (let i = 0; i < 3; i++) {
test(`send multiple requests - ${i}`, async t => {
await t.click('button');

require('./requestCounter').add(logger.requests.filter(r => !!r.response.body).length);
});
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let count = 0;

const requestCounter = {
add (value) {
count += value;
},
get () {
return count;
},
};

module.exports = requestCounter;

0 comments on commit c816af2

Please sign in to comment.