graphql_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. } {
  115. resp, err := http.Post(fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), "application/json", strings.NewReader(tt.body))
  116. if err != nil {
  117. t.Fatalf("could not post: %v", err)
  118. }
  119. bodyBytes, err := ioutil.ReadAll(resp.Body)
  120. if err != nil {
  121. t.Fatalf("could not read from response body: %v", err)
  122. }
  123. if have := string(bodyBytes); have != tt.want {
  124. t.Errorf("testcase %d %s,\nhave:\n%v\nwant:\n%v", i, tt.body, have, tt.want)
  125. }
  126. if tt.code != resp.StatusCode {
  127. t.Errorf("testcase %d %s,\nwrong statuscode, have: %v, want: %v", i, tt.body, resp.StatusCode, tt.code)
  128. }
  129. }
  130. }
  131. // Tests that a graphQL request is not handled successfully when graphql is not enabled on the specified endpoint
  132. func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
  133. stack := createNode(t, false)
  134. defer stack.Close()
  135. if err := stack.Start(); err != nil {
  136. t.Fatalf("could not start node: %v", err)
  137. }
  138. body := strings.NewReader(`{"query": "{block{number}}","variables": null}`)
  139. resp, err := http.Post(fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), "application/json", body)
  140. if err != nil {
  141. t.Fatalf("could not post: %v", err)
  142. }
  143. bodyBytes, err := ioutil.ReadAll(resp.Body)
  144. if err != nil {
  145. t.Fatalf("could not read from response body: %v", err)
  146. }
  147. // make sure the request is not handled successfully
  148. if want, have := "404 page not found\n", string(bodyBytes); have != want {
  149. t.Errorf("have:\n%v\nwant:\n%v", have, want)
  150. }
  151. if want, have := 404, resp.StatusCode; want != have {
  152. t.Errorf("wrong statuscode, have:\n%v\nwant:%v", have, want)
  153. }
  154. }
  155. func createNode(t *testing.T, gqlEnabled bool) *node.Node {
  156. stack, err := node.New(&node.Config{
  157. HTTPHost: "127.0.0.1",
  158. HTTPPort: 9393,
  159. WSHost: "127.0.0.1",
  160. WSPort: 9393,
  161. })
  162. if err != nil {
  163. t.Fatalf("could not create node: %v", err)
  164. }
  165. if !gqlEnabled {
  166. return stack
  167. }
  168. createGQLService(t, stack, "127.0.0.1:9393")
  169. return stack
  170. }
  171. func createGQLService(t *testing.T, stack *node.Node, endpoint string) {
  172. // create backend
  173. ethConf := &eth.Config{
  174. Genesis: &core.Genesis{
  175. Config: params.AllEthashProtocolChanges,
  176. GasLimit: 11500000,
  177. Difficulty: big.NewInt(1048576),
  178. },
  179. Ethash: ethash.Config{
  180. PowMode: ethash.ModeFake,
  181. },
  182. NetworkId: 1337,
  183. TrieCleanCache: 5,
  184. TrieCleanCacheJournal: "triecache",
  185. TrieCleanCacheRejournal: 60 * time.Minute,
  186. TrieDirtyCache: 5,
  187. TrieTimeout: 60 * time.Minute,
  188. SnapshotCache: 5,
  189. }
  190. ethBackend, err := eth.New(stack, ethConf)
  191. if err != nil {
  192. t.Fatalf("could not create eth backend: %v", err)
  193. }
  194. // Create some blocks and import them
  195. chain, _ := core.GenerateChain(params.AllEthashProtocolChanges, ethBackend.BlockChain().Genesis(),
  196. ethash.NewFaker(), ethBackend.ChainDb(), 10, func(i int, gen *core.BlockGen) {})
  197. _, err = ethBackend.BlockChain().InsertChain(chain)
  198. if err != nil {
  199. t.Fatalf("could not create import blocks: %v", err)
  200. }
  201. // create gql service
  202. err = New(stack, ethBackend.APIBackend, []string{}, []string{})
  203. if err != nil {
  204. t.Fatalf("could not create graphql service: %v", err)
  205. }
  206. }