File: url_param_test.rb

package info (click to toggle)
ruby-redis 5.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,168 kB
  • sloc: ruby: 11,501; makefile: 117; sh: 24
file content (106 lines) | stat: -rw-r--r-- 3,003 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require "helper"

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

  def test_url_defaults_to_localhost
    redis = Redis.new

    assert_equal "localhost", 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_unescape_password_from_url
    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_override_url_if_path_option_is_passed
    redis = Redis.new url: "redis://:secr3t@foo.com/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_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_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 "localhost", redis._client.host
  end

  def test_ipv6_url
    redis = Redis.new url: "redis://[::1]"

    assert_equal "::1", redis._client.host
  end

  def test_user_and_password
    redis = Redis.new(url: 'redis://johndoe:mysecret@foo.com:999/2')

    assert_equal('johndoe', redis._client.username)
    assert_equal('mysecret', redis._client.password)
    assert_equal('foo.com', redis._client.host)
    assert_equal(999, redis._client.port)
    assert_equal(2, redis._client.db)
  end
end