| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021 | 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').callsFake(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();            })          })        })      })    })  })})
 |