Install the github.com/gorilla/websocket and github.com/stretchr/testify/assert packages using the go get command, as follows:
$ go get github.com/gorilla/websocket $ go get github.com/stretchr/testify/assert
Create websocket-server_test.go where we will create a test server, connect to it using the Gorilla client, and eventually read and write messages to test the connection, as follows:
package main import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/gorilla/websocket" "github.com/stretchr/testify/assert" ) func TestWebSocketServer(t *testing.T) { server := httptest.NewServer(http.HandlerFunc (HandleClients)) defer server.Close() u := "ws" + strings.TrimPrefix(server.URL, "http") socket, _, err := websocket.DefaultDialer.Dial(u, nil) if err != nil { t.Fatalf("%v", err) } defer socket.Close() m := Message{Message: "hello"} if err := socket.WriteJSON(&m); err != nil { t.Fatalf("%v", err) } var message Message err = socket.ReadJSON(&message) if err != nil { t.Fatalf("%v", err) } assert.Equal(t, "hello", message.Message, "they should be equal") }