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 {
310
310
}
311
311
case "" :
312
312
if p .AuthSource == "" {
313
- p .AuthSource = "admin"
313
+ p .AuthSource = dbName
314
+ if p .AuthSource == "" {
315
+ p .AuthSource = "admin"
316
+ }
314
317
}
315
318
default :
316
319
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) {
66
66
}
67
67
}
68
68
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
+
69
94
func TestConnect (t * testing.T ) {
70
95
tests := []struct {
71
96
s string
You can’t perform that action at this time.
0 commit comments