builder_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package rdsutils_test
  2. import (
  3. "net/url"
  4. "regexp"
  5. "testing"
  6. "github.com/aws/aws-sdk-go/aws/credentials"
  7. "github.com/aws/aws-sdk-go/service/rds/rdsutils"
  8. )
  9. func TestConnectionStringBuilder(t *testing.T) {
  10. cases := []struct {
  11. user string
  12. endpoint string
  13. region string
  14. dbName string
  15. values url.Values
  16. format rdsutils.ConnectionFormat
  17. creds *credentials.Credentials
  18. expectedErr error
  19. expectedConnectRegex string
  20. }{
  21. {
  22. user: "foo",
  23. endpoint: "foo.bar",
  24. region: "region",
  25. dbName: "name",
  26. format: rdsutils.NoConnectionFormat,
  27. creds: credentials.NewStaticCredentials("AKID", "SECRET", "SESSION"),
  28. expectedErr: rdsutils.ErrNoConnectionFormat,
  29. expectedConnectRegex: "",
  30. },
  31. {
  32. user: "foo",
  33. endpoint: "foo.bar",
  34. region: "region",
  35. dbName: "name",
  36. format: rdsutils.TCPFormat,
  37. creds: credentials.NewStaticCredentials("AKID", "SECRET", "SESSION"),
  38. expectedConnectRegex: `^foo:foo.bar\?Action=connect\&DBUser=foo.*\@tcp\(foo.bar\)/name`,
  39. },
  40. }
  41. for _, c := range cases {
  42. b := rdsutils.NewConnectionStringBuilder(c.endpoint, c.region, c.user, c.dbName, c.creds)
  43. connectStr, err := b.WithFormat(c.format).Build()
  44. if e, a := c.expectedErr, err; e != a {
  45. t.Errorf("expected %v error, but received %v", e, a)
  46. }
  47. if re, a := regexp.MustCompile(c.expectedConnectRegex), connectStr; !re.MatchString(a) {
  48. t.Errorf("expect %s to match %s", re, a)
  49. }
  50. }
  51. }