post_data_spec.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. var needle = require('..'),
  2. http = require('http'),
  3. should = require('should'),
  4. sinon = require('sinon'),
  5. stream = require('stream'),
  6. helpers = require('./helpers');
  7. var multiparts = ['----------------------NODENEEDLEHTTPCLIENT'];
  8. multiparts.push(['Content-Disposition: form-data; name=\"foo\"'])
  9. multiparts.push(['\r\nbar\r\n----------------------NODENEEDLEHTTPCLIENT--'])
  10. // multiparts.push(['Content-Disposition: form-data; name=\"test\"'])
  11. // multiparts.push(['\r\næµè¯\r\n----------------------NODENEEDLEHTTPCLIENT--'])
  12. // multiparts.push(['\r\n' + Buffer.from('测试').toString() + '\r\n----------------------NODENEEDLEHTTPCLIENT--'])
  13. describe('post data (e.g. request body)', function() {
  14. var stub, spy, server;
  15. before(function(done) {
  16. server = helpers.server({ port: 4321 }, done);
  17. })
  18. after(function(done) {
  19. server.close(done);
  20. })
  21. afterEach(function() {
  22. if (stub) stub.restore();
  23. if (spy) spy.restore();
  24. })
  25. function get(data, opts, cb) {
  26. return needle.request('get', 'http://localhost:' + 4321, data, opts, cb)
  27. }
  28. function post(data, opts, cb) {
  29. return needle.request('post', 'http://localhost:' + 4321, data, opts, cb)
  30. }
  31. function spystub_request() {
  32. var http_req = http.request;
  33. stub = sinon.stub(http, 'request').callsFake(function(opts, cb) {
  34. var req = http_req(opts, cb);
  35. spy = sinon.spy(req, 'write');
  36. return req;
  37. })
  38. }
  39. function check_request(method) {
  40. stub.calledOnce.should.be.true;
  41. stub.args[0][0]['headers']['host'].should.equal('localhost:4321');
  42. stub.args[0][0]['method'].should.equal(method);
  43. }
  44. describe('with multipart: true', function() {
  45. describe('when null', function() {
  46. it('sends request (non multipart)', function(done) {
  47. spystub_request();
  48. post(null, { multipart: true }, function(err, resp) {
  49. check_request('post');
  50. done();
  51. })
  52. })
  53. it('doesnt set Content-Type header', function(done) {
  54. post(null, { multipart: true }, function(err, resp) {
  55. should.not.exist(resp.body.headers['content-type']);
  56. done();
  57. })
  58. })
  59. it('doesnt change default Accept header', function(done) {
  60. post(null, { multipart: true }, function(err, resp) {
  61. // resp.body contains 'header' and 'body', mirroring what we sent
  62. resp.body.headers['accept'].should.equal('*/*');
  63. done();
  64. })
  65. })
  66. it('doesnt write anything', function(done) {
  67. spystub_request();
  68. post(null, { multipart: true }, function(err, resp) {
  69. spy.called.should.be.false;
  70. resp.body.body.should.eql('');
  71. done();
  72. })
  73. })
  74. })
  75. describe('when string', function() {
  76. it('explodes', function() {
  77. (function() {
  78. post('foobar', { multipart: true })
  79. }).should.throw()
  80. })
  81. })
  82. describe('when object', function() {
  83. describe('get request', function() {
  84. it('sends request', function(done) {
  85. spystub_request();
  86. get({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
  87. check_request('get');
  88. done();
  89. })
  90. })
  91. it('sets Content-Type header', function(done) {
  92. post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
  93. resp.body.headers['content-type'].should.equal('multipart/form-data; boundary=--------------------NODENEEDLEHTTPCLIENT');
  94. done();
  95. })
  96. })
  97. it('doesnt change default Accept header', function(done) {
  98. post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
  99. resp.body.headers['accept'].should.equal('*/*');
  100. done();
  101. })
  102. })
  103. it('writes string as buffer', function(done) {
  104. spystub_request();
  105. get({ foo: 'bar' }, { multipart: true }, function(err, resp) {
  106. spy.called.should.be.true;
  107. spy.args[0][0].should.be.an.instanceof(String);
  108. spy.args[0][0].toString().should.equal(multiparts.join('\r\n'));
  109. resp.body.body.should.eql(multiparts.join('\r\n'));
  110. done();
  111. })
  112. })
  113. it('writes japanese chars correctly as binary', function(done) {
  114. spystub_request();
  115. get({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
  116. spy.called.should.be.true;
  117. spy.args[0][0].should.be.an.instanceof(String);
  118. Buffer.from(spy.args[0][0]).toString('hex').should.eql('2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d22666f6f220d0a0d0a6261720d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2274657374220d0a0d0ac3a6c2b5c28bc3a8c2afc2950d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e542d2d')
  119. done();
  120. })
  121. })
  122. })
  123. describe('post request', function() {
  124. it('sends request', function(done) {
  125. spystub_request();
  126. post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
  127. check_request('post');
  128. done();
  129. })
  130. })
  131. it('writes string as buffer', function(done) {
  132. spystub_request();
  133. post({ foo: 'bar' }, { multipart: true }, function(err, resp) {
  134. spy.called.should.be.true;
  135. spy.args[0][0].should.be.an.instanceof(String);
  136. spy.args[0][0].toString().should.equal(multiparts.join('\r\n'));
  137. resp.body.body.should.eql(multiparts.join('\r\n'));
  138. done();
  139. })
  140. })
  141. it('writes japanese chars correctly as binary', function(done) {
  142. spystub_request();
  143. post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
  144. spy.called.should.be.true;
  145. spy.args[0][0].should.be.an.instanceof(String);
  146. Buffer.from(spy.args[0][0]).toString('hex').should.eql('2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d22666f6f220d0a0d0a6261720d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2274657374220d0a0d0ac3a6c2b5c28bc3a8c2afc2950d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e542d2d')
  147. done();
  148. })
  149. })
  150. })
  151. })
  152. describe('when stream', function() {
  153. var stream_for_multipart;
  154. before(function() {
  155. stream_for_multipart = new stream.Readable();
  156. stream_for_multipart._read = function() {
  157. this.push('foobar');
  158. this.push(null);
  159. }
  160. })
  161. it('explodes', function() {
  162. (function() {
  163. post(stream_for_multipart, { multipart: true })
  164. }).should.throw()
  165. })
  166. })
  167. })
  168. describe('non multipart', function() {
  169. describe('when null', function() {
  170. describe('get request', function() {
  171. it('sends request', function(done) {
  172. spystub_request();
  173. get(null, {}, function(err, resp) {
  174. check_request('get');
  175. done();
  176. })
  177. })
  178. it('doesnt write anything', function(done) {
  179. spystub_request();
  180. get(null, {}, function(err, resp) {
  181. spy.called.should.be.false;
  182. resp.body.body.should.eql('');
  183. done();
  184. })
  185. })
  186. })
  187. describe('post request', function() {
  188. it('sends request', function(done) {
  189. spystub_request();
  190. post(null, {}, function(err, resp) {
  191. check_request('post');
  192. done();
  193. })
  194. })
  195. it('doesnt write anything', function(done) {
  196. spystub_request();
  197. post(null, {}, function(err, resp) {
  198. spy.called.should.be.false;
  199. resp.body.body.should.eql('');
  200. done();
  201. })
  202. })
  203. })
  204. })
  205. describe('when string with no equal sign', function() {
  206. describe('get request', function() {
  207. it('explodes', function() {
  208. (function() {
  209. get('foobar', {})
  210. }).should.throw()
  211. })
  212. })
  213. describe('post request', function() {
  214. it('sends request', function(done) {
  215. spystub_request();
  216. post('foobar', {}, function(err, resp) {
  217. check_request('post');
  218. done();
  219. })
  220. })
  221. it('writes string as buffer', function(done) {
  222. spystub_request();
  223. post('foobar', {}, function(err, resp) {
  224. spy.called.should.be.true;
  225. spy.args[0][0].should.be.an.instanceof(Buffer);
  226. spy.args[0][0].toString().should.equal('foobar');
  227. resp.body.body.should.eql('foobar');
  228. done();
  229. })
  230. })
  231. })
  232. })
  233. describe('when string WITH equal sign', function() {
  234. describe('get request', function() {
  235. describe('with json: false (default)', function() {
  236. it('sends request, adding data as querystring', function(done) {
  237. spystub_request();
  238. get('foo=bar', { json: false }, function(err, resp) {
  239. check_request('get');
  240. stub.args[0][0]['path'].should.equal('/?foo=bar')
  241. done();
  242. })
  243. })
  244. it('doesnt set Content-Type header', function(done) {
  245. get('foo=bar', { json: false }, function(err, resp) {
  246. // resp.body contains 'header' and 'body', mirroring what we sent
  247. should.not.exist(resp.body.headers['content-type']);
  248. done();
  249. })
  250. })
  251. it('doesnt change default Accept header', function(done) {
  252. get('foo=bar', { json: false }, function(err, resp) {
  253. // resp.body contains 'header' and 'body', mirroring what we sent
  254. resp.body.headers['accept'].should.equal('*/*');
  255. done();
  256. })
  257. })
  258. it('doesnt write anything', function(done) {
  259. get('foo=bar', { json: false }, function(err, resp) {
  260. spy.called.should.be.false;
  261. resp.body.body.should.eql('');
  262. done();
  263. })
  264. })
  265. })
  266. describe('with json: true', function() {
  267. it('sends request, without setting a querystring', function(done) {
  268. spystub_request();
  269. get('foo=bar', { json: true }, function(err, resp) {
  270. check_request('get');
  271. stub.args[0][0]['path'].should.equal('/')
  272. done();
  273. })
  274. })
  275. it('sets Content-Type header', function(done) {
  276. get('foo=bar', { json: true }, function(err, resp) {
  277. resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
  278. done();
  279. })
  280. })
  281. it('set Accept header to application/json', function(done) {
  282. get('foo=bar', { json: true }, function(err, resp) {
  283. resp.body.headers['accept'].should.equal('application/json');
  284. done();
  285. })
  286. })
  287. it('writes raw string (assuming it already is JSON, so no JSON.stringify)', function(done) {
  288. get('foo=bar', { json: true }, function(err, resp) {
  289. spy.called.should.be.true;
  290. spy.args[0][0].toString().should.eql('foo=bar')
  291. resp.body.body.should.eql('foo=bar');
  292. done();
  293. })
  294. })
  295. })
  296. })
  297. describe('post request', function() {
  298. describe('with json: false (default)', function() {
  299. it('sends request', function(done) {
  300. spystub_request();
  301. post('foo=bar', { json: false }, function(err, resp) {
  302. check_request('post');
  303. done();
  304. })
  305. })
  306. it('sets Content-Type header to www-form-urlencoded', function(done) {
  307. post('foo=bar', { json: false }, function(err, resp) {
  308. resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
  309. done();
  310. })
  311. })
  312. it('doesnt change default Accept header', function(done) {
  313. post('foo=bar', { json: false }, function(err, resp) {
  314. // resp.body contains 'header' and 'body', mirroring what we sent
  315. resp.body.headers['accept'].should.equal('*/*');
  316. done();
  317. })
  318. })
  319. it('writes as buffer', function(done) {
  320. post('foo=bar', { json: false }, function(err, resp) {
  321. spy.called.should.be.true;
  322. spy.args[0][0].should.be.an.instanceof(Buffer);
  323. spy.args[0][0].toString().should.equal('foo=bar');
  324. resp.body.body.should.eql('foo=bar');
  325. done();
  326. })
  327. })
  328. })
  329. describe('with json: true', function() {
  330. it('sends request', function(done) {
  331. spystub_request();
  332. post('foo=bar', { json: true }, function(err, resp) {
  333. check_request('post');
  334. done();
  335. })
  336. })
  337. it('sets Content-Type header', function(done) {
  338. post('foo=bar', { json: true }, function(err, resp) {
  339. resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
  340. done();
  341. })
  342. })
  343. it('set Accept header to application/json', function(done) {
  344. post('foo=bar', { json: true }, function(err, resp) {
  345. resp.body.headers['accept'].should.equal('application/json');
  346. done();
  347. })
  348. })
  349. it('writes raw string (assuming it already is JSON, so no JSON.stringify)', function(done) {
  350. post('foo=bar', { json: true }, function(err, resp) {
  351. spy.called.should.be.true;
  352. var json = JSON.stringify('foo=bar');
  353. spy.args[0][0].toString().should.eql('foo=bar')
  354. resp.body.body.should.eql('foo=bar');
  355. done();
  356. })
  357. })
  358. })
  359. })
  360. })
  361. describe('when object', function() {
  362. describe('get request', function() {
  363. describe('with json: false (default)', function() {
  364. it('sends request, adding data as querystring', function(done) {
  365. spystub_request();
  366. get({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
  367. check_request('get');
  368. stub.args[0][0]['path'].should.equal('/?foo=bar&test=%E6%B5%8B%E8%AF%95')
  369. done();
  370. })
  371. })
  372. it('doesnt set Content-Type header', function(done) {
  373. get({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
  374. // resp.body contains 'header' and 'body', mirroring what we sent
  375. should.not.exist(resp.body.headers['content-type']);
  376. done();
  377. })
  378. })
  379. it('doesnt change default Accept header', function(done) {
  380. get({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
  381. // resp.body contains 'header' and 'body', mirroring what we sent
  382. resp.body.headers['accept'].should.equal('*/*');
  383. done();
  384. })
  385. })
  386. it('doesnt write anything', function(done) {
  387. get({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
  388. spy.called.should.be.false;
  389. resp.body.body.should.eql('');
  390. done();
  391. })
  392. })
  393. })
  394. describe('with json: true', function() {
  395. it('sends request, without setting a querystring', function(done) {
  396. spystub_request();
  397. get({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
  398. check_request('get');
  399. stub.args[0][0]['path'].should.equal('/')
  400. done();
  401. })
  402. })
  403. it('sets Content-Type header', function(done) {
  404. get({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
  405. resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
  406. done();
  407. })
  408. })
  409. it('set Accept header to application/json', function(done) {
  410. get({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
  411. resp.body.headers['accept'].should.equal('application/json');
  412. done();
  413. })
  414. })
  415. it('writes JSON.stringify version of object', function(done) {
  416. get({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
  417. spy.called.should.be.true;
  418. var json = JSON.stringify({ foo: 'bar', test: '测试' })
  419. spy.args[0][0].toString().should.eql(json)
  420. resp.body.body.should.eql(json);
  421. done();
  422. })
  423. })
  424. })
  425. })
  426. describe('post request', function() {
  427. describe('with json: false (default)', function() {
  428. it('sends request', function(done) {
  429. spystub_request();
  430. post({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
  431. check_request('post');
  432. done();
  433. })
  434. })
  435. it('sets Content-Type header to www-form-urlencoded', function(done) {
  436. post({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
  437. resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
  438. done();
  439. })
  440. })
  441. it('doesnt change default Accept header', function(done) {
  442. post({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
  443. // resp.body contains 'header' and 'body', mirroring what we sent
  444. resp.body.headers['accept'].should.equal('*/*');
  445. done();
  446. })
  447. })
  448. it('writes as buffer', function(done) {
  449. post({ foo: 'bar', test: '测试' }, { json: false }, function(err, resp) {
  450. spy.called.should.be.true;
  451. spy.args[0][0].should.be.an.instanceof(Buffer);
  452. spy.args[0][0].toString().should.equal('foo=bar&test=%E6%B5%8B%E8%AF%95');
  453. resp.body.body.should.eql('foo=bar&test=%E6%B5%8B%E8%AF%95');
  454. done();
  455. })
  456. })
  457. })
  458. describe('with json: false and content_type = "application/json"', function() {
  459. var opts = { json: false, content_type: 'application/json' };
  460. it('sends request', function(done) {
  461. spystub_request();
  462. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  463. check_request('post');
  464. done();
  465. })
  466. })
  467. it('sets Content-Type header to application/json', function(done) {
  468. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  469. resp.body.headers['content-type'].should.equal('application/json');
  470. done();
  471. })
  472. })
  473. it('doesnt change default Accept header', function(done) {
  474. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  475. // resp.body contains 'header' and 'body', mirroring what we sent
  476. resp.body.headers['accept'].should.equal('*/*');
  477. done();
  478. })
  479. })
  480. it('writes as buffer', function(done) {
  481. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  482. spy.called.should.be.true;
  483. spy.args[0][0].constructor.name.should.eql('Buffer');
  484. spy.args[0][0].toString().should.equal('foo=bar&test=%E6%B5%8B%E8%AF%95');
  485. resp.body.body.should.eql('foo=bar&test=%E6%B5%8B%E8%AF%95');
  486. done();
  487. })
  488. })
  489. })
  490. describe('with json: undefined but content-type = application/json', function() {
  491. var opts = { headers: { 'content-type': 'application/json' } };
  492. it('sends request', function(done) {
  493. spystub_request();
  494. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  495. check_request('post');
  496. done();
  497. })
  498. })
  499. it('doesnt change Content-Type header', function(done) {
  500. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  501. resp.body.headers['content-type'].should.equal('application/json');
  502. done();
  503. })
  504. })
  505. it('leaves default Accept header', function(done) {
  506. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  507. resp.body.headers['accept'].should.equal('*/*');
  508. done();
  509. })
  510. })
  511. it('writes JSON.stringified object', function(done) {
  512. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  513. spy.called.should.be.true;
  514. var json = JSON.stringify({ foo: 'bar', test: '测试' })
  515. spy.args[0][0].toString().should.eql(json)
  516. resp.body.body.should.eql(json);
  517. done();
  518. })
  519. })
  520. })
  521. describe('with json: true', function() {
  522. it('sends request', function(done) {
  523. spystub_request();
  524. post({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
  525. check_request('post');
  526. done();
  527. })
  528. })
  529. it('sets Content-Type header', function(done) {
  530. post({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
  531. resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
  532. done();
  533. })
  534. })
  535. it('set Accept header to application/json', function(done) {
  536. post({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
  537. resp.body.headers['accept'].should.equal('application/json');
  538. done();
  539. })
  540. })
  541. it('writes JSON.stringified object', function(done) {
  542. post({ foo: 'bar', test: '测试' }, { json: true }, function(err, resp) {
  543. spy.called.should.be.true;
  544. var json = JSON.stringify({ foo: 'bar', test: '测试' })
  545. spy.args[0][0].toString().should.eql(json)
  546. resp.body.body.should.eql(json);
  547. done();
  548. })
  549. })
  550. })
  551. describe('with json: true and content_type: */* (passed, not default)', function() {
  552. var opts = { json: true, accept: '*/*' };
  553. it('sends request', function(done) {
  554. spystub_request();
  555. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  556. check_request('post');
  557. done();
  558. })
  559. })
  560. it('sets Content-Type header to application/json', function(done) {
  561. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  562. resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
  563. done();
  564. })
  565. })
  566. it('respects Accept header set by user', function(done) {
  567. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  568. resp.body.headers['accept'].should.equal('*/*');
  569. done();
  570. })
  571. })
  572. it('writes JSON.stringified object', function(done) {
  573. post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
  574. spy.called.should.be.true;
  575. var json = JSON.stringify({ foo: 'bar', test: '测试' })
  576. spy.args[0][0].toString().should.eql(json)
  577. resp.body.body.should.eql(json);
  578. done();
  579. })
  580. })
  581. })
  582. })
  583. })
  584. describe('when buffer', function() {
  585. describe('get request', function() {
  586. describe('with json: false (default)', function() {
  587. it('sends request', function(done) {
  588. spystub_request();
  589. get(Buffer.from('foobar'), { json: false }, function(err, resp) {
  590. check_request('get');
  591. done();
  592. })
  593. })
  594. it('sets Content-Type header', function(done) {
  595. get(Buffer.from('foobar'), { json: false }, function(err, resp) {
  596. // should.not.exist(resp.body.headers['content-type']);
  597. resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
  598. done();
  599. })
  600. })
  601. it('doesnt change default Accept header', function(done) {
  602. get(Buffer.from('foobar'), { json: false }, function(err, resp) {
  603. // resp.body contains 'header' and 'body', mirroring what we sent
  604. resp.body.headers['accept'].should.equal('*/*');
  605. done();
  606. })
  607. })
  608. it('writes as buffer', function(done) {
  609. get(Buffer.from('foobar'), { json: false }, function(err, resp) {
  610. spy.called.should.be.true;
  611. spy.args[0][0].should.be.an.instanceof(Buffer);
  612. spy.args[0][0].toString().should.equal('foobar');
  613. resp.body.body.should.eql('foobar');
  614. done();
  615. })
  616. })
  617. })
  618. describe('with json: true', function() {
  619. it('sends request, without setting a querystring', function(done) {
  620. spystub_request();
  621. get(Buffer.from('foobar'), { json: true }, function(err, resp) {
  622. check_request('get');
  623. done();
  624. })
  625. })
  626. it('sets Content-Type header', function(done) {
  627. get(Buffer.from('foobar'), { json: true }, function(err, resp) {
  628. resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
  629. done();
  630. })
  631. })
  632. it('set Accept header to application/json', function(done) {
  633. get(Buffer.from('foobar'), { json: true }, function(err, resp) {
  634. resp.body.headers['accept'].should.equal('application/json');
  635. done();
  636. })
  637. })
  638. it('writes JSON.stringify version of object', function(done) {
  639. get(Buffer.from('foobar'), { json: true }, function(err, resp) {
  640. spy.called.should.be.true;
  641. spy.args[0][0].toString().should.eql('foobar')
  642. resp.body.body.should.eql('foobar');
  643. done();
  644. })
  645. })
  646. })
  647. })
  648. describe('post request', function() {
  649. describe('with json: false (default)', function() {
  650. it('sends request', function(done) {
  651. spystub_request();
  652. post(Buffer.from('foobar'), { json: false }, function(err, resp) {
  653. check_request('post');
  654. done();
  655. })
  656. })
  657. it('sets Content-Type header to www-form-urlencoded', function(done) {
  658. post(Buffer.from('foobar'), { json: false }, function(err, resp) {
  659. resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
  660. done();
  661. })
  662. })
  663. it('doesnt change default Accept header', function(done) {
  664. post(Buffer.from('foobar'), { json: false }, function(err, resp) {
  665. // resp.body contains 'header' and 'body', mirroring what we sent
  666. resp.body.headers['accept'].should.equal('*/*');
  667. done();
  668. })
  669. })
  670. it('writes as buffer', function(done) {
  671. post(Buffer.from('foobar'), { json: false }, function(err, resp) {
  672. spy.called.should.be.true;
  673. spy.args[0][0].should.be.an.instanceof(Buffer);
  674. spy.args[0][0].toString().should.equal('foobar');
  675. resp.body.body.should.eql('foobar');
  676. done();
  677. })
  678. })
  679. })
  680. describe('with json: true', function() {
  681. it('sends request', function(done) {
  682. spystub_request();
  683. post(Buffer.from('foobar'), { json: true }, function(err, resp) {
  684. check_request('post');
  685. done();
  686. })
  687. })
  688. it('sets Content-Type header', function(done) {
  689. post(Buffer.from('foobar'), { json: true }, function(err, resp) {
  690. resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
  691. done();
  692. })
  693. })
  694. it('set Accept header to application/json', function(done) {
  695. post(Buffer.from('foobar'), { json: true }, function(err, resp) {
  696. resp.body.headers['accept'].should.equal('application/json');
  697. done();
  698. })
  699. })
  700. it('passes raw buffer (assuming its a JSON string beneath)', function(done) {
  701. post(Buffer.from('foobar'), { json: true }, function(err, resp) {
  702. spy.called.should.be.true;
  703. spy.args[0][0].toString().should.eql('foobar')
  704. resp.body.body.should.eql('foobar');
  705. done();
  706. })
  707. })
  708. })
  709. })
  710. })
  711. describe('when stream', function() {
  712. var input_stream;
  713. beforeEach(function() {
  714. input_stream = new stream.Readable();
  715. input_stream._read = function() {
  716. this.push('foobar');
  717. this.push(null);
  718. }
  719. })
  720. describe('get request', function() {
  721. it('explodes', function() {
  722. (function() {
  723. get(input_stream, {})
  724. }).should.throw()
  725. })
  726. });
  727. describe('post request', function() {
  728. describe('with json: false (default)', function() {
  729. it('sends request', function(done) {
  730. spystub_request();
  731. post(input_stream, { json: false }, function(err, resp) {
  732. check_request('post');
  733. done();
  734. })
  735. })
  736. it('sets Content-Type header to www-form-urlencoded', function(done) {
  737. post(input_stream, { json: false }, function(err, resp) {
  738. resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
  739. done();
  740. })
  741. })
  742. it('doesnt change default Accept header', function(done) {
  743. post(input_stream, { json: false }, function(err, resp) {
  744. // resp.body contains 'header' and 'body', mirroring what we sent
  745. resp.body.headers['accept'].should.equal('*/*');
  746. done();
  747. })
  748. })
  749. it('writes as buffer', function(done) {
  750. post(input_stream, { json: false }, function(err, resp) {
  751. spy.called.should.be.true;
  752. spy.args[0][0].should.be.an.instanceof(Buffer);
  753. spy.args[0][0].toString().should.equal('foobar');
  754. resp.body.body.should.eql('foobar');
  755. done();
  756. })
  757. })
  758. })
  759. describe('with json: true', function() {
  760. it('sends request', function(done) {
  761. spystub_request();
  762. post(input_stream, { json: true }, function(err, resp) {
  763. check_request('post');
  764. done();
  765. })
  766. })
  767. it('sets Content-Type header', function(done) {
  768. post(input_stream, { json: true }, function(err, resp) {
  769. resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
  770. done();
  771. })
  772. })
  773. it('set Accept header to application/json', function(done) {
  774. post(input_stream, { json: true }, function(err, resp) {
  775. resp.body.headers['accept'].should.equal('application/json');
  776. done();
  777. })
  778. })
  779. it('writes JSON.stringified object', function(done) {
  780. post(input_stream, { json: true }, function(err, resp) {
  781. spy.called.should.be.true;
  782. spy.args[0][0].toString().should.eql('foobar')
  783. resp.body.body.should.eql('foobar');
  784. done();
  785. })
  786. })
  787. })
  788. })
  789. })
  790. })
  791. })