Httprunner4 - websocket
本地 ws 服务
import asyncio
import websockets
# 接收客户端消息并处理,这里只是简单把客户端发来的返回回去
async def recv_user_msg(websocket):
while True:
recv_text = await websocket.recv()
print("recv_text:", websocket.pong, recv_text)
response_text = f"Server return: {recv_text}"
print("response_text:", response_text)
await websocket.send(response_text)
# 服务器端主逻辑
async def run(websocket, path):
while True:
try:
await recv_user_msg(websocket)
except websockets.ConnectionClosed:
print("ConnectionClosed...", path) # 链接断开
break
except websockets.InvalidState:
print("InvalidState...") # 无效状态
break
except Exception as e:
print("Exception:", e)
if __name__ == '__main__':
print("127.0.0.1:8181 websocket...")
asyncio.get_event_loop().run_until_complete(websockets.serve(run, "127.0.0.1", 8181))
asyncio.get_event_loop().run_forever()
Hrun 用例
config:
name: 本地websocket测试
base_url: ws://127.0.0.1:8181
teststeps:
- name: open connection
websocket:
type: open
url: "/"
headers:
User-Agent: HttpRunnerPlus
validate:
- check: status_code
assert: equals
expect: 101
msg: check open status code
- check: headers.Connection
assert: equals
expect: Upgrade
msg: check headers
- name: ping pong test
websocket:
type: ping
url: "/"
timeout: 5000
- name: 客户端发送消息
websocket:
type: w
url: "/"
text: "你好"
- name: 客户端读取服务端返回的消息
websocket:
type: r
url: "/"
validate:
- check: body
assert: contains
expect: "你好"
msg: validate msg
- name: write and read text
websocket:
type: wr
url: "/"
text: "先发后读"
validate:
- check: body
assert: contains
expect: "先发后读"
msg: check length equal
- name: close connection
websocket:
type: close
url: "/"
close_status: 1000
timeout: 30000
validate:
- check: status_code
assert: equals
expect: 1000
msg: check close status code
作者:ABEELAN
出处:https://www.cnblogs.com/abeelan/p/17188324.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
分类:
Python
Buy me a cup of coffee ☕.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!