Skip to content
Merged
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
7 changes: 6 additions & 1 deletion pgconn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,9 @@ func parseURLSettings(connString string) (map[string]string, error) {
}

if parsedURL.User != nil {
settings["user"] = parsedURL.User.Username()
if u := parsedURL.User.Username(); u != "" {
settings["user"] = u
}
if password, present := parsedURL.User.Password(); present {
settings["password"] = password
}
Expand Down Expand Up @@ -618,6 +620,9 @@ func parseKeywordValueSettings(s string) (map[string]string, error) {
return nil, errors.New("invalid keyword/value")
}

if key == "user" && val == "" {
continue
}
settings[key] = val
}

Expand Down
53 changes: 53 additions & 0 deletions pgconn/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,3 +1145,56 @@ application_name = spaced string
assertConfigsEqual(t, tt.config, config, fmt.Sprintf("Test %d (%s)", i, tt.name))
}
}

func TestParseConfigExplicitEmptyUserDefaultsToOSUser(t *testing.T) {
skipOnWindows(t)
clearPgEnvvars(t)

currentUser, err := user.Current()
if err != nil {
t.Skip("cannot determine current OS user")
}

tests := []struct {
name string
connString string
expected string
}{
{
name: "keyword value explicit empty user",
connString: "host=localhost dbname=test user=",
expected: currentUser.Username,
},
{
name: "keyword value quoted empty user",
connString: "host=localhost dbname=test user=''",
expected: currentUser.Username,
},
{
name: "url explicit empty user without password",
connString: "postgres://@localhost/test",
expected: currentUser.Username,
},
{
name: "url explicit empty user with password",
connString: "postgres://:secret@localhost/test",
expected: currentUser.Username,
},
}

for i, tt := range tests {
config, err := pgconn.ParseConfig(tt.connString)
if !assert.NoErrorf(t, err, "Test %d (%s)", i, tt.name) {
continue
}

assert.Equalf(
t,
tt.expected,
config.User,
"Test %d (%s): unexpected user",
i,
tt.name,
)
}
}