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