-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
writer.unit.ts
125 lines (103 loc) · 4.32 KB
/
writer.unit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright IBM Corp. and LoopBack contributors 2019. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {expect, ObservedResponse, stubExpressContext} from '@loopback/testlab';
import {Duplex} from 'stream';
import {Response, writeResultToResponse} from '../..';
describe('writer', () => {
let response: Response;
let observedResponse: Promise<ObservedResponse>;
beforeEach(setupResponseMock);
it('writes string result to response as text', async () => {
writeResultToResponse(response, 'Joe');
const result = await observedResponse;
// content-type should be 'application/json' since it's set
// into the response in writer.writeResultToResponse()
expect(result.headers['content-type']).to.eql('text/plain');
expect(result.payload).to.equal('Joe');
});
it('writes object result to response as JSON', async () => {
writeResultToResponse(response, {name: 'Joe'});
const result = await observedResponse;
expect(result.headers['content-type']).to.eql('application/json');
expect(result.payload).to.equal('{"name":"Joe"}');
});
it('writes boolean result to response as json', async () => {
writeResultToResponse(response, true);
const result = await observedResponse;
expect(result.headers['content-type']).to.eql('application/json');
expect(result.payload).to.equal('true');
});
it('writes number result to response as json', async () => {
writeResultToResponse(response, 2);
const result = await observedResponse;
expect(result.headers['content-type']).to.eql('application/json');
expect(result.payload).to.equal('2');
});
it('writes buffer result to response as binary', async () => {
const buf = Buffer.from('ABC123');
writeResultToResponse(response, buf);
const result = await observedResponse;
expect(result.headers['content-type']).to.eql('application/octet-stream');
expect(result.payload).to.equal('ABC123');
});
it('writes stream result to response as binary', async () => {
const buf = Buffer.from('ABC123');
const stream = new Duplex();
stream.push(buf);
stream.push(null);
writeResultToResponse(response, stream);
const result = await observedResponse;
expect(result.headers['content-type']).to.eql('application/octet-stream');
expect(result.payload).to.equal('ABC123');
});
it('writes null object result to response as JSON', async () => {
writeResultToResponse(response, null);
const result = await observedResponse;
expect(result.headers['content-type']).to.eql('application/json');
expect(result.payload).to.equal('null');
});
it('sends 204 No Content when the response is undefined', async () => {
writeResultToResponse(response, undefined);
const result = await observedResponse;
expect(result.statusCode).to.equal(204);
expect(result.headers).to.not.have.property('content-type');
expect(result.payload).to.equal('');
});
it('skips writing when the return value is the response', async () => {
response
.status(200)
.contentType('text/html; charset=utf-8')
.send('<html><body>Hi</body></html>');
writeResultToResponse(response, response);
const result = await observedResponse;
expect(result.statusCode).to.equal(200);
expect(result.headers).to.have.property(
'content-type',
'text/html; charset=utf-8',
);
expect(result.payload).to.equal('<html><body>Hi</body></html>');
});
it('skips writing when the response headers are sent', async () => {
response
.status(200)
.contentType('text/html; charset=utf-8')
.send('<html><body>Hi</body></html>');
writeResultToResponse(response, undefined);
const result = await observedResponse;
expect(result.statusCode).to.equal(200);
expect(result.headers).to.have.property(
'content-type',
'text/html; charset=utf-8',
);
expect(result.payload).to.equal('<html><body>Hi</body></html>');
});
function setupResponseMock() {
const responseMock = stubExpressContext();
response = responseMock.response;
observedResponse = responseMock.result;
// content-type should be undefined since it's not set in the response yet.
expect(response.getHeader('content-type')).to.eql(undefined);
}
});