github.com/jepp2078/gqlgen@v0.7.2/example/chat/chat_test.go (about) 1 package chat 2 3 import ( 4 "net/http/httptest" 5 "testing" 6 "time" 7 8 "github.com/99designs/gqlgen/client" 9 "github.com/99designs/gqlgen/handler" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestChatSubscriptions(t *testing.T) { 15 srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(New()))) 16 c := client.New(srv.URL) 17 18 sub := c.Websocket(`subscription { messageAdded(roomName:"#gophers") { text createdBy } }`) 19 defer sub.Close() 20 21 go func() { 22 var resp interface{} 23 time.Sleep(10 * time.Millisecond) 24 err := c.Post(`mutation { 25 a:post(text:"Hello!", roomName:"#gophers", username:"vektah") { id } 26 b:post(text:"Whats up?", roomName:"#gophers", username:"vektah") { id } 27 }`, &resp) 28 assert.NoError(t, err) 29 }() 30 31 var msg struct { 32 resp struct { 33 MessageAdded struct { 34 Text string 35 CreatedBy string 36 } 37 } 38 err error 39 } 40 41 msg.err = sub.Next(&msg.resp) 42 require.NoError(t, msg.err, "sub.Next") 43 require.Equal(t, "Hello!", msg.resp.MessageAdded.Text) 44 require.Equal(t, "vektah", msg.resp.MessageAdded.CreatedBy) 45 46 msg.err = sub.Next(&msg.resp) 47 require.NoError(t, msg.err, "sub.Next") 48 require.Equal(t, "Whats up?", msg.resp.MessageAdded.Text) 49 require.Equal(t, "vektah", msg.resp.MessageAdded.CreatedBy) 50 }