|
185 | 185 | adapter.get_all |
186 | 186 | }.to raise_error(Flipper::Adapters::Http::Error) |
187 | 187 | end |
| 188 | + |
| 189 | + it "stores ETag and sends If-None-Match on subsequent requests" do |
| 190 | + features_response = { |
| 191 | + "features" => [ |
| 192 | + { |
| 193 | + "key" => "search", |
| 194 | + "gates" => [ |
| 195 | + {"key" => "boolean", "value" => true} |
| 196 | + ] |
| 197 | + } |
| 198 | + ] |
| 199 | + } |
| 200 | + |
| 201 | + # First request - server returns ETag |
| 202 | + stub_request(:get, "http://app.com/flipper/features?exclude_gate_names=true") |
| 203 | + .to_return( |
| 204 | + status: 200, |
| 205 | + body: JSON.generate(features_response), |
| 206 | + headers: { 'ETag' => '"abc123"' } |
| 207 | + ) |
| 208 | + |
| 209 | + adapter = described_class.new(url: 'http://app.com/flipper') |
| 210 | + result = adapter.get_all |
| 211 | + |
| 212 | + expect(result).to have_key("search") |
| 213 | + |
| 214 | + # Second request - should send If-None-Match header |
| 215 | + stub_request(:get, "http://app.com/flipper/features?exclude_gate_names=true") |
| 216 | + .with(headers: { 'If-None-Match' => '"abc123"' }) |
| 217 | + .to_return( |
| 218 | + status: 200, |
| 219 | + body: JSON.generate(features_response), |
| 220 | + headers: { 'ETag' => '"abc123"' } |
| 221 | + ) |
| 222 | + |
| 223 | + etag_result = adapter.get_all |
| 224 | + |
| 225 | + expect(etag_result).to eq(result) |
| 226 | + expect(etag_result).to have_key("search") |
| 227 | + expect( |
| 228 | + a_request(:get, "http://app.com/flipper/features?exclude_gate_names=true") |
| 229 | + .with(headers: { 'If-None-Match' => '"abc123"' }) |
| 230 | + ).to have_been_made.once |
| 231 | + end |
| 232 | + |
| 233 | + it "returns cached result when server returns 304 Not Modified" do |
| 234 | + features_response = { |
| 235 | + "features" => [ |
| 236 | + { |
| 237 | + "key" => "search", |
| 238 | + "gates" => [ |
| 239 | + {"key" => "boolean", "value" => true} |
| 240 | + ] |
| 241 | + } |
| 242 | + ] |
| 243 | + } |
| 244 | + |
| 245 | + # First request - server returns ETag |
| 246 | + stub_request(:get, "http://app.com/flipper/features?exclude_gate_names=true") |
| 247 | + .to_return( |
| 248 | + status: 200, |
| 249 | + body: JSON.generate(features_response), |
| 250 | + headers: { 'ETag' => '"abc123"' } |
| 251 | + ) |
| 252 | + |
| 253 | + adapter = described_class.new(url: 'http://app.com/flipper') |
| 254 | + first_result = adapter.get_all |
| 255 | + |
| 256 | + expect(first_result).to have_key("search") |
| 257 | + |
| 258 | + # Second request - server returns 304 |
| 259 | + stub_request(:get, "http://app.com/flipper/features?exclude_gate_names=true") |
| 260 | + .with(headers: { 'If-None-Match' => '"abc123"' }) |
| 261 | + .to_return(status: 304, headers: { 'ETag' => '"abc123"' }) |
| 262 | + |
| 263 | + second_result = adapter.get_all |
| 264 | + |
| 265 | + # Should return the cached result |
| 266 | + expect(second_result).to eq(first_result) |
| 267 | + expect(second_result).to have_key("search") |
| 268 | + end |
| 269 | + |
| 270 | + it "raises error when 304 received without cached result" do |
| 271 | + # Server returns 304 without any prior request |
| 272 | + stub_request(:get, "http://app.com/flipper/features?exclude_gate_names=true") |
| 273 | + .to_return(status: 304) |
| 274 | + |
| 275 | + adapter = described_class.new(url: 'http://app.com/flipper') |
| 276 | + expect { |
| 277 | + adapter.get_all |
| 278 | + }.to raise_error(Flipper::Adapters::Http::Error) |
| 279 | + end |
| 280 | + |
| 281 | + it "does not send If-None-Match for other endpoints" do |
| 282 | + stub_request(:get, "http://app.com/flipper/features/search") |
| 283 | + .to_return(status: 404) |
| 284 | + |
| 285 | + adapter = described_class.new(url: 'http://app.com/flipper') |
| 286 | + adapter.get(flipper[:search]) |
| 287 | + |
| 288 | + # Verify no If-None-Match header was sent |
| 289 | + expect( |
| 290 | + a_request(:get, "http://app.com/flipper/features/search") |
| 291 | + .with { |req| req.headers['If-None-Match'].nil? } |
| 292 | + ).to have_been_made.once |
| 293 | + end |
188 | 294 | end |
189 | 295 |
|
190 | 296 | describe "#features" do |
|
0 commit comments