4
4
// License text available at https://opensource.org/licenses/MIT
5
5
6
6
import { ServerResponse , writeResultToResponse } from '../..' ;
7
-
7
+ import { Duplex } from 'stream' ;
8
8
import { expect , mockResponse , ShotObservedResponse } from '@loopback/testlab' ;
9
9
10
10
describe ( 'writer' , ( ) => {
@@ -31,22 +31,43 @@ describe('writer', () => {
31
31
expect ( result . payload ) . to . equal ( '{"name":"Joe"}' ) ;
32
32
} ) ;
33
33
34
- it ( 'writes boolean result to response as text ' , async ( ) => {
34
+ it ( 'writes boolean result to response as json ' , async ( ) => {
35
35
writeResultToResponse ( response , true ) ;
36
36
const result = await observedResponse ;
37
37
38
- expect ( result . headers [ 'content-type' ] ) . to . eql ( 'text/plain ' ) ;
38
+ expect ( result . headers [ 'content-type' ] ) . to . eql ( 'application/json ' ) ;
39
39
expect ( result . payload ) . to . equal ( 'true' ) ;
40
40
} ) ;
41
41
42
- it ( 'writes number result to response as text ' , async ( ) => {
42
+ it ( 'writes number result to response as json ' , async ( ) => {
43
43
writeResultToResponse ( response , 2 ) ;
44
44
const result = await observedResponse ;
45
45
46
- expect ( result . headers [ 'content-type' ] ) . to . eql ( 'text/plain ' ) ;
46
+ expect ( result . headers [ 'content-type' ] ) . to . eql ( 'application/json ' ) ;
47
47
expect ( result . payload ) . to . equal ( '2' ) ;
48
48
} ) ;
49
49
50
+ it ( 'writes buffer result to response as binary' , async ( ) => {
51
+ const buf = Buffer . from ( 'ABC123' ) ;
52
+ writeResultToResponse ( response , buf ) ;
53
+ const result = await observedResponse ;
54
+
55
+ expect ( result . headers [ 'content-type' ] ) . to . eql ( 'application/octet-stream' ) ;
56
+ expect ( result . payload ) . to . equal ( 'ABC123' ) ;
57
+ } ) ;
58
+
59
+ it ( 'writes stream result to response as binary' , async ( ) => {
60
+ const buf = Buffer . from ( 'ABC123' ) ;
61
+ const stream = new Duplex ( ) ;
62
+ stream . push ( buf ) ;
63
+ stream . push ( null ) ;
64
+ writeResultToResponse ( response , stream ) ;
65
+ const result = await observedResponse ;
66
+
67
+ expect ( result . headers [ 'content-type' ] ) . to . eql ( 'application/octet-stream' ) ;
68
+ expect ( result . payload ) . to . equal ( 'ABC123' ) ;
69
+ } ) ;
70
+
50
71
function setupResponseMock ( ) {
51
72
const responseMock = mockResponse ( ) ;
52
73
response = responseMock . response ;
0 commit comments