File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -310,7 +310,10 @@ func (p *parser) setDefaultAuthParams(dbName string) error {
310310 }
311311 case "" :
312312 if p .AuthSource == "" {
313- p .AuthSource = "admin"
313+ p .AuthSource = dbName
314+ if p .AuthSource == "" {
315+ p .AuthSource = "admin"
316+ }
314317 }
315318 default :
316319 return fmt .Errorf ("invalid auth mechanism" )
Original file line number Diff line number Diff line change @@ -66,6 +66,31 @@ func TestAuthMechanism(t *testing.T) {
6666 }
6767}
6868
69+ func TestAuthSource (t * testing.T ) {
70+ tests := []struct {
71+ s string
72+ expected string
73+ err bool
74+ }{
75+ {s : "foobar?authSource=bazqux" , expected : "bazqux" },
76+ {s : "foobar" , expected : "foobar" },
77+ {s : "" , expected : "admin" },
78+ }
79+
80+ for _ , test := range tests {
81+ s := fmt .Sprintf ("mongodb://user:pass@localhost/%s" , test .s )
82+ t .Run (s , func (t * testing.T ) {
83+ cs , err := connstring .Parse (s )
84+ if test .err {
85+ require .Error (t , err )
86+ } else {
87+ require .NoError (t , err )
88+ require .Equal (t , test .expected , cs .AuthSource )
89+ }
90+ })
91+ }
92+ }
93+
6994func TestConnect (t * testing.T ) {
7095 tests := []struct {
7196 s string
You can’t perform that action at this time.
0 commit comments