Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the json response of a RequestLogger

RequestLogger

A have this test outside the main test controller, using page model and this recipe.

/**
  Used to get the periodic analytic id.
  Whenever we are viewing an asset, the server must respond with an id.
  This id is later used by the client, to send periodic analytics.

  @param {object} t          Testcafe's test controller
  @param {object} logger     A testcafe's RequestLogger.
  @returns {string}          Returns the periodic analytic id.
*/
async getPeriodicAnalyticId(t, logger) {
  const logPrefix = 'Get periodic analytic id > ';
  let responseBody;

  await t
    .expect(logger.requests.length).gt(0, logPrefix + 'No requests logged.');

  responseBody = logger.requests[0].response.body;

  await t
    .expect(responseBody).notTypeOf('undefined', logPrefix + 'Logged request does not have a response a body.')
    .expect(Buffer.isBuffer(responseBody)).eql(true, logPrefix + 'Invalid response body (not buffer)');

  // Periodic analytic id can be found on the server response of the 1st analytic sent.
  return JSON.parse(logger.requests[0].response.body.toString()).id;
}

Error

When running this test targeting a local Http server, works ok. But it fails when tests are targeting an https remote server. I get this error:

SyntaxError: Unexpected token  in JSON at position 0

on this line

return JSON.parse(logger.requests[0].response.body.toString()).id;

Debug info

Response Body from local Http server that works: localBuffer It translates to: localBufferToString

Response Body from remote https server that fails: remoteBuffer It translates to: remoteBufferToString

Question

I'm wondering if the method i use to convert the response body to json is ok. Currently i use:

JSON.parse(logger.requests[0].response.body.toString())

My environment

operating system: Windows 10
testcafe version: 0.21.1
node.js version: 9.3.0
like image 643
Aris Gatoudis Avatar asked Aug 22 '18 18:08

Aris Gatoudis


1 Answers

I had the same problem, because the server response was a gzipped response in my case, and testcafe logger API does not automatically unzip the response.

So we followed this steps :

Configure a logger with logResponseBody parameter (also as mentionned in comment, do not set 'stringifyResponseBody' parameter to true)

const logger = RequestLogger(
  /yourApiRegex/,
  { logResponseBody: true },
);

Write a helper method to unzip request body :

import zlib from 'zlib';

export const getBody = async body => new Promise((resolve, reject) => {
  zlib.gunzip(body, async (error, buff) => {
    if (error !== null) {
      return reject(error);
    }
    return resolve(JSON.parse(buff.toString()));
  });
});

Sample usage in tests

await t.expect(logger.contains(
  async (record) => {
    const body = await (getBody(record.response.body));
    return body.someProps === 'someValue';
  },
)).ok();
like image 184
Ricovitch Avatar answered Nov 15 '22 01:11

Ricovitch