1 | var needle = require('../'),
|
---|
2 | fs = require('fs'),
|
---|
3 | should = require('should'),
|
---|
4 | helpers = require('./helpers');
|
---|
5 |
|
---|
6 | describe('stream events', function() {
|
---|
7 |
|
---|
8 | var server,
|
---|
9 | port = 3456,
|
---|
10 | responseData,
|
---|
11 | serverOpts = {},
|
---|
12 | requestHandler = function(req, res) { res.end('OK') }
|
---|
13 |
|
---|
14 | before(function() {
|
---|
15 | var opts = {
|
---|
16 | port: port,
|
---|
17 | handler: function(req, res) { requestHandler(req, res) }
|
---|
18 | }
|
---|
19 | server = helpers.server(opts);
|
---|
20 | })
|
---|
21 |
|
---|
22 | after(function() {
|
---|
23 | server.close();
|
---|
24 | })
|
---|
25 |
|
---|
26 | beforeEach(function() {
|
---|
27 | responseData = '';
|
---|
28 | })
|
---|
29 |
|
---|
30 | describe('when consuming data directly', function() {
|
---|
31 |
|
---|
32 | function send_request(opts, cb) {
|
---|
33 | return needle
|
---|
34 | .get('http://localhost:' + port, opts)
|
---|
35 | .on('data', function(data) { responseData += data })
|
---|
36 | }
|
---|
37 |
|
---|
38 | describe('and request stream fails', function() {
|
---|
39 |
|
---|
40 | it('emits done event with error', function(done) {
|
---|
41 | requestHandler = function(req, res) { req.socket.destroy() }
|
---|
42 |
|
---|
43 | send_request({}).on('done', function(err) {
|
---|
44 | err.code.should.eql('ECONNRESET');
|
---|
45 | responseData.should.eql('');
|
---|
46 | done()
|
---|
47 | })
|
---|
48 | })
|
---|
49 |
|
---|
50 | })
|
---|
51 |
|
---|
52 | describe('and request succeeds but decoding fails', function() {
|
---|
53 |
|
---|
54 | it('emits done event without error', function(done) {
|
---|
55 | requestHandler = function(req, res) {
|
---|
56 | res.setHeader('Content-Type', 'application/json')
|
---|
57 | res.end('invalid:json')
|
---|
58 | }
|
---|
59 |
|
---|
60 | send_request({ json: true }).on('done', function(err) {
|
---|
61 | should.not.exist(err);
|
---|
62 | responseData.should.eql('invalid:json');
|
---|
63 | done()
|
---|
64 | })
|
---|
65 | })
|
---|
66 |
|
---|
67 | })
|
---|
68 |
|
---|
69 | describe('and request succeeds and pipeline works ok', function() {
|
---|
70 |
|
---|
71 | it('emits done event without error', function(done) {
|
---|
72 | requestHandler = function(req, res) { res.end('{"ok":1}') }
|
---|
73 |
|
---|
74 | send_request({ json: true }).on('done', function(err) {
|
---|
75 | should.not.exist(err);
|
---|
76 | responseData.should.eql('{"ok":1}');
|
---|
77 | done()
|
---|
78 | })
|
---|
79 | })
|
---|
80 |
|
---|
81 | })
|
---|
82 |
|
---|
83 | })
|
---|
84 |
|
---|
85 | describe('when piping to a fs writableStream', function() {
|
---|
86 |
|
---|
87 | var outFile = 'test/tmp.dat';
|
---|
88 |
|
---|
89 | function send_request(opts, cb) {
|
---|
90 | return needle
|
---|
91 | .get('http://localhost:' + port, opts)
|
---|
92 | .pipe(fs.createWriteStream(outFile))
|
---|
93 | .on('data', function(data) { responseData += data })
|
---|
94 | }
|
---|
95 |
|
---|
96 | after(function(done) {
|
---|
97 | fs.unlink(outFile, done)
|
---|
98 | })
|
---|
99 |
|
---|
100 | describe('and request stream fails', function() {
|
---|
101 |
|
---|
102 | it('final stream emits done event with error', function(done) {
|
---|
103 | requestHandler = function(req, res) { req.socket.destroy() }
|
---|
104 |
|
---|
105 | send_request({}).on('done', function(err) {
|
---|
106 | err.code.should.eql('ECONNRESET');
|
---|
107 | done()
|
---|
108 | })
|
---|
109 | })
|
---|
110 |
|
---|
111 | })
|
---|
112 |
|
---|
113 | describe('and request succeeds but decoding fails', function() {
|
---|
114 |
|
---|
115 | it('final stream emits done event without error', function(done) {
|
---|
116 | requestHandler = function(req, res) {
|
---|
117 | res.setHeader('Content-Type', 'application/json')
|
---|
118 | res.end('invalid:json')
|
---|
119 | }
|
---|
120 |
|
---|
121 | send_request({ json: true }).on('done', function(err) {
|
---|
122 | should.not.exist(err);
|
---|
123 | done()
|
---|
124 | })
|
---|
125 | })
|
---|
126 |
|
---|
127 | })
|
---|
128 |
|
---|
129 | describe('and request succeeds and pipeline works ok', function() {
|
---|
130 |
|
---|
131 | it('final stream emits done event without error', function(done) {
|
---|
132 | requestHandler = function(req, res) { res.end('{"ok":1}') }
|
---|
133 |
|
---|
134 | send_request({ json: true }).on('done', function(err) {
|
---|
135 | should.not.exist(err);
|
---|
136 | done()
|
---|
137 | })
|
---|
138 | })
|
---|
139 |
|
---|
140 | })
|
---|
141 |
|
---|
142 | })
|
---|
143 |
|
---|
144 | }) |
---|