mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
Tests for handling websocket and http requests on the same server (#6)
This commit is contained in:
parent
bdd1091b48
commit
51c6d696f6
2 changed files with 99 additions and 0 deletions
|
|
@ -19,6 +19,7 @@ package node
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -27,6 +28,8 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -597,3 +600,61 @@ func TestAPIGather(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWebsocketHTTPOnSamePort_WebsocketRequest(t *testing.T) {
|
||||||
|
startHTTP(t)
|
||||||
|
|
||||||
|
wsReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:4343", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("could not issue new http request ", err)
|
||||||
|
}
|
||||||
|
wsReq.Header.Set("Connection", "upgrade")
|
||||||
|
wsReq.Header.Set("Upgrade", "websocket")
|
||||||
|
wsReq.Header.Set("Sec-WebSocket-Version", "13")
|
||||||
|
wsReq.Header.Set("Sec-Websocket-Key", "SGVsbG8sIHdvcmxkIQ==")
|
||||||
|
|
||||||
|
wsResponses := make(chan *http.Response)
|
||||||
|
go doHTTPRequest(wsResponses, wsReq, t)
|
||||||
|
wsResponse := <-wsResponses
|
||||||
|
|
||||||
|
assert.Equal(t, "websocket", wsResponse.Header.Get("Upgrade"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWebsocketHTTPOnSamePort_HTTPRequest(t *testing.T) {
|
||||||
|
startHTTP(t)
|
||||||
|
|
||||||
|
httpReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:4343", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("could not issue new http request ", err)
|
||||||
|
}
|
||||||
|
httpReq.Header.Set("Accept-Encoding", "gzip")
|
||||||
|
|
||||||
|
httpResponses := make(chan *http.Response)
|
||||||
|
go doHTTPRequest(httpResponses, httpReq, t)
|
||||||
|
httpResponse := <-httpResponses
|
||||||
|
|
||||||
|
assert.Equal(t,"gzip", httpResponse.Header.Get("Content-Encoding"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func startHTTP(t *testing.T) {
|
||||||
|
conf := &Config{HTTPPort: 4343, WSPort: 4343}
|
||||||
|
node, err := New(conf)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("could not create a new node ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = node.startHTTP("127.0.0.1:4343", []rpc.API{}, []string{}, []string{}, []string{}, rpc.HTTPTimeouts{}, []string{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error("could not start http service on node ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func doHTTPRequest(responses chan *http.Response, req *http.Request, t *testing.T) {
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("could not issue a GET request to the given endpoint", err)
|
||||||
|
}
|
||||||
|
responses <- resp
|
||||||
|
}
|
||||||
|
|
|
||||||
38
node/rpcstack_test.go
Normal file
38
node/rpcstack_test.go
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
package node
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewWebsocketUpgradeHandler_websocket(t *testing.T) {
|
||||||
|
srv := rpc.NewServer()
|
||||||
|
|
||||||
|
handler := NewWebsocketUpgradeHandler(nil, srv.WebsocketHandler([]string{}))
|
||||||
|
ts := httptest.NewServer(handler)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
responses := make(chan *http.Response)
|
||||||
|
go func(responses chan *http.Response) {
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
req, _ := http.NewRequest(http.MethodGet, ts.URL, nil)
|
||||||
|
req.Header.Set("Connection", "upgrade")
|
||||||
|
req.Header.Set("Upgrade", "websocket")
|
||||||
|
req.Header.Set("Sec-WebSocket-Version", "13")
|
||||||
|
req.Header.Set("Sec-Websocket-Key", "SGVsbG8sIHdvcmxkIQ==")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("could not issue a GET request to the test http server", err)
|
||||||
|
}
|
||||||
|
responses <- resp
|
||||||
|
}(responses)
|
||||||
|
|
||||||
|
response := <- responses
|
||||||
|
assert.Equal(t,"websocket", response.Header.Get("Upgrade"))
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue