You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/06/30 02:33:21 UTC

[apisix] branch master updated: fix(request-id): we can use different ids with the same request (#4479)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 756fad1  fix(request-id): we can use different ids with the same request (#4479)
756fad1 is described below

commit 756fad16cce55de918f36b46d49eefd8c7fcd3a9
Author: jackfu <ja...@gmail.com>
AuthorDate: Wed Jun 30 10:33:14 2021 +0800

    fix(request-id): we can use different ids with the same request (#4479)
---
 apisix/plugins/request-id.lua |  4 +--
 t/plugin/request-id.t         | 83 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/apisix/plugins/request-id.lua b/apisix/plugins/request-id.lua
index 60e590a..79b8b18 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 ff4778a..5fadc44 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]