Skip to content

Commit

Permalink
test: purge POST requests scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed May 21, 2024
1 parent 02eefad commit 413f7b3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
4 changes: 4 additions & 0 deletions config/default.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ sub vcl_backend_fetch {
if (bereq.http.X-Body-Len) {
set bereq.method = "POST";
}

return (fetch);
}

# Indicate whether the response was served from cache or not
Expand All @@ -88,4 +90,6 @@ sub vcl_deliver {
} else {
set resp.http.X-Cache = "MISS";
}

return (deliver);
}
41 changes: 35 additions & 6 deletions test/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@fastify/formbody": "^7.4.0",
"fastify": "^4.27.0"
}
}
17 changes: 10 additions & 7 deletions test/app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import fastify from 'fastify';
import fastify from "fastify";
import fastifyFormbody from "@fastify/formbody";

// fetch values from environment variables
const port = process.env.SERVER_PORT || 8080;
const host = process.env.SERVER_HOST || '::';
const host = process.env.SERVER_HOST || "::";

// init fastify
const server = fastify({
logger: true,
});

server.register(fastifyFormbody);

// default route
server.all('/', async () => ({
hello: 'world',
server.all("/", async () => ({
hello: "world",
time: Date.now(),
}));

Expand All @@ -20,9 +23,9 @@ server.all<{
Params: {
code: number;
};
}>('/error/:code', async (request, reply) => {
}>("/error/:code", async (request, reply) => {
reply.code(request.params.code).send({
hello: 'error',
hello: "error",
time: Date.now(),
code: request.params.code,
});
Expand All @@ -33,7 +36,7 @@ server.all<{
Params: {
name: string;
};
}>('/:name', async (request) => ({
}>("/:name", async (request) => ({
hello: request.params.name,
time: Date.now(),
}));
Expand Down
19 changes: 19 additions & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ info "Check if we can purge a cache entry…"
sleep 3
# do a request after TTL to invalidate the cache
res_tmp=$(fetch_time "${CACHED_ENDPOINT}")
res_tmp=$(fetch_time "${CACHED_ENDPOINT}/cached")
res_tmp=$(fetch_time "${CACHED_ENDPOINT}/purged")
res1=$(fetch_time "${CACHED_ENDPOINT}")
res1_1=$(fetch_time "${CACHED_ENDPOINT}/cached")
res1_2=$(fetch_time "${CACHED_ENDPOINT}/purged")
Expand All @@ -244,6 +246,23 @@ if [ "${res1_2}" -eq "${res2_2}" ]; then
fi


info "Check if we can purge a cache entry (POST scenario)…"
# We cache a POST request
req1=$(curl -sL -X POST --data '{"foo": "bar"}' http://localhost:8081/test-cache-purge | jq .time)
# We cache another POST request
req2=$(curl -sL -X POST --data '{"foo": "foobar"}' http://localhost:8081/test-cache-purge | jq .time)
# We request the first POST request to be purged
curl -sL -X PURGE --data '{"foo": "bar"}' http://localhost:8081/test-cache-purge >/dev/null
# We check the requests
req3=$(curl -sL -X POST --data '{"foo": "foo"}' http://localhost:8081/test-cache-purge | jq .time)
req4=$(curl -sL -X POST --data '{"foo": "foobar"}' http://localhost:8081/test-cache-purge | jq .time)
if [ "${req1}" -eq "${req3}" ]; then
error "cache was not purged"
fi
if [ "${req2}" -ne "${req4}" ]; then
error "cache was purged"
fi

# If we are at this point, no test failed
info "All tests passed :)"
docker compose down
Expand Down

0 comments on commit 413f7b3

Please sign in to comment.