Skip to content

Instantly share code, notes, and snippets.

@rezan
Created March 20, 2020 20:41
Show Gist options
  • Save rezan/d49cc61c5ff70dfd89f47ff08fccfe2c to your computer and use it in GitHub Desktop.
Save rezan/d49cc61c5ff70dfd89f47ff08fccfe2c to your computer and use it in GitHub Desktop.
Generate an ETag with xbody
varnishtest "Use xbody to generate an Etag"
server s1 {
rxreq
txresp -body "This is a test"
} -start
varnish v1 -vcl+backend {
import crypto;
import xbody;
sub vcl_backend_response {
xbody.regsub("test", "cat");
xbody.hash_body(sha1);
}
sub vcl_deliver {
set resp.http.ETag = {"""} + crypto.hex_encode(xbody.get_hash()) + {"""};
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.ETag == {"2630a8082bcee40c4a9b341ac30c325378428c21"}
expect resp.body == "This is a cat"
txreq -hdr {If-None-Match: "2630a8082bcee40c4a9b341ac30c325378428c21"}
rxresp
expect resp.status == 304
expect resp.bodylen == 0
} -run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment