graphql_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "net/http"
  21. "strings"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/eth"
  24. "github.com/ethereum/go-ethereum/node"
  25. "github.com/stretchr/testify/assert"
  26. )
  27. func TestBuildSchema(t *testing.T) {
  28. stack, err := node.New(&node.DefaultConfig)
  29. if err != nil {
  30. t.Fatalf("could not create new node: %v", err)
  31. }
  32. // Make sure the schema can be parsed and matched up to the object model.
  33. if err := newHandler(stack, nil, []string{}, []string{}); err != nil {
  34. t.Errorf("Could not construct GraphQL handler: %v", err)
  35. }
  36. }
  37. // Tests that a graphQL request is successfully handled when graphql is enabled on the specified endpoint
  38. func TestGraphQLHTTPOnSamePort_GQLRequest_Successful(t *testing.T) {
  39. stack := createNode(t, true)
  40. defer stack.Close()
  41. // start node
  42. if err := stack.Start(); err != nil {
  43. t.Fatalf("could not start node: %v", err)
  44. }
  45. // create http request
  46. body := strings.NewReader("{\"query\": \"{block{number}}\",\"variables\": null}")
  47. gqlReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), body)
  48. if err != nil {
  49. t.Error("could not issue new http request ", err)
  50. }
  51. gqlReq.Header.Set("Content-Type", "application/json")
  52. // read from response
  53. resp := doHTTPRequest(t, gqlReq)
  54. bodyBytes, err := ioutil.ReadAll(resp.Body)
  55. if err != nil {
  56. t.Fatalf("could not read from response body: %v", err)
  57. }
  58. expected := "{\"data\":{\"block\":{\"number\":\"0x0\"}}}"
  59. assert.Equal(t, expected, string(bodyBytes))
  60. }
  61. // Tests that a graphQL request is not handled successfully when graphql is not enabled on the specified endpoint
  62. func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
  63. stack := createNode(t, false)
  64. defer stack.Close()
  65. if err := stack.Start(); err != nil {
  66. t.Fatalf("could not start node: %v", err)
  67. }
  68. // create http request
  69. body := strings.NewReader("{\"query\": \"{block{number}}\",\"variables\": null}")
  70. gqlReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), body)
  71. if err != nil {
  72. t.Error("could not issue new http request ", err)
  73. }
  74. gqlReq.Header.Set("Content-Type", "application/json")
  75. // read from response
  76. resp := doHTTPRequest(t, gqlReq)
  77. bodyBytes, err := ioutil.ReadAll(resp.Body)
  78. if err != nil {
  79. t.Fatalf("could not read from response body: %v", err)
  80. }
  81. // make sure the request is not handled successfully
  82. assert.Equal(t, 404, resp.StatusCode)
  83. assert.Equal(t, "404 page not found\n", string(bodyBytes))
  84. }
  85. func createNode(t *testing.T, gqlEnabled bool) *node.Node {
  86. stack, err := node.New(&node.Config{
  87. HTTPHost: "127.0.0.1",
  88. HTTPPort: 9393,
  89. WSHost: "127.0.0.1",
  90. WSPort: 9393,
  91. })
  92. if err != nil {
  93. t.Fatalf("could not create node: %v", err)
  94. }
  95. if !gqlEnabled {
  96. return stack
  97. }
  98. createGQLService(t, stack, "127.0.0.1:9393")
  99. return stack
  100. }
  101. func createGQLService(t *testing.T, stack *node.Node, endpoint string) {
  102. // create backend
  103. ethBackend, err := eth.New(stack, &eth.DefaultConfig)
  104. if err != nil {
  105. t.Fatalf("could not create eth backend: %v", err)
  106. }
  107. // create gql service
  108. err = New(stack, ethBackend.APIBackend, []string{}, []string{})
  109. if err != nil {
  110. t.Fatalf("could not create graphql service: %v", err)
  111. }
  112. }
  113. func doHTTPRequest(t *testing.T, req *http.Request) *http.Response {
  114. client := &http.Client{}
  115. resp, err := client.Do(req)
  116. if err != nil {
  117. t.Fatal("could not issue a GET request to the given endpoint", err)
  118. }
  119. return resp
  120. }