How to get success count, failure count and failure reason when testing rest webservices from file using shell script

淺唱寂寞╮ 提交于 2019-12-21 22:46:10

问题


Hi i am testing web services using shell script by having multiple if condition, with the shell script coding i am getting success count, failure count and failure reason

success=0
failure=0

if curl -s --head --request DELETE http://localhost/bimws/delete/deleteUser?email=pradeepkumarhe1989@gmail.com | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
echo "DeleteUser is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

if curl -s --head  --request GET http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
 curl -s --head  --request GET http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com > f1.txt
 echo "getUserDetails is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

if curl -s -i -X POST -H "Content-Type:application/json"  http://localhost/bimws/post/addProjectLocationAddress -d '{"companyid":"10","projectid":"200","addresstypeid":"5","address":"1234 main st","city":"san jose","state":"CA","zip":"989898","country":"United States"}' | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
echo "addProjectLocationAddress is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

echo $success Success
echo $failure failure

but i am looking forward to test the web services from a file like i have file called web_services.txt which contains all my web services using shell script how do i execute and success count, failure count and failure reason

web_services.txt

All are different calls delete,get and post

http://localhost/bimws/delete/deleteUser?email=pradeepkumarhe1989@gmail.com


http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com



http://localhost/bimws/post/addProjectLocationAddress -d '{"companyid":"10","projectid":"200","addresstypeid":"5","address":"1234 main st"
,"city":"san jose","state":"CA","zip":"989898","country":"United States"}'

回答1:


First of all, your current code does not correctly deal with empty lines. You need to skip those.

Your lines already contain shell commands. Running curl on them makes no sense. Instead, you should evaluate these commands.

Then, you need to modify curl so that it reports whether the request was successful by adding -f:

FILE=D:/WS.txt
success=0
failure=0
while read LINE; do
    if test -z "$LINE"; then
        continue
    fi
    if eval $(echo "$LINE" | sed 's/^curl/curl -f -s/') > /dev/null; then 
        success=$((success+1))
    else
        echo $LINE >> aNewFile.txt
        failure=$((failure+1))
    fi
done < $FILE
echo $success Success
echo $failure failure


来源:https://stackoverflow.com/questions/47526927/how-to-get-success-count-failure-count-and-failure-reason-when-testing-rest-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!