Skip to content

Commit

Permalink
fix(request-id): we can use different ids with the same request (#4479)
Browse files Browse the repository at this point in the history
  • Loading branch information
zuiyangqingzhou authored Jun 30, 2021
1 parent c074fe7 commit 756fad1
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
4 changes: 2 additions & 2 deletions apisix/plugins/request-id.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function _M.rewrite(conf, ctx)
end

if conf.include_in_response then
ctx.x_request_id = uuid_val
ctx["request-id-" .. conf.header_name] = uuid_val
end
end

Expand All @@ -61,7 +61,7 @@ function _M.header_filter(conf, ctx)

local headers = ngx.resp.get_headers()
if not headers[conf.header_name] then
core.response.set_header(conf.header_name, ctx.x_request_id)
core.response.set_header(conf.header_name, ctx["request-id-" .. conf.header_name])
end
end

Expand Down
83 changes: 83 additions & 0 deletions t/plugin/request-id.t
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,86 @@ GET /t
request header not present
--- no_error_log
[error]



=== TEST 10: add plugin with custom header name in global rule and add plugin with default header name in specific route
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/global_rules/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"request-id": {
"header_name":"Custom-Header-Name"
}
}
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"request-id": {
}
},
"upstream": {
"nodes": {
"127.0.0.1:1982": 1
},
"type": "roundrobin"
},
"uri": "/opentracing"
}]]
)
if code >= 300 then
ngx.status = code
return
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 11: check for multiple request-ids in the response header are different
--- config
location /t {
content_by_lua_block {
local http = require "resty.http"
local httpc = http.new()
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/opentracing"
local res, err = httpc:request_uri(uri,
{
method = "GET",
headers = {
["Content-Type"] = "application/json",
}
})

if res.headers["X-Request-Id"] ~= res.headers["Custom-Header-Name"] then
ngx.say("X-Request-Id and Custom-Header-Name are different")
else
ngx.say("failed")
end
}
}
--- request
GET /t
--- response_body
X-Request-Id and Custom-Header-Name are different
--- no_error_log
[error]

0 comments on commit 756fad1

Please sign in to comment.