How to get the json response of a RequestLogger

廉价感情. 提交于 2021-01-27 06:10:13

问题


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

回答1:


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();


来源:https://stackoverflow.com/questions/51972855/how-to-get-the-json-response-of-a-requestlogger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!