如何在测试咖啡厅中使用数据进行发布请求?

编程入门 行业动态 更新时间:2024-10-27 12:24:41
本文介绍了如何在测试咖啡厅中使用数据进行发布请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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: 'localhost:3000/user/details', { includeHeaders: true, includeBody: true }); fixture `POST` .page('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 来模拟或记录测试请求,但不能创建请求。如果要发送请求并从服务器接收答案,请使用请求或 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.    

更多推荐

如何在测试咖啡厅中使用数据进行发布请求?

本文发布于:2023-10-29 09:32:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:厅中   咖啡   测试   数据   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!