Skip to content

Commit

Permalink
fix(rpc): pass gRPC meta as headers
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 13, 2023
1 parent 6065b4a commit a5b596a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Fix passing RPC context via headers when using HTTP RPC. ([@palkan][])

- Add support for batched broadcasts. ([@palkan][])

It's now possible to publish an array of broadcasting messages (e.g., `[{"stream":"a","data":"..."},"stream":"b","data":"..."}]`). The messages will be delivered in the same order as they were published (within a stream).
Expand Down
2 changes: 1 addition & 1 deletion rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (s *HTTPService) performRequest(ctx context.Context, path string, payload [
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.conf.Secret))
}

if md, _, ok := metadata.FromOutgoingContextRaw(ctx); ok {
if md, ok := metadata.FromIncomingContext(ctx); ok {
// Set headers from metadata
for k, v := range md {
req.Header.Set(fmt.Sprintf("x-anycable-meta-%s", k), v[0])
Expand Down
6 changes: 3 additions & 3 deletions rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestHTTPServiceRPC(t *testing.T) {
}

md := metadata.Pairs("album", "Kamni", "year", "2008")
ctx := metadata.NewOutgoingContext(context.Background(), md)
ctx := metadata.NewIncomingContext(context.Background(), md)
res, err := service.Connect(ctx, protocol.NewConnectMessage(
common.NewSessionEnv("ws://anycable.io/cable", &map[string]string{"cookie": "foo=bar"}),
))
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestHTTPServiceRPC(t *testing.T) {
}

md := metadata.Pairs("error", "test error")
ctx := metadata.NewOutgoingContext(context.Background(), md)
ctx := metadata.NewIncomingContext(context.Background(), md)
res, err := service.Disconnect(ctx, protocol.NewDisconnectMessage(
common.NewSessionEnv("ws://anycable.io/cable", &map[string]string{"cookie": "foo=bar"}),
"test-session",
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestHTTPServiceRPC(t *testing.T) {
}

md := metadata.Pairs("track", "easy-way-out")
ctx := metadata.NewOutgoingContext(context.Background(), md)
ctx := metadata.NewIncomingContext(context.Background(), md)
res, err := service.Command(ctx, protocol.NewCommandMessage(
common.NewSessionEnv("ws://anycable.io/cable", &map[string]string{"cookie": "foo=bar"}),
"subscribe",
Expand Down
12 changes: 12 additions & 0 deletions rpc/rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rpc

import (
"context"
"errors"
"testing"

Expand All @@ -11,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
)

type MockState struct {
Expand Down Expand Up @@ -478,5 +480,15 @@ func TestCustomDialFun(t *testing.T) {
assert.Equal(t, "user=john", res.Identifier)
assert.Equal(t, map[string]string{"_s_": "test-session"}, res.CState)
assert.Empty(t, res.Broadcasts)

call := service.Calls[0]
requestCtx, ok := call.Arguments[0].(context.Context)

require.True(t, ok)

md, ok := metadata.FromIncomingContext(requestCtx)
require.True(t, ok)

assert.Equal(t, []string{"42"}, md.Get("sid"))
})
}

0 comments on commit a5b596a

Please sign in to comment.