source: trip-planner-front/node_modules/needle/test/request_stream_spec.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.6 KB
Line 
1var fs = require('fs'),
2 needle = require('..'),
3 stream = require('stream'),
4 http = require('http'),
5 should = require('should'),
6 sinon = require('sinon');
7
8var port = 2233;
9
10var node_major_ver = parseInt(process.version.split('.')[0].replace('v', ''));
11var node_minor_ver = parseInt(process.version.split('.')[1]);
12
13describe('request stream length', function() {
14
15 var server, writable;
16
17 function createServer() {
18 return http.createServer(function(req, res) {
19
20 req.on('data', function(chunk) {
21 // console.log(chunk.length);
22 })
23
24 req.on('end', function() {
25 res.writeHeader(200, { 'Content-Type': 'application/json'})
26 res.end(JSON.stringify({ headers: req.headers }))
27 })
28
29 })
30 }
31
32 before(function(done) {
33 server = createServer();
34 server.listen(port, done)
35 })
36
37 beforeEach(function() {
38 writable = new stream.Readable();
39 writable._read = function() {
40 this.push('hello world');
41 this.push(null);
42 }
43 })
44
45 after(function(done) {
46 server.close(done)
47 })
48
49 function send_request(opts, cb) {
50 needle.post('http://localhost:' + port, writable, opts, cb)
51 }
52
53 describe('no stream_length set', function() {
54
55 it('doesnt set Content-Length header', function(done) {
56 send_request({}, function(err, resp) {
57 should.not.exist(resp.body.headers['content-length']);
58 done()
59 })
60 })
61
62 if (node_major_ver >= 10) {
63 it('returns 400 if Transfer-Encoding is set to a blank string', function(done) {
64 send_request({ headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
65 should.not.exist(err);
66 resp.statusCode.should.eql(400);
67 done()
68 })
69 })
70 } else {
71 it('doesnt work if Transfer-Encoding is set to a blank string', function(done) {
72 send_request({ headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
73 err.code.should.eql('ECONNRESET');
74 done()
75 })
76 })
77 }
78
79 it('works if Transfer-Encoding is not set', function(done) {
80 send_request({}, function(err, resp) {
81 should.not.exist(err);
82 resp.statusCode.should.eql(200);
83 done()
84 })
85 })
86
87 })
88
89 describe('stream_length set to invalid value', function() {
90
91 if (node_major_ver >= 10) {
92
93 it('returns 400 if Transfer-Encoding is set to a blank string', function(done) {
94 send_request({ stream_length: 5, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
95 should.not.exist(err);
96 resp.statusCode.should.eql(400);
97 done()
98 })
99 })
100
101 it('returns 400 if Transfer-Encoding is not set', function(done) {
102 send_request({ stream_length: 5 }, function(err, resp) {
103 should.not.exist(err);
104 resp.statusCode.should.eql(400);
105 done()
106 })
107 })
108
109 } else {
110
111 it('doesnt work if Transfer-Encoding is set to a blank string', function(done) {
112 send_request({ stream_length: 5, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
113 err.code.should.eql('ECONNRESET');
114 done()
115 })
116 })
117 it('doesnt work if Transfer-Encoding is not set', function(done) {
118 send_request({ stream_length: 5 }, function(err, resp) {
119 err.code.should.eql('ECONNRESET');
120 done()
121 })
122 })
123
124 }
125
126 })
127
128 describe('stream_length is set to valid value', function() {
129
130 it('sets Content-Length header to that value', function(done) {
131 send_request({ stream_length: 11 }, function(err, resp) {
132 resp.body.headers['content-length'].should.eql('11');
133 done()
134 })
135 })
136
137 it('works if Transfer-Encoding is set to a blank string', function(done) {
138 send_request({ stream_length: 11, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
139 should.not.exist(err);
140 var code = node_major_ver == 10 && node_minor_ver > 15 ? 400 : 200;
141 resp.statusCode.should.eql(code);
142 done()
143 })
144 })
145
146 it('works if Transfer-Encoding is not set', function(done) {
147 send_request({ stream_length: 11 }, function(err, resp) {
148 should.not.exist(err);
149 resp.statusCode.should.eql(200);
150 done()
151 })
152 })
153
154 })
155
156
157 describe('stream_length set to 0', function() {
158
159 describe('stream with path', function() {
160
161 var stub;
162
163 beforeEach(function() {
164 writable.path = '/foo/bar';
165 stub = sinon.stub(fs, 'stat').callsFake(function(path, cb) {
166 cb(null, { size: 11 })
167 })
168 })
169
170 afterEach(function() {
171 stub.restore();
172 })
173
174 it('sets Content-Length header to streams length', function(done) {
175 send_request({ stream_length: 0 }, function(err, resp) {
176 resp.body.headers['content-length'].should.eql('11');
177 done()
178 })
179 })
180
181 it('works if Transfer-Encoding is set to a blank string', function(done) {
182 send_request({ stream_length: 0, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
183 should.not.exist(err);
184 var code = node_major_ver == 10 && node_minor_ver > 15 ? 400 : 200;
185 resp.statusCode.should.eql(code);
186 done()
187 })
188 })
189
190 it('works if Transfer-Encoding is not set', function(done) {
191 send_request({ stream_length: 0 }, function(err, resp) {
192 should.not.exist(err);
193 resp.statusCode.should.eql(200);
194 done()
195 })
196 })
197
198 })
199
200 describe('stream without path', function() {
201
202 it('does not set Content-Length header', function(done) {
203 send_request({ stream_length: 0 }, function(err, resp) {
204 should.not.exist(resp.body.headers['content-length']);
205 done()
206 })
207 })
208
209 if (node_major_ver >= 10) {
210 it('returns 400 if Transfer-Encoding is set to a blank string', function(done) {
211 send_request({ stream_length: 0, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
212 should.not.exist(err);
213 resp.statusCode.should.eql(400);
214 done()
215 })
216 })
217 } else {
218 it('throws ECONNRESET if Transfer-Encoding is set to a blank string', function(done) {
219 send_request({ stream_length: 0, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
220 err.code.should.eql('ECONNRESET');
221 done()
222 })
223 })
224 }
225
226 it('works if Transfer-Encoding is not set', function(done) {
227 send_request({ stream_length: 0 }, function(err, resp) {
228 should.not.exist(err);
229 resp.statusCode.should.eql(200);
230 done()
231 })
232 })
233 })
234
235 })
236
237})
Note: See TracBrowser for help on using the repository browser.