diff --git a/apisix/plugins/request-id.lua b/apisix/plugins/request-id.lua index 60e590a3498c..79b8b183dad1 100644 --- a/apisix/plugins/request-id.lua +++ b/apisix/plugins/request-id.lua @@ -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 @@ -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 diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index ff4778a24d17..5fadc447738e 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -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]