Skip to content

Commit

Permalink
it kinda works
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jan 15, 2025
1 parent 3ed757a commit 468d807
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 254 deletions.
186 changes: 102 additions & 84 deletions integration-tests/js-compute/fixtures/module-mode/src/http-cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env serviceworker */
import { strictEqual, assertRejects } from './assertions.js';
import { strictEqual, deepStrictEqual, assertRejects } from './assertions.js';
import { routes } from './routes.js';
import { CacheOverride } from 'fastly:cache-override';
import { Backend } from 'fastly:backend';
Expand Down Expand Up @@ -77,75 +77,6 @@ const httpBinBackend = () =>
);
});

// Test invalid transform stream
routes.set('/http-cache/invalid-transform', async () => {
const url = getTestUrl();

await assertRejects(
() =>
fetch(url, {
cacheOverride: new CacheOverride({
afterSend() {
return {
bodyTransform: 'not a transform stream',
};
},
}),
}),
TypeError,
);
});
}

// Test suite: Response Property Coverage
{
// Test readonly properties
routes.set('/http-cache/readonly-properties', async () => {
const url = getTestUrl();
const backend = new Backend({
name: 'test_backend',
target: 'httpbin.org',
});

const cacheOverride = new CacheOverride({
afterSend(res) {
res.ttl = 3600;
return { cache: true };
},
});

// Initial request
const res1 = await fetch(url, { backend, cacheOverride });
strictEqual(res1.backend, backend);
strictEqual(res1.age, 0);

// Wait a bit and verify age increased
await new Promise((resolve) => setTimeout(resolve, 1000));
const res2 = await fetch(url, { backend, cacheOverride });
strictEqual(res2.age > 0, true);
strictEqual(res2.backend, backend);

// Verify readonly properties cannot be modified
assertRejects(() => {
res2.cached = false;
}, TypeError);
assertRejects(() => {
res2.isCacheable = false;
}, TypeError);
assertRejects(() => {
res2.isStale = true;
}, TypeError);
assertRejects(() => {
res2.age = 0;
}, TypeError);
assertRejects(() => {
res2.backend = null;
}, TypeError);
});
}

// Test suite: Property Error Handling
{
// Test invalid property assignments
routes.set('/http-cache/property-errors', async () => {
const url = getTestUrl();
Expand All @@ -169,7 +100,7 @@ const httpBinBackend = () =>
fetch(url, {
cacheOverride: new CacheOverride({
afterSend(res) {
res.vary = ['not-a-set'];
res.vary = new Set(['not-an-array']);
},
}),
}),
Expand All @@ -182,7 +113,7 @@ const httpBinBackend = () =>
fetch(url, {
cacheOverride: new CacheOverride({
afterSend(res) {
res.surrogateKeys = ['not-a-set'];
res.surrogateKeys = new Set(['not-an-array']);
},
}),
}),
Expand All @@ -208,7 +139,7 @@ const httpBinBackend = () =>
fetch(url, {
cacheOverride: new CacheOverride({
afterSend(res) {
res.vary = new Set([1, 2, 3]); // Should only accept strings
res.vary = [1, 2, 3]; // Should only accept strings
},
}),
}),
Expand All @@ -221,7 +152,7 @@ const httpBinBackend = () =>
fetch(url, {
cacheOverride: new CacheOverride({
afterSend(res) {
res.surrogateKeys = new Set([1, 2, 3]); // Should only accept strings
res.surrogateKeys = [1, 2, 3]; // Should only accept strings
},
}),
}),
Expand All @@ -230,25 +161,93 @@ const httpBinBackend = () =>
});

// Test property access on invalid states
routes.set('/http-cache/property-access-errors', async () => {
routes.set('/http-cache/candidate-response-properties', async () => {
const url = getTestUrl();

// Test accessing cache properties on non-cached response
{
let candidateRes;
const cacheOverride = new CacheOverride({
afterSend(res) {
candidateRes = res;
return { cache: false };
},
});

await fetch(url, { cacheOverride });
strictEqual(candidateRes.cached, false);

strictEqual(candidateRes.isStale, false);
strictEqual(candidateRes.ttl, 3600);
strictEqual(candidateRes.age, 0);
deepStrictEqual(candidateRes.vary, []);
strictEqual(candidateRes.surrogateKeys.length, 1);
strictEqual(typeof candidateRes.surrogateKeys[0], 'string');
strictEqual(candidateRes.surrogateKeys[0].length > 10, true);
}

// Test accessing cache properties on non-cached candidate response
{
const cacheOverride = new CacheOverride({
afterSend(candidateRes) {
strictEqual(candidateRes.cached, false);
strictEqual(candidateRes.isStale, false);
strictEqual(candidateRes.ttl, 3600);
strictEqual(candidateRes.age, 0);
deepStrictEqual(candidateRes.vary, []]);
strictEqual(candidateRes.surrogateKeys.length, 1);
strictEqual(typeof candidateRes.surrogateKeys[0], 'string');
strictEqual(candidateRes.surrogateKeys[0].length > 10, true);
return { cache: false };
},
});

await fetch(url, { cacheOverride });
}
});

// Test readonly properties
routes.set('/http-cache/readonly-properties', async () => {
const url = getTestUrl();
const backend = new Backend({
name: 'test_backend',
target: 'httpbin.org',
});

const cacheOverride = new CacheOverride({
afterSend(res) {
return { cache: false };
res.ttl = 3600;
return { cache: true };
},
});

const res = await fetch(url, { cacheOverride });
strictEqual(res.cached, false);
// Initial request
const res1 = await fetch(url, { backend, cacheOverride });
strictEqual(res1.backend, backend);
strictEqual(res1.age, 0);

// Wait a bit and verify age increased
await new Promise((resolve) => setTimeout(resolve, 1000));
const res2 = await fetch(url, { backend, cacheOverride });
strictEqual(res2.age > 0, true);
strictEqual(res2.backend, backend);

// These should all be undefined for non-cached responses
strictEqual(res.isStale, undefined);
strictEqual(res.ttl, undefined);
strictEqual(res.age, undefined);
strictEqual(res.vary, undefined);
strictEqual(res.surrogateKeys, undefined);
// Verify readonly properties cannot be modified
assertRejects(() => {
res2.cached = false;
}, TypeError);
assertRejects(() => {
res2.isCacheable = false;
}, TypeError);
assertRejects(() => {
res2.isStale = true;
}, TypeError);
assertRejects(() => {
res2.age = 0;
}, TypeError);
assertRejects(() => {
res2.backend = null;
}, TypeError);
});
}

Expand Down Expand Up @@ -458,6 +457,25 @@ const httpBinBackend = () =>

// Test suite: Body transform
{
// Test invalid transform stream
routes.set('/http-cache/invalid-transform', async () => {
const url = getTestUrl();

await assertRejects(
() =>
fetch(url, {
cacheOverride: new CacheOverride({
afterSend() {
return {
bodyTransform: 'not a transform stream',
};
},
}),
}),
TypeError,
);
});

routes.set('/http-cache/body-transform', async () => {
const url = getTestUrl();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ addEventListener('fetch', (event) => {
event.respondWith(app(event));
});

if (fastly.debugMessages) {
const { debug: consoleDebug } = console;
console.debug = function debug (...args) {
fastly.debugMessages.push(...args);
consoleDebug(...args);
};
}

/**
* @param {FetchEvent} event
* @returns {Response}
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/js-compute/fixtures/module-mode/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,13 @@
"GET /http-cache/invalid-properties": {
"environments": ["compute"]
},
"GET /http-cache/invalid-transform": {
"environments": ["compute"]
},
"GET /http-cache/readonly-properties": {
"environments": ["compute"]
},
"GET /http-cache/property-errors": {
"environments": ["compute"]
},
"GET /http-cache/property-access-errors": {
"GET /http-cache/candidate-response-properties": {
"environments": ["compute"]
},
"GET /http-cache/after-send-edge-cache": {
Expand All @@ -191,6 +188,9 @@
"GET /http-cache/stale-responses": {
"environments": ["compute"]
},
"GET /http-cache/invalid-transform": {
"environments": ["compute"]
},
"GET /http-cache/body-transform": {
"environments": ["compute"]
},
Expand Down
Loading

0 comments on commit 468d807

Please sign in to comment.