Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rezan
Last active December 2, 2019 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezan/38cf0d17dad9fe7a3e144c62a055f221 to your computer and use it in GitHub Desktop.
Save rezan/38cf0d17dad9fe7a3e144c62a055f221 to your computer and use it in GitHub Desktop.
Varnish VCL JSON support (test)
varnishtest "JSONP support"
server s1 -repeat 2 {
rxreq
txresp -hdr "Content-Type: application/json" -body {"json"}
} -start
varnish v1 -vcl+backend {
import urlplus;
import xbody;
sub vcl_backend_response {
# Insert the JSONP callback
if (urlplus.query_get("callback") && beresp.http.Content-Type ~ "json") {
xbody.regsub("^", urlplus.query_get("callback") + "(");
xbody.regsub("$", ");");
set beresp.http.Content-Type = regsub(beresp.http.Content-Type, "json", "javascript");
}
}
} -start
client c1 {
txreq -url /1
rxresp
expect resp.status == 200
expect resp.body == {"json"}
expect resp.http.Content-Type ~ "json"
txreq -url /1?callback=my_func
rxresp
expect resp.status == 200
expect resp.body == {my_func("json");}
expect resp.http.Content-Type ~ "javascript"
} -run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment