Skip to content

Commit

Permalink
Add tests for the duplex ping-pong
Browse files Browse the repository at this point in the history
  • Loading branch information
zdraganov committed Nov 22, 2021
1 parent 739de31 commit a38d175
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions graphql/handler/transport/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,35 @@ func TestWebsocketGraphqltransportwsSubprotocol(t *testing.T) {
})
}

func TestWebsocketWithPingPongInterval(t *testing.T) {
handler := testserver.New()
handler.AddTransport(transport.Websocket{
PingPongInterval: time.Second * 1,
})

srv := httptest.NewServer(handler)
defer srv.Close()

t.Run("client receives ping and responds with pong", func(t *testing.T) {
c := wsConnectWithSubprocotol(srv.URL, graphqltransportwsSubprotocol)
defer c.Close()

require.NoError(t, c.WriteJSON(&operationMessage{Type: graphqltransportwsConnectionInitMsg}))
assert.Equal(t, graphqltransportwsPingMsg, readOp(c).Type)
require.NoError(t, c.WriteJSON(&operationMessage{Type: graphqltransportwsPongMsg}))
assert.Equal(t, graphqltransportwsPingMsg, readOp(c).Type)
})

t.Run("client sends ping and expects pong", func(t *testing.T) {
c := wsConnectWithSubprocotol(srv.URL, graphqltransportwsSubprotocol)
defer c.Close()

require.NoError(t, c.WriteJSON(&operationMessage{Type: graphqltransportwsConnectionInitMsg}))
require.NoError(t, c.WriteJSON(&operationMessage{Type: graphqltransportwsPingMsg}))
assert.Equal(t, graphqltransportwsPongMsg, readOp(c).Type)
})
}

func wsConnect(url string) *websocket.Conn {
return wsConnectWithSubprocotol(url, "")
}
Expand Down Expand Up @@ -374,6 +403,8 @@ const (
graphqltransportwsSubscribeMsg = "subscribe"
graphqltransportwsNextMsg = "next"
graphqltransportwsCompleteMsg = "complete"
graphqltransportwsPingMsg = "ping"
graphqltransportwsPongMsg = "pong"
)

type operationMessage struct {
Expand Down

0 comments on commit a38d175

Please sign in to comment.