File: url_param_test.rb

package info (click to toggle)
ruby-redis 4.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,168 kB
  • sloc: ruby: 12,820; makefile: 107; sh: 24
file content (137 lines) | stat: -rw-r--r-- 4,248 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true

require_relative "helper"

class TestUrlParam < Minitest::Test
  include Helper::Client

  def test_url_defaults_to_______________
    redis = Redis.new

    assert_equal "127.0.0.1", redis._client.host
    assert_equal 6379, redis._client.port
    assert_equal 0, redis._client.db
    assert_nil redis._client.password
  end

  def test_allows_to_pass_in_a_url
    redis = Redis.new url: "redis://:secr3t@foo.com:999/2"

    assert_equal "foo.com", redis._client.host
    assert_equal 999, redis._client.port
    assert_equal 2, redis._client.db
    assert_equal "secr3t", redis._client.password
  end

  def test_allows_to_pass_in_a_url_with_string_key
    redis = Redis.new "url" => "redis://:secr3t@foo.com:999/2"

    assert_equal "foo.com", redis._client.host
    assert_equal 999, redis._client.port
    assert_equal 2, redis._client.db
    assert_equal "secr3t", redis._client.password
  end

  def test_unescape_password_from_url
    redis = Redis.new url: "redis://:secr3t%3A@foo.com:999/2"

    assert_equal "secr3t:", redis._client.password
  end

  def test_unescape_password_from_url_with_string_key
    redis = Redis.new "url" => "redis://:secr3t%3A@foo.com:999/2"

    assert_equal "secr3t:", redis._client.password
  end

  def test_does_not_unescape_password_when_explicitly_passed
    redis = Redis.new url: "redis://:secr3t%3A@foo.com:999/2", password: "secr3t%3A"

    assert_equal "secr3t%3A", redis._client.password
  end

  def test_does_not_unescape_password_when_explicitly_passed_with_string_key
    redis = Redis.new :url => "redis://:secr3t%3A@foo.com:999/2", "password" => "secr3t%3A"

    assert_equal "secr3t%3A", redis._client.password
  end

  def test_override_url_if_path_option_is_passed
    redis = Redis.new url: "redis://:secr3t@foo.com/foo:999/2", path: "/tmp/redis.sock"

    assert_equal "/tmp/redis.sock", redis._client.path
    assert_nil redis._client.host
    assert_nil redis._client.port
  end

  def test_override_url_if_path_option_is_passed_with_string_key
    redis = Redis.new :url => "redis://:secr3t@foo.com/foo:999/2", "path" => "/tmp/redis.sock"

    assert_equal "/tmp/redis.sock", redis._client.path
    assert_nil redis._client.host
    assert_nil redis._client.port
  end

  def test_overrides_url_if_another_connection_option_is_passed
    redis = Redis.new url: "redis://:secr3t@foo.com:999/2", port: 1000

    assert_equal "foo.com", redis._client.host
    assert_equal 1000, redis._client.port
    assert_equal 2, redis._client.db
    assert_equal "secr3t", redis._client.password
  end

  def test_overrides_url_if_another_connection_option_is_passed_with_string_key
    redis = Redis.new :url => "redis://:secr3t@foo.com:999/2", "port" => 1000

    assert_equal "foo.com", redis._client.host
    assert_equal 1000, redis._client.port
    assert_equal 2, redis._client.db
    assert_equal "secr3t", redis._client.password
  end

  def test_does_not_overrides_url_if_a_nil_option_is_passed
    redis = Redis.new url: "redis://:secr3t@foo.com:999/2", port: nil

    assert_equal "foo.com", redis._client.host
    assert_equal 999, redis._client.port
    assert_equal 2, redis._client.db
    assert_equal "secr3t", redis._client.password
  end

  def test_does_not_overrides_url_if_a_nil_option_is_passed_with_string_key
    redis = Redis.new :url => "redis://:secr3t@foo.com:999/2", "port" => nil

    assert_equal "foo.com", redis._client.host
    assert_equal 999, redis._client.port
    assert_equal 2, redis._client.db
    assert_equal "secr3t", redis._client.password
  end

  def test_does_not_modify_the_passed_options
    options = { url: "redis://:secr3t@foo.com:999/2" }

    Redis.new(options)

    assert(options == { url: "redis://:secr3t@foo.com:999/2" })
  end

  def test_uses_redis_url_over_default_if_available
    ENV["REDIS_URL"] = "redis://:secr3t@foo.com:999/2"

    redis = Redis.new

    assert_equal "foo.com", redis._client.host
    assert_equal 999, redis._client.port
    assert_equal 2, redis._client.db
    assert_equal "secr3t", redis._client.password

    ENV.delete("REDIS_URL")
  end

  def test_defaults_to_localhost
    redis = Redis.new(url: "redis:///")

    assert_equal "127.0.0.1", redis._client.host
  end
end