fetch 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ module Fetch
2
+ module Simple
3
+ def self.included(base)
4
+ base.define_callback :url,
5
+ :timeout,
6
+ :user_agent,
7
+ :headers,
8
+ :process
9
+
10
+ base.request do |req|
11
+ req.url = url
12
+ req.timeout = timeout if callback?(:timeout)
13
+ req.user_agent = user_agent if callback?(:user_agent)
14
+ req.headers.merge!(headers) if callback?(:headers)
15
+ req.process do |body, url, final_url|
16
+ process(body, url, final_url)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- VERSION = "0.0.0"
2
+ VERSION = "0.0.1"
3
3
  end
@@ -0,0 +1,97 @@
1
+ require "test_helper"
2
+
3
+ class CallbackTest < Minitest::Test
4
+ def test_callbacks
5
+ actions = []
6
+ klass = Class.new do
7
+ include Fetch::Callbacks
8
+ define_callback :before, :after
9
+ before { actions << "something before" }
10
+ after { actions << "something after" }
11
+ def do_something
12
+ before
13
+ after
14
+ end
15
+ end
16
+ klass.new.do_something
17
+ assert_equal ["something before", "something after"], actions
18
+ end
19
+
20
+ def test_all_callbacks_are_run
21
+ actions = []
22
+ klass = Class.new do
23
+ include Fetch::Callbacks
24
+ define_callback :before
25
+ before { actions << "first" }
26
+ before { actions << "second" }
27
+ def do_something
28
+ before
29
+ end
30
+ end
31
+ klass.new.do_something
32
+ assert_equal ["first", "second"], actions
33
+ end
34
+
35
+ def test_value_from_last_callback_is_returned
36
+ klass = Class.new do
37
+ include Fetch::Callbacks
38
+ define_callback :before
39
+ before { "first" }
40
+ before { "second" }
41
+ end
42
+ assert_equal "second", klass.new.before
43
+ end
44
+
45
+ def test_callbacks_take_optional_arguments
46
+ actions = []
47
+ klass = Class.new do
48
+ include Fetch::Callbacks
49
+ define_callback :before, :after
50
+ before { |some_arg| actions << "one: #{some_arg}" }
51
+ after { |some_arg| actions << "two: #{some_arg.inspect}" }
52
+ def do_something
53
+ before("first")
54
+ after
55
+ end
56
+ end
57
+ klass.new.do_something
58
+ assert_equal ["one: first", "two: nil"], actions
59
+ end
60
+
61
+ def test_callbacks_are_inherited
62
+ before1 = Proc.new {}
63
+ before2 = Proc.new {}
64
+ superclass = Class.new do
65
+ include Fetch::Callbacks
66
+ define_callback :before
67
+ before(&before1)
68
+ before(&before2)
69
+ end
70
+ subclass = Class.new(superclass)
71
+ assert_equal [before1, before2], subclass.callbacks[:before]
72
+ end
73
+
74
+ def test_callbacks_are_not_added_to_superclass
75
+ before1, before2, before3 = 3.times.map { Proc.new {} }
76
+ superclass = Class.new do
77
+ include Fetch::Callbacks
78
+ define_callback :before
79
+ before(&before1)
80
+ before(&before2)
81
+ end
82
+ subclass = Class.new(superclass) do
83
+ before(&before3)
84
+ end
85
+ assert_equal [before1, before2], superclass.callbacks[:before]
86
+ assert_equal [before1, before2, before3], subclass.callbacks[:before]
87
+ end
88
+
89
+ def test_callbacks_can_take_fixed_values
90
+ klass = Class.new do
91
+ include Fetch::Callbacks
92
+ define_callback :modules
93
+ modules "one", "two", "three"
94
+ end
95
+ assert_equal ["one", "two", "three"], klass.new.modules
96
+ end
97
+ end
@@ -1,4 +1,691 @@
1
1
  require "test_helper"
2
2
 
3
- class FetchTest < Test::Unit::TestCase
4
- end
3
+ class FetchTest < Minitest::Test
4
+ def test_fetch_using_get
5
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
6
+ actions = []
7
+ mod = Class.new(Fetch::Module) do
8
+ request do |req|
9
+ req.url = "http://test.com/one"
10
+ req.process do |body|
11
+ actions << "body: #{body}"
12
+ end
13
+ end
14
+ end
15
+ MockFetcher(mod).new.fetch
16
+ assert_equal ["body: got one"], actions
17
+ end
18
+
19
+ def test_fetch_using_post
20
+ stub_request(:post, "http://test.com/create").to_return(->(req) { { body: "you posted: #{req.body}" } })
21
+ actions = []
22
+ mod = Class.new(Fetch::Module) do
23
+ request do |req|
24
+ req.method = :post
25
+ req.url = "http://test.com/create"
26
+ req.body = { one: 1, two: 2 }
27
+ req.process do |body|
28
+ actions << "body: #{body}"
29
+ end
30
+ end
31
+ end
32
+ MockFetcher(mod).new.fetch
33
+ assert_equal ["body: you posted: one=1&two=2"], actions
34
+ end
35
+
36
+ def test_url_not_set
37
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
38
+
39
+ actions = []
40
+ mod = Class.new(Fetch::Module) do
41
+ 2.times do
42
+ request do |req|
43
+ req.url = "http://test.com/one"
44
+ req.process do |body|
45
+ actions << "process: #{body}"
46
+ end
47
+ end
48
+ end
49
+ 2.times do
50
+ request do |req|
51
+ req.process do |body|
52
+ actions << "process: #{body}"
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ updates = []
59
+ klass = Class.new(MockFetcher(mod)) do
60
+ progress do |percent|
61
+ updates << percent
62
+ end
63
+ end
64
+
65
+ klass.new.fetch
66
+ assert_equal ["process: got one", "process: got one"], actions
67
+ assert_equal [0, 50, 100], updates
68
+ end
69
+
70
+ def test_initializes_modules
71
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
72
+ actions = []
73
+ mod = Class.new(Fetch::Module) do
74
+ attr_reader :word
75
+ def initialize(word)
76
+ @word = word
77
+ end
78
+ request do |req|
79
+ req.url = "http://test.com/one"
80
+ req.process do |body|
81
+ actions << "process: #{body} (#{word})"
82
+ end
83
+ end
84
+ end
85
+
86
+ klass = Class.new(MockFetcher(mod)) do
87
+ attr_reader :something
88
+ def initialize(something)
89
+ @something = something
90
+ end
91
+ init do |klass|
92
+ klass.new(something)
93
+ end
94
+ end
95
+
96
+ klass.new("a word").fetch
97
+ assert_equal ["process: got one (a word)"], actions
98
+ end
99
+
100
+ def test_process_block_scope
101
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
102
+ actions = []
103
+ mod = Class.new(Fetch::Module) do
104
+ request do |req|
105
+ req.url = "http://test.com/one"
106
+ req.process do |body|
107
+ actions << "body: #{body} (#{some_instance_method})"
108
+ end
109
+ end
110
+
111
+ def some_instance_method
112
+ "it worked"
113
+ end
114
+ end
115
+ MockFetcher(mod).new.fetch
116
+ assert_equal ["body: got one (it worked)"], actions
117
+ end
118
+
119
+ def test_before_process_callback_set_in_request
120
+ words = %w{one two}
121
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
122
+
123
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
124
+ actions = []
125
+ mod = Class.new(Fetch::Module) do
126
+ words.each do |word|
127
+ request do |req|
128
+ req.url = "http://test.com/#{word}"
129
+ req.before_process do
130
+ actions << "before process #{word}"
131
+ end
132
+ req.process do |body|
133
+ actions << "process #{word}"
134
+ end
135
+ end
136
+ end
137
+ end
138
+ MockFetcher(mod).new.fetch
139
+ assert_equal ["before process one", "process one", "before process two", "process two"], actions
140
+ end
141
+
142
+ def test_before_process_callback_scope_set_in_request
143
+ words = %w{one two}
144
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
145
+
146
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
147
+ actions = []
148
+ mod = Class.new(Fetch::Module) do
149
+ words.each do |word|
150
+ request do |req|
151
+ req.url = "http://test.com/#{word}"
152
+ req.before_process do
153
+ actions << "before process #{word} (#{some_instance_method})"
154
+ end
155
+ req.process do |body|
156
+ actions << "process #{word}"
157
+ end
158
+ end
159
+ end
160
+ def some_instance_method
161
+ "ok"
162
+ end
163
+ end
164
+ MockFetcher(mod).new.fetch
165
+ assert_equal ["before process one (ok)", "process one", "before process two (ok)", "process two"], actions
166
+ end
167
+
168
+ def test_before_process_callback_set_in_module
169
+ words = %w{one two}
170
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
171
+
172
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
173
+ actions = []
174
+ mod = Class.new(Fetch::Module) do
175
+ words.each do |word|
176
+ request do |req|
177
+ req.url = "http://test.com/#{word}"
178
+ req.process do |body|
179
+ actions << "process #{word}"
180
+ end
181
+ end
182
+ end
183
+
184
+ before_process do
185
+ actions << "before process"
186
+ end
187
+ end
188
+ MockFetcher(mod).new.fetch
189
+ assert_equal ["before process", "process one", "before process", "process two"], actions
190
+ end
191
+
192
+ def test_before_process_callback_scope_set_in_module
193
+ words = %w{one two}
194
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
195
+
196
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
197
+ actions = []
198
+ mod = Class.new(Fetch::Module) do
199
+ words.each do |word|
200
+ request do |req|
201
+ req.url = "http://test.com/#{word}"
202
+ req.process do |body|
203
+ actions << "process #{word}"
204
+ end
205
+ end
206
+ end
207
+
208
+ before_process do
209
+ actions << "before process (#{some_instance_method})"
210
+ end
211
+
212
+ def some_instance_method
213
+ "ok"
214
+ end
215
+ end
216
+ MockFetcher(mod).new.fetch
217
+ assert_equal ["before process (ok)", "process one", "before process (ok)", "process two"], actions
218
+ end
219
+
220
+ def test_after_process_callback_set_in_request
221
+ words = %w{one two}
222
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
223
+
224
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
225
+ actions = []
226
+ mod = Class.new(Fetch::Module) do
227
+ words.each do |word|
228
+ request do |req|
229
+ req.url = "http://test.com/#{word}"
230
+ req.after_process do
231
+ actions << "after process #{word}"
232
+ end
233
+ req.process do |body|
234
+ actions << "process #{word}"
235
+ end
236
+ end
237
+ end
238
+ end
239
+ MockFetcher(mod).new.fetch
240
+ assert_equal ["process one", "after process one", "process two", "after process two"], actions
241
+ end
242
+
243
+ def test_after_process_callback_scope_set_in_request
244
+ words = %w{one two}
245
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
246
+
247
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
248
+ actions = []
249
+ mod = Class.new(Fetch::Module) do
250
+ words.each do |word|
251
+ request do |req|
252
+ req.url = "http://test.com/#{word}"
253
+ req.after_process do
254
+ actions << "after process #{word} (#{some_instance_method})"
255
+ end
256
+ req.process do |body|
257
+ actions << "process #{word}"
258
+ end
259
+ end
260
+ end
261
+ def some_instance_method
262
+ "ok"
263
+ end
264
+ end
265
+ MockFetcher(mod).new.fetch
266
+ assert_equal ["process one", "after process one (ok)", "process two", "after process two (ok)"], actions
267
+ end
268
+
269
+ def test_after_process_callback_set_in_module
270
+ words = %w{one two}
271
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
272
+
273
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
274
+ actions = []
275
+ mod = Class.new(Fetch::Module) do
276
+ words.each do |word|
277
+ request do |req|
278
+ req.url = "http://test.com/#{word}"
279
+ req.process do |body|
280
+ actions << "process #{word}"
281
+ end
282
+ end
283
+ end
284
+
285
+ after_process do
286
+ actions << "after process"
287
+ end
288
+ end
289
+ MockFetcher(mod).new.fetch
290
+ assert_equal ["process one", "after process", "process two", "after process"], actions
291
+ end
292
+
293
+ def test_after_process_callback_scope_set_in_module
294
+ words = %w{one two}
295
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
296
+
297
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
298
+ actions = []
299
+ mod = Class.new(Fetch::Module) do
300
+ words.each do |word|
301
+ request do |req|
302
+ req.url = "http://test.com/#{word}"
303
+ req.process do |body|
304
+ actions << "process #{word}"
305
+ end
306
+ end
307
+ end
308
+
309
+ after_process do
310
+ actions << "after process (#{some_instance_method})"
311
+ end
312
+
313
+ def some_instance_method
314
+ "ok"
315
+ end
316
+ end
317
+ MockFetcher(mod).new.fetch
318
+ assert_equal ["process one", "after process (ok)", "process two", "after process (ok)"], actions
319
+ end
320
+
321
+ def test_positive_fetch_if_filter
322
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
323
+ actions = []
324
+ mod = Class.new(Fetch::Module) do
325
+ fetch_if { true }
326
+
327
+ request do |req|
328
+ req.url = "http://test.com/one"
329
+ req.process do |body|
330
+ actions << "body: #{body}"
331
+ end
332
+ end
333
+ end
334
+ MockFetcher(mod).new.fetch
335
+ assert_equal ["body: got one"], actions
336
+ end
337
+
338
+ def test_negative_fetch_if_filter
339
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
340
+ actions = []
341
+ mod = Class.new(Fetch::Module) do
342
+ fetch_if { false }
343
+
344
+ request do |req|
345
+ req.url = "http://test.com/one"
346
+ req.process do |body|
347
+ actions << "body: #{body}"
348
+ end
349
+ end
350
+ end
351
+ MockFetcher(mod).new.fetch
352
+ assert_equal [], actions
353
+ end
354
+
355
+ def test_nil_fetch_if_filter
356
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
357
+ actions = []
358
+ mod = Class.new(Fetch::Module) do
359
+ fetch_if { nil }
360
+
361
+ request do |req|
362
+ req.url = "http://test.com/one"
363
+ req.process do |body|
364
+ actions << "body: #{body}"
365
+ end
366
+ end
367
+ end
368
+ MockFetcher(mod).new.fetch
369
+ assert_equal [], actions
370
+ end
371
+
372
+ def test_if_filter_scope
373
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
374
+ actions = []
375
+ mod = Class.new(Fetch::Module) do
376
+ fetch_if { should_i_fetch? }
377
+
378
+ request do |req|
379
+ req.url = "http://test.com/one"
380
+ req.process do |body|
381
+ actions << "body: #{body}"
382
+ end
383
+ end
384
+
385
+ def should_i_fetch?
386
+ true
387
+ end
388
+ end
389
+ MockFetcher(mod).new.fetch
390
+ assert_equal ["body: got one"], actions
391
+ end
392
+
393
+ def test_multiple_requests
394
+ words = %w{one two three}
395
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
396
+ actions = []
397
+
398
+ mod = Class.new(Fetch::Module) do
399
+ words.each do |w|
400
+ request do |req|
401
+ req.url = "http://test.com/#{w}"
402
+ req.process do |body|
403
+ actions << "body: #{body}"
404
+ end
405
+ end
406
+ end
407
+ end
408
+ MockFetcher(mod).new.fetch
409
+ assert_equal ["body: got one", "body: got two", "body: got three"], actions
410
+ end
411
+
412
+ def test_unhandled_http_failure
413
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
414
+ actions = []
415
+ mod = Class.new(Fetch::Module) do
416
+ request do |req|
417
+ req.url = "http://test.com/one"
418
+ req.process do |body|
419
+ actions << "body: #{body}"
420
+ end
421
+ end
422
+ end
423
+ assert_equal [], actions
424
+ end
425
+
426
+ def test_http_failure_handled_in_request
427
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
428
+ actions = []
429
+ mod = Class.new(Fetch::Module) do
430
+ request do |req|
431
+ req.url = "http://test.com/one"
432
+ req.failure do |code, url|
433
+ actions << "handled error #{code} from #{url}"
434
+ end
435
+ req.process do |body|
436
+ actions << "body: #{body}"
437
+ end
438
+ end
439
+ end
440
+ MockFetcher(mod).new.fetch
441
+ assert_equal ["handled error 500 from http://test.com/one"], actions
442
+ end
443
+
444
+ def test_http_failure_scope_handled_in_request
445
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
446
+ actions = []
447
+ mod = Class.new(Fetch::Module) do
448
+ request do |req|
449
+ req.url = "http://test.com/one"
450
+ req.failure do |code, url|
451
+ actions << "handled error #{code} from #{url} (#{some_instance_method})"
452
+ end
453
+ req.process do |body|
454
+ actions << "body: #{body}"
455
+ end
456
+ end
457
+
458
+ def some_instance_method
459
+ "it worked"
460
+ end
461
+ end
462
+ MockFetcher(mod).new.fetch
463
+ assert_equal ["handled error 500 from http://test.com/one (it worked)"], actions
464
+ end
465
+
466
+ def test_http_failure_handled_in_module
467
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
468
+ actions = []
469
+ mod = Class.new(Fetch::Module) do
470
+ request do |req|
471
+ req.url = "http://test.com/one"
472
+ req.process do |body|
473
+ actions << "body: #{body}"
474
+ end
475
+ end
476
+ failure do |code, url|
477
+ actions << "handled error #{code} from #{url}"
478
+ end
479
+ end
480
+ MockFetcher(mod).new.fetch
481
+ assert_equal ["handled error 500 from http://test.com/one"], actions
482
+ end
483
+
484
+ def test_http_failure_scope_handled_in_module
485
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
486
+ actions = []
487
+ mod = Class.new(Fetch::Module) do
488
+ request do |req|
489
+ req.url = "http://test.com/one"
490
+ req.process do |body|
491
+ actions << "body: #{body}"
492
+ end
493
+ end
494
+ failure do |code, url|
495
+ actions << "handled error #{code} from #{url} (#{some_instance_method})"
496
+ end
497
+ def some_instance_method
498
+ "it worked"
499
+ end
500
+ end
501
+ MockFetcher(mod).new.fetch
502
+ assert_equal ["handled error 500 from http://test.com/one (it worked)"], actions
503
+ end
504
+
505
+ def test_unhandled_process_error
506
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
507
+ actions = []
508
+ mod = Class.new(Fetch::Module) do
509
+ request do |req|
510
+ req.url = "http://test.com/one"
511
+ req.process do |body|
512
+ this_wont_work
513
+ end
514
+ end
515
+ end
516
+ assert_raises NameError do
517
+ MockFetcher(mod).new.fetch
518
+ end
519
+ end
520
+
521
+ def test_process_error_handled_in_request
522
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
523
+ actions = []
524
+ mod = Class.new(Fetch::Module) do
525
+ request do |req|
526
+ req.url = "http://test.com/one"
527
+ req.error do |e|
528
+ actions << "handled #{e.class.name}"
529
+ end
530
+ req.process do |body|
531
+ this_wont_work
532
+ end
533
+ end
534
+ end
535
+ MockFetcher(mod).new.fetch
536
+ assert_equal ["handled NameError"], actions
537
+ end
538
+
539
+ def test_process_error_scope_handled_in_request
540
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
541
+ actions = []
542
+ mod = Class.new(Fetch::Module) do
543
+ request do |req|
544
+ req.url = "http://test.com/one"
545
+ req.error do |e|
546
+ actions << "handled #{e.class.name} (#{some_instance_method})"
547
+ end
548
+ req.process do |body|
549
+ this_wont_work
550
+ end
551
+ end
552
+
553
+ def some_instance_method
554
+ "it worked"
555
+ end
556
+ end
557
+ MockFetcher(mod).new.fetch
558
+ assert_equal ["handled NameError (it worked)"], actions
559
+ end
560
+
561
+ def test_process_error_handled_in_module
562
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
563
+ actions = []
564
+ mod = Class.new(Fetch::Module) do
565
+ request do |req|
566
+ req.url = "http://test.com/one"
567
+ req.process do |body|
568
+ this_wont_work
569
+ end
570
+ end
571
+ error do |e|
572
+ actions << "handled #{e.class.name}"
573
+ end
574
+ end
575
+ MockFetcher(mod).new.fetch
576
+ assert_equal ["handled NameError"], actions
577
+ end
578
+
579
+ def test_process_error_scope_handled_in_module
580
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
581
+ actions = []
582
+ mod = Class.new(Fetch::Module) do
583
+ request do |req|
584
+ req.url = "http://test.com/one"
585
+ req.process do |body|
586
+ this_wont_work
587
+ end
588
+ end
589
+ error do |e|
590
+ actions << "handled #{e.class.name} (#{some_instance_method})"
591
+ end
592
+ def some_instance_method
593
+ "it worked"
594
+ end
595
+ end
596
+ MockFetcher(mod).new.fetch
597
+ assert_equal ["handled NameError (it worked)"], actions
598
+ end
599
+
600
+ def test_progress_with_single_module
601
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
602
+
603
+ mod = Class.new(Fetch::Module) do
604
+ 3.times do
605
+ request do |req|
606
+ req.url = "http://test.com/one"
607
+ end
608
+ end
609
+ end
610
+
611
+ updates = []
612
+
613
+ klass = Class.new(MockFetcher(mod)) do
614
+ progress do |percent|
615
+ updates << percent
616
+ end
617
+ end
618
+
619
+ klass.new.fetch
620
+ assert_equal [0, 33, 66, 100], updates
621
+ end
622
+
623
+ def test_progress_with_multiple_modules
624
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
625
+
626
+ mods = 3.times.map do
627
+ Class.new(Fetch::Module) do
628
+ 2.times do
629
+ request do |req|
630
+ req.url = "http://test.com/one"
631
+ end
632
+ end
633
+ end
634
+ end
635
+
636
+ updates = []
637
+
638
+ klass = Class.new(MockFetcher(mods)) do
639
+ progress do |percent|
640
+ updates << percent
641
+ end
642
+ end
643
+
644
+ klass.new.fetch
645
+ assert_equal [0, 16, 33, 50, 66, 83, 100], updates
646
+ end
647
+
648
+ def test_progress_with_http_failure
649
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
650
+ updates = []
651
+ mods = 3.times.map do
652
+ Class.new(Fetch::Module) do
653
+ request do |req|
654
+ req.url = "http://test.com/one"
655
+ end
656
+ end
657
+ end
658
+ klass = Class.new(MockFetcher(mods)) do
659
+ progress do |percent|
660
+ updates << percent
661
+ end
662
+ end
663
+
664
+ klass.new.fetch
665
+ assert_equal [0, 33, 66, 100], updates
666
+ end
667
+
668
+ def test_progress_with_handled_process_error
669
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
670
+ updates = []
671
+ mods = 3.times.map do
672
+ Class.new(Fetch::Module) do
673
+ request do |req|
674
+ req.url = "http://test.com/one"
675
+ req.process do |body|
676
+ wont_work
677
+ end
678
+ req.error { }
679
+ end
680
+ end
681
+ end
682
+ klass = Class.new(MockFetcher(mods)) do
683
+ progress do |percent|
684
+ updates << percent
685
+ end
686
+ end
687
+
688
+ klass.new.fetch
689
+ assert_equal [0, 33, 66, 100], updates
690
+ end
691
+ end