Remove excessive copyright/license boilerplate
[platform/upstream/nodejs.git] / test / parallel / test-http-zero-length-write.js
1 var common = require('../common');
2 var assert = require('assert');
3
4 var http = require('http');
5
6 var Stream = require('stream');
7
8 function getSrc() {
9   // An old-style readable stream.
10   // The Readable class prevents this behavior.
11   var src = new Stream();
12
13   // start out paused, just so we don't miss anything yet.
14   var paused = false;
15   src.pause = function() {
16     paused = true;
17   };
18   src.resume = function() {
19     paused = false;
20   };
21
22   var chunks = [ '', 'asdf', '', 'foo', '', 'bar', '' ];
23   var interval = setInterval(function() {
24     if (paused)
25       return
26
27     var chunk = chunks.shift();
28     if (chunk !== undefined) {
29       src.emit('data', chunk);
30     } else {
31       src.emit('end');
32       clearInterval(interval);
33     }
34   }, 1);
35
36   return src;
37 }
38
39
40 var expect = 'asdffoobar';
41
42 var server = http.createServer(function(req, res) {
43   var actual = '';
44   req.setEncoding('utf8');
45   req.on('data', function(c) {
46     actual += c;
47   });
48   req.on('end', function() {
49     assert.equal(actual, expect);
50     getSrc().pipe(res);
51   });
52   server.close();
53 });
54
55 server.listen(common.PORT, function() {
56   var req = http.request({ port: common.PORT, method: 'POST' });
57   var actual = '';
58   req.on('response', function(res) {
59     res.setEncoding('utf8');
60     res.on('data', function(c) {
61       actual += c;
62     });
63     res.on('end', function() {
64       assert.equal(actual, expect);
65     });
66   });
67   getSrc().pipe(req);
68 });
69
70 process.on('exit', function(c) {
71   if (!c) console.log('ok');
72 });