errors_spec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. callcount++;
  67. })
  68. setTimeout(function() {
  69. callcount.should.equal(1);
  70. done();
  71. }, 200)
  72. })
  73. it('error should be ENOTFOUND or EADDRINFO or EAI_AGAIN', function(done) {
  74. var errorific,
  75. stream = needle.get(url);
  76. stream.on('done', function(err) {
  77. errorific = err;
  78. })
  79. setTimeout(function() {
  80. should.exist(errorific);
  81. errorific.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/)
  82. done();
  83. }, 200)
  84. })
  85. it('does not emit a readable event', function(done) {
  86. var called = false,
  87. stream = needle.get(url);
  88. stream.on('readable', function() {
  89. called = true;
  90. })
  91. setTimeout(function() {
  92. called.should.be.false;
  93. done();
  94. }, 50)
  95. })
  96. it('does not emit an error event', function(done) {
  97. var emitted = false,
  98. req = needle.get(url);
  99. req.on('error', function() {
  100. emitted = true;
  101. })
  102. setTimeout(function() {
  103. emitted.should.eql(false);
  104. done();
  105. }, 100);
  106. })
  107. })
  108. })
  109. describe('when request times out waiting for response', function() {
  110. var server,
  111. url = 'http://localhost:3333/foo';
  112. var send_request = function(cb) {
  113. return needle.get(url, { response_timeout: 200 }, cb);
  114. }
  115. before(function() {
  116. server = helpers.server({ port: 3333, wait: 1000 });
  117. })
  118. after(function() {
  119. server.close();
  120. })
  121. describe('with callback', function() {
  122. it('aborts the request', function(done) {
  123. var time = new Date();
  124. send_request(function(err) {
  125. var timediff = (new Date() - time);
  126. timediff.should.be.within(200, 300);
  127. done();
  128. })
  129. })
  130. it('callbacks an error', function(done) {
  131. send_request(function(err) {
  132. err.should.be.a.Error;
  133. done();
  134. })
  135. })
  136. it('error should be ECONNRESET', function(done) {
  137. send_request(function(err) {
  138. err.code.should.equal('ECONNRESET')
  139. done();
  140. })
  141. })
  142. it('does not callback a response', function(done) {
  143. send_request(function(err, resp) {
  144. should.not.exist(resp);
  145. done();
  146. })
  147. })
  148. it('does not emit an error event', function(done) {
  149. var emitted = false;
  150. var req = send_request(function(err, resp) {
  151. should.not.exist(resp);
  152. })
  153. req.on('error', function() {
  154. emitted = true;
  155. })
  156. setTimeout(function() {
  157. emitted.should.eql(false);
  158. done();
  159. }, 350);
  160. })
  161. })
  162. describe('without callback', function() {
  163. it('emits done event once, with error', function(done) {
  164. var called = 0,
  165. stream = send_request();
  166. stream.on('done', function(err) {
  167. called++;
  168. })
  169. setTimeout(function() {
  170. called.should.equal(1);
  171. done();
  172. }, 250)
  173. })
  174. it('aborts the request', function(done) {
  175. var time = new Date();
  176. var stream = send_request();
  177. stream.on('done', function(err) {
  178. var timediff = (new Date() - time);
  179. timediff.should.be.within(200, 300);
  180. done();
  181. })
  182. })
  183. it('error should be ECONNRESET', function(done) {
  184. var error,
  185. stream = send_request();
  186. stream.on('done', function(err) {
  187. error = err;
  188. })
  189. setTimeout(function() {
  190. error.code.should.equal('ECONNRESET')
  191. done();
  192. }, 250)
  193. })
  194. it('does not emit a readable event', function(done) {
  195. var called = false,
  196. stream = send_request();
  197. stream.on('readable', function() {
  198. called = true;
  199. })
  200. setTimeout(function() {
  201. called.should.be.false;
  202. done();
  203. }, 250)
  204. })
  205. it('does not emit an error event', function(done) {
  206. var emitted = false;
  207. var req = send_request();
  208. req.on('error', function() {
  209. emitted = true;
  210. })
  211. setTimeout(function() {
  212. emitted.should.eql(false);
  213. done();
  214. }, 100);
  215. })
  216. })
  217. })
  218. })