@@ -372,3 +372,87 @@ push $res, "GET /force_report_to_backend";
372
372
$res
373
373
--- no_error_log
374
374
[error]
375
+
376
+ === TEST 5: with caching policy (resilient mode)
377
+ The purpose of this test is to test that the 3scale batcher policy works
378
+ correctly when combined with the caching one.
379
+ In this case, the caching policy is configured as "resilient". We define a
380
+ backend that returns "limits exceeded" on the first request, and an error in
381
+ all the rest. The caching policy will cache the first result and return it
382
+ while backend is down. Notice that the caching policy does not store the
383
+ rejection reason, it just returns a generic error (403/Authentication failed).
384
+ To make sure that nothing is cached in the 3scale batcher policy, we flush its
385
+ auth cache on every request (see rewrite_by_lua_block).
386
+ --- http_config
387
+ include $TEST_NGINX_UPSTREAM_CONFIG;
388
+ lua_shared_dict api_keys 10m;
389
+ lua_shared_dict cached_auths 1m;
390
+ lua_shared_dict batched_reports 1m;
391
+ lua_shared_dict batched_reports_locks 1m;
392
+ lua_package_path "$TEST_NGINX_LUA_PATH";
393
+
394
+ init_by_lua_block {
395
+ require('apicast.configuration_loader').mock({
396
+ services = {
397
+ {
398
+ id = 42,
399
+ backend_version = 1,
400
+ backend_authentication_type = 'service_token',
401
+ backend_authentication_value = 'token-value',
402
+ proxy = {
403
+ backend = { endpoint = "http://127.0.0.1:$TEST_NGINX_SERVER_PORT" },
404
+ api_backend = "http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api-backend/",
405
+ proxy_rules = {
406
+ { pattern = '/', http_method = 'GET', metric_system_name = 'hits', delta = 2 }
407
+ },
408
+ policy_chain = {
409
+ {
410
+ name = 'apicast.policy.3scale_batcher',
411
+ configuration = { }
412
+ },
413
+ {
414
+ name = 'apicast.policy.apicast'
415
+ },
416
+ {
417
+ name = 'apicast.policy.caching',
418
+ configuration = { caching_type = 'resilient' }
419
+ }
420
+ }
421
+ }
422
+ }
423
+ }
424
+ })
425
+ }
426
+
427
+ rewrite_by_lua_block {
428
+ ngx.shared.cached_auths:flush_all()
429
+ }
430
+ --- config
431
+ include $TEST_NGINX_APICAST_CONFIG;
432
+
433
+ location /transactions/authorize.xml {
434
+ content_by_lua_block {
435
+ local test_counter = ngx.shared.test_counter or 0
436
+ if test_counter == 0 then
437
+ ngx.shared.test_counter = test_counter + 1
438
+ ngx.header['3scale-rejection-reason'] = 'limits_exceeded'
439
+ ngx.status = 409
440
+ ngx.exit(ngx.HTTP_OK)
441
+ else
442
+ ngx.shared.test_counter = test_counter + 1
443
+ ngx.exit(502)
444
+ end
445
+ }
446
+ }
447
+
448
+ location /api-backend {
449
+ echo 'yay, api backend';
450
+ }
451
+ --- request eval
452
+ ["GET /test?user_key=foo", "GET /foo?user_key=foo", "GET /?user_key=foo"]
453
+ --- response_body eval
454
+ ["Limits exceeded", "Authentication failed", "Authentication failed"]
455
+ --- error_code eval
456
+ [ 429, 403, 403 ]
457
+ --- no_error_log
458
+ [error]
0 commit comments