github.com/aswedchain/aswed@v1.0.1/graphql/graphql_test.go (about) 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 17 package graphql 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 "strings" 24 "testing" 25 26 "github.com/aswedchain/aswed/eth" 27 "github.com/aswedchain/aswed/node" 28 "github.com/stretchr/testify/assert" 29 ) 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 42 // Tests that a graphQL request is successfully handled when graphql is enabled on the specified endpoint 43 func TestGraphQLHTTPOnSamePort_GQLRequest_Successful(t *testing.T) { 44 stack := createNode(t, true) 45 defer stack.Close() 46 // start node 47 if err := stack.Start(); err != nil { 48 t.Fatalf("could not start node: %v", err) 49 } 50 // create http request 51 body := strings.NewReader("{\"query\": \"{block{number}}\",\"variables\": null}") 52 gqlReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), body) 53 if err != nil { 54 t.Error("could not issue new http request ", err) 55 } 56 gqlReq.Header.Set("Content-Type", "application/json") 57 // read from response 58 resp := doHTTPRequest(t, gqlReq) 59 bodyBytes, err := ioutil.ReadAll(resp.Body) 60 if err != nil { 61 t.Fatalf("could not read from response body: %v", err) 62 } 63 expected := "{\"data\":{\"block\":{\"number\":\"0x0\"}}}" 64 assert.Equal(t, expected, string(bodyBytes)) 65 } 66 67 // Tests that a graphQL request is not handled successfully when graphql is not enabled on the specified endpoint 68 func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) { 69 stack := createNode(t, false) 70 defer stack.Close() 71 if err := stack.Start(); err != nil { 72 t.Fatalf("could not start node: %v", err) 73 } 74 75 // create http request 76 body := strings.NewReader("{\"query\": \"{block{number}}\",\"variables\": null}") 77 gqlReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), body) 78 if err != nil { 79 t.Error("could not issue new http request ", err) 80 } 81 gqlReq.Header.Set("Content-Type", "application/json") 82 // read from response 83 resp := doHTTPRequest(t, gqlReq) 84 bodyBytes, err := ioutil.ReadAll(resp.Body) 85 if err != nil { 86 t.Fatalf("could not read from response body: %v", err) 87 } 88 // make sure the request is not handled successfully 89 assert.Equal(t, 404, resp.StatusCode) 90 assert.Equal(t, "404 page not found\n", string(bodyBytes)) 91 } 92 93 func createNode(t *testing.T, gqlEnabled bool) *node.Node { 94 stack, err := node.New(&node.Config{ 95 HTTPHost: "127.0.0.1", 96 HTTPPort: 9393, 97 WSHost: "127.0.0.1", 98 WSPort: 9393, 99 }) 100 if err != nil { 101 t.Fatalf("could not create node: %v", err) 102 } 103 if !gqlEnabled { 104 return stack 105 } 106 107 createGQLService(t, stack, "127.0.0.1:9393") 108 109 return stack 110 } 111 112 func createGQLService(t *testing.T, stack *node.Node, endpoint string) { 113 // create backend 114 ethBackend, err := eth.New(stack, ð.DefaultConfig) 115 if err != nil { 116 t.Fatalf("could not create eth backend: %v", err) 117 } 118 119 // create gql service 120 err = New(stack, ethBackend.APIBackend, []string{}, []string{}) 121 if err != nil { 122 t.Fatalf("could not create graphql service: %v", err) 123 } 124 } 125 126 func doHTTPRequest(t *testing.T, req *http.Request) *http.Response { 127 client := &http.Client{} 128 resp, err := client.Do(req) 129 if err != nil { 130 t.Fatal("could not issue a GET request to the given endpoint", err) 131 132 } 133 return resp 134 }