From 4a7a608e7634755849c11b130f3227be978066ed Mon Sep 17 00:00:00 2001 From: ShiningRush <277040271@qq.com> Date: Tue, 19 May 2020 22:18:11 +0800 Subject: [PATCH 1/3] optimize: default copy cookie to every request, add headers option to admin test --- apisix/plugins/batch-requests.lua | 16 +++--- t/lib/test_admin.lua | 13 +++-- t/plugin/batch-requests.t | 81 +++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 10 deletions(-) diff --git a/apisix/plugins/batch-requests.lua b/apisix/plugins/batch-requests.lua index 34d784a89f97..9122986bed4f 100644 --- a/apisix/plugins/batch-requests.lua +++ b/apisix/plugins/batch-requests.lua @@ -114,14 +114,18 @@ end local function set_common_header(data) - if not data.headers then - return - end + local ck = core.request.header(nil, "Cookie") for i,req in ipairs(data.pipeline) do if not req.headers then - req.headers = data.headers - else + req.headers = {} + end + + if ck then + req.headers["Cookie"] = ck + end + + if data.headers then for k, v in pairs(data.headers) do if not req.headers[k] then req.headers[k] = v @@ -238,4 +242,4 @@ function _M.api() end -return _M +return _M \ No newline at end of file diff --git a/t/lib/test_admin.lua b/t/lib/test_admin.lua index cda7514d09da..834446e4c06b 100644 --- a/t/lib/test_admin.lua +++ b/t/lib/test_admin.lua @@ -119,7 +119,14 @@ function _M.comp_tab(left_tab, right_tab) end -function _M.test(uri, method, body, pattern) +function _M.test(uri, method, body, pattern, headers) + if not headers then + headers = {} + end + if not headers["Content-Type"] then + headers["Content-Type"] = "application/x-www-form-urlencoded" + end + if type(body) == "table" then body = json.encode(body) end @@ -141,9 +148,7 @@ function _M.test(uri, method, body, pattern) method = method, body = body, keepalive = false, - headers = { - ["Content-Type"] = "application/x-www-form-urlencoded", - }, + headers = headers, } ) if not res then diff --git a/t/plugin/batch-requests.t b/t/plugin/batch-requests.t index 9c784a6bd481..3cdea86c0d03 100644 --- a/t/plugin/batch-requests.t +++ b/t/plugin/batch-requests.t @@ -684,3 +684,84 @@ GET /aggregate passed --- no_error_log [error] + + + +=== TEST 15: copy cookie to every rquest +--- config + client_body_in_file_only on; + location = /aggregate { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code, body = t('/apisix/batch-requests', + ngx.HTTP_POST, + [=[{ + "timeout": 100, + "pipeline":[ + { + "path": "/b", + "headers": { + "Header1": "hello", + "Header2": "world" + } + },{ + "path": "/c", + "method": "PUT" + },{ + "path": "/d" + }] + }]=], + [=[[ + { + "status": 200, + "headers": { + "X-Cookie": "request-cookies-b" + } + }, + { + "status": 201, + "headers": { + "X-Cookie": "request-cookies-c" + } + }, + { + "status": 202, + "headers": { + "X-Cookie": "request-cookies-d" + } + } + ]]=], + { + Cookie = "request-cookies" + }) + + ngx.status = code + ngx.say(body) + } + } + + location = /b { + content_by_lua_block { + ngx.status = 200 + ngx.header["X-Cookie"] = ngx.req.get_headers()["Cookie"] .. "-b" + } + } + location = /c { + content_by_lua_block { + ngx.status = 201 + ngx.header["X-Cookie"] = ngx.req.get_headers()["Cookie"] .. "-c" + } + } + location = /d { + content_by_lua_block { + ngx.status = 202 + ngx.header["X-Cookie"] = ngx.req.get_headers()["Cookie"] .. "-d" + } + } +--- request +GET /aggregate +--- response_body +passed +--- no_error_log +[error] From ddad00d167c5ca62f247a4d084af2f32cf7c0fcc Mon Sep 17 00:00:00 2001 From: ShiningRush <277040271@qq.com> Date: Tue, 19 May 2020 22:21:24 +0800 Subject: [PATCH 2/3] append EOL --- apisix/plugins/batch-requests.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugins/batch-requests.lua b/apisix/plugins/batch-requests.lua index 9122986bed4f..faeeb1181b70 100644 --- a/apisix/plugins/batch-requests.lua +++ b/apisix/plugins/batch-requests.lua @@ -242,4 +242,4 @@ function _M.api() end -return _M \ No newline at end of file +return _M From b6b4238f90935f166eba7ec1cbb48ae69551a0a4 Mon Sep 17 00:00:00 2001 From: ShiningRush <277040271@qq.com> Date: Tue, 26 May 2020 20:02:17 +0800 Subject: [PATCH 3/3] fix: typo word --- t/plugin/batch-requests.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/batch-requests.t b/t/plugin/batch-requests.t index 3cdea86c0d03..928ebfbbd39f 100644 --- a/t/plugin/batch-requests.t +++ b/t/plugin/batch-requests.t @@ -687,7 +687,7 @@ passed -=== TEST 15: copy cookie to every rquest +=== TEST 15: copy cookie to every request --- config client_body_in_file_only on; location = /aggregate {