@@ -22,8 +22,13 @@ import (
2222 "net/http"
2323 "strings"
2424 "testing"
25+ "time"
2526
27+ "github.com/ethereum/go-ethereum/common"
28+ "github.com/ethereum/go-ethereum/consensus/ethash"
29+ "github.com/ethereum/go-ethereum/core"
2630 "github.com/ethereum/go-ethereum/eth"
31+ "github.com/ethereum/go-ethereum/miner"
2732 "github.com/ethereum/go-ethereum/node"
2833 "github.com/stretchr/testify/assert"
2934)
@@ -61,6 +66,7 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Successful(t *testing.T) {
6166 t .Fatalf ("could not read from response body: %v" , err )
6267 }
6368 expected := "{\" data\" :{\" block\" :{\" number\" :\" 0x0\" }}}"
69+ assert .Equal (t , 200 , resp .StatusCode )
6470 assert .Equal (t , expected , string (bodyBytes ))
6571}
6672
@@ -90,6 +96,32 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
9096 assert .Equal (t , "404 page not found\n " , string (bodyBytes ))
9197}
9298
99+ // Tests that 400 is returned when an invalid RPC request is made.
100+ func TestGraphQL_BadRequest (t * testing.T ) {
101+ stack := createNode (t , true )
102+ defer stack .Close ()
103+ // start node
104+ if err := stack .Start (); err != nil {
105+ t .Fatalf ("could not start node: %v" , err )
106+ }
107+ // create http request
108+ body := strings .NewReader ("{\" query\" : \" {bleh{number}}\" ,\" variables\" : null}" )
109+ gqlReq , err := http .NewRequest (http .MethodGet , fmt .Sprintf ("http://%s/graphql" , "127.0.0.1:9393" ), body )
110+ if err != nil {
111+ t .Error ("could not issue new http request " , err )
112+ }
113+ gqlReq .Header .Set ("Content-Type" , "application/json" )
114+ // read from response
115+ resp := doHTTPRequest (t , gqlReq )
116+ bodyBytes , err := ioutil .ReadAll (resp .Body )
117+ if err != nil {
118+ t .Fatalf ("could not read from response body: %v" , err )
119+ }
120+ expected := "{\" errors\" :[{\" message\" :\" Cannot query field \\ \" bleh\\ \" on type \\ \" Query\\ \" .\" ,\" locations\" :[{\" line\" :1,\" column\" :2}]}]}"
121+ assert .Equal (t , expected , string (bodyBytes ))
122+ assert .Equal (t , 400 , resp .StatusCode )
123+ }
124+
93125func createNode (t * testing.T , gqlEnabled bool ) * node.Node {
94126 stack , err := node .New (& node.Config {
95127 HTTPHost : "127.0.0.1" ,
@@ -110,8 +142,24 @@ func createNode(t *testing.T, gqlEnabled bool) *node.Node {
110142}
111143
112144func createGQLService (t * testing.T , stack * node.Node , endpoint string ) {
113- // create backend
114- ethBackend , err := eth .New (stack , & eth .DefaultConfig )
145+ // create backend (use a config which is light on mem consumption)
146+ ethConf := & eth.Config {
147+ Genesis : core .DeveloperGenesisBlock (15 , common.Address {}),
148+ Miner : miner.Config {
149+ Etherbase : common .HexToAddress ("0xaabb" ),
150+ },
151+ Ethash : ethash.Config {
152+ PowMode : ethash .ModeTest ,
153+ },
154+ NetworkId : 1337 ,
155+ TrieCleanCache : 5 ,
156+ TrieCleanCacheJournal : "triecache" ,
157+ TrieCleanCacheRejournal : 60 * time .Minute ,
158+ TrieDirtyCache : 5 ,
159+ TrieTimeout : 60 * time .Minute ,
160+ SnapshotCache : 5 ,
161+ }
162+ ethBackend , err := eth .New (stack , ethConf )
115163 if err != nil {
116164 t .Fatalf ("could not create eth backend: %v" , err )
117165 }
0 commit comments