From b7ea7a86107b1695d5fe07e780a54b713caa7426 Mon Sep 17 00:00:00 2001 From: An Tran Date: Thu, 23 Nov 2023 15:08:32 +1000 Subject: [PATCH] apicast-mapping-rules.t conversion to APIcast::Blackbox --- t/apicast-mapping-rules.t | 416 ++++++++++++++++++-------------------- 1 file changed, 202 insertions(+), 214 deletions(-) diff --git a/t/apicast-mapping-rules.t b/t/apicast-mapping-rules.t index 10189dfbd..85c635a26 100644 --- a/t/apicast-mapping-rules.t +++ b/t/apicast-mapping-rules.t @@ -1,139 +1,130 @@ use lib 't'; -use Test::APIcast 'no_plan'; +use Test::APIcast::Blackbox 'no_plan'; run_tests(); __DATA__ -=== TEST 1: call to backend is cached -First call is done synchronously and the second out of band. ---- http_config - include $TEST_NGINX_UPSTREAM_CONFIG; - lua_package_path "$TEST_NGINX_LUA_PATH"; - init_by_lua_block { - require('apicast.configuration_loader').mock({ - services = { - { - id = 42, - backend_version = 1, - backend_authentication_type = 'service_token', - backend_authentication_value = 'token-value', - proxy = { - api_backend = "http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api-backend/", - proxy_rules = { - { id = 1, http_method = "GET", - pattern = "/{env}/video/encode?size={size}&speed=2x", - metric_system_name = "weeee", delta = 1, - parameters = { "env" }, - querystring_parameters = { size = "{size}", speed = "2x" } - } - } +=== TEST 1: mapping rules when GET request has url params +--- configuration +{ + "services": [ + { + "id": 42, + "backend_version": 1, + "backend_authentication_type": "service_token", + "backend_authentication_value": "token-value", + "proxy": { + "api_backend": "http://test:$TEST_NGINX_SERVER_PORT/api-backend", + "proxy_rules": [ + { + "pattern": "/{env}/video/encode?size={size}&speed=2x", + "http_method": "GET", + "metric_system_name": "hits", + "delta": 2, + "querystring_parameters": { "size": "{size}", "speed": "2x" } } - } + ] } - }) - } - lua_shared_dict api_keys 10m; ---- config - include $TEST_NGINX_APICAST_CONFIG; - + } + ] +} +--- backend location /transactions/authrep.xml { content_by_lua_block { ngx.exit(200) } } - +--- upstream location /api-backend/ { - echo 'yay, api backend'; + echo 'yay, api backend'; } --- request GET /staging/video/encode?size=100&speed=3x&user_key=foo&speed=2x --- response_body yay, api backend --- error_code: 200 +--- no_error_log +[error] + + === TEST 2: mapping rules when POST request has url parms url params in a POST call are taken into account when matching mapping rules. - ---- http_config - include $TEST_NGINX_UPSTREAM_CONFIG; - lua_package_path "$TEST_NGINX_LUA_PATH"; - init_by_lua_block { - require('apicast.configuration_loader').mock({ - services = { - { - id = 42, - backend_version = 1, - backend_authentication_type = 'service_token', - backend_authentication_value = 'my-token', - proxy = { - api_backend = 'http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api/', - proxy_rules = { - { pattern = '/foo?bar=baz', - querystring_parameters = { bar = 'baz' }, - http_method = 'POST', - metric_system_name = 'bar', - delta = 7 } - } +--- configuration +{ + "services" : [ + { + "id": 42, + "backend_version": 1, + "backend_authentication_type": "service_token", + "backend_authentication_value" : "token-value", + "proxy": { + "api_backend": "http://test:$TEST_NGINX_SERVER_PORT/api-backend/", + "proxy_rules": [ + { + "pattern" : "/foo?bar=baz", + "http_method" : "POST", + "metric_system_name" : "bar", + "delta" : 7, + "querystring_parameters": { "bar": "baz" } } - }, + ] } - }) - } ---- config - include $TEST_NGINX_APICAST_CONFIG; - - location /api/ { - echo "api response"; + } + ] +} +--- upstream + location /api-backend { + echo 'yay, api backend'; } - +--- backend location /transactions/authrep.xml { content_by_lua_block { ngx.exit(200) } } --- request POST /foo?bar=baz&user_key=somekey --- more_headers -X-3scale-Debug: my-token +X-3scale-Debug: token-value --- response_body -api response +yay, api backend --- error_code: 200 --- response_headers X-3scale-matched-rules: /foo?bar=baz X-3scale-usage: usage%5Bbar%5D=7 +--- no_error_log +[error] + + === TEST 3: mapping rules when POST request has body params request body params in a POST call are taken into account when matching mapping rules. - ---- http_config - include $TEST_NGINX_UPSTREAM_CONFIG; - lua_package_path "$TEST_NGINX_LUA_PATH"; - init_by_lua_block { - require('apicast.configuration_loader').mock({ - services = { - { - id = 42, - backend_version = 1, - backend_authentication_type = 'service_token', - backend_authentication_value = 'my-token', - proxy = { - api_backend = 'http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api/', - proxy_rules = { - { pattern = '/foo?bar=baz', - querystring_parameters = { bar = 'baz' }, - http_method = 'POST', - metric_system_name = 'bar', - delta = 7 } - } +--- configuration +{ + "services" : [ + { + "id": 42, + "backend_version": 1, + "backend_authentication_type": "service_token", + "backend_authentication_value" : "token-value", + "proxy": { + "api_backend": "http://test:$TEST_NGINX_SERVER_PORT/api-backend/", + "proxy_rules": [ + { + "pattern" : "/foo?bar=baz", + "http_method" : "POST", + "metric_system_name" : "bar", + "delta" : 7, + "querystring_parameters": { "bar": "baz" } } - }, + ] } - }) - } ---- config - include $TEST_NGINX_APICAST_CONFIG; - - location /api/ { - echo "api response"; + } + ] +} +--- upstream + location /api-backend { + echo 'yay, api backend'; } - +--- backend location /transactions/authrep.xml { content_by_lua_block { ngx.exit(200) } } @@ -142,51 +133,50 @@ POST /foo?user_key=somekey bar=baz --- more_headers Content-Type: application/x-www-form-urlencoded -X-3scale-Debug: my-token +X-3scale-Debug: token-value --- response_body -api response +yay, api backend --- error_code: 200 --- response_headers X-3scale-matched-rules: /foo?bar=baz X-3scale-usage: usage%5Bbar%5D=7 +--- no_error_log +[error] + + === TEST 4: mapping rules when POST request has body params and url params Both body params and url params are taken into account when matching mapping rules. When a param is both in the url and the body, the one in the body takes precedence. - ---- http_config - include $TEST_NGINX_UPSTREAM_CONFIG; - lua_package_path "$TEST_NGINX_LUA_PATH"; - init_by_lua_block { - require('apicast.configuration_loader').mock({ - services = { - { - id = 42, - backend_version = 1, - backend_authentication_type = 'service_token', - backend_authentication_value = 'my-token', - proxy = { - api_backend = 'http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api/', - proxy_rules = { - { pattern = '/foo?a_param=val1&another_param=val2', - querystring_parameters = { a_param = 'val1', another_param = 'val2' }, - http_method = 'POST', - metric_system_name = 'bar', - delta = 7 } - } +--- configuration +{ + "services" : [ + { + "id": 42, + "backend_version": 1, + "backend_authentication_type": "service_token", + "backend_authentication_value" : "token-value", + "proxy": { + "api_backend": "http://test:$TEST_NGINX_SERVER_PORT/api-backend/", + "proxy_rules": [ + { + "pattern" : "/foo?a_param=val1&another_param=val2", + "http_method" : "POST", + "metric_system_name" : "bar", + "delta" : 7, + "querystring_parameters": { "a_param": "val1", "another_param": "val2" } } - }, + ] } - }) - } ---- config - include $TEST_NGINX_APICAST_CONFIG; - - location /api/ { - echo "api response"; + } + ] +} +--- upstream + location /api-backend { + echo 'yay, api backend'; } - +--- backend location /transactions/authrep.xml { content_by_lua_block { ngx.exit(200) } } @@ -195,13 +185,17 @@ POST /foo?a_param=val3&another_param=val2&user_key=somekey a_param=val1 --- more_headers Content-Type: application/x-www-form-urlencoded -X-3scale-Debug: my-token +X-3scale-Debug: token-value --- response_body -api response +yay, api backend --- error_code: 200 --- response_headers X-3scale-matched-rules: /foo?a_param=val1&another_param=val2 X-3scale-usage: usage%5Bbar%5D=7 +--- no_error_log +[error] + + === TEST 5: mapping rules with "last" attribute Mapping rules can have a "last" attribute. When this attribute is set to true, @@ -212,116 +206,110 @@ In the example, we have 4 rules: - the second has last = true but does not match, so the matcher will continue. - the third one matches and has last = true so the matcher will stop here. The usage is checked in the 3scale backend endpoint. ---- http_config - include $TEST_NGINX_UPSTREAM_CONFIG; - lua_package_path "$TEST_NGINX_LUA_PATH"; - init_by_lua_block { - require('apicast.configuration_loader').mock({ - services = { - { - id = 42, - backend_version = 1, - backend_authentication_type = 'service_token', - backend_authentication_value = 'token-value', - proxy = { - api_backend = "http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api-backend/", - proxy_rules = { - { - last = false, - id = 1, - http_method = "GET", - pattern = "/", - metric_system_name = "hits", - delta = 1 - }, - { - last = true, - id = 2, - http_method = "GET", - pattern = "/i_dont_match", - metric_system_name = "hits", - delta = 100 - }, - { - last = true, - id = 3, - http_method = "GET", - pattern = "/abc", - metric_system_name = "hits", - delta = 2 - }, - { - id = 4, - http_method = "GET", - pattern = "/abc/def", - metric_system_name = "hits", - delta = 10 - } - } +--- configuration +{ + "services" : [ + { + "id": 42, + "backend_version": 1, + "backend_authentication_type": "service_token", + "backend_authentication_value" : "token-value", + "proxy": { + "api_backend": "http://test:$TEST_NGINX_SERVER_PORT/api-backend/", + "proxy_rules": [ + { + "last": false, + "id": 1, + "http_method": "GET", + "pattern": "/", + "metric_system_name": "hits", + "delta": 1 + }, + { + "last": true, + "id": 2, + "http_method": "GET", + "pattern": "/i_dont_match", + "metric_system_name": "hits", + "delta": 100 + }, + { + "last": true, + "id": 3, + "http_method": "GET", + "pattern": "/abc", + "metric_system_name": "hits", + "delta": 2 + }, + { + "id": 4, + "http_method": "GET", + "pattern": "/abc/def", + "metric_system_name": "hits", + "delta": 10 } - } + ] } - }) + } + ] +} +--- upstream + location /api-backend { + echo 'yay, api backend'; } - lua_shared_dict api_keys 10m; ---- config - include $TEST_NGINX_APICAST_CONFIG; - +--- backend location /transactions/authrep.xml { content_by_lua_block { local hits = tonumber(ngx.req.get_uri_args()["usage[hits]"]) require('luassert').equals(3, hits) -- rule 1 + rule 3 } } - - location /api-backend/ { - echo 'yay, api backend'; - } --- request GET /abc/def?user_key=uk --- response_body yay, api backend --- error_code: 200 +--- no_error_log +[error] + + === TEST 6: request uri with special chars Call with special chars and validate that are correctly matched. ---- http_config - include $TEST_NGINX_UPSTREAM_CONFIG; - lua_package_path "$TEST_NGINX_LUA_PATH"; - init_by_lua_block { - require('apicast.configuration_loader').mock({ - services = { - { - id = 42, - backend_version = 1, - backend_authentication_type = 'service_token', - backend_authentication_value = 'token-value', - proxy = { - api_backend = "http://127.0.0.1:$TEST_NGINX_SERVER_PORT/api-backend/", - proxy_rules = { - { id = 1, http_method = "GET", - pattern = "/foo%20/bar/", - metric_system_name = "weeee", delta = 1, - } - } +--- configuration +{ + "services" : [ + { + "id": 42, + "backend_version": 1, + "backend_authentication_type": "service_token", + "backend_authentication_value" : "token-value", + "proxy": { + "api_backend": "http://test:$TEST_NGINX_SERVER_PORT/api-backend/", + "proxy_rules": [ + { + "pattern" : "/foo%20/bar/", + "http_method" : "GET", + "metric_system_name" : "hits", + "delta" : 1 } - } + ] } - }) + } + ] +} +--- upstream + location /api-backend { + echo 'yay, api backend'; } - lua_shared_dict api_keys 10m; ---- config - include $TEST_NGINX_APICAST_CONFIG; - +--- backend location /transactions/authrep.xml { content_by_lua_block { ngx.exit(200) } } - - location /api-backend/ { - echo 'yay, api backend'; - } --- request GET /foo%20/bar/?user_key=foo --- response_body yay, api backend --- error_code: 200 +--- no_error_log +[error]