接口一般都有权限的校验,一般是需要登录后才可以调用
对于接口的认证,一般通过两种方式来实现
1、校验用户请求中是否包含某项指定的cookie
2、校验用户的请求的header中是否包含某项指定的字段(token)
案例一:cookie的处理
LR会自动处理cookie,如果需要手动新增cookie,使用web_add_cookie函数
案例二:token的处理
token一般需要手动添加到HTTP信息头中,使用web_add_header函数
先看笔记 token是什么 cookie是什么 https://www.cnblogs.com/zhenyu1/p/10544209.html
1 Action() 2 { 3 4 5 web_reg_save_param("token", 6 "LB=data":"", 7 "RB="}", 8 "Ord=1", 9 LAST); 10 11 12 web_custom_request("post", "Method=POST", 13 14 "URL=http://localhost:8080/pinter/bank/api/login2", 15 16 "Body=userName=admin&password=1234", 17 18 "TargetFrame=", 19 20 LAST ); 21 22 web_add_header("testfan-token","{token}"); 23 24 web_url("query", 25 26 "URL=http://localhost:8080/pinter/bank/api/query2?userName=admin", 27 28 LAST ); 29 30 return 0; 31 }
返回日志如下:
log
1 Virtual User Script started at : 2019-03-16 18:54:49 2 Starting action vuser_init. 3 Web Turbo Replay of LoadRunner 11.0.0 for WINXP; build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143] 4 Run Mode: HTML [MsgId: MMSG-26000] 5 Run-Time Settings file: "C:Documents and Settings estLocal SettingsTemp oname35\default.cfg" [MsgId: MMSG-27141] 6 Ending action vuser_init. 7 Running Vuser... 8 Starting iteration 1. 9 Starting action Action. 10 Action.c(5): Registering web_reg_save_param was successful [MsgId: MMSG-26390] 11 Action.c(12): Notify: Transaction "post_json2" started. 12 Action.c(15): Warning -26593: The header being added may cause unpredictable results when applied to all ensuing URLs. It is added anyway [MsgId: MWAR-26593] 13 Action.c(15): web_add_header("Content-type") highest severity level was "warning" [MsgId: MMSG-26391] 14 Action.c(17): Warning: The string '"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"beihe@163.com","address":"beijing"' with parameter delimiters is not a parameter. 15 Action.c(17): t=691ms: 128-byte response headers for "http://localhost:8080/pinter/com/register" (RelFrameId=1, Internal ID=1) 16 Action.c(17): HTTP/1.1 200 17 Action.c(17): Content-Type: application/json;charset=UTF-8 18 Action.c(17): Transfer-Encoding: chunked 19 Action.c(17): Date: Sat, 16 Mar 2019 10:54:49 GMT 20 Action.c(17): 21 Action.c(17): t=731ms: 4-byte chunked response overhead for "http://localhost:8080/pinter/com/register" (RelFrameId=1, Internal ID=1) 22 Action.c(17): 31 23 Action.c(17): t=742ms: 7-byte chunked response overhead for "http://localhost:8080/pinter/com/register" (RelFrameId=1, Internal ID=1) 24 Action.c(17): 25 Action.c(17): 0 26 Action.c(17): 27 Action.c(17): t=783ms: 49-byte chunked response body for "http://localhost:8080/pinter/com/register" (RelFrameId=1, Internal ID=1) 28 Action.c(17): {"code":"0","message":"娉ㄥ唽鎴愬姛","data":null} 29 Action.c(17): Notify: Saving Parameter "msg = 娉ㄥ唽鎴愬姛". 30 Action.c(17): HTML parsing not performed for Content-Type "application/json" ("ParseHtmlContentType" Run-Time Setting is "TEXT"). URL="http://localhost:8080/pinter/com/register" [MsgId: MMSG-26548] 31 Action.c(17): web_custom_request("post_json") was successful, 49 body bytes, 128 header bytes, 11 chunking overhead bytes [MsgId: MMSG-26385] 32 Action.c(30): Notify: Parameter Substitution: parameter "msg" = "娉ㄥ唽鎴愬姛" 33 Action.c(30): Notify: Saving Parameter "sEncoding = 注册成功x00". 34 Action.c(33): Notify: Parameter Substitution: parameter "sEncoding" = "注册成功x00" 35 Action.c(34): Notify: Transaction "post_json2" ended with "Pass" status (Duration: 0.7704 Wasted Time: 0.4959). 36 Ending action Action. 37 Ending iteration 1. 38 Ending Vuser... 39 Starting action vuser_end. 40 Ending action vuser_end. 41 Vuser Terminated.