stream_events_spec.js
3.51 KB
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
var needle = require('../'),
fs = require('fs'),
should = require('should'),
helpers = require('./helpers');
describe('stream events', function() {
var server,
port = 3456,
responseData,
serverOpts = {},
requestHandler = function(req, res) { res.end('OK') }
before(function() {
var opts = {
port: port,
handler: function(req, res) { requestHandler(req, res) }
}
server = helpers.server(opts);
})
after(function() {
server.close();
})
beforeEach(function() {
responseData = '';
})
describe('when consuming data directly', function() {
function send_request(opts, cb) {
return needle
.get('http://localhost:' + port, opts)
.on('data', function(data) { responseData += data })
}
describe('and request stream fails', function() {
it('emits done event with error', function(done) {
requestHandler = function(req, res) { req.socket.destroy() }
send_request({}).on('done', function(err) {
err.code.should.eql('ECONNRESET');
responseData.should.eql('');
done()
})
})
})
describe('and request succeeds but decoding fails', function() {
it('emits done event without error', function(done) {
requestHandler = function(req, res) {
res.setHeader('Content-Type', 'application/json')
res.end('invalid:json')
}
send_request({ json: true }).on('done', function(err) {
should.not.exist(err);
responseData.should.eql('invalid:json');
done()
})
})
})
describe('and request succeeds and pipeline works ok', function() {
it('emits done event without error', function(done) {
requestHandler = function(req, res) { res.end('{"ok":1}') }
send_request({ json: true }).on('done', function(err) {
should.not.exist(err);
responseData.should.eql('{"ok":1}');
done()
})
})
})
})
describe('when piping to a fs writableStream', function() {
var outFile = 'test/tmp.dat';
function send_request(opts, cb) {
return needle
.get('http://localhost:' + port, opts)
.pipe(fs.createWriteStream(outFile))
.on('data', function(data) { responseData += data })
}
after(function(done) {
fs.unlink(outFile, done)
})
describe('and request stream fails', function() {
it('final stream emits done event with error', function(done) {
requestHandler = function(req, res) { req.socket.destroy() }
send_request({}).on('done', function(err) {
err.code.should.eql('ECONNRESET');
done()
})
})
})
describe('and request succeeds but decoding fails', function() {
it('final stream emits done event without error', function(done) {
requestHandler = function(req, res) {
res.setHeader('Content-Type', 'application/json')
res.end('invalid:json')
}
send_request({ json: true }).on('done', function(err) {
should.not.exist(err);
done()
})
})
})
describe('and request succeeds and pipeline works ok', function() {
it('final stream emits done event without error', function(done) {
requestHandler = function(req, res) { res.end('{"ok":1}') }
send_request({ json: true }).on('done', function(err) {
should.not.exist(err);
done()
})
})
})
})
})