HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/node_modules/needle/test/stream_events_spec.js
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()
        })
      })

    })

  })

})