我是 api 测试的初学者,我正在使用 test-cafe
我已经写了一个测试来制作 GET
使用工作正常的 RequestHook 请求,我能够获取数据但是当我尝试制作 POST
时请求使用相同的 RequestHook 并且在发出请求时我无法发送数据,因为它需要是缓冲区类型。
我无法将 JSON 类型的数据转换为缓冲区。在制作 POST
时要求。
我想知道这是否是制作 POST
的正确方法使用RequestHook 请求还是我们需要使用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请求成功后应该给status 200,但目前无法调用上述api端点,因为它无法将JSON数据转换为buffer。
最佳答案
关于node.js - 如何在 test-cafe 中使用数据发出 post 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56427410/