Skip to content

Commit

Permalink
integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Dec 13, 2024
1 parent feee5ce commit 2f2633d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
89 changes: 89 additions & 0 deletions integration-tests/js-compute/fixtures/app/src/http-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-env serviceworker */
import { strictEqual, assertRejects } from './assertions.js';
import { routes } from './routes.js';
import { allowDynamicBackends } from 'fastly:experimental';
allowDynamicBackends(true);

// generate a unique URL everytime so that we never work on a populated cache
const url = `https://httpbin.org/anything?${Math.random().toString().slice(2)}`;

routes.set('/http-cache/hook-errors', async () => {
await assertRejects(() => fetch(url, {
cacheOverride: new CacheOverride({
beforeSend() {
throw new Error('before send error');
}
})
}), Error, 'before send error');

await assertRejects(() => fetch(url, {
cacheOverride: new CacheOverride({
afterSend() {
throw new Error('after send error');
}
})
}), Error, 'after send error');
})

// HTTP cache freshness
routes.set('/http-cache/after-send-edge-cache', async () => {
let calledAfterSend = false;
const cacheOverride = new CacheOverride({
afterSend(res) {
calledAfterSend = true;
res.headers.set('Cache-Control', 'private, no-store');
res.ttl = 3600 - res.age;
// This is the default, so it is not needed to provide explicitly
// return { cache: true };
}
});
let res = await fetch(url, { cacheOverride });
strictEqual(calledAfterSend, true);
strictEqual(res.headers.get('Cache-Control'), 'private, no-store');
calledAfterSend = false;
res = await fetch(url, { cacheOverride });
strictEqual(calledAfterSend, true);
strictEqual(res.headers.get('Cache-Control'), 'private, no-store');
});

routes.set('/http-cache/after-send-browser-cache', async () => {
let calledAfterSend = false;
const cacheOverride = new CacheOverride({
afterSend(res) {
calledAfterSend = true;
res.headers.set('Cache-Control', 'max-age=3600');
return { cache: false };
}
});
let res = await fetch(url, { cacheOverride });
strictEqual(calledAfterSend);
strictEqual(res.headers.get('Cache-Control'), 'max-age=3600');
res = await fetch(url, { cacheOverride });
// should be cached
strictEqual(res.cached, true);
strictEqual(calledAfterSend, false);
strictEqual(res.headers.get('Cache-Control'), 'max-age=3600');
});

routes.set('/http-cache/before-send', async () => {
let calledBeforeSend = false;
const cacheOverride = new CacheOverride({
beforeSend(req) {
strictEqual(calledBeforeSend, false);
calledBeforeSend = true;
req.headers.set('Foo', 'Baz');
}
});
const headers = {
'Foo': 'Bar'
};
const [res1, res2] = await Promise.all([fetch(url, { cacheOverride, headers }), fetch(url, { cacheOverride, headers })]);
strictEqual(res1.cached, true);
strictEqual(res2.cached, true);
strictEqual(res1.headers.get('Foo'), 'Baz');
strictEqual(res2.headers.get('Foo'), 'Baz');
});

// Testing TODO:
// - new properties
// - body transform
1 change: 1 addition & 0 deletions integration-tests/js-compute/fixtures/app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import './fastly-global.js';
import './fetch-errors.js';
import './geoip.js';
import './headers.js';
import './http-cache.js';
import './include-bytes.js';
import './logger.js';
import './manual-framing-headers.js';
Expand Down

0 comments on commit 2f2633d

Please sign in to comment.