haproxy/reg-tests/converter/bytes.vtc
Lokesh Jindal 915e48675a MEDIUM: sample: Enhances converter "bytes" to take variable names as arguments
Prior to this commit, converter "bytes" takes only integer values as
arguments.  After this commit, it can take variable names as inputs.
This allows us to dynamically determine the offset/length and capture
them in variables.  These variables can then be used with the converter.
Example use case: parsing a token present in a request header.
2023-09-22 08:48:51 +02:00

157 lines
4.7 KiB
Plaintext

varnishtest "bytes converter Test"
feature cmd "$HAPROXY_PROGRAM -cc 'version_atleast(2.9-dev4)'"
feature ignore_unknown_macro
# TEST - 1
server s1 {
rxreq
txresp -hdr "Connection: close"
} -repeat 1 -start
haproxy h1 -conf {
defaults
mode http
timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
frontend fe
bind "fd@${fe}"
#### requests
http-request set-var(txn.input) req.hdr(input)
http-response set-header bytes_0 "%[var(txn.input),bytes(0)]"
http-response set-header bytes_1 "%[var(txn.input),bytes(1)]"
http-response set-header bytes_0_3 "%[var(txn.input),bytes(0,3)]"
http-response set-header bytes_1_3 "%[var(txn.input),bytes(1,3)]"
http-response set-header bytes_99 "%[var(txn.input),bytes(99)]"
http-response set-header bytes_5 "%[var(txn.input),bytes(5)]"
http-response set-header bytes_6 "%[var(txn.input),bytes(6)]"
http-response set-header bytes_0_6 "%[var(txn.input),bytes(0,6)]"
http-response set-header bytes_0_7 "%[var(txn.input),bytes(0,7)]"
http-response set-var(txn.var_start) int(0)
http-response set-header bytes_var0 "%[var(txn.input),bytes(txn.var_start)]"
http-response set-var(txn.var_start) int(1)
http-response set-var(txn.var_length) int(3)
http-response set-header bytes_var1_var3 "%[var(txn.input),bytes(txn.var_start,txn.var_length)]"
http-response set-var(txn.var_start) int(99)
http-response set-header bytes_var99 "%[var(txn.input),bytes(txn.var_start)]"
http-response set-var(txn.var_start) int(0)
http-response set-var(txn.var_length) int(7)
http-response set-header bytes_var0_var7 "%[var(txn.input),bytes(txn.var_start,txn.var_length)]"
http-response set-var(txn.var_start) int(1)
http-response set-var(txn.var_length) int(3)
http-response set-header bytes_var1_3 "%[var(txn.input),bytes(txn.var_start,3)]"
http-response set-header bytes_1_var3 "%[var(txn.input),bytes(1,txn.var_length)]"
http-response set-var(txn.var_start) int(-1)
http-response set-var(txn.var_length) int(-1)
http-response set-header bytes_varminus1 "%[var(txn.input),bytes(txn.var_start)]"
http-response set-header bytes_0_varminus1 "%[var(txn.input),bytes(0,txn.var_length)]"
http-response set-header bytes_varNA "%[var(txn.input),bytes(txn.NA)]"
http-response set-header bytes_1_varNA "%[var(txn.input),bytes(1,txn.NA)]"
default_backend be
backend be
server s1 ${s1_addr}:${s1_port}
} -start
client c1 -connect ${h1_fe_sock} {
txreq -url "/" \
-hdr "input: 012345"
rxresp
expect resp.status == 200
expect resp.http.bytes_0 == "012345"
expect resp.http.bytes_1 == "12345"
expect resp.http.bytes_0_3 == "012"
expect resp.http.bytes_1_3 == "123"
expect resp.http.bytes_99 == ""
expect resp.http.bytes_5 == "5"
expect resp.http.bytes_6 == ""
expect resp.http.bytes_0_6 == "012345"
# since specified length is > input length, response contains the input till the end
expect resp.http.bytes_0_7 == "012345"
expect resp.http.bytes_var0 == "012345"
expect resp.http.bytes_var1_var3 == "123"
expect resp.http.bytes_var99 == ""
expect resp.http.bytes_var0_var7 == "012345"
expect resp.http.bytes_var1_3 == "123"
expect resp.http.bytes_1_var3 == "123"
expect resp.http.bytes_varminus1 == ""
expect resp.http.bytes_0_varminus1 == ""
expect resp.http.bytes_varNA == ""
expect resp.http.bytes_1_varNA == ""
} -run
# TEST - 2
# negative starting index causes startup failure
haproxy h2 -conf {
defaults
mode http
timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
frontend fe
bind "fd@${fe}"
http-response set-header bytes_output "%[var(txn.input),bytes(-1)]"
default_backend be
backend be
server s1 ${s1_addr}:${s1_port}
} -start -expectexit 1
# TEST - 3
# negative length causes startup failure
haproxy h3 -conf {
defaults
mode http
timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
frontend fe
bind "fd@${fe}"
http-response set-header bytes_output "%[var(txn.input),bytes(0,-1)]"
default_backend be
backend be
server s1 ${s1_addr}:${s1_port}
} -start -expectexit 1
# TEST - 4
# 0 length causes startup failure
haproxy h4 -conf {
defaults
mode http
timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
frontend fe
bind "fd@${fe}"
http-response set-header bytes_output "%[var(txn.input),bytes(0,0)]"
default_backend be
backend be
server s1 ${s1_addr}:${s1_port}
} -start -expectexit 1