I'm trying to testcafe RequestLogger and then get the api response logged. But it looks like I'm missing something, when the content-encoding is not specified.
It doesn't return anything in the 'else' section in the 'getLoggerBody' method, even after waiting for long.
Here's my code:
import { RequestLogger } from 'testcafe';
import { get } from "lodash";
import zlib from "zlib";
export const getLogger = (url, method = 'GET') => RequestLogger(url, { logResponseBody: true, logResponseHeaders: true });
export const getLoggerResponseBody = logger => {
const request = logger.requests[0];
const contentEncoding = get(request, ['response', 'headers', 'content-encoding']);
if (contentEncoding && contentEncoding === 'gzip') {
const unzippedBody = zlib.gunzipSync(request.response.body);
return JSON.parse(unzippedBody.toString());
} else {
return JSON.parse(request.response.body.toString());
}
};
The 'request.response.body' looks like a buffer as shown in the snippet below.
<Buffer 7b 22 6d 65 73 73 61 67 65 ... 4973 more bytes>
Could you help me fix this? What am I doing wrong here?
CodePudding user response:
I tried to use your simplified code on the simple website. It looks like everything works as expected. It's expected that the request.response.body
contains Buffer
content. Please take a look at the https://testcafe.io/documentation/402769/reference/test-api/requestlogger/constructor#header article and pay attention to the logResponseBody
and stringifyResponseBody
sections.
I prepared a sample which demonstrates that your approach is correct in general case. Please see it:
import { RequestLogger } from 'testcafe';
export const getLogger = (url, method = 'GET') => RequestLogger(url, { logResponseBody: true, logResponseHeaders: true });
export const getLoggerResponseBody = logger => {
const request = logger.requests[0];
return request.response.body.toString();
};
const logger = getLogger(/.*/)
fixture `f`
.page `http://example.com`
.requestHooks(logger);
test('test', async t => {
await t.click('h1');
console.log(getLoggerResponseBody(logger));
});
If you would like the TestCafe Team to research your issue further, please create a sample that reproduces the issue at the following link: https://github.com/DevExpress/testcafe/issues/new?assignees=&labels=TYPE: bug&template=bug_report.yaml