curl 系统自带的发请求工具,无需安装!
##
# @Author lambert
# @date 2022/3/28
# 用于自动化测试接口(适用于Spring Cloud 带有网关的场景,端口地址随机分配,uuid等信息会更换。省去重复的工作!!)
##
echo "自动测试脚本V1.1"
echo "请输入配置信息(Enter自动读取默认配置)"
read -p "url: " -t 120 url
if [ ! ${url} ];then
echo "未输入url,读取默认配置"
url='localhost:100010' # 默认url
else
echo "url初始化完成"
fi
xhr1="$(curl -X POST $(url)/satellite/start | grep "port")" # 发送curl请求到url,并使用grep对结果信息过滤
port=$(echo $xhr1|sed -rn "s/.*\"port\":([0-9]*)}}/\1/p") # 使用sed对有效信息进行提取 -r 开启正则、-n显示匹配结果、/p显示符合条件的信息
echo "-------------------------------------------------------------------------------------------------------------------"
echo $xhr1
echo "xhr1: ${url} 请求完成"
echo "-------------------------------------------------------------------------------------------------------------------"
url2='192.168.54.41:8888/api/configsim'
param2="{\"test\":${port}}"
xhr2="$(curl -H "Accept:application/json" -H 'Content-Type:application/json' -X POST -d $param2 ${url2} | grep "uuid")" # 发送curl请求到url2,并用grep对结果信息过滤
uuid=$(echo $xhr2|sed -rn "s/.*\"uuid\":(.*)}/\1/p") # 使用sed对有效信息进行提取 -r 开启正则、-n显示匹配结果、/p显示符合条件的信息
echo "-------------------------------------------------------------------------------------------------------------------"
echo $xhr2
echo "xhr2: ${url2} 请求完成"
echo "-------------------------------------------------------------------------------------------------------------------"
echo "脚本执行结束"
read -p "end" -t 120 end # 保证脚本执行完不会一闪而过