Skip to content

Commit

Permalink
Fix building of connection string parameters (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcwarman authored Feb 2, 2021
1 parent 0ff7af9 commit 9cbcf9a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion postgresql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (c *Config) connParams() []string {

paramsArray := []string{}
for key, value := range params {
paramsArray = append(paramsArray, "%s=%s", key, url.QueryEscape(value))
paramsArray = append(paramsArray, fmt.Sprintf("%s=%s", key, url.QueryEscape(value)))
}

return paramsArray
Expand Down
69 changes: 69 additions & 0 deletions postgresql/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package postgresql

import (
"reflect"
"sort"
"strings"
"testing"

"github.com/blang/semver"
)

func TestConfigConnParams(t *testing.T) {
var tests = []struct {
input *Config
want []string
}{
{&Config{ConnectTimeoutSec: 10}, []string{"connect_timeout=10"}},
{&Config{Scheme: "postgres", SSLMode: "disable"}, []string{"connect_timeout=0", "sslmode=disable"}},
{&Config{Scheme: "awspostgres", SSLMode: "disable"}, []string{"connect_timeout=0"}},
{&Config{ExpectedVersion: semver.MustParse("9.0.0"), ApplicationName: "Terraform provider"}, []string{"connect_timeout=0", "fallback_application_name=Terraform+provider"}},
{&Config{ExpectedVersion: semver.MustParse("8.0.0"), ApplicationName: "Terraform provider"}, []string{"connect_timeout=0"}},
{&Config{SSLClientCert: &ClientCertificateConfig{CertificatePath: "/path/to/public-certificate.pem", KeyPath: "/path/to/private-key.pem"}}, []string{"connect_timeout=0", "sslcert=%2Fpath%2Fto%2Fpublic-certificate.pem", "sslkey=%2Fpath%2Fto%2Fprivate-key.pem"}},
{&Config{SSLRootCertPath: "/path/to/root.pem"}, []string{"connect_timeout=0", "sslrootcert=%2Fpath%2Fto%2Froot.pem"}},
}

for _, test := range tests {

connParams := test.input.connParams()

sort.Strings(connParams)
sort.Strings(test.want)

if !reflect.DeepEqual(connParams, test.want) {
t.Errorf("Config.connParams(%+v) returned %#v, want %#v", test.input, connParams, test.want)
}

}
}

func TestConfigConnStr(t *testing.T) {
var tests = []struct {
input *Config
wantDbURL string
wantDbParams []string
}{
{&Config{Scheme: "postgres", Host: "localhost", Port: 5432, Username: "postgres_user", Password: "postgres_password", SSLMode: "disable"}, "postgres://postgres_user:postgres_password@localhost:5432/postgres", []string{"connect_timeout=0", "sslmode=disable"}},
}

for _, test := range tests {

connStr := test.input.connStr("postgres")

splitConnStr := strings.Split(connStr, "?")

if splitConnStr[0] != test.wantDbURL {
t.Errorf("Config.connStr(%+v) returned %#v, want %#v", test.input, splitConnStr[0], test.wantDbURL)
}

connParams := strings.Split(splitConnStr[1], "&")

sort.Strings(connParams)
sort.Strings(test.wantDbParams)

if !reflect.DeepEqual(connParams, test.wantDbParams) {
t.Errorf("Config.connStr(%+v) returned %#v, want %#v", test.input, connParams, test.wantDbParams)
}

}
}

0 comments on commit 9cbcf9a

Please sign in to comment.