|
| 1 | +use Test::Nginx::Socket 'no_plan'; |
| 2 | +use Cwd qw(cwd); |
| 3 | + |
| 4 | +my $pwd = cwd(); |
| 5 | + |
| 6 | +our $HttpConfig = qq{ |
| 7 | +lua_package_path "$pwd/lib/?.lua;;"; |
| 8 | +
|
| 9 | +init_by_lua_block { |
| 10 | + require("luacov.runner").init() |
| 11 | +} |
| 12 | +}; |
| 13 | + |
| 14 | +$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; |
| 15 | +$ENV{TEST_NGINX_REDIS_PORT} ||= 6379; |
| 16 | + |
| 17 | +no_long_string(); |
| 18 | +run_tests(); |
| 19 | + |
| 20 | +__DATA__ |
| 21 | + |
| 22 | +=== TEST 1: Proxy mode disables commands |
| 23 | +--- http_config eval: $::HttpConfig |
| 24 | +--- config |
| 25 | +location /t { |
| 26 | + content_by_lua_block { |
| 27 | + local rc = require("resty.redis.connector").new({ |
| 28 | + port = $TEST_NGINX_REDIS_PORT, |
| 29 | + connection_is_proxied = true |
| 30 | + }) |
| 31 | + |
| 32 | + local redis, err = assert(rc:connect(params), |
| 33 | + "connect should return positively") |
| 34 | + |
| 35 | + assert(redis:set("dog", "an animal"), |
| 36 | + "redis:set should return positively") |
| 37 | + |
| 38 | + local ok, err = redis:multi() |
| 39 | + assert(ok == nil, "redis:multi should return nil") |
| 40 | + assert(err == "Command multi is disabled") |
| 41 | + |
| 42 | + redis:close() |
| 43 | + } |
| 44 | +} |
| 45 | +--- request |
| 46 | +GET /t |
| 47 | +--- no_error_log |
| 48 | +[error] |
| 49 | + |
| 50 | + |
| 51 | +=== TEST 2: Proxy mode disables custom commands |
| 52 | +--- http_config eval: $::HttpConfig |
| 53 | +--- config |
| 54 | +location /t { |
| 55 | + content_by_lua_block { |
| 56 | + local rc = require("resty.redis.connector").new({ |
| 57 | + port = $TEST_NGINX_REDIS_PORT, |
| 58 | + connection_is_proxied = true, |
| 59 | + disabled_commands = { "foobar", "hget"} |
| 60 | + }) |
| 61 | + |
| 62 | + local redis, err = assert(rc:connect(params), |
| 63 | + "connect should return positively") |
| 64 | + |
| 65 | + assert(redis:set("dog", "an animal"), |
| 66 | + "redis:set should return positively") |
| 67 | + |
| 68 | + assert(redis:multi(), |
| 69 | + "redis:multi should return positively") |
| 70 | + |
| 71 | + local ok, err = redis:hget() |
| 72 | + assert(ok == nil, "redis:hget should return nil") |
| 73 | + assert(err == "Command hget is disabled") |
| 74 | + |
| 75 | + local ok, err = redis:foobar() |
| 76 | + assert(ok == nil, "redis:foobar should return nil") |
| 77 | + assert(err == "Command foobar is disabled") |
| 78 | + |
| 79 | + redis:close() |
| 80 | + } |
| 81 | +} |
| 82 | +--- request |
| 83 | +GET /t |
| 84 | +--- no_error_log |
| 85 | +[error] |
| 86 | + |
| 87 | +=== TEST 3: Proxy mode does not switch DB |
| 88 | +--- http_config eval: $::HttpConfig |
| 89 | +--- config |
| 90 | +location /t { |
| 91 | + content_by_lua_block { |
| 92 | + local redis = require("resty.redis.connector").new({ |
| 93 | + port = $TEST_NGINX_REDIS_PORT, |
| 94 | + db = 2 |
| 95 | + }):connect() |
| 96 | + |
| 97 | + local proxy = require("resty.redis.connector").new({ |
| 98 | + port = $TEST_NGINX_REDIS_PORT, |
| 99 | + connection_is_proxied = true, |
| 100 | + db = 2 |
| 101 | + }):connect() |
| 102 | + |
| 103 | + assert(redis:set("proxy", "test"), |
| 104 | + "redis:set should return positively") |
| 105 | + |
| 106 | + assert(proxy:get("proxy") == ngx.null, |
| 107 | + "proxy key should not exist in proxy") |
| 108 | + |
| 109 | + redis:seelct(2) |
| 110 | + assert(redis:get("proxy") == "test", |
| 111 | + "proxy key should be 'test' in db 1") |
| 112 | + |
| 113 | + redis:close() |
| 114 | + } |
| 115 | +} |
| 116 | +--- request |
| 117 | +GET /t |
| 118 | +--- no_error_log |
| 119 | +[error] |
| 120 | + |
| 121 | + |
| 122 | +=== TEST 4: Commands are disabled without proxy mode |
| 123 | +--- http_config eval: $::HttpConfig |
| 124 | +--- config |
| 125 | +location /t { |
| 126 | + content_by_lua_block { |
| 127 | + local rc = require("resty.redis.connector").new({ |
| 128 | + port = $TEST_NGINX_REDIS_PORT, |
| 129 | + disabled_commands = { "foobar", "hget"} |
| 130 | + }) |
| 131 | + |
| 132 | + local redis, err = assert(rc:connect(params), |
| 133 | + "connect should return positively") |
| 134 | + |
| 135 | + assert(redis:set("dog", "an animal"), |
| 136 | + "redis:set should return positively") |
| 137 | + |
| 138 | + assert(redis:multi(), |
| 139 | + "redis:multi should return positively") |
| 140 | + |
| 141 | + local ok, err = redis:hget() |
| 142 | + assert(ok == nil, "redis:hget should return nil") |
| 143 | + assert(err == "Command hget is disabled") |
| 144 | + |
| 145 | + local ok, err = redis:foobar() |
| 146 | + assert(ok == nil, "redis:foobar should return nil") |
| 147 | + assert(err == "Command foobar is disabled") |
| 148 | + |
| 149 | + redis:close() |
| 150 | + } |
| 151 | +} |
| 152 | +--- request |
| 153 | +GET /t |
| 154 | +--- no_error_log |
| 155 | +[error] |
0 commit comments