github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/interceptor_test.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/milvus-io/milvus-sdk-go/v2/internal/utils/crypto"
     9  	"github.com/stretchr/testify/assert"
    10  	"google.golang.org/grpc/metadata"
    11  )
    12  
    13  const (
    14  	testDBName = "TestDBName"
    15  )
    16  
    17  func TestAuthenticationInterceptor(t *testing.T) {
    18  	ctx := context.Background()
    19  	ctx = authenticationInterceptor(ctx, testUsername, testPassword)
    20  	md, ok := metadata.FromOutgoingContext(ctx)
    21  	value := crypto.Base64Encode(fmt.Sprintf("%s:%s", testUsername, testPassword))
    22  	assert.True(t, ok)
    23  	assert.Equal(t, 1, len(md["authorization"]))
    24  	assert.Equal(t, value, md["authorization"][0])
    25  }
    26  
    27  func TestDBNameInterceptor(t *testing.T) {
    28  	ctx := context.Background()
    29  	ctx = databaseNameInterceptor(ctx, func() string { return testDBName })
    30  	md, ok := metadata.FromOutgoingContext(ctx)
    31  	assert.True(t, ok)
    32  	assert.Equal(t, 1, len(md["dbname"]))
    33  	assert.Equal(t, testDBName, md["dbname"][0])
    34  }
    35  
    36  func TestAPIInterceptor(t *testing.T) {
    37  	ctx := context.Background()
    38  	ctx = apiKeyInterceptor(ctx, "test-token")
    39  	md, ok := metadata.FromOutgoingContext(ctx)
    40  	value := crypto.Base64Encode("test-token")
    41  	assert.True(t, ok)
    42  	assert.Equal(t, 1, len(md[authorizationHeader]))
    43  	assert.Equal(t, value, md[authorizationHeader][0])
    44  }