url_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 TestFullParseURL(t *testing.T) {
  16. expected := `dbname=database host=hostname.remote password=top\ secret port=1234 user=username`
  17. str, err := ParseURL("postgres://username:top%20secret@hostname.remote:1234/database")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. if str != expected {
  22. t.Fatalf("unexpected result from ParseURL:\n+ %s\n- %s", str, expected)
  23. }
  24. }
  25. func TestInvalidProtocolParseURL(t *testing.T) {
  26. _, err := ParseURL("http://hostname.remote")
  27. switch err {
  28. case nil:
  29. t.Fatal("Expected an error from parsing invalid protocol")
  30. default:
  31. msg := "invalid connection protocol: http"
  32. if err.Error() != msg {
  33. t.Fatalf("Unexpected error message:\n+ %s\n- %s",
  34. err.Error(), msg)
  35. }
  36. }
  37. }
  38. func TestMinimalURL(t *testing.T) {
  39. cs, err := ParseURL("postgres://")
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if cs != "" {
  44. t.Fatalf("expected blank connection string, got: %q", cs)
  45. }
  46. }