rpcstack_test.go 977 B

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