Skip to content

Instantly share code, notes, and snippets.

@gquintard
Created January 26, 2017 11:02
Show Gist options
  • Save gquintard/19f08338c7e63aca252b2ead38403a31 to your computer and use it in GitHub Desktop.
Save gquintard/19f08338c7e63aca252b2ead38403a31 to your computer and use it in GitHub Desktop.
varnishtest "Systematic revalidation"
server s1 {
rxreq
txresp -hdr "Etag: 01234" -body "foobar"
expect req.method == "GET"
rxreq
txresp -hdr "Etag: 01234"
expect req.method == "HEAD"
rxreq
txresp -hdr "Etag: 01234"
expect req.method == "HEAD"
rxreq
txresp -hdr "Etag: 56789"
expect req.method == "HEAD"
rxreq
txresp -hdr "Etag: 56789" -body "bazqux"
expect req.method == "GET"
rxreq
txresp -hdr "Etag: 56789"
expect req.method == "HEAD"
} -start
varnish v1 -vcl+backend {
sub vcl_recv {
if (req.restarts == 0) {
set req.http.x-state = "cache_check";
return (hash);
} else if (req.http.x-state == "backend_check") {
return (pass);
} else {
return (hash);
}
}
sub vcl_hit {
if (req.http.x-state == "cache_check") {
set req.http.x-state = "backend_check";
set req.http.etag = obj.http.etag;
return (restart);
} else {
return (deliver);
}
}
sub vcl_backend_fetch {
if (bereq.http.x-state == "backend_check") {
set bereq.method = "HEAD";
set bereq.http.method = "HEAD";
}
}
sub vcl_backend_response {
if (bereq.http.x-state == "backend_check") {
if (bereq.http.etag != beresp.http.etag) {
ban("obj.http.etag == " + bereq.http.etag);
}
}
}
sub vcl_deliver {
if (req.http.x-state == "backend_check") {
set req.http.x-state = "valid";
return (restart);
}
}
} -start
client c1 {
txreq -hdr "n: 1"
rxresp
expect resp.status == 200
expect resp.body == "foobar"
txreq -hdr "n: 2"
rxresp
expect resp.status == 200
expect resp.body == "foobar"
txreq -hdr "n: 3" -hdr "If-None-Match: 01234"
rxresp
expect resp.status == 304
txreq -hdr "n: 4"
rxresp
expect resp.status == 200
expect resp.body == "bazqux"
txreq -hdr "n: 5" -hdr "If-None-Match: 56789"
rxresp
expect resp.status == 304
} -run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment