forked from sony/appsync-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql_client_test.go
82 lines (76 loc) · 1.8 KB
/
graphql_client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package appsync
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/sony/appsync-client-go/graphql"
)
func TestPostReturnsData(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := graphql.Response{
Data: "data",
Errors: nil,
}
b, err := json.Marshal(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(b)
}))
defer server.Close()
api := NewGraphQLClient(graphql.NewClient(server.URL))
res, err := api.Post(http.Header{}, graphql.PostRequest{})
if err != nil {
t.Fatal(err)
}
if res == nil {
t.FailNow()
}
if res.Data != "data" {
t.FailNow()
}
if res.Errors != nil {
t.Fatal(res.Errors)
}
if *res.StatusCode != http.StatusOK {
t.Fatal(res.StatusCode)
}
}
func TestPostAsyncReturnsData(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := graphql.Response{
Data: "data",
Errors: nil,
}
b, err := json.Marshal(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(b)
}))
defer server.Close()
ch := make(chan ret, 1)
api := NewGraphQLClient(graphql.NewClient(server.URL))
cancel, err := api.PostAsync(http.Header{}, graphql.PostRequest{}, func(r *graphql.Response, err error) { ch <- ret{r, err} })
if err != nil {
t.Fatal(err)
}
if cancel == nil {
t.FailNow()
}
ret := <-ch
if ret.response.Data != "data" {
t.FailNow()
}
if ret.response.Errors != nil {
t.Fatal(ret.response.Errors)
}
if *ret.response.StatusCode != http.StatusOK {
t.Fatal(ret.response.StatusCode)
}
}