options_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // +build go1.7
  2. package redis
  3. import (
  4. "errors"
  5. "testing"
  6. )
  7. func TestParseURL(t *testing.T) {
  8. cases := []struct {
  9. u string
  10. addr string
  11. db int
  12. tls bool
  13. err error
  14. }{
  15. {
  16. "redis://localhost:123/1",
  17. "localhost:123",
  18. 1, false, nil,
  19. },
  20. {
  21. "redis://localhost:123",
  22. "localhost:123",
  23. 0, false, nil,
  24. },
  25. {
  26. "redis://localhost/1",
  27. "localhost:6379",
  28. 1, false, nil,
  29. },
  30. {
  31. "redis://12345",
  32. "12345:6379",
  33. 0, false, nil,
  34. },
  35. {
  36. "rediss://localhost:123",
  37. "localhost:123",
  38. 0, true, nil,
  39. },
  40. {
  41. "redis://localhost/?abc=123",
  42. "",
  43. 0, false, errors.New("no options supported"),
  44. },
  45. {
  46. "http://google.com",
  47. "",
  48. 0, false, errors.New("invalid redis URL scheme: http"),
  49. },
  50. {
  51. "redis://localhost/1/2/3/4",
  52. "",
  53. 0, false, errors.New("invalid redis URL path: /1/2/3/4"),
  54. },
  55. {
  56. "12345",
  57. "",
  58. 0, false, errors.New("invalid redis URL scheme: "),
  59. },
  60. {
  61. "redis://localhost/iamadatabase",
  62. "",
  63. 0, false, errors.New(`invalid redis database number: "iamadatabase"`),
  64. },
  65. }
  66. for _, c := range cases {
  67. t.Run(c.u, func(t *testing.T) {
  68. o, err := ParseURL(c.u)
  69. if c.err == nil && err != nil {
  70. t.Fatalf("unexpected error: '%q'", err)
  71. return
  72. }
  73. if c.err != nil && err != nil {
  74. if c.err.Error() != err.Error() {
  75. t.Fatalf("got %q, expected %q", err, c.err)
  76. }
  77. return
  78. }
  79. if o.Addr != c.addr {
  80. t.Errorf("got %q, want %q", o.Addr, c.addr)
  81. }
  82. if o.DB != c.db {
  83. t.Errorf("got %q, expected %q", o.DB, c.db)
  84. }
  85. if c.tls && o.TLSConfig == nil {
  86. t.Errorf("got nil TLSConfig, expected a TLSConfig")
  87. }
  88. })
  89. }
  90. }