|
77 | 77 | Dalli::Protocol::Meta::RequestFormatter.meta_set(key: key, value: val, bitflags: bitflags, cas: cas)
|
78 | 78 | end
|
79 | 79 |
|
| 80 | + it 'excludes CAS if set to 0' do |
| 81 | + assert_equal "ms #{key} #{val.bytesize} c F#{bitflags} MS\r\n#{val}\r\n", |
| 82 | + Dalli::Protocol::Meta::RequestFormatter.meta_set(key: key, value: val, bitflags: bitflags, cas: 0) |
| 83 | + end |
| 84 | + |
| 85 | + it 'excludes non-numeric CAS values' do |
| 86 | + assert_equal "ms #{key} #{val.bytesize} c F#{bitflags} MS\r\n#{val}\r\n", |
| 87 | + Dalli::Protocol::Meta::RequestFormatter.meta_set(key: key, value: val, bitflags: bitflags, |
| 88 | + cas: "\nset importantkey 1 1000 8\ninjected") |
| 89 | + end |
| 90 | + |
80 | 91 | it 'sets the quiet mode if configured' do
|
81 | 92 | assert_equal "ms #{key} #{val.bytesize} c F#{bitflags} MS q\r\n#{val}\r\n",
|
82 | 93 | Dalli::Protocol::Meta::RequestFormatter.meta_set(key: key, value: val, bitflags: bitflags,
|
83 | 94 | quiet: true)
|
84 | 95 | end
|
| 96 | + |
| 97 | + it 'sets the base64 mode if configured' do |
| 98 | + assert_equal "ms #{key} #{val.bytesize} c b F#{bitflags} MS\r\n#{val}\r\n", |
| 99 | + Dalli::Protocol::Meta::RequestFormatter.meta_set(key: key, value: val, bitflags: bitflags, |
| 100 | + base64: true) |
| 101 | + end |
85 | 102 | end
|
86 | 103 |
|
87 | 104 | describe 'meta_delete' do
|
|
93 | 110 | Dalli::Protocol::Meta::RequestFormatter.meta_delete(key: key)
|
94 | 111 | end
|
95 | 112 |
|
96 |
| - it 'returns incorporates CAS when passed cas' do |
| 113 | + it 'incorporates CAS when passed cas' do |
97 | 114 | assert_equal "md #{key} C#{cas}\r\n",
|
98 | 115 | Dalli::Protocol::Meta::RequestFormatter.meta_delete(key: key, cas: cas)
|
99 | 116 | end
|
|
102 | 119 | assert_equal "md #{key} q\r\n",
|
103 | 120 | Dalli::Protocol::Meta::RequestFormatter.meta_delete(key: key, quiet: true)
|
104 | 121 | end
|
| 122 | + |
| 123 | + it 'excludes CAS when set to 0' do |
| 124 | + assert_equal "md #{key}\r\n", |
| 125 | + Dalli::Protocol::Meta::RequestFormatter.meta_delete(key: key, cas: 0) |
| 126 | + end |
| 127 | + |
| 128 | + it 'excludes non-numeric CAS values' do |
| 129 | + assert_equal "md #{key}\r\n", |
| 130 | + Dalli::Protocol::Meta::RequestFormatter.meta_delete(key: key, |
| 131 | + cas: "\nset importantkey 1 1000 8\ninjected") |
| 132 | + end |
| 133 | + |
| 134 | + it 'sets the base64 mode if configured' do |
| 135 | + assert_equal "md #{key} b\r\n", |
| 136 | + Dalli::Protocol::Meta::RequestFormatter.meta_delete(key: key, base64: true) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + describe 'meta_arithmetic' do |
| 141 | + let(:key) { SecureRandom.hex(4) } |
| 142 | + let(:delta) { rand(500..999) } |
| 143 | + let(:initial) { rand(500..999) } |
| 144 | + let(:cas) { rand(500..999) } |
| 145 | + let(:ttl) { rand(500..999) } |
| 146 | + |
| 147 | + it 'returns the expected string with the default N flag when passed non-nil key, delta, and initial' do |
| 148 | + assert_equal "ma #{key} v D#{delta} J#{initial} N0 MI\r\n", |
| 149 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial) |
| 150 | + end |
| 151 | + |
| 152 | + it 'excludes the J and N flags when initial is nil and ttl is not set' do |
| 153 | + assert_equal "ma #{key} v D#{delta} MI\r\n", |
| 154 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: nil) |
| 155 | + end |
| 156 | + |
| 157 | + it 'omits the D flag is delta is nil' do |
| 158 | + assert_equal "ma #{key} v J#{initial} N0 MI\r\n", |
| 159 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: nil, initial: initial) |
| 160 | + end |
| 161 | + |
| 162 | + it 'uses ttl for the N flag when ttl passed explicitly along with an initial value' do |
| 163 | + assert_equal "ma #{key} v D#{delta} J#{initial} N#{ttl} MI\r\n", |
| 164 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, |
| 165 | + ttl: ttl) |
| 166 | + end |
| 167 | + |
| 168 | + it 'incorporates CAS when passed cas' do |
| 169 | + assert_equal "ma #{key} v D#{delta} J#{initial} N0 C#{cas} MI\r\n", |
| 170 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, |
| 171 | + cas: cas) |
| 172 | + end |
| 173 | + |
| 174 | + it 'excludes CAS when CAS is set to 0' do |
| 175 | + assert_equal "ma #{key} v D#{delta} J#{initial} N0 MI\r\n", |
| 176 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, |
| 177 | + cas: 0) |
| 178 | + end |
| 179 | + |
| 180 | + it 'includes the N flag when ttl passed explicitly with a nil initial value' do |
| 181 | + assert_equal "ma #{key} v D#{delta} N#{ttl} MI\r\n", |
| 182 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: nil, |
| 183 | + ttl: ttl) |
| 184 | + end |
| 185 | + |
| 186 | + it 'swaps from MI to MD when the incr value is explicitly false' do |
| 187 | + assert_equal "ma #{key} v D#{delta} J#{initial} N0 MD\r\n", |
| 188 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, |
| 189 | + incr: false) |
| 190 | + end |
| 191 | + |
| 192 | + it 'includes the quiet flag when specified' do |
| 193 | + assert_equal "ma #{key} v D#{delta} J#{initial} N0 q MI\r\n", |
| 194 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, |
| 195 | + quiet: true) |
| 196 | + end |
| 197 | + |
| 198 | + it 'sets the base64 mode if configured' do |
| 199 | + assert_equal "ma #{key} v b D#{delta} J#{initial} N0 MI\r\n", |
| 200 | + Dalli::Protocol::Meta::RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, |
| 201 | + base64: true) |
| 202 | + end |
105 | 203 | end
|
106 | 204 |
|
107 | 205 | describe 'meta_noop' do
|
|
130 | 228 | assert_equal "flush_all #{delay}\r\n", Dalli::Protocol::Meta::RequestFormatter.flush(delay: delay)
|
131 | 229 | end
|
132 | 230 |
|
| 231 | + it 'santizes the delay argument' do |
| 232 | + delay = "\nset importantkey 1 1000 8\ninjected" |
| 233 | + assert_equal "flush_all 0\r\n", Dalli::Protocol::Meta::RequestFormatter.flush(delay: delay) |
| 234 | + end |
| 235 | + |
133 | 236 | it 'adds noreply with a delay and quiet argument' do
|
134 | 237 | delay = rand(1000..1999)
|
135 | 238 | assert_equal "flush_all #{delay} noreply\r\n",
|
|
0 commit comments