Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Nov 12, 2023
1 parent aab1a28 commit c25ffa5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/integrations/node/test/api-route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,23 @@ describe('API routes', () => {
let [out] = await done;
expect(new Uint8Array(out.buffer)).to.deep.equal(expectedDigest);
});

it('Can bail on streaming', async () => {
const { handler } = await import('./fixtures/api-route/dist/server/entry.mjs');
let { req, res, done } = createRequestAndResponse({
url: '/streaming',
});

let locals = { cancelledByTheServer: false };

handler(req, res, () => {}, locals);
req.send();

await new Promise((resolve) => setTimeout(resolve, 500));
res.emit("close");

await done;

expect(locals).to.deep.include({ cancelledByTheServer: true });
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'node:crypto';

export async function post({ request }: { request: Request }) {
export async function POST({ request }: { request: Request }) {
const hash = crypto.createHash('sha256');

const iterable = request.body as unknown as AsyncIterable<Uint8Array>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const GET = ({ locals }) => {
let sentChunks = 0;

const readableStream = new ReadableStream({
async pull(controller) {
if (sentChunks === 3) return controller.close();
else sentChunks++;

await new Promise(resolve => setTimeout(resolve, 1000));
controller.enqueue(new TextEncoder().encode('hello\n'));
},
cancel() {
locals.cancelledByTheServer = true;
}
});

return new Response(readableStream, {
headers: {
"Content-Type": "text/event-stream"
}
});
}

0 comments on commit c25ffa5

Please sign in to comment.