errors_spec.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. var needle = require('../'),
  2. sinon = require('sinon'),
  3. should = require('should'),
  4. http = require('http'),
  5. Emitter = require('events').EventEmitter,
  6. helpers = require('./helpers');
  7. var get_catch = function(url, opts) {
  8. var err;
  9. try {
  10. needle.get(url, opts);
  11. } catch(e) {
  12. err = e;
  13. }
  14. return err;
  15. }
  16. describe('errors', function() {
  17. after(function(done) {
  18. setTimeout(done, 100)
  19. })
  20. describe('when host does not exist', function() {
  21. var url = 'http://unexistinghost/foo';
  22. describe('with callback', function() {
  23. it('does not throw', function() {
  24. var ex = get_catch(url);
  25. should.not.exist(ex);
  26. })
  27. it('callbacks an error', function(done) {
  28. needle.get(url, function(err) {
  29. err.should.be.a.Error;
  30. done();
  31. })
  32. })
  33. it('error should be ENOTFOUND or EADDRINFO or EAI_AGAIN', function(done) {
  34. needle.get(url, function(err) {
  35. err.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/)
  36. done();
  37. })
  38. })
  39. it('does not callback a response', function(done) {
  40. needle.get(url, function(err, resp) {
  41. should.not.exist(resp);
  42. done();
  43. })
  44. })
  45. it('does not emit an error event', function(done) {
  46. var emitted = false;
  47. var req = needle.get(url, function(err, resp) { })
  48. req.on('error', function() {
  49. emitted = true;
  50. })
  51. setTimeout(function() {
  52. emitted.should.eql(false);
  53. done();
  54. }, 100);
  55. })
  56. })
  57. describe('without callback', function() {
  58. it('does not throw', function() {
  59. var ex = get_catch(url);
  60. should.not.exist(ex);
  61. })
  62. it('emits end event once, with error', function(done) {
  63. var callcount = 0,
  64. stream = needle.get(url);
  65. stream.on('done', function(err) {
  66. err.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/)
  67. callcount++;
  68. })
  69. setTimeout(function() {
  70. callcount.should.equal(1);
  71. done();
  72. }, 200)
  73. })
  74. it('does not emit a readable event', function(done) {
  75. var called = false,
  76. stream = needle.get(url);
  77. stream.on('readable', function() {
  78. called = true;
  79. })
  80. stream.on('done', function(err) {
  81. called.should.be.false;
  82. done();
  83. })
  84. })
  85. it('does not emit an error event', function(done) {
  86. var emitted = false,
  87. stream = needle.get(url);
  88. stream.on('error', function() {
  89. emitted = true;
  90. })
  91. stream.on('done', function(err) {
  92. emitted.should.eql(false);
  93. done();
  94. })
  95. })
  96. })
  97. })
  98. describe('when request times out waiting for response', function() {
  99. var server,
  100. url = 'http://localhost:3333/foo';
  101. var send_request = function(cb) {
  102. return needle.get(url, { response_timeout: 200 }, cb);
  103. }
  104. before(function() {
  105. server = helpers.server({ port: 3333, wait: 1000 });
  106. })
  107. after(function() {
  108. server.close();
  109. })
  110. describe('with callback', function() {
  111. it('aborts the request', function(done) {
  112. var time = new Date();
  113. send_request(function(err) {
  114. var timediff = (new Date() - time);
  115. timediff.should.be.within(200, 300);
  116. done();
  117. })
  118. })
  119. it('callbacks an error', function(done) {
  120. send_request(function(err) {
  121. err.should.be.a.Error;
  122. done();
  123. })
  124. })
  125. it('error should be ECONNRESET', function(done) {
  126. send_request(function(err) {
  127. err.code.should.equal('ECONNRESET')
  128. done();
  129. })
  130. })
  131. it('does not callback a response', function(done) {
  132. send_request(function(err, resp) {
  133. should.not.exist(resp);
  134. done();
  135. })
  136. })
  137. it('does not emit an error event', function(done) {
  138. var emitted = false;
  139. var req = send_request(function(err, resp) {
  140. should.not.exist(resp);
  141. })
  142. req.on('error', function() {
  143. emitted = true;
  144. })
  145. setTimeout(function() {
  146. emitted.should.eql(false);
  147. done();
  148. }, 350);
  149. })
  150. })
  151. describe('without callback', function() {
  152. it('emits done event once, with error', function(done) {
  153. var error,
  154. called = 0,
  155. stream = send_request();
  156. stream.on('done', function(err) {
  157. err.code.should.equal('ECONNRESET');
  158. called++;
  159. })
  160. setTimeout(function() {
  161. called.should.equal(1);
  162. done();
  163. }, 250)
  164. })
  165. it('aborts the request', function(done) {
  166. var time = new Date();
  167. var stream = send_request();
  168. stream.on('done', function(err) {
  169. var timediff = (new Date() - time);
  170. timediff.should.be.within(200, 300);
  171. done();
  172. })
  173. })
  174. it('error should be ECONNRESET', function(done) {
  175. var error,
  176. stream = send_request();
  177. stream.on('done', function(err) {
  178. err.code.should.equal('ECONNRESET')
  179. done();
  180. })
  181. })
  182. it('does not emit a readable event', function(done) {
  183. var called = false,
  184. stream = send_request();
  185. stream.on('readable', function() {
  186. called = true;
  187. })
  188. stream.on('done', function(err) {
  189. called.should.be.false;
  190. done();
  191. })
  192. })
  193. it('does not emit an error event', function(done) {
  194. var emitted = false;
  195. var stream = send_request();
  196. stream.on('error', function() {
  197. emitted = true;
  198. })
  199. stream.on('done', function(err) {
  200. err.should.be.a.Error;
  201. err.code.should.equal('ECONNRESET')
  202. emitted.should.eql(false);
  203. done();
  204. })
  205. })
  206. })
  207. })
  208. var node_major_ver = process.version.split('.')[0].replace('v', '');
  209. if (node_major_ver >= 16) {
  210. describe('when request is aborted by signal', function() {
  211. var server,
  212. url = 'http://localhost:3333/foo';
  213. before(function() {
  214. server = helpers.server({ port: 3333, wait: 600 });
  215. })
  216. after(function() {
  217. server.close();
  218. })
  219. afterEach(function() {
  220. // reset signal to default
  221. needle.defaults({signal: null});
  222. })
  223. it('works if passing an already aborted signal aborts the request', function(done) {
  224. var abortedSignal = AbortSignal.abort();
  225. var start = new Date();
  226. abortedSignal.aborted.should.equal(true);
  227. needle.get(url, { signal: abortedSignal, response_timeout: 10000 }, function(err, res) {
  228. var timediff = (new Date() - start);
  229. should.not.exist(res);
  230. err.code.should.equal('ABORT_ERR');
  231. timediff.should.be.within(0, 50);
  232. done();
  233. });
  234. })
  235. it('works if request aborts before timing out', function(done) {
  236. var cancel = new AbortController();
  237. var start = new Date();
  238. needle.get(url, { signal: cancel.signal, response_timeout: 500, open_timeout: 500, read_timeout: 500 }, function(err, res) {
  239. var timediff = (new Date() - start);
  240. should.not.exist(res);
  241. if (node_major_ver <= 16)
  242. err.code.should.equal('ECONNRESET');
  243. if (node_major_ver > 16)
  244. err.code.should.equal('ABORT_ERR');
  245. cancel.signal.aborted.should.equal(true);
  246. timediff.should.be.within(200, 250);
  247. done();
  248. });
  249. function abort() {
  250. cancel.abort();
  251. }
  252. setTimeout(abort, 200);
  253. })
  254. it('works if request times out before being aborted', function(done) {
  255. var cancel = new AbortController();
  256. var start = new Date();
  257. needle.get(url, { signal: cancel.signal, response_timeout: 200, open_timeout: 200, read_timeout: 200 }, function(err, res) {
  258. var timediff = (new Date() - start);
  259. should.not.exist(res);
  260. err.code.should.equal('ECONNRESET');
  261. timediff.should.be.within(200, 250);
  262. });
  263. function abort() {
  264. cancel.signal.aborted.should.equal(false);
  265. done();
  266. }
  267. setTimeout(abort, 500);
  268. })
  269. it('works if setting default signal aborts all requests', function(done) {
  270. var cancel = new AbortController();
  271. needle.defaults({signal: cancel.signal});
  272. var start = new Date();
  273. var count = 0;
  274. function cb(err, res) {
  275. var timediff = (new Date() - start);
  276. should.not.exist(res);
  277. if (node_major_ver <= 16)
  278. err.code.should.equal('ECONNRESET');
  279. if (node_major_ver > 16)
  280. err.code.should.equal('ABORT_ERR');
  281. cancel.signal.aborted.should.equal(true);
  282. timediff.should.be.within(200, 250);
  283. if ( count++ === 2 ) done();
  284. }
  285. needle.get(url, { timeout: 300 }, cb);
  286. needle.get(url, { timeout: 350 }, cb);
  287. needle.get(url, { timeout: 400 }, cb);
  288. function abort() {
  289. cancel.abort();
  290. }
  291. setTimeout(abort, 200);
  292. })
  293. it('does not work if invalid signal passed', function(done) {
  294. try {
  295. needle.get(url, { signal: 'invalid signal' }, function(err, res) {
  296. done(new Error('A bad option error expected to be thrown'));
  297. });
  298. } catch(e) {
  299. e.should.be.a.TypeError;
  300. done();
  301. }
  302. })
  303. it('does not work if invalid signal set by default', function(done) {
  304. try {
  305. needle.defaults({signal: new Error(), timeout: 1200});
  306. done(new Error('A bad option error expected to be thrown'));
  307. } catch(e) {
  308. e.should.be.a.TypeError;
  309. done();
  310. }
  311. })
  312. })
  313. }
  314. })