J. Hartman
J. Hartman

Reputation: 91

TestCafe requestHook response body is not giving text

I am trying to use TestCafe's request hooks to get the body of a response. I am able to log the body of the request and can see the xml of the request body no problem. For the response though I am getting globby gook. I am thinking I am having some kind of ssl issue but not exactly sure. It seems strange because I am getting a 200 status code, and am able to see the headers of the response. If it was an ssl thing do think I should see the headers.

anyway here is my code the custom requestHook

import { RequestHook} from 'testcafe' 

export default class AdultHomeScreenHook extends RequestHook{

    constructor(requestFilter:any, responceOptions:any){
        super(requestFilter,responceOptions)
    }

    onRequest (event:any) {
        console.log('========================')
        console.log('Request Body')
        let buf = event._requestContext.reqBody as Buffer
        console.log(buf.toLocaleString())

    }

    onResponse (event:any) {
        console.log('========================')
        let buf = event.body as Buffer
        console.log(event)
    }
}

this is the important parts of the test fixture

import AdultHomeHook from '../requestHooks/adultHomeScreenHook'

let adultHomeHook = new AdultHomeHook({url:'https://url.com/login?language=en', 
method:'post'},{ includeHeaders: true, includeBody: true })

fixture.only`Adult Home Screen 
Tests`.page`localhost:8080`.requestHooks(adultHomeHook);

and then the code to launch the webapp and start the tests

const fs = require('fs');
const selfSigned = require('openssl-self-signed-certificate');

const createTestCafe = require('testcafe');
let testcafe = null;


var options = {
    key: selfSigned.key,
    cert: selfSigned.cert
};

createTestCafe('localhost', 1337, 1338, options)
    .then(tc => {
        testcafe = tc;
        const runner = testcafe.createRunner();
        return runner
            .startApp('node scripts/run start', 45000)
            .src([
                './testcafe/tests/testsAccountDetails.ts'
            ])
            .browsers('chrome --allow-insecure-localhost')
            .run({
                selectorTimeout: 30000
            });
    })
    .then(failedCount => {
        console.log('Tests failed: ' + failedCount);
        testcafe.close();
    });

I have tried a couple of different things for the ssl options object, tried a self-signed cert and also using the cert for the webapp and a good number of other things to no avail.

when I run everything I am able to see the body of the request as expected

<device><type>web</type><deviceId>547564be-fd2d-6ea8-76db-77c1f3d05e3e</deviceId></device>

but the response body is not right something like this

U�Os�0��~n� ��锶3m��������$h�Z�=���{ﷇ��2��.۾���]I=�!/ylƴ�4p%��P�G�����~��0�jݧ�NUn��(���IQ�
                                                                =2� 

I can also see the headers for both request and response no problem

Upvotes: 3

Views: 801

Answers (1)

J. Hartman
J. Hartman

Reputation: 91

turns out this was not an ssl issue at all. the response body from the server was coming in a zipped format. I had to unzip the response body buffer and then could run .toString() on the unzipped Buffer

onResponse (event:any) {
    console.log('========================')
    let buf = event.body as Buffer
    let unzippedBody = zlib.gunzipSync(buf) as Buffer
    console.log(unzippedBody.toLocaleString())
}

Upvotes: 6

Related Questions