Skip to content

Commit 419c16c

Browse files
committed
feat: add response helpers to inspect the type of response body
1 parent 3fb7afc commit 419c16c

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

src/response.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,19 @@ export class Response extends Macroable {
9090
}
9191

9292
/**
93-
* Returns true when response body is set using "response.download"
94-
* and "response.attachment" methods
93+
* Returns true when response body is set using "response.stream"
94+
* method
9595
*/
9696
get hasStream(): boolean {
97-
return !!(this.lazyBody.stream || this.lazyBody.fileToStream)
97+
return !!this.lazyBody.stream
98+
}
99+
100+
/**
101+
* Returns true when response body is set using "response.download"
102+
* or "response.attachment" methods
103+
*/
104+
get hasFileToStream(): boolean {
105+
return !!this.lazyBody.fileToStream
98106
}
99107

100108
/**
@@ -105,6 +113,27 @@ export class Response extends Macroable {
105113
return this.lazyBody.content
106114
}
107115

116+
/**
117+
* Returns reference to the stream set using "response.stream"
118+
* method
119+
*/
120+
get outgoingStream() {
121+
return this.lazyBody.stream?.[0]
122+
}
123+
124+
/**
125+
* Returns reference to the file path set using "response.stream"
126+
* method.
127+
*/
128+
get fileToStream() {
129+
return this.lazyBody.fileToStream
130+
? {
131+
path: this.lazyBody.fileToStream[0],
132+
generateEtag: this.lazyBody.fileToStream[1],
133+
}
134+
: undefined
135+
}
136+
108137
/**
109138
* Lazy body is used to set the response body. However, do not
110139
* write it on the socket immediately unless `response.finish`

tests/response.spec.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import etag from 'etag'
11-
import fsExtra from 'fs-extra'
11+
import fsExtra, { read } from 'fs-extra'
1212
import { join } from 'node:path'
1313
import supertest from 'supertest'
1414
import { test } from '@japa/runner'
@@ -400,16 +400,27 @@ test.group('Response', (group) => {
400400
assert.equal(text, 'true')
401401
})
402402

403-
test('hasLazyBody must return true after download has been called', async ({ assert }) => {
403+
test('hasLazyBody and hasFileToStream must return true after download has been called', async ({
404+
assert,
405+
}) => {
404406
const server = createServer((req, res) => {
405407
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
406408

407409
response.download('./foo.html')
408-
res.end(String(response.hasLazyBody))
410+
res.setHeader('content-type', 'application/json')
411+
res.end(
412+
JSON.stringify({
413+
hasLazyBody: response.hasLazyBody,
414+
hasFileToStream: response.hasFileToStream,
415+
})
416+
)
409417
})
410418

411-
const { text } = await supertest(server).get('/')
412-
assert.equal(text, 'true')
419+
const { body } = await supertest(server).get('/')
420+
assert.deepEqual(body, {
421+
hasLazyBody: true,
422+
hasFileToStream: true,
423+
})
413424
})
414425

415426
test('hasContent must return false after download has been called', async ({ assert }) => {
@@ -424,16 +435,27 @@ test.group('Response', (group) => {
424435
assert.equal(text, 'false')
425436
})
426437

427-
test('hasLazyBody must return true after stream has been called', async ({ assert }) => {
438+
test('hasLazyBody and hasStream must return true after stream has been called', async ({
439+
assert,
440+
}) => {
428441
const server = createServer((req, res) => {
429442
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
430443

431444
response.stream(new Readable())
432-
res.end(String(response.hasLazyBody))
445+
res.setHeader('content-type', 'application/json')
446+
res.end(
447+
JSON.stringify({
448+
hasLazyBody: response.hasLazyBody,
449+
hasStream: response.hasStream,
450+
})
451+
)
433452
})
434453

435-
const { text } = await supertest(server).get('/')
436-
assert.equal(text, 'true')
454+
const { body } = await supertest(server).get('/')
455+
assert.deepEqual(body, {
456+
hasLazyBody: true,
457+
hasStream: true,
458+
})
437459
})
438460

439461
test('hasContent must return false after stream has been called', async ({ assert }) => {
@@ -505,8 +527,12 @@ test.group('Response', (group) => {
505527

506528
const server = createServer((req, res) => {
507529
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
508-
response.stream(createReadStream(join(BASE_PATH, 'hello.txt')))
530+
const readableStream = createReadStream(join(BASE_PATH, 'hello.txt'))
531+
response.stream(readableStream)
532+
509533
assert.isTrue(response.hasStream)
534+
assert.strictEqual(response.outgoingStream, readableStream)
535+
510536
response.finish()
511537
})
512538

@@ -626,7 +652,15 @@ test.group('Response', (group) => {
626652
const server = createServer((req, res) => {
627653
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
628654
response.download(join(BASE_PATH, 'hello.html'))
629-
assert.isTrue(response.hasStream)
655+
656+
assert.isFalse(response.hasStream)
657+
assert.isTrue(response.hasFileToStream)
658+
659+
assert.deepEqual(response.fileToStream, {
660+
path: join(BASE_PATH, 'hello.html'),
661+
generateEtag: false,
662+
})
663+
630664
response.finish()
631665
})
632666

0 commit comments

Comments
 (0)