-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Update metadata keys in fetchSession functions and refreshG…
…rpcHeaders Update the metadata keys to use constants instead of literal strings in the fetchSessionPeerID, fetchSessionUserAddress, and refreshGrpcHeaders functions.
- Loading branch information
Showing
3 changed files
with
86 additions
and
16 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,70 @@ | ||
package local_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/di-dao/sonr/internal/local" | ||
"github.com/segmentio/ksuid" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
const ( | ||
valAddr = "validator-address" | ||
chainID = "chain-id" | ||
) | ||
|
||
func setupMetadata() metadata.MD { | ||
return metadata.Pairs( | ||
local.MetadataSessionIDKey, ksuid.New().String(), | ||
local.MetadataAuthTokenKey, "token", | ||
local.MetadataUserAddressKey, "user-address", | ||
local.MetadataServiceOriginKey, "service-origin", | ||
local.MetadataIPFSPeerIDKey, "peer-id", | ||
) | ||
} | ||
|
||
func TestUnwrapContext_NoMetadata(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// Unwrap context with no metadata | ||
sctx := local.UnwrapContext(ctx) | ||
if sctx.SessionID == "" { | ||
t.Errorf("Expected SessionID to be set, got empty string") | ||
} | ||
if sctx.Token != "" { | ||
t.Errorf("Expected Token to be empty, got %s", sctx.Token) | ||
} | ||
if sctx.UserAddress != "" { | ||
t.Errorf("Expected UserAddress to be empty, got %s", sctx.UserAddress) | ||
} | ||
if sctx.ServiceOrigin != "" { | ||
t.Errorf("Expected ServiceOrigin to be empty, got %s", sctx.ServiceOrigin) | ||
} | ||
if sctx.PeerID != "" { | ||
t.Errorf("Expected PeerID to be empty, got %s", sctx.PeerID) | ||
} | ||
} | ||
|
||
func TestUnwrapContext_WithMetadata(t *testing.T) { | ||
md := setupMetadata() | ||
ctx := metadata.NewIncomingContext(context.Background(), md) | ||
|
||
// Unwrap context with metadata | ||
sctx := local.UnwrapContext(ctx) | ||
if sctx.SessionID == "" { | ||
t.Errorf("Expected SessionID to be set, got empty string") | ||
} | ||
if sctx.Token == "" { | ||
t.Errorf("Expected Token to be set, got empty string") | ||
} | ||
if sctx.UserAddress == "" { | ||
t.Errorf("Expected UserAddress to be set, got empty string") | ||
} | ||
if sctx.ServiceOrigin == "" { | ||
t.Errorf("Expected ServiceOrigin to be set, got empty string") | ||
} | ||
if sctx.PeerID == "" { | ||
t.Errorf("Expected PeerID to be set, got empty string") | ||
} | ||
} |