-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
feee5ce
commit 2f2633d
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
integration-tests/js-compute/fixtures/app/src/http-cache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters