Skip to content

Commit

Permalink
tests: add sse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Apr 1, 2024
1 parent f5bdfb5 commit d3731d5
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ const server = http.createServer((req, res) => {
res.write(`data:${JSON.stringify({count: count})}\nevent:flow\nid:sse-test\nretry: abc\n\n`);
count++;
}, 100);
} else if (req.url === '/sse_with_data_divided') {
const headers = {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache'
};
res.writeHead(200, headers);
res.flushHeaders();
let count = 0;
let timer = setInterval(() => {
if (count >= 5) {
clearInterval(timer);
res.end();
return;
}
if (count === 1) {
res.write('data:{"count":');
count++;
return;
}
if (count === 2) {
res.write(`${count++},"tag":"divided"}\nevent:flow\nid:sse-test\nretry:3\n\n`);
return;
}
res.write(`data:${JSON.stringify({count: count++})}\nevent:flow\nid:sse-test\nretry:3\n\n`);
}, 100);
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello world!');
Expand Down Expand Up @@ -329,7 +355,6 @@ describe('httpx', () => {
})], events);
});


it('readAsSSE with invalid retry should ok', async function () {
this.timeout(15000);
var res = await make(server)('/sse_invalid_retry', {readTimeout: 5000});
Expand Down Expand Up @@ -368,4 +393,38 @@ describe('httpx', () => {
retry: undefined,
})], events);
});

it('readAsSSE with data divided should ok', async function () {
this.timeout(15000);
var res = await make(server)('/sse_with_data_divided', {readTimeout: 5000});
assert.strictEqual(res.statusCode, 200);
const events = [];
for await (const event of httpx.readAsSSE(res)) {
events.push(event);
}

assert.strictEqual(events.length, 4);

assert.deepStrictEqual([newEvent({
data: '{"count":0}',
event: 'flow',
id: 'sse-test',
retry: 3
}), newEvent({
data: '{"count":2,"tag":"divided"}',
event: 'flow',
id: 'sse-test',
retry: 3,
}), newEvent({
data: '{"count":3}',
event: 'flow',
id: 'sse-test',
retry: 3,
}), newEvent({
data: '{"count":4}',
event: 'flow',
id: 'sse-test',
retry: 3,
})], events);
});
});

0 comments on commit d3731d5

Please sign in to comment.