node: refactor package node (#21105)
This PR significantly changes the APIs for instantiating Ethereum nodes in a Go program. The new APIs are not backwards-compatible, but we feel that this is made up for by the much simpler way of registering services on node.Node. You can find more information and rationale in the design document: https://gist.github.com/renaynay/5bec2de19fde66f4d04c535fd24f0775. There is also a new feature in Node's Go API: it is now possible to register arbitrary handlers on the user-facing HTTP server. In geth, this facility is used to enable GraphQL. There is a single minor change relevant for geth users in this PR: The GraphQL API is no longer available separately from the JSON-RPC HTTP server. If you want GraphQL, you need to enable it using the ./geth --http --graphql flag combination. The --graphql.port and --graphql.addr flags are no longer available.
This commit is contained in:
@ -17,12 +17,118 @@
|
||||
package graphql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/node"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBuildSchema(t *testing.T) {
|
||||
stack, err := node.New(&node.DefaultConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("could not create new node: %v", err)
|
||||
}
|
||||
// Make sure the schema can be parsed and matched up to the object model.
|
||||
if _, err := newHandler(nil); err != nil {
|
||||
if err := newHandler(stack, nil, []string{}, []string{}); err != nil {
|
||||
t.Errorf("Could not construct GraphQL handler: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Tests that a graphQL request is successfully handled when graphql is enabled on the specified endpoint
|
||||
func TestGraphQLHTTPOnSamePort_GQLRequest_Successful(t *testing.T) {
|
||||
stack := createNode(t, true)
|
||||
defer stack.Close()
|
||||
// start node
|
||||
if err := stack.Start(); err != nil {
|
||||
t.Fatalf("could not start node: %v", err)
|
||||
}
|
||||
// create http request
|
||||
body := strings.NewReader("{\"query\": \"{block{number}}\",\"variables\": null}")
|
||||
gqlReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), body)
|
||||
if err != nil {
|
||||
t.Error("could not issue new http request ", err)
|
||||
}
|
||||
gqlReq.Header.Set("Content-Type", "application/json")
|
||||
// read from response
|
||||
resp := doHTTPRequest(t, gqlReq)
|
||||
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("could not read from response body: %v", err)
|
||||
}
|
||||
expected := "{\"data\":{\"block\":{\"number\":\"0x0\"}}}"
|
||||
assert.Equal(t, expected, string(bodyBytes))
|
||||
}
|
||||
|
||||
// Tests that a graphQL request is not handled successfully when graphql is not enabled on the specified endpoint
|
||||
func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
|
||||
stack := createNode(t, false)
|
||||
defer stack.Close()
|
||||
if err := stack.Start(); err != nil {
|
||||
t.Fatalf("could not start node: %v", err)
|
||||
}
|
||||
|
||||
// create http request
|
||||
body := strings.NewReader("{\"query\": \"{block{number}}\",\"variables\": null}")
|
||||
gqlReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), body)
|
||||
if err != nil {
|
||||
t.Error("could not issue new http request ", err)
|
||||
}
|
||||
gqlReq.Header.Set("Content-Type", "application/json")
|
||||
// read from response
|
||||
resp := doHTTPRequest(t, gqlReq)
|
||||
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("could not read from response body: %v", err)
|
||||
}
|
||||
// make sure the request is not handled successfully
|
||||
assert.Equal(t, 404, resp.StatusCode)
|
||||
assert.Equal(t, "404 page not found\n", string(bodyBytes))
|
||||
}
|
||||
|
||||
func createNode(t *testing.T, gqlEnabled bool) *node.Node {
|
||||
stack, err := node.New(&node.Config{
|
||||
HTTPHost: "127.0.0.1",
|
||||
HTTPPort: 9393,
|
||||
WSHost: "127.0.0.1",
|
||||
WSPort: 9393,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("could not create node: %v", err)
|
||||
}
|
||||
if !gqlEnabled {
|
||||
return stack
|
||||
}
|
||||
|
||||
createGQLService(t, stack, "127.0.0.1:9393")
|
||||
|
||||
return stack
|
||||
}
|
||||
|
||||
func createGQLService(t *testing.T, stack *node.Node, endpoint string) {
|
||||
// create backend
|
||||
ethBackend, err := eth.New(stack, ð.DefaultConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("could not create eth backend: %v", err)
|
||||
}
|
||||
|
||||
// create gql service
|
||||
err = New(stack, ethBackend.APIBackend, []string{}, []string{})
|
||||
if err != nil {
|
||||
t.Fatalf("could not create graphql service: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func doHTTPRequest(t *testing.T, req *http.Request) *http.Response {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal("could not issue a GET request to the given endpoint", err)
|
||||
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
Reference in New Issue
Block a user