graphql_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package graphql
  17. import (
  18. "fmt"
  19. "io/ioutil"
  20. "math/big"
  21. "net/http"
  22. "strings"
  23. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/consensus/ethash"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/eth"
  28. "github.com/ethereum/go-ethereum/node"
  29. "github.com/ethereum/go-ethereum/params"
  30. )
  31. func TestBuildSchema(t *testing.T) {
  32. stack, err := node.New(&node.DefaultConfig)
  33. if err != nil {
  34. t.Fatalf("could not create new node: %v", err)
  35. }
  36. // Make sure the schema can be parsed and matched up to the object model.
  37. if err := newHandler(stack, nil, []string{}, []string{}); err != nil {
  38. t.Errorf("Could not construct GraphQL handler: %v", err)
  39. }
  40. }
  41. // Tests that a graphQL request is successfully handled when graphql is enabled on the specified endpoint
  42. func TestGraphQLBlockSerialization(t *testing.T) {
  43. stack := createNode(t, true)
  44. defer stack.Close()
  45. // start node
  46. if err := stack.Start(); err != nil {
  47. t.Fatalf("could not start node: %v", err)
  48. }
  49. for i, tt := range []struct {
  50. body string
  51. want string
  52. code int
  53. }{
  54. { // Should return latest block
  55. body: `{"query": "{block{number}}","variables": null}`,
  56. want: `{"data":{"block":{"number":10}}}`,
  57. code: 200,
  58. },
  59. { // Should return info about latest block
  60. body: `{"query": "{block{number,gasUsed,gasLimit}}","variables": null}`,
  61. want: `{"data":{"block":{"number":10,"gasUsed":0,"gasLimit":11500000}}}`,
  62. code: 200,
  63. },
  64. {
  65. body: `{"query": "{block(number:0){number,gasUsed,gasLimit}}","variables": null}`,
  66. want: `{"data":{"block":{"number":0,"gasUsed":0,"gasLimit":11500000}}}`,
  67. code: 200,
  68. },
  69. {
  70. body: `{"query": "{block(number:-1){number,gasUsed,gasLimit}}","variables": null}`,
  71. want: `{"data":{"block":null}}`,
  72. code: 200,
  73. },
  74. {
  75. body: `{"query": "{block(number:-500){number,gasUsed,gasLimit}}","variables": null}`,
  76. want: `{"data":{"block":null}}`,
  77. code: 200,
  78. },
  79. {
  80. body: `{"query": "{block(number:\"0\"){number,gasUsed,gasLimit}}","variables": null}`,
  81. want: `{"data":{"block":{"number":0,"gasUsed":0,"gasLimit":11500000}}}`,
  82. code: 200,
  83. },
  84. {
  85. body: `{"query": "{block(number:\"-33\"){number,gasUsed,gasLimit}}","variables": null}`,
  86. want: `{"data":{"block":null}}`,
  87. code: 200,
  88. },
  89. {
  90. body: `{"query": "{block(number:\"1337\"){number,gasUsed,gasLimit}}","variables": null}`,
  91. want: `{"data":{"block":null}}`,
  92. code: 200,
  93. },
  94. {
  95. body: `{"query": "{block(number:\"0xbad\"){number,gasUsed,gasLimit}}","variables": null}`,
  96. want: `{"errors":[{"message":"strconv.ParseInt: parsing \"0xbad\": invalid syntax"}],"data":{}}`,
  97. code: 400,
  98. },
  99. { // hex strings are currently not supported. If that's added to the spec, this test will need to change
  100. body: `{"query": "{block(number:\"0x0\"){number,gasUsed,gasLimit}}","variables": null}`,
  101. want: `{"errors":[{"message":"strconv.ParseInt: parsing \"0x0\": invalid syntax"}],"data":{}}`,
  102. code: 400,
  103. },
  104. {
  105. body: `{"query": "{block(number:\"a\"){number,gasUsed,gasLimit}}","variables": null}`,
  106. want: `{"errors":[{"message":"strconv.ParseInt: parsing \"a\": invalid syntax"}],"data":{}}`,
  107. code: 400,
  108. },
  109. {
  110. body: `{"query": "{bleh{number}}","variables": null}"`,
  111. want: `{"errors":[{"message":"Cannot query field \"bleh\" on type \"Query\".","locations":[{"line":1,"column":2}]}]}`,
  112. code: 400,
  113. },
  114. // should return `estimateGas` as decimal
  115. {
  116. body: `{"query": "{block{ estimateGas(data:{}) }}"}`,
  117. want: `{"data":{"block":{"estimateGas":53000}}}`,
  118. code: 200,
  119. },
  120. } {
  121. resp, err := http.Post(fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), "application/json", strings.NewReader(tt.body))
  122. if err != nil {
  123. t.Fatalf("could not post: %v", err)
  124. }
  125. bodyBytes, err := ioutil.ReadAll(resp.Body)
  126. if err != nil {
  127. t.Fatalf("could not read from response body: %v", err)
  128. }
  129. if have := string(bodyBytes); have != tt.want {
  130. t.Errorf("testcase %d %s,\nhave:\n%v\nwant:\n%v", i, tt.body, have, tt.want)
  131. }
  132. if tt.code != resp.StatusCode {
  133. t.Errorf("testcase %d %s,\nwrong statuscode, have: %v, want: %v", i, tt.body, resp.StatusCode, tt.code)
  134. }
  135. }
  136. }
  137. // Tests that a graphQL request is not handled successfully when graphql is not enabled on the specified endpoint
  138. func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
  139. stack := createNode(t, false)
  140. defer stack.Close()
  141. if err := stack.Start(); err != nil {
  142. t.Fatalf("could not start node: %v", err)
  143. }
  144. body := strings.NewReader(`{"query": "{block{number}}","variables": null}`)
  145. resp, err := http.Post(fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), "application/json", body)
  146. if err != nil {
  147. t.Fatalf("could not post: %v", err)
  148. }
  149. bodyBytes, err := ioutil.ReadAll(resp.Body)
  150. if err != nil {
  151. t.Fatalf("could not read from response body: %v", err)
  152. }
  153. // make sure the request is not handled successfully
  154. if want, have := "404 page not found\n", string(bodyBytes); have != want {
  155. t.Errorf("have:\n%v\nwant:\n%v", have, want)
  156. }
  157. if want, have := 404, resp.StatusCode; want != have {
  158. t.Errorf("wrong statuscode, have:\n%v\nwant:%v", have, want)
  159. }
  160. }
  161. func createNode(t *testing.T, gqlEnabled bool) *node.Node {
  162. stack, err := node.New(&node.Config{
  163. HTTPHost: "127.0.0.1",
  164. HTTPPort: 9393,
  165. WSHost: "127.0.0.1",
  166. WSPort: 9393,
  167. })
  168. if err != nil {
  169. t.Fatalf("could not create node: %v", err)
  170. }
  171. if !gqlEnabled {
  172. return stack
  173. }
  174. createGQLService(t, stack, "127.0.0.1:9393")
  175. return stack
  176. }
  177. func createGQLService(t *testing.T, stack *node.Node, endpoint string) {
  178. // create backend
  179. ethConf := &eth.Config{
  180. Genesis: &core.Genesis{
  181. Config: params.AllEthashProtocolChanges,
  182. GasLimit: 11500000,
  183. Difficulty: big.NewInt(1048576),
  184. },
  185. Ethash: ethash.Config{
  186. PowMode: ethash.ModeFake,
  187. },
  188. NetworkId: 1337,
  189. TrieCleanCache: 5,
  190. TrieCleanCacheJournal: "triecache",
  191. TrieCleanCacheRejournal: 60 * time.Minute,
  192. TrieDirtyCache: 5,
  193. TrieTimeout: 60 * time.Minute,
  194. SnapshotCache: 5,
  195. }
  196. ethBackend, err := eth.New(stack, ethConf)
  197. if err != nil {
  198. t.Fatalf("could not create eth backend: %v", err)
  199. }
  200. // Create some blocks and import them
  201. chain, _ := core.GenerateChain(params.AllEthashProtocolChanges, ethBackend.BlockChain().Genesis(),
  202. ethash.NewFaker(), ethBackend.ChainDb(), 10, func(i int, gen *core.BlockGen) {})
  203. _, err = ethBackend.BlockChain().InsertChain(chain)
  204. if err != nil {
  205. t.Fatalf("could not create import blocks: %v", err)
  206. }
  207. // create gql service
  208. err = New(stack, ethBackend.APIBackend, []string{}, []string{})
  209. if err != nil {
  210. t.Fatalf("could not create graphql service: %v", err)
  211. }
  212. }