github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/test/protocol/websocket_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gospider007/requests"
     7  	"github.com/gospider007/websocket"
     8  )
     9  
    10  func TestWebSocket(t *testing.T) {
    11  	response, err := requests.Get(nil, "ws://82.157.123.54:9010/ajaxchattest", requests.RequestOption{Headers: map[string]string{
    12  		"Origin": "http://coolaf.com",
    13  	}}) // Send WebSocket request
    14  	if err != nil {
    15  		t.Error(err)
    16  	}
    17  	defer response.CloseBody()
    18  	wsCli := response.WebSocket()
    19  	defer wsCli.Close()
    20  	if err = wsCli.WriteMessage(websocket.TextMessage, "test"); err != nil { // Send text message
    21  		t.Error(err)
    22  	}
    23  	msgType, con, err := wsCli.ReadMessage() // Receive message
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  	if msgType != websocket.TextMessage {
    28  		t.Error("Message type is not text")
    29  	}
    30  	if string(con) != "test" {
    31  		t.Error("Message content is not test")
    32  	}
    33  }