我是api测试的初学者,正在使用test-cafe
,我已经编写了一个使用RequestHook进行GET
请求的测试,它可以正常工作,我能够获取数据。但是当我尝试使用相同的RequestHook进行POST
请求时,我无法发送数据,因为它需要是缓冲类型。
我无法将JSON数据转换为缓冲区类型。在进行POST
请求时,我想知道是否这是使用RequestHook进行POST
请求的正确方式,还是我们需要使用RequestLogger来进行POST
请求?如果这两种方法都不正确,您能否指导我使用test-cafe进行api测试的任何教程!
class MyRequestHook extends RequestHook {
constructor (requestFilterRules, responseEventConfigureOpts) {
super(requestFilterRules, responseEventConfigureOpts);
// ...
}
async onRequest (event) {
const userData = {
name: "Avinash",
gender: "male",
year : 1984,
month: 12,
day : 12,
place : "Bengaluru, Karnataka, India"
};
const bufferedData = Buffer.from(JSON.stringify(userData));
// the above code can't convert the data of type json to buffer type and the console stucks here, not able to print anything past this.
event.requestOptions.body = bufferedData;
}
async onResponse (e) {
console.log(e.body);
}
}
const myRequestHook = new MyRequestHook(url: 'http://localhost:3000/user/details', {
includeHeaders: true,
includeBody: true
});
fixture `POST`
.page('http://localhost:3000/user/details')
.requestHooks(myRequestHook);
test('basic', async t => {
/* some actions */
});
预期结果是在POST请求成功后,状态码应该是200,但目前无法调用上述API端点,因为无法将JSON数据转换为缓冲区。