Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Feb 9, 2024
1 parent 93a5608 commit bc1c161
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ const fileSystem = {
headers.append('Set-Cookie', 'world');
return new Response(null, { headers });
}`,
'/src/pages/streaming.js': `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'));
},
cancel() {
locals.cancelledByTheServer = true;
}
});
return new Response(readableStream, {
headers: {
"Content-Type": "text/event-stream"
}
})
}`,
};

describe('endpoints', () => {
Expand Down Expand Up @@ -60,4 +82,23 @@ describe('endpoints', () => {
'set-cookie': ['hello', 'world'],
});
});

it('Headers with multisple values (set-cookie special case)', async () => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/streaming',
});

const locals = { cancelledByTheServer: false }
req[Symbol.for("astro.locals")] = locals

container.handle(req, res);

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

await done;

expect(locals).to.deep.equal({ cancelledByTheServer: true });
});
});

0 comments on commit bc1c161

Please sign in to comment.