TestCafe requestHook 响应 body 未提供文本

TestCafe requestHook response body is not giving text

我正在尝试使用 TestCafe 的请求挂钩来获取响应的 body。我能够记录请求的 body 并且可以看到请求 body 的 xml 没问题。对于响应,尽管我变得很笨拙。我想我遇到了某种 ssl 问题,但不确定。这看起来很奇怪,因为我收到了 200 状态代码,并且能够看到响应的 headers。如果它是一个 ssl 东西,我认为我应该看到 headers.

无论如何这是我的代码 自定义 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)
    }
}

这是测试夹具的重要部分

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

然后是启动 webapp 和开始测试的代码

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

我已经为 ssl 选项 object 尝试了几种不同的方法,尝试了 self-signed 证书,还使用了 webapp 的证书和许多其他方法,但都无济于事。

当我 运行 我能够按预期看到请求的 body 时

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

但是响应 body 是不正确的

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

我也可以看到 headers 请求和响应都没问题

原来这根本不是 ssl 问题。来自服务器的响应正文以压缩格式出现。我不得不解压缩响应主体缓冲区,然后可以 运行 .toString() 在解压缩的缓冲区

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