How to do it...

  1. 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
  1. 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 mainimport (  "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, ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.