如何在测试咖啡厅中使用数据进行发布请求? [英] How to make post request with data in test-cafe?

查看:123
本文介绍了如何在测试咖啡厅中使用数据进行发布请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是api测试的初学者,我使用的是 test-cafe ,并且我已经编写了测试以制作 GET 请求,使用 RequestHook 正常运行,我能够获取数据,但是当我尝试发出 POST 请求时使用相同的 RequestHook ,并且在发出请求时我无法发送数据,因为它需要是缓冲区类型。

I am a beginner in api-testing, I'm using test-cafe and I have written test to make a GET request using RequestHook which is working fine, I was able to get the data but when I'm trying to make the POST request using the same RequestHook and I'm unable to send the data when making a request as it needs to be of type buffer.

我可以不会将JSON类型的数据转换为缓冲区。同时发出 POST 请求。
我想知道这是否是使用 RequestHook 发出 POST 请求的正确方法,还是我们需要使用 RequestLogger 发出 POST 请求?如果两种方法都不正确,您可以指导我使用test-cafe进行有关api测试的任何教程!

I can't convert the data of type JSON to buffer. While making a POST request. I want to know whether this is the correct way to make a POST request using RequestHook or do we need to use the RequestLogger to make a POSTrequest? If both the methods are wrong can u guide me with any tutorials on api-testing using test-cafe!

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 */
});

预期的结果是,发布请求成功后,其状态应为200 ,但目前无法调用上述api端点,因为它无法将JSON数据转换为缓冲区。

The expected result to be that it should give status 200 after the post request is successful, but at present, it is not able to call the above-mentioned api endpoint as it can't convert the JSON data to buffer.

推荐答案

已创建 RequestHook 来模拟或记录测试请求,但不能创建请求。如果要发送请求并从服务器接收答案,请使用请求或<一个href = https://www.npmjs.com/package/request-promise rel = nofollow noreferrer> request-promise 模块。

The RequestHook has been created to mock or log requests for testing but not to create requests. If you want to send a request and receive an answer from the server, use the request or request-promise module.    

这篇关于如何在测试咖啡厅中使用数据进行发布请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆