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 5550f3e5e feat: add version header for Admin API (#7613)
5550f3e5e is described below
commit 5550f3e5e3aee8089077b797654b88bcca47494d
Author: Zeping Bai <[email protected]>
AuthorDate: Fri Aug 5 16:27:05 2022 +0800
feat: add version header for Admin API (#7613)
---
apisix/admin/init.lua | 6 +++++
t/admin/api.t | 65 ++++++++++++++++++++++++++++++---------------------
2 files changed, 44 insertions(+), 27 deletions(-)
diff --git a/apisix/admin/init.lua b/apisix/admin/init.lua
index a7f6d1044..0e7f8f399 100644
--- a/apisix/admin/init.lua
+++ b/apisix/admin/init.lua
@@ -18,6 +18,7 @@ local require = require
local core = require("apisix.core")
local route = require("apisix.utils.router")
local plugin = require("apisix.plugin")
+local v3_adapter = require("apisix.admin.v3_adapter")
local ngx = ngx
local get_method = ngx.req.get_method
local ngx_time = ngx.time
@@ -188,6 +189,11 @@ local function run()
local code, data = resource[method](seg_id, req_body, seg_sub_path,
uri_args)
if code then
+ if v3_adapter.enable_v3() then
+ core.response.set_header("X-API-VERSION", "v3")
+ else
+ core.response.set_header("X-API-VERSION", "v2")
+ end
data = strip_etcd_resp(data)
core.response.exit(code, data)
end
diff --git a/t/admin/api.t b/t/admin/api.t
index 98007aecd..71c924915 100644
--- a/t/admin/api.t
+++ b/t/admin/api.t
@@ -24,7 +24,7 @@ add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->request) {
- $block->set_value("request", "GET /t");
+ $block->set_value("request", "GET /apisix/admin/routes");
}
if (!$block->no_error_log && !$block->error_log) {
@@ -37,19 +37,8 @@ run_tests;
__DATA__
=== TEST 1: Server header for admin API
---- config
- location /t {
- content_by_lua_block {
- local http = require("resty.http")
- local httpc = http.new()
- uri = ngx.var.scheme .. "://" .. ngx.var.server_addr
- .. ":" .. ngx.var.server_port .. "/apisix/admin/routes"
- local res, err = httpc:request_uri(uri)
- ngx.say(res.headers["Server"])
- }
- }
---- response_body eval
-qr/APISIX\//
+--- response_headers_like
+Server: APISIX/(.*)
@@ -58,16 +47,38 @@ qr/APISIX\//
apisix:
node_listen: 1984
enable_server_tokens: false
---- config
- location /t {
- content_by_lua_block {
- local http = require("resty.http")
- local httpc = http.new()
- uri = ngx.var.scheme .. "://" .. ngx.var.server_addr
- .. ":" .. ngx.var.server_port .. "/apisix/admin/routes"
- local res, err = httpc:request_uri(uri)
- ngx.say(res.headers["Server"])
- }
- }
---- response_body
-APISIX
+--- error_code: 401
+--- response_headers
+Server: APISIX
+
+
+
+=== TEST 3: Version header for admin API (without apikey)
+--- yaml_config
+apisix:
+ admin_api_version: default
+--- error_code: 401
+--- response_headers
+! X-API-VERSION
+
+
+
+=== TEST 4: Version header for admin API (v2)
+--- yaml_config
+apisix:
+ admin_api_version: v2 # default may change
+--- more_headers
+X-API-KEY: edd1c9f034335f136f87ad84b625c8f1
+--- response_headers
+X-API-VERSION: v2
+
+
+
+=== TEST 5: Version header for admin API (v3)
+--- yaml_config
+apisix:
+ admin_api_version: v3
+--- more_headers
+X-API-KEY: edd1c9f034335f136f87ad84b625c8f1
+--- response_headers
+X-API-VERSION: v3