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

should log concurrent requests (closes #7977) #7990

Merged
merged 1 commit into from
Sep 5, 2023
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
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;