/
test_stream.js
346 lines (282 loc) · 7.24 KB
/
test_stream.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'use strict'
const events = require('events')
const http = require('http')
const path = require('path')
const stream = require('stream')
const util = require('util')
const { expect } = require('chai')
const sinon = require('sinon')
const nock = require('..')
const got = require('./got_client')
require('./setup')
const textFilePath = path.resolve(__dirname, './assets/reply_file_1.txt')
it('reply with file and pipe response', done => {
const scope = nock('http://example.test')
.get('/')
.replyWithFile(200, textFilePath)
let text = ''
const fakeStream = new stream.Stream()
fakeStream.writable = true
fakeStream.write = d => {
text += d
}
fakeStream.end = () => {
expect(text).to.equal('Hello from the file!')
scope.done()
done()
}
got.stream('http://example.test/').pipe(fakeStream)
})
// TODO Convert to async / got.
it('pause response after data', done => {
const response = new stream.PassThrough()
const scope = nock('http://example.test')
.get('/')
// Node does not pause the 'end' event so we need to use a stream to simulate
// multiple 'data' events.
.reply(200, response)
http.get(
{
host: 'example.test',
path: '/',
},
res => {
const didTimeout = sinon.spy()
setTimeout(() => {
didTimeout()
res.resume()
}, 500)
res.on('data', data => res.pause())
res.on('end', () => {
expect(didTimeout).to.have.been.calledOnce()
scope.done()
done()
})
}
)
// Manually simulate multiple 'data' events.
response.emit('data', 'one')
setTimeout(() => {
response.emit('data', 'two')
response.end()
}, 0)
})
// https://github.com/nock/nock/issues/1493
it("response has 'complete' property and it's true after end", done => {
const response = new stream.PassThrough()
const scope = nock('http://example.test')
.get('/')
// Node does not pause the 'end' event so we need to use a stream to simulate
// multiple 'data' events.
.reply(200, response)
http.get(
{
host: 'example.test',
path: '/',
},
res => {
const onData = sinon.spy()
res.on('data', onData)
res.on('end', () => {
expect(onData).to.have.been.called()
expect(res.complete).to.be.true()
scope.done()
done()
})
}
)
// Manually simulate multiple 'data' events.
response.emit('data', 'one')
setTimeout(() => {
response.end()
}, 0)
})
// TODO Convert to async / got.
it('response pipe', done => {
const dest = (() => {
function Constructor() {
events.EventEmitter.call(this)
this.buffer = Buffer.alloc(0)
this.writable = true
}
util.inherits(Constructor, events.EventEmitter)
Constructor.prototype.end = function() {
this.emit('end')
}
Constructor.prototype.write = function(chunk) {
const buf = Buffer.alloc(this.buffer.length + chunk.length)
this.buffer.copy(buf)
chunk.copy(buf, this.buffer.length)
this.buffer = buf
return true
}
return new Constructor()
})()
const scope = nock('http://example.test')
.get('/')
.reply(200, 'nobody')
http.get(
{
host: 'example.test',
path: '/',
},
res => {
const onPipeEvent = sinon.spy()
dest.on('pipe', onPipeEvent)
dest.on('end', () => {
scope.done()
expect(onPipeEvent).to.have.been.calledOnce()
expect(dest.buffer.toString()).to.equal('nobody')
done()
})
res.pipe(dest)
}
)
})
// TODO Convert to async / got.
it('response pipe without implicit end', done => {
const dest = (() => {
function Constructor() {
events.EventEmitter.call(this)
this.buffer = Buffer.alloc(0)
this.writable = true
}
util.inherits(Constructor, events.EventEmitter)
Constructor.prototype.end = function() {
this.emit('end')
}
Constructor.prototype.write = function(chunk) {
const buf = Buffer.alloc(this.buffer.length + chunk.length)
this.buffer.copy(buf)
chunk.copy(buf, this.buffer.length)
this.buffer = buf
return true
}
return new Constructor()
})()
const scope = nock('http://example.test')
.get('/')
.reply(200, 'nobody')
http.get(
{
host: 'example.test',
path: '/',
},
res => {
dest.on('end', () => expect.fail('should not call end implicitly'))
res.on('end', () => {
scope.done()
done()
})
res.pipe(dest, { end: false })
}
)
})
it('response is streams2 compatible', done => {
const responseText = 'streams2 streams2 streams2'
nock('http://example.test')
.get('/somepath')
.reply(200, responseText)
http
.request(
{
host: 'example.test',
path: '/somepath',
},
function(res) {
res.setEncoding('utf8')
let body = ''
res.on('readable', function() {
let buf
while ((buf = res.read())) body += buf
})
res.once('end', function() {
expect(body).to.equal(responseText)
done()
})
}
)
.end()
})
it('when a stream is used for the response body, it will not be read until after the response event', done => {
let responseEvent = false
const responseText = 'Hello World\n'
class SimpleStream extends stream.Readable {
_read() {
expect(responseEvent).to.be.true()
this.push(responseText)
this.push(null)
}
}
nock('http://localhost')
.get('/')
.reply(201, () => new SimpleStream())
http.get('http://localhost/', res => {
responseEvent = true
res.setEncoding('utf8')
let body = ''
expect(res.statusCode).to.equal(201)
res.on('data', function(chunk) {
body += chunk
})
res.once('end', function() {
expect(body).to.equal(responseText)
done()
})
})
})
// https://github.com/nock/nock/issues/193
it('response readable pull stream works as expected', done => {
nock('http://example.test')
.get('/ssstream')
.reply(200, 'this is the response body yeah')
const req = http.request(
{
host: 'example.test',
path: '/ssstream',
port: 80,
},
res => {
let ended = false
let responseBody = ''
expect(res.statusCode).to.equal(200)
res.on('readable', function() {
let chunk
while ((chunk = res.read()) !== null) {
responseBody += chunk.toString()
}
if (chunk === null && !ended) {
ended = true
expect(responseBody).to.equal('this is the response body yeah')
done()
}
})
}
)
req.end()
})
it('error events on reply streams proxy to the response', done => {
// This test could probably be written to use got, however, that lib has a lot
// of built in error handling and this test would get convoluted.
const replyBody = new stream.PassThrough()
const scope = nock('http://example.test')
.get('/')
.reply(201, replyBody)
http.get(
{
host: 'example.test',
method: 'GET',
path: '/',
},
res => {
res.on('error', err => {
expect(err).to.equal('oh no!')
done()
})
}
)
scope.done()
replyBody.end(() => {
replyBody.emit('error', 'oh no!')
})
})