forked from machinebox/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql_subscription_test.go
90 lines (69 loc) · 2.26 KB
/
graphql_subscription_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
83
84
85
86
87
88
89
90
package graphql
import (
"context"
"encoding/json"
"flag"
"github.com/gorilla/websocket"
"github.com/matryer/is"
"log"
"net/http"
"net/http/httptest"
"testing"
)
var upgrader = websocket.Upgrader{} // use default options
func TestSub(t *testing.T) {
is := is.New(t)
flag.Parse()
log.SetFlags(0)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
is.NoErr(err)
var pl subscriptionMessage
defer c.Close()
is.NoErr(c.ReadJSON(&pl))
is.Equal(string(pl.Type), string(gql_connection_init))
pl.Type = gql_connection_ack
is.NoErr(c.WriteJSON(pl))
is.NoErr(c.ReadJSON(&pl))
is.Equal(string(pl.Type), gql_start)
var tmp1 struct {Query string `json:"query"`; Variables map[string]string `json:"variables"`}
is.NoErr(json.Unmarshal(*pl.Payload, &tmp1))
is.Equal(tmp1.Query, `subscription ($q: String) { cnt }`)
is.Equal(len(tmp1.Variables),1)
is.Equal(tmp1.Variables["q"],"foo")
is.Equal(*pl.Id, "0")
id := *pl.Id
pl.Payload = nil
pl.Id = nil
pl.Type = gql_connection_ack
is.NoErr(c.WriteJSON(pl))
pl.Id = &id
pl_pl := json.RawMessage(`{"data": "bar"}`)
pl.Payload = &pl_pl
pl.Type = gql_data
is.NoErr(c.WriteJSON(pl))
is.NoErr(c.ReadJSON(&pl))
is.Equal(string(pl.Type), gql_stop)
is.Equal(*pl.Id, id)
pl.Payload = nil
pl.Type = gql_complete
is.NoErr(c.WriteJSON(pl))
is.NoErr(c.ReadJSON(&pl))
is.Equal(string(pl.Type), gql_connection_terminate)
}))
defer srv.Close()
ctx, cancel := context.WithCancel(context.Background())
client := NewClient(srv.URL)
header := http.Header{}
cl, err := client.SubscriptionClient(ctx, header)
is.NoErr(err)
vars := make(map[string]interface{})
vars["q"] = "foo"
sub, err := cl.Subscribe(&Request{Header:header, q:`subscription ($q: String) { cnt }`, vars: vars})
is.NoErr(err)
res := <- sub
is.Equal(string(*res.Data), `{"data":"bar"}`)
cl.Unsubscribe(sub)
defer cancel()
defer is.NoErr(cl.Close())
}