Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(option): skip nil bytes for CredentialsJSON #2643

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func WithCredentialsJSON(p []byte) ClientOption {
type withCredentialsJSON []byte

func (w withCredentialsJSON) Apply(o *internal.DialSettings) {
if w == nil {
return
}
o.CredentialsJSON = make([]byte, len(w))
copy(o.CredentialsJSON, w)
}
Expand Down
33 changes: 31 additions & 2 deletions option/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
package option

import (
"testing"

"crypto/tls"
"math/big"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -133,3 +132,33 @@ func TestApplyClientCertSource(t *testing.T) {
t.Errorf(cmp.Diff(certGot, certWant, cmpopts.IgnoreUnexported(big.Int{}), cmpopts.IgnoreFields(tls.Certificate{}, "Leaf")))
}
}

func TestCredentialsJSON(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var got internal.DialSettings

WithCredentialsJSON(nil).Apply(&got)

want := internal.DialSettings{
CredentialsJSON: nil,
}

if !cmp.Equal(got, want) {
t.Errorf(cmp.Diff(got, want))
}
})

t.Run("empty", func(t *testing.T) {
var got internal.DialSettings

WithCredentialsJSON([]byte{}).Apply(&got)

want := internal.DialSettings{
CredentialsJSON: []byte{},
}

if !cmp.Equal(got, want) {
t.Errorf(cmp.Diff(got, want))
}
})
}