github.com/matthieu/go-ethereum@v1.13.2/node/rpcstack_test.go (about)

     1  package node
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	"github.com/matthieu/go-ethereum/rpc"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestNewWebsocketUpgradeHandler_websocket(t *testing.T) {
    13  	srv := rpc.NewServer()
    14  
    15  	handler := NewWebsocketUpgradeHandler(nil, srv.WebsocketHandler([]string{}))
    16  	ts := httptest.NewServer(handler)
    17  	defer ts.Close()
    18  
    19  	responses := make(chan *http.Response)
    20  	go func(responses chan *http.Response) {
    21  		client := &http.Client{}
    22  
    23  		req, _ := http.NewRequest(http.MethodGet, ts.URL, nil)
    24  		req.Header.Set("Connection", "upgrade")
    25  		req.Header.Set("Upgrade", "websocket")
    26  		req.Header.Set("Sec-WebSocket-Version", "13")
    27  		req.Header.Set("Sec-Websocket-Key", "SGVsbG8sIHdvcmxkIQ==")
    28  
    29  		resp, err := client.Do(req)
    30  		if err != nil {
    31  			t.Error("could not issue a GET request to the test http server", err)
    32  		}
    33  		responses <- resp
    34  	}(responses)
    35  
    36  	response := <-responses
    37  	assert.Equal(t, "websocket", response.Header.Get("Upgrade"))
    38  }