可以使用k6框架测试graphql订阅吗?我试着做这件事,但没有太多成功。我也尝试过使用k6的websockets,但是没有帮助。谢谢
发布于 2021-05-11 05:47:20
Grapqhql Subscription是基于Websockets的,因此理论上可以使用k6 WebSocket实现。
您还可以参考subscriptions here的文档。您还可以使用开发人员工具中的游乐场和网络选项卡来确定发送到服务器的消息/请求。
下面是我是如何做到的:
import ws from "k6/ws";
export default function(){
const url = "ws://localhost:4000/graphql" // replace with your url
const token = null; // replace with your auth token
const operation = `
subscription PostFeed {
postCreated {
author
comment
}
}` // replace with your subscription
const headers = {
"Sec-WebSocket-Protocol": "graphql-ws",
};
if (token != null) Object.assign(headers,{ Authorization: `Bearer ${token}`});
ws.connect(
url,
{
headers,
},
(socket) => {
socket.on("message", (msg) => {
const message = JSON.parse(msg);
if (message.type == "connection_ack")
console.log("Connection Established with WebSocket");
if (message.type == "data") console.log(`Message Received: ${message}`)
});
socket.on("open", () => {
socket.send(
JSON.stringify({
type: "connection_init",
payload: headers,
})
);
socket.send(
JSON.stringify({
type: "start",
payload: {
query: operation,
},
})
);
});
}
);
}希望这能有所帮助!?
https://stackoverflow.com/questions/66410400
复制相似问题