Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/batch-requests-cookie #1599

Merged
merged 3 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions apisix/plugins/batch-requests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions t/lib/test_admin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
81 changes: 81 additions & 0 deletions t/plugin/batch-requests.t
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,84 @@ GET /aggregate
passed
--- no_error_log
[error]



=== TEST 15: copy cookie to every request
--- 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]