package main import ( "context" "crypto/tls" "testing" "time" "github.com/gorilla/websocket" "github.com/stretchr/testify/assert" ) func TestWebsocket(t *testing.T) { ctx := mainCtx ctx, clearTimeout := context.WithTimeout(ctx, time.Second*30) defer clearTimeout() t.Run("disabled", func(t *testing.T) { ws, _, err := (&websocket.Dialer{ NetDialContext: testcluster.Transport.DialContext, TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, }).DialContext(ctx, "wss://disabled-ws-echo.localhost.pomerium.io", nil) if !assert.Error(t, err, "expected bad handshake when websocket is not enabled") { ws.Close() return } }) t.Run("enabled", func(t *testing.T) { ws, _, err := (&websocket.Dialer{ NetDialContext: testcluster.Transport.DialContext, TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, }).DialContext(ctx, "wss://enabled-ws-echo.localhost.pomerium.io", nil) if !assert.NoError(t, err, "expected no error when creating websocket") { return } defer ws.Close() msg := "hello world" err = ws.WriteJSON("hello world") assert.NoError(t, err, "expected no error when writing json to websocket") err = ws.ReadJSON(&msg) assert.NoError(t, err, "expected no error when reading json from websocket") }) }