vtest "vinylncsa -j json escaping"

feature cmd "python3 -c 'import json'"

server s1 -repeat 3 {
	rxreq
	txresp
} -start

vinyl v1 -vcl+backend {} -start

client c1 {
	txreq -hdr "x-request-id: YES" -hdr "test:ascii"
	rxresp
	txreq -hdr "x-request-id: ∃" -hdr "test:unicode"
	rxresp
	txreq -hdr "test:null"
	rxresp
} -run

delay 1

# ASCII string with -j is valid JSON
shell -match "^YES$" {
	vinylncsa -n ${v1_name} -d -q 'ReqHeader:test eq "ascii"' -j \
		-F '{"xrid": "%{X-REQUEST-ID}i"}' |
	python3 -c 'import sys, json; print(json.load(sys.stdin)["xrid"])'
}

# ∃ without -j is not valid JSON
shell -err -expect "Invalid \\escape" {
	vinylncsa -n ${v1_name} -d -q 'ReqHeader:test eq "unicode"' \
		-F '{"xrid": "%{X-REQUEST-ID}i"}' |
	python3 -c 'import sys, json; print(json.load(sys.stdin)["xrid"])'
}

# ∃ with -j is valid JSON
shell -match "^∃$" {
	vinylncsa -n ${v1_name} -d -q 'ReqHeader:test eq "unicode"' -j \
		-F '{"xrid": "%{X-REQUEST-ID}i"}' |
	python3 -c 'import sys, json; print(json.load(sys.stdin)["xrid"])'
}

# Empty strings are not replaced with "-"
shell -match "" {
	vinylncsa -n ${v1_name} -d -q 'ReqHeader:test eq "null"' -j \
		-F '{"xrid": "%{X-REQUEST-ID}i"}' |
	python3 -c 'import sys, json; print(json.load(sys.stdin)["xrid"])'
}

# Empty VCL_Log entries are not replaced with "-"
shell -match "" {
	vinylncsa -n ${v1_name} -d -q 'ReqHeader:test eq "null"' -j \
		-F '{"xrid": "%{VCL_Log:vinylncsa}x"}' |
	python3 -c 'import sys, json; print(json.load(sys.stdin)["xrid"])'
}
