errors_spec.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. })