source: trip-planner-front/node_modules/needle/test/errors_spec.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.9 KB
Line 
1var needle = require('../'),
2 sinon = require('sinon'),
3 should = require('should'),
4 http = require('http'),
5 Emitter = require('events').EventEmitter,
6 helpers = require('./helpers');
7
8var get_catch = function(url, opts) {
9 var err;
10 try {
11 needle.get(url, opts);
12 } catch(e) {
13 err = e;
14 }
15 return err;
16}
17
18describe('errors', function() {
19
20 after(function(done) {
21 setTimeout(done, 100)
22 })
23
24 describe('when host does not exist', function() {
25
26 var url = 'http://unexistinghost/foo';
27
28 describe('with callback', function() {
29
30 it('does not throw', function() {
31 var ex = get_catch(url);
32 should.not.exist(ex);
33 })
34
35 it('callbacks an error', function(done) {
36 needle.get(url, function(err) {
37 err.should.be.a.Error;
38 done();
39 })
40 })
41
42 it('error should be ENOTFOUND or EADDRINFO or EAI_AGAIN', function(done) {
43 needle.get(url, function(err) {
44 err.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/)
45 done();
46 })
47 })
48
49 it('does not callback a response', function(done) {
50 needle.get(url, function(err, resp) {
51 should.not.exist(resp);
52 done();
53 })
54 })
55
56 it('does not emit an error event', function(done) {
57 var emitted = false;
58 var req = needle.get(url, function(err, resp) { })
59
60 req.on('error', function() {
61 emitted = true;
62 })
63
64 setTimeout(function() {
65 emitted.should.eql(false);
66 done();
67 }, 100);
68 })
69
70 })
71
72 describe('without callback', function() {
73
74 it('does not throw', function() {
75 var ex = get_catch(url);
76 should.not.exist(ex);
77 })
78
79 it('emits end event once, with error', function(done) {
80 var callcount = 0,
81 stream = needle.get(url);
82
83 stream.on('done', function(err) {
84 err.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/)
85 callcount++;
86 })
87
88 setTimeout(function() {
89 callcount.should.equal(1);
90 done();
91 }, 200)
92 })
93
94 it('does not emit a readable event', function(done) {
95 var called = false,
96 stream = needle.get(url);
97
98 stream.on('readable', function() {
99 called = true;
100 })
101
102 stream.on('done', function(err) {
103 called.should.be.false;
104 done();
105 })
106 })
107
108 it('does not emit an error event', function(done) {
109 var emitted = false,
110 stream = needle.get(url);
111
112 stream.on('error', function() {
113 emitted = true;
114 })
115
116 stream.on('done', function(err) {
117 emitted.should.eql(false);
118 done();
119 })
120 })
121
122 })
123
124 })
125
126 describe('when request times out waiting for response', function() {
127
128 var server,
129 url = 'http://localhost:3333/foo';
130
131 var send_request = function(cb) {
132 return needle.get(url, { response_timeout: 200 }, cb);
133 }
134
135 before(function() {
136 server = helpers.server({ port: 3333, wait: 1000 });
137 })
138
139 after(function() {
140 server.close();
141 })
142
143 describe('with callback', function() {
144
145 it('aborts the request', function(done) {
146
147 var time = new Date();
148
149 send_request(function(err) {
150 var timediff = (new Date() - time);
151 timediff.should.be.within(200, 300);
152 done();
153 })
154
155 })
156
157 it('callbacks an error', function(done) {
158 send_request(function(err) {
159 err.should.be.a.Error;
160 done();
161 })
162 })
163
164 it('error should be ECONNRESET', function(done) {
165 send_request(function(err) {
166 err.code.should.equal('ECONNRESET')
167 done();
168 })
169 })
170
171 it('does not callback a response', function(done) {
172 send_request(function(err, resp) {
173 should.not.exist(resp);
174 done();
175 })
176 })
177
178 it('does not emit an error event', function(done) {
179 var emitted = false;
180
181 var req = send_request(function(err, resp) {
182 should.not.exist(resp);
183 })
184
185 req.on('error', function() {
186 emitted = true;
187 })
188
189 setTimeout(function() {
190 emitted.should.eql(false);
191 done();
192 }, 350);
193 })
194
195 })
196
197 describe('without callback', function() {
198
199 it('emits done event once, with error', function(done) {
200 var error,
201 called = 0,
202 stream = send_request();
203
204 stream.on('done', function(err) {
205 err.code.should.equal('ECONNRESET');
206 called++;
207 })
208
209 setTimeout(function() {
210 called.should.equal(1);
211 done();
212 }, 250)
213 })
214
215 it('aborts the request', function(done) {
216
217 var time = new Date();
218 var stream = send_request();
219
220 stream.on('done', function(err) {
221 var timediff = (new Date() - time);
222 timediff.should.be.within(200, 300);
223 done();
224 })
225
226 })
227
228 it('error should be ECONNRESET', function(done) {
229 var error,
230 stream = send_request();
231
232 stream.on('done', function(err) {
233 err.code.should.equal('ECONNRESET')
234 done();
235 })
236 })
237
238 it('does not emit a readable event', function(done) {
239 var called = false,
240 stream = send_request();
241
242 stream.on('readable', function() {
243 called = true;
244 })
245
246 stream.on('done', function(err) {
247 called.should.be.false;
248 done();
249 })
250 })
251
252 it('does not emit an error event', function(done) {
253 var emitted = false;
254 var stream = send_request();
255
256 stream.on('error', function() {
257 emitted = true;
258 })
259
260 stream.on('done', function(err) {
261 err.should.be.a.Error;
262 err.code.should.equal('ECONNRESET')
263 emitted.should.eql(false);
264 done();
265 })
266 })
267
268 })
269
270 })
271
272})
Note: See TracBrowser for help on using the repository browser.