| | @@ -1,0 +1,66 @@ |
| 1 | +// Smoke test for Response.clone() on a streaming body. GET / answers a JSON |
| 2 | +// report; GET /served returns a streamed response after handing a clone to |
| 3 | +// cache.put() in waitUntil (palm's media proxy), checked by run.sh's |
| 4 | +// paths.txt. |
| 5 | +const streamOf = (...parts) => { |
| 6 | + const encoder = new TextEncoder(); |
| 7 | + return new ReadableStream({ |
| 8 | + async pull(controller) { |
| 9 | + const next = parts.shift(); |
| 10 | + if (next === undefined) controller.close(); |
| 11 | + else controller.enqueue(encoder.encode(next)); |
| 12 | + }, |
| 13 | + }); |
| 14 | +}; |
| 15 | + |
| 16 | +const expectEqual = (name, got, want) => ({ |
| 17 | + name, |
| 18 | + ok: JSON.stringify(got) === JSON.stringify(want), |
| 19 | + got, |
| 20 | +}); |
| 21 | + |
| 22 | +export default { |
| 23 | + async fetch(request, env, ctx) { |
| 24 | + const url = new URL(request.url); |
| 25 | + if (url.pathname === "/served") { |
| 26 | + const response = new Response(streamOf("streamed ", "through ", "the host"), { |
| 27 | + headers: { "content-type": "text/plain", "x-smoke": "1" }, |
| 28 | + }); |
| 29 | + ctx.waitUntil(caches.default.put(new Request("https://smoke.invalid/served"), response.clone())); |
| 30 | + return response; |
| 31 | + } |
| 32 | + |
| 33 | + const checks = []; |
| 34 | + const original = new Response(streamOf("a", "b", "c"), { status: 201, headers: { "x-k": "v" } }); |
| 35 | + const copy = original.clone(); |
| 36 | + checks.push(expectEqual("original not used by clone", original.bodyUsed, false)); |
| 37 | + checks.push(expectEqual("clone keeps status", copy.status, 201)); |
| 38 | + checks.push(expectEqual("clone keeps headers", copy.headers.get("x-k"), "v")); |
| 39 | + checks.push(expectEqual("clone headers are separate", copy.headers === original.headers, false)); |
| 40 | + // Read the clone first: a tee must buffer for the branch read later. |
| 41 | + checks.push(expectEqual("clone body", await copy.text(), "abc")); |
| 42 | + checks.push(expectEqual("original body", await original.text(), "abc")); |
| 43 | + |
| 44 | + const twice = new Response(streamOf("x", "y")); |
| 45 | + const first = twice.clone(); |
| 46 | + const second = twice.clone(); |
| 47 | + const texts = await Promise.all([twice.text(), first.text(), second.text()]); |
| 48 | + checks.push(expectEqual("clone of a clone", texts, ["xy", "xy", "xy"])); |
| 49 | + |
| 50 | + const used = new Response(streamOf("z")); |
| 51 | + await used.text(); |
| 52 | + let threw = ""; |
| 53 | + try { used.clone(); } catch (error) { threw = error.constructor.name; } |
| 54 | + checks.push(expectEqual("clone after use throws", threw, "TypeError")); |
| 55 | + |
| 56 | + checks.push(expectEqual("null body clone", new Response(null, { status: 204 }).clone().body, null)); |
| 57 | + checks.push(expectEqual("bytes body clone", await new Response("bytes").clone().text(), "bytes")); |
| 58 | + |
| 59 | + const forCache = new Response(streamOf("cached ", "copy")); |
| 60 | + await caches.default.put(new Request("https://smoke.invalid/x"), forCache.clone()); |
| 61 | + checks.push(expectEqual("original readable after cache.put(clone)", await forCache.text(), "cached copy")); |
| 62 | + |
| 63 | + const failed = checks.filter((c) => !c.ok); |
| 64 | + return Response.json({ ok: failed.length === 0, failed, checks }, { status: failed.length ? 500 : 200 }); |
| 65 | + }, |
| 66 | +}; |