51Testing软件测试论坛
标题: postman_操作 《三》 [打印本页]
作者: 德实赋值 时间: 2021-4-20 16:08
标题: postman_操作 《三》
三、示例最后完整的看一下我用的例程。这个例子是一个非常简单的小系统,用户可以注册并登录,然后在系统里新建充值卡,并给这张卡充值。整个流程如下:
1. 注册生成一个随机字符串作为用户名和昵称
postman.setEnvironmentVariable("random_username", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
发起请求
POST /index.php/users HTTP/1.1Host: postmanexample.bayes.cafeCache-Control: no-cachePostman-Token: 76791813-aac2-71fb-cad4-3e737f37c4d0Content-Type: application/x-www-form-urlencoded username=2mjk&password=123456&nickname=2mjk
运行测试、检查结果
tests["Status code is 201"] = responseCode.code === 201;
2. 登录直接用刚才生成的环境变量发起请求
POST /index.php/authentication HTTP/1.1Host: postmanexample.bayes.cafeCache-Control: no-cachePostman-Token: aac7d0ac-e0e3-ecf2-39da-b8dca672e3d7Content-Type: application/x-www-form-urlencoded username=2mjk&password=123456
运行测试、检查结果,并将返回的token记录下来
tests["Status code is 200"] = responseCode.code === 200; var data = JSON.parse(responseBody);postman.setEnvironmentVariable("token", data.token);
3. 添加一张卡先生成一个卡号和卡名
postman.setEnvironmentVariable("random_cardno", Math.round(Math.random()*9999999)); postman.setEnvironmentVariable("random_cardname", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
然后发起请求,这里调用了刚才获取到的Token,放在header的自定义字段里作为鉴权(SAE不能用Authorization这个字段,不清楚原因)
POST /index.php/cards HTTP/1.1Host: postmanexample.bayes.cafeX-Authorization: d4c4a0b7b36c73e7a13b7e24a596093bCache-Control: no-cachePostman-Token: d44d573f-f17a-366c-2cd7-1d5b8b709233Content-Type: application/x-www-form-urlencoded cardno=1385526&desc=2mo8
运行测试
tests["Status code is 200"] = responseCode.code === 200;
4. 查询刚才生成的卡发起请求,调用了刚才生成的卡号
GET /index.php/cards/1385526 HTTP/1.1Host: postmanexample.bayes.cafeCache-Control: no-cachePostman-Token: 1e5aca57-c3bb-7404-2791-c639cd60b5c8
运行验证,和刚才生成的卡名对比,并记录新卡的ID
var data = JSON.parse(responseBody);tests["check cardname"] = data.desc === environment.random_cardname; postman.setEnvironmentVariable("new_card_id", data.id);
5. 充值发起请求,使用了刚才获得的新卡ID
POST /index.php/deposit HTTP/1.1Host: postmanexample.bayes.cafeX-Authorization: d4c4a0b7b36c73e7a13b7e24a596093bCache-Control: no-cachePostman-Token: 388c95e0-b5ce-9bbf-5816-084db7523384Content-Type: application/x-www-form-urlencoded cardid=1&amount=10
运行验证(由于是新建的用户,没有余额,无法给卡片充值,故返回403 Forbidden)
tests["Status code is 403"] = responseCode.code === 403;
服务器端程序的代码已经在GitHub开源。
作者: Miss_love 时间: 2021-4-22 11:01
支持分享
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) |
Powered by Discuz! X3.2 |