credential_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package auth
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "google.golang.org/grpc/metadata"
  7. )
  8. func TestParseCredential(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. withNil bool
  12. withEmptyMd bool
  13. app string
  14. token string
  15. }{
  16. {
  17. name: "nil",
  18. withNil: true,
  19. },
  20. {
  21. name: "empty md",
  22. withEmptyMd: true,
  23. },
  24. {
  25. name: "empty",
  26. },
  27. {
  28. name: "valid",
  29. app: "foo",
  30. token: "bar",
  31. },
  32. }
  33. for _, test := range tests {
  34. test := test
  35. t.Run(test.name, func(t *testing.T) {
  36. t.Parallel()
  37. var ctx context.Context
  38. if test.withNil {
  39. ctx = context.Background()
  40. } else if test.withEmptyMd {
  41. ctx = metadata.NewIncomingContext(context.Background(), metadata.MD{})
  42. } else {
  43. md := metadata.New(map[string]string{
  44. "app": test.app,
  45. "token": test.token,
  46. })
  47. ctx = metadata.NewIncomingContext(context.Background(), md)
  48. }
  49. cred := ParseCredential(ctx)
  50. assert.False(t, cred.RequireTransportSecurity())
  51. m, err := cred.GetRequestMetadata(context.Background())
  52. assert.Nil(t, err)
  53. assert.Equal(t, test.app, m[appKey])
  54. assert.Equal(t, test.token, m[tokenKey])
  55. })
  56. }
  57. }