O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required