Skip to content

Commit 468d807

Browse files
committed
it kinda works
1 parent 3ed757a commit 468d807

File tree

8 files changed

+467
-254
lines changed

8 files changed

+467
-254
lines changed

integration-tests/js-compute/fixtures/module-mode/src/http-cache.js

+102-84
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env serviceworker */
2-
import { strictEqual, assertRejects } from './assertions.js';
2+
import { strictEqual, deepStrictEqual, assertRejects } from './assertions.js';
33
import { routes } from './routes.js';
44
import { CacheOverride } from 'fastly:cache-override';
55
import { Backend } from 'fastly:backend';
@@ -77,75 +77,6 @@ const httpBinBackend = () =>
7777
);
7878
});
7979

80-
// Test invalid transform stream
81-
routes.set('/http-cache/invalid-transform', async () => {
82-
const url = getTestUrl();
83-
84-
await assertRejects(
85-
() =>
86-
fetch(url, {
87-
cacheOverride: new CacheOverride({
88-
afterSend() {
89-
return {
90-
bodyTransform: 'not a transform stream',
91-
};
92-
},
93-
}),
94-
}),
95-
TypeError,
96-
);
97-
});
98-
}
99-
100-
// Test suite: Response Property Coverage
101-
{
102-
// Test readonly properties
103-
routes.set('/http-cache/readonly-properties', async () => {
104-
const url = getTestUrl();
105-
const backend = new Backend({
106-
name: 'test_backend',
107-
target: 'httpbin.org',
108-
});
109-
110-
const cacheOverride = new CacheOverride({
111-
afterSend(res) {
112-
res.ttl = 3600;
113-
return { cache: true };
114-
},
115-
});
116-
117-
// Initial request
118-
const res1 = await fetch(url, { backend, cacheOverride });
119-
strictEqual(res1.backend, backend);
120-
strictEqual(res1.age, 0);
121-
122-
// Wait a bit and verify age increased
123-
await new Promise((resolve) => setTimeout(resolve, 1000));
124-
const res2 = await fetch(url, { backend, cacheOverride });
125-
strictEqual(res2.age > 0, true);
126-
strictEqual(res2.backend, backend);
127-
128-
// Verify readonly properties cannot be modified
129-
assertRejects(() => {
130-
res2.cached = false;
131-
}, TypeError);
132-
assertRejects(() => {
133-
res2.isCacheable = false;
134-
}, TypeError);
135-
assertRejects(() => {
136-
res2.isStale = true;
137-
}, TypeError);
138-
assertRejects(() => {
139-
res2.age = 0;
140-
}, TypeError);
141-
assertRejects(() => {
142-
res2.backend = null;
143-
}, TypeError);
144-
});
145-
}
146-
147-
// Test suite: Property Error Handling
148-
{
14980
// Test invalid property assignments
15081
routes.set('/http-cache/property-errors', async () => {
15182
const url = getTestUrl();
@@ -169,7 +100,7 @@ const httpBinBackend = () =>
169100
fetch(url, {
170101
cacheOverride: new CacheOverride({
171102
afterSend(res) {
172-
res.vary = ['not-a-set'];
103+
res.vary = new Set(['not-an-array']);
173104
},
174105
}),
175106
}),
@@ -182,7 +113,7 @@ const httpBinBackend = () =>
182113
fetch(url, {
183114
cacheOverride: new CacheOverride({
184115
afterSend(res) {
185-
res.surrogateKeys = ['not-a-set'];
116+
res.surrogateKeys = new Set(['not-an-array']);
186117
},
187118
}),
188119
}),
@@ -208,7 +139,7 @@ const httpBinBackend = () =>
208139
fetch(url, {
209140
cacheOverride: new CacheOverride({
210141
afterSend(res) {
211-
res.vary = new Set([1, 2, 3]); // Should only accept strings
142+
res.vary = [1, 2, 3]; // Should only accept strings
212143
},
213144
}),
214145
}),
@@ -221,7 +152,7 @@ const httpBinBackend = () =>
221152
fetch(url, {
222153
cacheOverride: new CacheOverride({
223154
afterSend(res) {
224-
res.surrogateKeys = new Set([1, 2, 3]); // Should only accept strings
155+
res.surrogateKeys = [1, 2, 3]; // Should only accept strings
225156
},
226157
}),
227158
}),
@@ -230,25 +161,93 @@ const httpBinBackend = () =>
230161
});
231162

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

236167
// Test accessing cache properties on non-cached response
168+
{
169+
let candidateRes;
170+
const cacheOverride = new CacheOverride({
171+
afterSend(res) {
172+
candidateRes = res;
173+
return { cache: false };
174+
},
175+
});
176+
177+
await fetch(url, { cacheOverride });
178+
strictEqual(candidateRes.cached, false);
179+
180+
strictEqual(candidateRes.isStale, false);
181+
strictEqual(candidateRes.ttl, 3600);
182+
strictEqual(candidateRes.age, 0);
183+
deepStrictEqual(candidateRes.vary, []);
184+
strictEqual(candidateRes.surrogateKeys.length, 1);
185+
strictEqual(typeof candidateRes.surrogateKeys[0], 'string');
186+
strictEqual(candidateRes.surrogateKeys[0].length > 10, true);
187+
}
188+
189+
// Test accessing cache properties on non-cached candidate response
190+
{
191+
const cacheOverride = new CacheOverride({
192+
afterSend(candidateRes) {
193+
strictEqual(candidateRes.cached, false);
194+
strictEqual(candidateRes.isStale, false);
195+
strictEqual(candidateRes.ttl, 3600);
196+
strictEqual(candidateRes.age, 0);
197+
deepStrictEqual(candidateRes.vary, []]);
198+
strictEqual(candidateRes.surrogateKeys.length, 1);
199+
strictEqual(typeof candidateRes.surrogateKeys[0], 'string');
200+
strictEqual(candidateRes.surrogateKeys[0].length > 10, true);
201+
return { cache: false };
202+
},
203+
});
204+
205+
await fetch(url, { cacheOverride });
206+
}
207+
});
208+
209+
// Test readonly properties
210+
routes.set('/http-cache/readonly-properties', async () => {
211+
const url = getTestUrl();
212+
const backend = new Backend({
213+
name: 'test_backend',
214+
target: 'httpbin.org',
215+
});
216+
237217
const cacheOverride = new CacheOverride({
238218
afterSend(res) {
239-
return { cache: false };
219+
res.ttl = 3600;
220+
return { cache: true };
240221
},
241222
});
242223

243-
const res = await fetch(url, { cacheOverride });
244-
strictEqual(res.cached, false);
224+
// Initial request
225+
const res1 = await fetch(url, { backend, cacheOverride });
226+
strictEqual(res1.backend, backend);
227+
strictEqual(res1.age, 0);
228+
229+
// Wait a bit and verify age increased
230+
await new Promise((resolve) => setTimeout(resolve, 1000));
231+
const res2 = await fetch(url, { backend, cacheOverride });
232+
strictEqual(res2.age > 0, true);
233+
strictEqual(res2.backend, backend);
245234

246-
// These should all be undefined for non-cached responses
247-
strictEqual(res.isStale, undefined);
248-
strictEqual(res.ttl, undefined);
249-
strictEqual(res.age, undefined);
250-
strictEqual(res.vary, undefined);
251-
strictEqual(res.surrogateKeys, undefined);
235+
// Verify readonly properties cannot be modified
236+
assertRejects(() => {
237+
res2.cached = false;
238+
}, TypeError);
239+
assertRejects(() => {
240+
res2.isCacheable = false;
241+
}, TypeError);
242+
assertRejects(() => {
243+
res2.isStale = true;
244+
}, TypeError);
245+
assertRejects(() => {
246+
res2.age = 0;
247+
}, TypeError);
248+
assertRejects(() => {
249+
res2.backend = null;
250+
}, TypeError);
252251
});
253252
}
254253

@@ -458,6 +457,25 @@ const httpBinBackend = () =>
458457

459458
// Test suite: Body transform
460459
{
460+
// Test invalid transform stream
461+
routes.set('/http-cache/invalid-transform', async () => {
462+
const url = getTestUrl();
463+
464+
await assertRejects(
465+
() =>
466+
fetch(url, {
467+
cacheOverride: new CacheOverride({
468+
afterSend() {
469+
return {
470+
bodyTransform: 'not a transform stream',
471+
};
472+
},
473+
}),
474+
}),
475+
TypeError,
476+
);
477+
});
478+
461479
routes.set('/http-cache/body-transform', async () => {
462480
const url = getTestUrl();
463481

integration-tests/js-compute/fixtures/module-mode/src/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ addEventListener('fetch', (event) => {
1515
event.respondWith(app(event));
1616
});
1717

18+
if (fastly.debugMessages) {
19+
const { debug: consoleDebug } = console;
20+
console.debug = function debug (...args) {
21+
fastly.debugMessages.push(...args);
22+
consoleDebug(...args);
23+
};
24+
}
25+
1826
/**
1927
* @param {FetchEvent} event
2028
* @returns {Response}

integration-tests/js-compute/fixtures/module-mode/tests.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,13 @@
155155
"GET /http-cache/invalid-properties": {
156156
"environments": ["compute"]
157157
},
158-
"GET /http-cache/invalid-transform": {
159-
"environments": ["compute"]
160-
},
161158
"GET /http-cache/readonly-properties": {
162159
"environments": ["compute"]
163160
},
164161
"GET /http-cache/property-errors": {
165162
"environments": ["compute"]
166163
},
167-
"GET /http-cache/property-access-errors": {
164+
"GET /http-cache/candidate-response-properties": {
168165
"environments": ["compute"]
169166
},
170167
"GET /http-cache/after-send-edge-cache": {
@@ -191,6 +188,9 @@
191188
"GET /http-cache/stale-responses": {
192189
"environments": ["compute"]
193190
},
191+
"GET /http-cache/invalid-transform": {
192+
"environments": ["compute"]
193+
},
194194
"GET /http-cache/body-transform": {
195195
"environments": ["compute"]
196196
},

0 commit comments

Comments
 (0)