Skip to content

Commit

Permalink
test(stream): should forward given headers
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Oct 23, 2023
1 parent 16670a7 commit 9251c0b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ export class Stream extends Transform {
pipe<T extends HeaderStream>(
destination: T,
options?: { end?: boolean },
headers?: Record<string, any>
forwardHeaders?: Record<string, any>
): T {
if (destination.writeHead) {
// @see https://github.com/dunglas/mercure/blob/9e080c8dc9a141d4294412d14efdecfb15bf7f43/subscribe.go#L219
destination.writeHead(200, {
...headers,
...forwardHeaders,
'Cache-Control': 'private, no-cache, no-store, must-revalidate, max-age=0, no-transform',
'Connection': 'keep-alive',
'Content-Type': 'text/event-stream',
Expand Down
24 changes: 23 additions & 1 deletion tests/stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test.group('Stream', () => {
)
})

test('sets headers on the response', async ({ assert }) => {
test('should sets headers on the response', async ({ assert }) => {
assert.plan(2)

const stream = new Stream(randomUUID())
Expand All @@ -42,4 +42,26 @@ test.group('Stream', () => {

stream.pipe(sink)
})

test('should forward headers to 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',
'X-Foo': 'bar',
})
})

stream.pipe(sink, undefined, { 'X-Foo': 'bar' })
})
})

0 comments on commit 9251c0b

Please sign in to comment.