Files

45 lines
1.3 KiB
Go

package kmsvc
import (
"context"
"testing"
kafkamgmtv1 "forgejo.riotpiao.com/rock/kmsvc-proto/gen/kafkamgmt/v1"
)
func TestClientAttachesBearerToken(t *testing.T) {
fake := &fakeQueueService{
sendMessage: func(ctx context.Context, req *kafkamgmtv1.SendMessageRequest) (*kafkamgmtv1.SendMessageResponse, error) {
return &kafkamgmtv1.SendMessageResponse{MessageId: "m1"}, nil
},
}
client := newTestClient(t, fake, WithTokenSource(StaticToken("test-token")))
_, err := client.SendMessage(context.Background(), SendMessageInput{QueueName: "q", Body: []byte("hi")})
if err != nil {
t.Fatalf("SendMessage: %v", err)
}
if got, want := fake.lastIncomingAuth, "Bearer test-token"; got != want {
t.Errorf("authorization header = %q, want %q", got, want)
}
}
func TestClientWithoutTokenSourceSendsNoAuthHeader(t *testing.T) {
fake := &fakeQueueService{
sendMessage: func(ctx context.Context, req *kafkamgmtv1.SendMessageRequest) (*kafkamgmtv1.SendMessageResponse, error) {
return &kafkamgmtv1.SendMessageResponse{MessageId: "m1"}, nil
},
}
client := newTestClient(t, fake)
_, err := client.SendMessage(context.Background(), SendMessageInput{QueueName: "q", Body: []byte("hi")})
if err != nil {
t.Fatalf("SendMessage: %v", err)
}
if fake.lastIncomingAuth != "" {
t.Errorf("authorization header = %q, want empty", fake.lastIncomingAuth)
}
}