在golang中为ws创建单元测试 [英] create unit test for ws in golang
本文介绍了在golang中为ws创建单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用大猩猩网络套接字框架并使用以下客户端在本地运行web套接字并调试它
https://github.com/gorilla/websocket
ws = new WebSocket(ws:// localhost:8080 / mypath)
ws.onmessage = function(ev){console.log(ev.data)}
ws.send( hello)
当我在镀铬控制台中使用它时,这是行得通的,但是我的问题是否存在如何在go中进行单元测试并避免使用chrome控制台?
解决方案 创建一个测试服务器使用 net / http / httptest 包。使用Gorilla客户端连接到该服务器。读取和写入消息以测试连接。
包主
导入(
net / http
net / http / httptest
strings
测试
github.com/gorilla/websocket
)
var upgrade = websocket.Upgrader {}
func echo(w http.ResponseWriter,r * http.Request){
c,err:= upgrader。升级(w,r,nil)
如果err!= nil {
return
}
推迟c.Close()
为{
mt, message,err:= c.ReadMessage()
if err!= nil {
break
}
err = c.WriteMessage(mt,message)
if err != nil {
break
}
}
}
func TestExample(t * testing.T){
//创建测试服务器与回声处理程序。
s:= httptest.NewServer(http.HandlerFunc(echo))
推迟s.Close()
//将http://127.0.0.1转换为ws:// 127.0.0。
u:=ws+ strings.TrimPrefix(s.URL,http)
//连接服务器
ws,_,err:= websocket.DefaultDialer .Dial(u,nil)
if err!= nil {
t.Fatalf(%v,err)
}
推迟ws.Close()
//将消息发送到服务器,阅读响应并检查它是否符合我们的预期。
for i:= 0;我< 10; i ++ {
if err:= ws.WriteMessage(websocket.TextMessage,[] byte(hello)); err!= nil {
t.Fatalf(%v,err)
}
_,p,err:= ws.ReadMessage()
if err!= nil {
t.Fatalf(%v,err)
}
if string(p)!=hello{
t.Fatalf(bad message)
}
}
}
I use the gorilla web socket framework and use the following client to run the web socket locally and debug it
https://github.com/gorilla/websocket
ws = new WebSocket("ws://localhost:8080/mypath")
ws.onmessage = function(ev) { console.log(ev.data) }
ws.send("hello")
This is working when I use it in the chrome console but my question if there is a way to do some unit test in go and avoid using the chrome console?
解决方案
Create a test server using the net/http/httptest package. Connect to that server using the Gorilla client. Read and write messages to test the connection.
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{}
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
break
}
err = c.WriteMessage(mt, message)
if err != nil {
break
}
}
}
func TestExample(t *testing.T) {
// Create test server with the echo handler.
s := httptest.NewServer(http.HandlerFunc(echo))
defer s.Close()
// Convert http://127.0.0.1 to ws://127.0.0.
u := "ws" + strings.TrimPrefix(s.URL, "http")
// Connect to the server
ws, _, err := websocket.DefaultDialer.Dial(u, nil)
if err != nil {
t.Fatalf("%v", err)
}
defer ws.Close()
// Send message to server, read response and check to see if it's what we expect.
for i := 0; i < 10; i++ {
if err := ws.WriteMessage(websocket.TextMessage, []byte("hello")); err != nil {
t.Fatalf("%v", err)
}
_, p, err := ws.ReadMessage()
if err != nil {
t.Fatalf("%v", err)
}
if string(p) != "hello" {
t.Fatalf("bad message")
}
}
}
这篇关于在golang中为ws创建单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文