Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket subscribe missing header bugfix #3383

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions graphql/handler/transport/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type (
receivedPong bool
exec graphql.GraphExecutor
closed bool
headers http.Header

initPayload InitPayload
}
Expand Down Expand Up @@ -119,6 +120,7 @@ func (t Websocket) Do(w http.ResponseWriter, r *http.Request, exec graphql.Graph
ctx: r.Context(),
exec: exec,
me: me,
headers: r.Header,
Websocket: t,
}

Expand Down Expand Up @@ -387,6 +389,8 @@ func (c *wsConnection) subscribe(start time.Time, msg *message) {
End: graphql.Now(),
}

params.Headers = c.headers

rc, err := c.exec.CreateOperationContext(ctx, params)
if err != nil {
resp := c.exec.DispatchError(graphql.WithOperationContext(ctx, rc), err)
Expand Down
42 changes: 42 additions & 0 deletions graphql/handler/transport/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,48 @@ func TestWebsocketWithKeepAlive(t *testing.T) {
assert.Equal(t, connectionKeepAliveMsg, msg.Type)
}

func TestWebsocketWithPassedHeaders(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.Websocket{
KeepAlivePingInterval: 100 * time.Millisecond,
})

h.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
assert.NotNil(t, graphql.GetOperationContext(ctx).Headers)

return next(ctx)
})

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

c := wsConnect(srv.URL)
defer c.Close()

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))
assert.Equal(t, connectionAckMsg, readOp(c).Type)
assert.Equal(t, connectionKeepAliveMsg, readOp(c).Type)

require.NoError(t, c.WriteJSON(&operationMessage{
Type: startMsg,
ID: "test_1",
Payload: json.RawMessage(`{"query": "subscription { name }"}`),
}))

// keepalive
msg := readOp(c)
assert.Equal(t, connectionKeepAliveMsg, msg.Type)

// server message
h.SendNextSubscriptionMessage()
msg = readOp(c)
assert.Equal(t, dataMsg, msg.Type)

// keepalive
msg = readOp(c)
assert.Equal(t, connectionKeepAliveMsg, msg.Type)
}

func TestWebsocketInitFunc(t *testing.T) {
t.Run("accept connection if WebsocketInitFunc is NOT provided", func(t *testing.T) {
h := testserver.New()
Expand Down
Loading