-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(stream): ensure it writes head & pipe
- Loading branch information
1 parent
0054d0d
commit bff12b4
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Writable } from 'node:stream' | ||
import type { HeaderStream } from '../src/stream.js' | ||
|
||
export class Sink extends Writable implements HeaderStream { | ||
#chunks: Buffer[] = [] | ||
|
||
constructor() { | ||
super({ objectMode: true }) | ||
} | ||
|
||
assertWriteHead(assertion: (statusCode: number, headers: any) => void) { | ||
// @ts-expect-error - Mocking the writeHead method | ||
this.writeHead = (statusCode, headers) => { | ||
assertion(statusCode, headers) | ||
} | ||
} | ||
|
||
get content() { | ||
return this.#chunks.join('') | ||
} | ||
|
||
_write(chunk: Buffer, _encoding: BufferEncoding, callback: (error?: Error | null) => void): void { | ||
this.#chunks.push(chunk) | ||
callback() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { randomUUID } from 'node:crypto' | ||
import { test } from '@japa/runner' | ||
import { Stream } from '../src/stream.js' | ||
import { Sink } from '../test-helpers/sink.js' | ||
|
||
test.group('Stream', () => { | ||
test('should write multiple chunks to the stream', async ({ assert }) => { | ||
const stream = new Stream(randomUUID()) | ||
const sink = new Sink() | ||
stream.pipe(sink) | ||
|
||
stream.writeMessage({ data: { channel: 'foo', payload: 'bar' } }) | ||
stream.writeMessage({ data: { channel: 'baz', payload: 'qux' } }) | ||
|
||
assert.equal( | ||
sink.content, | ||
[ | ||
`:ok\n\n`, | ||
`data: {"channel":"foo","payload":"bar"}\n\n`, | ||
`data: {"channel":"baz","payload":"qux"}\n\n`, | ||
].join('') | ||
) | ||
}) | ||
|
||
test('sets headers on the response', async ({ assert }) => { | ||
assert.plan(2) | ||
|
||
const stream = new Stream(randomUUID()) | ||
const sink = new Sink() | ||
|
||
sink.assertWriteHead((statusCode, headers) => { | ||
assert.equal(statusCode, 200) | ||
assert.deepEqual(headers, { | ||
'Cache-Control': 'private, no-cache, no-store, must-revalidate, max-age=0, no-transform', | ||
'Connection': 'keep-alive', | ||
'Content-Type': 'text/event-stream', | ||
'Expire': '0', | ||
'Pragma': 'no-cache', | ||
'X-Accel-Buffering': 'no', | ||
}) | ||
}) | ||
|
||
stream.pipe(sink) | ||
}) | ||
}) |