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(request-id): we can use different ids with the same request #4479

Merged
merged 10 commits into from
Jun 30, 2021
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be 3 blank lines between test cases, or you can use the reindex to do. the CI error is about this.




=== 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]