good morning!!!!

Skip to content
Snippets Groups Projects
ws_js_test.go 1.28 KiB
Newer Older
Anmol Sethi's avatar
Anmol Sethi committed
package websocket_test
Anmol Sethi's avatar
Anmol Sethi committed
import (
	"context"
	"net/http"
	"os"
	"testing"
	"time"
Anmol Sethi's avatar
Anmol Sethi committed

	"nhooyr.io/websocket"
Anmol Sethi's avatar
Anmol Sethi committed
	"nhooyr.io/websocket/internal/test/assert"
Anmol Sethi's avatar
Anmol Sethi committed
	"nhooyr.io/websocket/internal/test/wstest"
Anmol Sethi's avatar
Anmol Sethi committed
)

Anmol Sethi's avatar
Anmol Sethi committed
func TestWasm(t *testing.T) {
Anmol Sethi's avatar
Anmol Sethi committed
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
Anmol Sethi's avatar
Anmol Sethi committed
	c, resp, err := websocket.Dial(ctx, os.Getenv("WS_ECHO_SERVER_URL"), &websocket.DialOptions{
		Subprotocols: []string{"echo"},
	})
Anmol Sethi's avatar
Anmol Sethi committed
	assert.Success(t, err)
Anmol Sethi's avatar
Anmol Sethi committed
	defer c.Close(websocket.StatusInternalError, "")

Anmol Sethi's avatar
Anmol Sethi committed
	assert.Equal(t, "subprotocol", "echo", c.Subprotocol())
	assert.Equal(t, "response code", http.StatusSwitchingProtocols, resp.StatusCode)
Anmol Sethi's avatar
Anmol Sethi committed
	c.SetReadLimit(65536)
	for i := 0; i < 10; i++ {
		err = wstest.Echo(ctx, c, 65536)
Anmol Sethi's avatar
Anmol Sethi committed
		assert.Success(t, err)
Anmol Sethi's avatar
Anmol Sethi committed
	}
Anmol Sethi's avatar
Anmol Sethi committed
	err = c.Close(websocket.StatusNormalClosure, "")
Anmol Sethi's avatar
Anmol Sethi committed
	assert.Success(t, err)

func TestWasmDialTimeout(t *testing.T) {
	t.Parallel()

	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
	defer cancel()

	beforeDial := time.Now()
	_, _, err := websocket.Dial(ctx, "ws://example.com:9893", &websocket.DialOptions{
		Subprotocols: []string{"echo"},
	})
	assert.Error(t, err)
	if time.Since(beforeDial) >= time.Second {
		t.Fatal("wasm context dial timeout is not working", time.Since(beforeDial))
	}
}