Skip to content

Commit

Permalink
feat: add version header for Admin API (apache#7613)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored and Liu-Junlin committed Nov 4, 2022
1 parent d470a11 commit 1ad8166
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
6 changes: 6 additions & 0 deletions apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
65 changes: 38 additions & 27 deletions t/admin/api.t
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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/(.*)
Expand All @@ -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

0 comments on commit 1ad8166

Please sign in to comment.