Unable to get the response when content-encoding is not specified

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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%3A+bug&template=bug_report.yaml


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x