]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REGTESTS: lua: test the httpclient:get() feature
authorWilliam Lallemand <wlallemand@haproxy.org>
Fri, 24 Sep 2021 17:02:50 +0000 (19:02 +0200)
committerWilliam Lallemand <wlallemand@haproxy.org>
Fri, 24 Sep 2021 17:05:53 +0000 (19:05 +0200)
This reg-test is heavily inspired by the lua_socket.vtc one.

It replaces the HTTP/1.1 request made manually with a socket object with
an httpclient object.

reg-tests/lua/lua_httpclient.lua [new file with mode: 0644]
reg-tests/lua/lua_httpclient.vtc [new file with mode: 0644]

diff --git a/reg-tests/lua/lua_httpclient.lua b/reg-tests/lua/lua_httpclient.lua
new file mode 100644 (file)
index 0000000..390ba41
--- /dev/null
@@ -0,0 +1,29 @@
+
+local vtc_port = 0
+
+core.register_service("fakeserv", "http", function(applet)
+       vtc_port = applet.headers["vtcport"][0]
+       core.Info("APPLET START")
+       local response = "OK"
+       applet:add_header("Server", "haproxy/webstats")
+       applet:add_header("Content-Length", string.len(response))
+       applet:add_header("Content-Type", "text/html")
+       applet:start_response()
+       applet:send(response)
+       core.Info("APPLET DONE")
+end)
+
+local function cron()
+       -- wait for until the correct port is set through the c0 request..
+       while vtc_port == 0 do
+               core.msleep(1)
+       end
+       core.Debug('CRON port:' .. vtc_port)
+
+       local httpclient = core.httpclient()
+       local response = httpclient:get("http://127.0.0.1:" .. vtc_port)
+
+       core.Info("Received: " .. response.body)
+end
+
+core.register_task(cron)
diff --git a/reg-tests/lua/lua_httpclient.vtc b/reg-tests/lua/lua_httpclient.vtc
new file mode 100644 (file)
index 0000000..6d68ffb
--- /dev/null
@@ -0,0 +1,34 @@
+varnishtest "Lua: check httpclient functionality from a lua-task"
+feature cmd "$HAPROXY_PROGRAM -cc 'version_atleast(2.5-dev7)'"
+feature ignore_unknown_macro
+
+#REQUIRE_OPTIONS=LUA
+
+server s1 {
+    rxreq
+    txresp -bodylen 20
+} -start
+
+haproxy h1 -conf {
+    global
+        lua-load ${testdir}/lua_httpclient.lua
+
+    frontend fe1
+        mode http
+        bind "fd@${fe1}"
+        default_backend b1
+
+    backend b1
+        mode http
+        http-request use-service lua.fakeserv
+
+} -start
+
+client c0 -connect ${h1_fe1_sock} {
+    txreq -url "/" -hdr "vtcport: ${s1_port}"
+    rxresp
+    expect resp.status == 200
+} -run
+
+
+server s1 -wait