Skip to content

Commit

Permalink
fix: add http2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
oowl committed Sep 10, 2024
1 parent d4973c6 commit 626d838
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
12 changes: 10 additions & 2 deletions spec/03-plugins/38-ai-proxy/08-encoding_integration_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -369,3 +376,4 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
----

end end
end
50 changes: 48 additions & 2 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 " ..
Expand Down Expand Up @@ -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

Expand All @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 626d838

Please sign in to comment.