• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25
26const http = require('http');
27
28const Stream = require('stream');
29
30function getSrc() {
31  // An old-style readable stream.
32  // The Readable class prevents this behavior.
33  const src = new Stream();
34
35  // Start out paused, just so we don't miss anything yet.
36  let paused = false;
37  src.pause = function() {
38    paused = true;
39  };
40  src.resume = function() {
41    paused = false;
42  };
43
44  const chunks = [ '', 'asdf', '', 'foo', '', 'bar', '' ];
45  const interval = setInterval(function() {
46    if (paused)
47      return;
48
49    const chunk = chunks.shift();
50    if (chunk !== undefined) {
51      src.emit('data', chunk);
52    } else {
53      src.emit('end');
54      clearInterval(interval);
55    }
56  }, 1);
57
58  return src;
59}
60
61
62const expect = 'asdffoobar';
63
64const server = http.createServer(function(req, res) {
65  let actual = '';
66  req.setEncoding('utf8');
67  req.on('data', function(c) {
68    actual += c;
69  });
70  req.on('end', function() {
71    assert.strictEqual(actual, expect);
72    getSrc().pipe(res);
73  });
74  server.close();
75});
76
77server.listen(0, function() {
78  const req = http.request({ port: this.address().port, method: 'POST' });
79  let actual = '';
80  req.on('response', function(res) {
81    res.setEncoding('utf8');
82    res.on('data', function(c) {
83      actual += c;
84    });
85    res.on('end', function() {
86      assert.strictEqual(actual, expect);
87    });
88  });
89  getSrc().pipe(req);
90});
91
92process.on('exit', function(c) {
93  if (!c) console.log('ok');
94});
95