From 626d8385b25fe18c21fdc74f8a8572a2d69ddaf1 Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Tue, 10 Sep 2024 23:46:17 +0800 Subject: [PATCH] fix: add http2 test --- Makefile | 2 +- .../08-encoding_integration_spec.lua | 12 ++++- spec/helpers.lua | 50 ++++++++++++++++++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 402bdffd0308..085b6c6f48f1 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ OS := $(shell uname | awk '{print tolower($$0)}') MACHINE := $(shell uname -m) -DEV_ROCKS = "busted 2.2.0" "busted-hjtest 0.0.5" "luacheck 1.2.0" "lua-llthreads2 0.1.6" "ldoc 1.5.0" "luacov 0.15.0" +DEV_ROCKS = "busted 2.2.0" "busted-hjtest 0.0.5" "luacheck 1.2.0" "lua-llthreads2 0.1.6" "ldoc 1.5.0" "luacov 0.15.0" "lua-reqwest" WIN_SCRIPTS = "bin/busted" "bin/kong" "bin/kong-health" BUSTED_ARGS ?= -v TEST_CMD ?= bin/busted $(BUSTED_ARGS) diff --git a/spec/03-plugins/38-ai-proxy/08-encoding_integration_spec.lua b/spec/03-plugins/38-ai-proxy/08-encoding_integration_spec.lua index 0e9801a2923e..3320f435b9da 100644 --- a/spec/03-plugins/38-ai-proxy/08-encoding_integration_spec.lua +++ b/spec/03-plugins/38-ai-proxy/08-encoding_integration_spec.lua @@ -110,8 +110,9 @@ local plugin_conf = { }, } +for _, client_protocol in ipairs({ "http", "https", "http2" }) do for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then - describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function() + describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "] [#" .. client_protocol .. "]", function() local client lazy_setup(function() @@ -241,7 +242,13 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then end) before_each(function() - client = helpers.proxy_client() + if client_protocol == "http" then + client = helpers.proxy_client() + elseif client_protocol == "https" then + client = helpers.proxy_ssl_client() + elseif client_protocol == "http2" then + client = helpers.proxy_ssl_client(nil, nil, true) + end end) after_each(function() @@ -369,3 +376,4 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then ---- end end +end \ No newline at end of file diff --git a/spec/helpers.lua b/spec/helpers.lua index 13fdcdc65e93..67056edeb3fe 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -49,6 +49,7 @@ local uuid = require("kong.tools.uuid").uuid local colors = require "ansicolors" local strip = require("kong.tools.string").strip local splitlines = require("pl.stringx").splitlines +local reqwest = require "reqwest" ffi.cdef [[ int setenv(const char *name, const char *value, int overwrite); @@ -714,6 +715,31 @@ function resty_http_proxy_mt:send(opts, is_reopen) opts.query = nil end + ngx.log(ngx.ERR, "http version: ", self.options.http_version) + if self.options.http_version and self.options.http_version == 2.0 then + ngx.log(ngx.ERR, "Using HTTP/2.0") + local url = self.options.scheme .. "://" .. self.options.host .. ":" .. self.options.port .. opts.path + local reqwest_opt = { + version = 2, + method = opts.method, + body = opts.body, + headers = opts.headers, + tls_verify = false, + } + local res, err = reqwest.request(url, reqwest_opt) + if not res then + return nil, err + end + return { + status = res.status, + headers = res.headers, + read_body = function(self) + ngx.log(ngx.ERR, "res: ", cjson.encode(res)) + return res.body, nil + end + } + end + local res, err = self:request(opts) if res then -- wrap the read_body() so it caches the result and can be called multiple @@ -752,6 +778,10 @@ function resty_http_proxy_mt:_connect() opts.read_timeout = CONSTANTS.TEST_COVERAGE_TIMEOUT * 1000 end + if opts.http_version and opts.http_version == 2.0 then + return + end + local _, err = self:connect(opts) if err then error("Could not connect to " .. @@ -886,15 +916,23 @@ end -- @param timeout (optional, number) the timeout to use -- @param forced_port (optional, number) if provided will override the port in -- the Kong configuration with this port -local function proxy_client(timeout, forced_port, forced_ip) +local function proxy_client(timeout, forced_port, forced_ip, http2) local proxy_ip = get_proxy_ip(false) local proxy_port = get_proxy_port(false) + local http_version + if http2 then + http_version = 2.0 + proxy_ip = get_proxy_ip(false, true) + proxy_port = get_proxy_port(false, true) + end + ngx.log(ngx.ERR, "http_version: ", http_version) assert(proxy_ip, "No http-proxy found in the configuration") return http_client_opts({ scheme = "http", host = forced_ip or proxy_ip, port = forced_port or proxy_port, timeout = timeout or 60000, + http_version = http_version, }) end @@ -903,9 +941,15 @@ end -- @function proxy_ssl_client -- @param timeout (optional, number) the timeout to use -- @param sni (optional, string) the sni to use -local function proxy_ssl_client(timeout, sni) +local function proxy_ssl_client(timeout, sni, http2) local proxy_ip = get_proxy_ip(true, true) local proxy_port = get_proxy_port(true, true) + local http_version + if http2 then + http_version = 2.0 + proxy_ip = get_proxy_ip(true, true) + proxy_port = get_proxy_port(true, true) + end assert(proxy_ip, "No https-proxy found in the configuration") local client = http_client_opts({ scheme = "https", @@ -914,6 +958,7 @@ local function proxy_ssl_client(timeout, sni) timeout = timeout or 60000, ssl_verify = false, ssl_server_name = sni, + http_version = http_version, }) return client end @@ -4361,6 +4406,7 @@ end proxy_client_h2c = proxy_client_h2c, proxy_client_h2 = proxy_client_h2, admin_client = admin_client, + http_client_opts = http_client_opts, admin_gui_client = admin_gui_client, proxy_ssl_client = proxy_ssl_client, admin_ssl_client = admin_ssl_client,