url_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package pq
  2. import (
  3. "testing"
  4. )
  5. func TestSimpleParseURL(t *testing.T) {
  6. expected := "host=hostname.remote"
  7. str, err := ParseURL("postgres://hostname.remote")
  8. if err != nil {
  9. t.Fatal(err)
  10. }
  11. if str != expected {
  12. t.Fatalf("unexpected result from ParseURL:\n+ %v\n- %v", str, expected)
  13. }
  14. }
  15. func TestIPv6LoopbackParseURL(t *testing.T) {
  16. expected := "host=::1 port=1234"
  17. str, err := ParseURL("postgres://[::1]:1234")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. if str != expected {
  22. t.Fatalf("unexpected result from ParseURL:\n+ %v\n- %v", str, expected)
  23. }
  24. }
  25. func TestFullParseURL(t *testing.T) {
  26. expected := `dbname=database host=hostname.remote password=top\ secret port=1234 user=username`
  27. str, err := ParseURL("postgres://username:top%20secret@hostname.remote:1234/database")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if str != expected {
  32. t.Fatalf("unexpected result from ParseURL:\n+ %s\n- %s", str, expected)
  33. }
  34. }
  35. func TestInvalidProtocolParseURL(t *testing.T) {
  36. _, err := ParseURL("http://hostname.remote")
  37. switch err {
  38. case nil:
  39. t.Fatal("Expected an error from parsing invalid protocol")
  40. default:
  41. msg := "invalid connection protocol: http"
  42. if err.Error() != msg {
  43. t.Fatalf("Unexpected error message:\n+ %s\n- %s",
  44. err.Error(), msg)
  45. }
  46. }
  47. }
  48. func TestMinimalURL(t *testing.T) {
  49. cs, err := ParseURL("postgres://")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if cs != "" {
  54. t.Fatalf("expected blank connection string, got: %q", cs)
  55. }
  56. }