-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split into 'roundtripWebSocketClient'
- Loading branch information
1 parent
f67ebe1
commit bf9a285
Showing
3 changed files
with
62 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Khan/genqlient/graphql" | ||
"github.com/gorilla/websocket" | ||
) | ||
|
||
type webSocketClient[T any] struct { | ||
wrapped graphql.WebSocketClient[T] | ||
t *testing.T | ||
} | ||
|
||
func (c *webSocketClient[_]) Start(ctx context.Context) (errChan chan error, err error) { | ||
return c.wrapped.Start(ctx) | ||
} | ||
|
||
func (c *webSocketClient[_]) Close() error { | ||
return c.wrapped.Close() | ||
} | ||
|
||
func (c *webSocketClient[T]) Subscribe(req *graphql.Request, dataChan chan graphql.WsResponse[T], forwardDataFunc graphql.ForwardDataFunction[T]) (string, error) { | ||
return c.wrapped.Subscribe(req, dataChan, forwardDataFunc) | ||
} | ||
|
||
func (c *webSocketClient[_]) Unsubscribe(subscriptionID string) error { | ||
return c.wrapped.Unsubscribe(subscriptionID) | ||
} | ||
|
||
type MyDialer struct { | ||
*websocket.Dialer | ||
} | ||
|
||
func (md *MyDialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (graphql.WSConn, error) { | ||
conn, resp, err := md.Dialer.DialContext(ctx, urlStr, requestHeader) | ||
resp.Body.Close() | ||
return graphql.WSConn(conn), err | ||
} | ||
|
||
func wsAdress(endpoint string) string { | ||
if !strings.HasPrefix(endpoint, "ws") { | ||
_, address, _ := strings.Cut(endpoint, "://") | ||
endpoint = "ws://" + address | ||
} | ||
return endpoint | ||
} | ||
|
||
func newCountWebSocketClient(t *testing.T, endpoint string) graphql.WebSocketClient[countResponse] { | ||
return &webSocketClient[countResponse]{ | ||
wrapped: countClientUsingWebSocket( | ||
wsAdress(endpoint), | ||
&MyDialer{Dialer: websocket.DefaultDialer}, | ||
nil, | ||
), | ||
t: t, | ||
} | ||
} |