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