fetch 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ require "test_helper"
2
+
3
+ class ProcessTest < Minitest::Test
4
+ def test_process_block_scope
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} (#{some_instance_method})"
12
+ end
13
+ end
14
+
15
+ def some_instance_method
16
+ "it worked"
17
+ end
18
+ end
19
+ MockFetcher(mod).new.fetch
20
+ assert_equal ["body: got one (it worked)"], actions
21
+ end
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lasse Bunk
@@ -108,15 +108,25 @@ files:
108
108
  - lib/fetch/request.rb
109
109
  - lib/fetch/simple.rb
110
110
  - lib/fetch/version.rb
111
+ - test/base/after_fetch_test.rb
112
+ - test/base/base_test.rb
113
+ - test/base/before_fetch_test.rb
114
+ - test/base/error_test.rb
115
+ - test/base/init_test.rb
116
+ - test/base/load_test.rb
117
+ - test/base/progress_test.rb
111
118
  - test/callbacks_test.rb
112
- - test/defaults_test.rb
113
- - test/error_test.rb
114
- - test/fetch_test.rb
115
- - test/init_test.rb
116
- - test/json_test.rb
117
- - test/load_test.rb
118
- - test/parse_test.rb
119
- - test/simple_test.rb
119
+ - test/module/after_process_test.rb
120
+ - test/module/before_process_test.rb
121
+ - test/module/defaults_test.rb
122
+ - test/module/error_test.rb
123
+ - test/module/failure_test.rb
124
+ - test/module/fetch_if_test.rb
125
+ - test/module/json_test.rb
126
+ - test/module/module_test.rb
127
+ - test/module/parse_test.rb
128
+ - test/module/process_test.rb
129
+ - test/module/simple_test.rb
120
130
  - test/test_helper.rb
121
131
  homepage: https://github.com/lassebunk/fetch
122
132
  licenses:
@@ -143,13 +153,23 @@ signing_key:
143
153
  specification_version: 4
144
154
  summary: Coming
145
155
  test_files:
156
+ - test/base/after_fetch_test.rb
157
+ - test/base/base_test.rb
158
+ - test/base/before_fetch_test.rb
159
+ - test/base/error_test.rb
160
+ - test/base/init_test.rb
161
+ - test/base/load_test.rb
162
+ - test/base/progress_test.rb
146
163
  - test/callbacks_test.rb
147
- - test/defaults_test.rb
148
- - test/error_test.rb
149
- - test/fetch_test.rb
150
- - test/init_test.rb
151
- - test/json_test.rb
152
- - test/load_test.rb
153
- - test/parse_test.rb
154
- - test/simple_test.rb
164
+ - test/module/after_process_test.rb
165
+ - test/module/before_process_test.rb
166
+ - test/module/defaults_test.rb
167
+ - test/module/error_test.rb
168
+ - test/module/failure_test.rb
169
+ - test/module/fetch_if_test.rb
170
+ - test/module/json_test.rb
171
+ - test/module/module_test.rb
172
+ - test/module/parse_test.rb
173
+ - test/module/process_test.rb
174
+ - test/module/simple_test.rb
155
175
  - test/test_helper.rb
@@ -1,677 +0,0 @@
1
- require "test_helper"
2
-
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_sends_fetchable_to_modules
71
- stub_request(:get, "https://api.github.com/users/lassebunk").to_return(body: "id: 1234")
72
- actions = []
73
- mod = Class.new(Fetch::Module) do
74
- request do |req|
75
- req.url = "https://api.github.com/users/#{fetchable.login}"
76
- req.process do |body|
77
- actions << "process: #{body}"
78
- end
79
- end
80
- end
81
- user = OpenStruct.new(login: "lassebunk")
82
- MockFetcher(mod).new(user).fetch
83
- assert_equal ["process: id: 1234"], actions
84
- end
85
-
86
- def test_process_block_scope
87
- stub_request(:get, "http://test.com/one").to_return(body: "got one")
88
- actions = []
89
- mod = Class.new(Fetch::Module) do
90
- request do |req|
91
- req.url = "http://test.com/one"
92
- req.process do |body|
93
- actions << "body: #{body} (#{some_instance_method})"
94
- end
95
- end
96
-
97
- def some_instance_method
98
- "it worked"
99
- end
100
- end
101
- MockFetcher(mod).new.fetch
102
- assert_equal ["body: got one (it worked)"], actions
103
- end
104
-
105
- def test_before_process_callback_set_in_request
106
- words = %w{one two}
107
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
108
-
109
- stub_request(:get, "http://test.com/two").to_return(body: "got two")
110
- actions = []
111
- mod = Class.new(Fetch::Module) do
112
- words.each do |word|
113
- request do |req|
114
- req.url = "http://test.com/#{word}"
115
- req.before_process do
116
- actions << "before process #{word}"
117
- end
118
- req.process do |body|
119
- actions << "process #{word}"
120
- end
121
- end
122
- end
123
- end
124
- MockFetcher(mod).new.fetch
125
- assert_equal ["before process one", "process one", "before process two", "process two"], actions
126
- end
127
-
128
- def test_before_process_callback_scope_set_in_request
129
- words = %w{one two}
130
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
131
-
132
- stub_request(:get, "http://test.com/two").to_return(body: "got two")
133
- actions = []
134
- mod = Class.new(Fetch::Module) do
135
- words.each do |word|
136
- request do |req|
137
- req.url = "http://test.com/#{word}"
138
- req.before_process do
139
- actions << "before process #{word} (#{some_instance_method})"
140
- end
141
- req.process do |body|
142
- actions << "process #{word}"
143
- end
144
- end
145
- end
146
- def some_instance_method
147
- "ok"
148
- end
149
- end
150
- MockFetcher(mod).new.fetch
151
- assert_equal ["before process one (ok)", "process one", "before process two (ok)", "process two"], actions
152
- end
153
-
154
- def test_before_process_callback_set_in_module
155
- words = %w{one two}
156
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
157
-
158
- stub_request(:get, "http://test.com/two").to_return(body: "got two")
159
- actions = []
160
- mod = Class.new(Fetch::Module) do
161
- words.each do |word|
162
- request do |req|
163
- req.url = "http://test.com/#{word}"
164
- req.process do |body|
165
- actions << "process #{word}"
166
- end
167
- end
168
- end
169
-
170
- before_process do
171
- actions << "before process"
172
- end
173
- end
174
- MockFetcher(mod).new.fetch
175
- assert_equal ["before process", "process one", "before process", "process two"], actions
176
- end
177
-
178
- def test_before_process_callback_scope_set_in_module
179
- words = %w{one two}
180
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
181
-
182
- stub_request(:get, "http://test.com/two").to_return(body: "got two")
183
- actions = []
184
- mod = Class.new(Fetch::Module) do
185
- words.each do |word|
186
- request do |req|
187
- req.url = "http://test.com/#{word}"
188
- req.process do |body|
189
- actions << "process #{word}"
190
- end
191
- end
192
- end
193
-
194
- before_process do
195
- actions << "before process (#{some_instance_method})"
196
- end
197
-
198
- def some_instance_method
199
- "ok"
200
- end
201
- end
202
- MockFetcher(mod).new.fetch
203
- assert_equal ["before process (ok)", "process one", "before process (ok)", "process two"], actions
204
- end
205
-
206
- def test_after_process_callback_set_in_request
207
- words = %w{one two}
208
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
209
-
210
- stub_request(:get, "http://test.com/two").to_return(body: "got two")
211
- actions = []
212
- mod = Class.new(Fetch::Module) do
213
- words.each do |word|
214
- request do |req|
215
- req.url = "http://test.com/#{word}"
216
- req.after_process do
217
- actions << "after process #{word}"
218
- end
219
- req.process do |body|
220
- actions << "process #{word}"
221
- end
222
- end
223
- end
224
- end
225
- MockFetcher(mod).new.fetch
226
- assert_equal ["process one", "after process one", "process two", "after process two"], actions
227
- end
228
-
229
- def test_after_process_callback_scope_set_in_request
230
- words = %w{one two}
231
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
232
-
233
- stub_request(:get, "http://test.com/two").to_return(body: "got two")
234
- actions = []
235
- mod = Class.new(Fetch::Module) do
236
- words.each do |word|
237
- request do |req|
238
- req.url = "http://test.com/#{word}"
239
- req.after_process do
240
- actions << "after process #{word} (#{some_instance_method})"
241
- end
242
- req.process do |body|
243
- actions << "process #{word}"
244
- end
245
- end
246
- end
247
- def some_instance_method
248
- "ok"
249
- end
250
- end
251
- MockFetcher(mod).new.fetch
252
- assert_equal ["process one", "after process one (ok)", "process two", "after process two (ok)"], actions
253
- end
254
-
255
- def test_after_process_callback_set_in_module
256
- words = %w{one two}
257
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
258
-
259
- stub_request(:get, "http://test.com/two").to_return(body: "got two")
260
- actions = []
261
- mod = Class.new(Fetch::Module) do
262
- words.each do |word|
263
- request do |req|
264
- req.url = "http://test.com/#{word}"
265
- req.process do |body|
266
- actions << "process #{word}"
267
- end
268
- end
269
- end
270
-
271
- after_process do
272
- actions << "after process"
273
- end
274
- end
275
- MockFetcher(mod).new.fetch
276
- assert_equal ["process one", "after process", "process two", "after process"], actions
277
- end
278
-
279
- def test_after_process_callback_scope_set_in_module
280
- words = %w{one two}
281
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
282
-
283
- stub_request(:get, "http://test.com/two").to_return(body: "got two")
284
- actions = []
285
- mod = Class.new(Fetch::Module) do
286
- words.each do |word|
287
- request do |req|
288
- req.url = "http://test.com/#{word}"
289
- req.process do |body|
290
- actions << "process #{word}"
291
- end
292
- end
293
- end
294
-
295
- after_process do
296
- actions << "after process (#{some_instance_method})"
297
- end
298
-
299
- def some_instance_method
300
- "ok"
301
- end
302
- end
303
- MockFetcher(mod).new.fetch
304
- assert_equal ["process one", "after process (ok)", "process two", "after process (ok)"], actions
305
- end
306
-
307
- def test_positive_fetch_if_filter
308
- stub_request(:get, "http://test.com/one").to_return(body: "got one")
309
- actions = []
310
- mod = Class.new(Fetch::Module) do
311
- fetch_if { true }
312
-
313
- request do |req|
314
- req.url = "http://test.com/one"
315
- req.process do |body|
316
- actions << "body: #{body}"
317
- end
318
- end
319
- end
320
- MockFetcher(mod).new.fetch
321
- assert_equal ["body: got one"], actions
322
- end
323
-
324
- def test_negative_fetch_if_filter
325
- stub_request(:get, "http://test.com/one").to_return(body: "got one")
326
- actions = []
327
- mod = Class.new(Fetch::Module) do
328
- fetch_if { false }
329
-
330
- request do |req|
331
- req.url = "http://test.com/one"
332
- req.process do |body|
333
- actions << "body: #{body}"
334
- end
335
- end
336
- end
337
- MockFetcher(mod).new.fetch
338
- assert_equal [], actions
339
- end
340
-
341
- def test_nil_fetch_if_filter
342
- stub_request(:get, "http://test.com/one").to_return(body: "got one")
343
- actions = []
344
- mod = Class.new(Fetch::Module) do
345
- fetch_if { nil }
346
-
347
- request do |req|
348
- req.url = "http://test.com/one"
349
- req.process do |body|
350
- actions << "body: #{body}"
351
- end
352
- end
353
- end
354
- MockFetcher(mod).new.fetch
355
- assert_equal [], actions
356
- end
357
-
358
- def test_if_filter_scope
359
- stub_request(:get, "http://test.com/one").to_return(body: "got one")
360
- actions = []
361
- mod = Class.new(Fetch::Module) do
362
- fetch_if { should_i_fetch? }
363
-
364
- request do |req|
365
- req.url = "http://test.com/one"
366
- req.process do |body|
367
- actions << "body: #{body}"
368
- end
369
- end
370
-
371
- def should_i_fetch?
372
- true
373
- end
374
- end
375
- MockFetcher(mod).new.fetch
376
- assert_equal ["body: got one"], actions
377
- end
378
-
379
- def test_multiple_requests
380
- words = %w{one two three}
381
- words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
382
- actions = []
383
-
384
- mod = Class.new(Fetch::Module) do
385
- words.each do |w|
386
- request do |req|
387
- req.url = "http://test.com/#{w}"
388
- req.process do |body|
389
- actions << "body: #{body}"
390
- end
391
- end
392
- end
393
- end
394
- MockFetcher(mod).new.fetch
395
- assert_equal ["body: got one", "body: got two", "body: got three"], actions
396
- end
397
-
398
- def test_unhandled_http_failure
399
- stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
400
- actions = []
401
- mod = Class.new(Fetch::Module) do
402
- request do |req|
403
- req.url = "http://test.com/one"
404
- req.process do |body|
405
- actions << "body: #{body}"
406
- end
407
- end
408
- end
409
- assert_equal [], actions
410
- end
411
-
412
- def test_http_failure_handled_in_request
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.failure do |code, url|
419
- actions << "handled error #{code} from #{url}"
420
- end
421
- req.process do |body|
422
- actions << "body: #{body}"
423
- end
424
- end
425
- end
426
- MockFetcher(mod).new.fetch
427
- assert_equal ["handled error 500 from http://test.com/one"], actions
428
- end
429
-
430
- def test_http_failure_scope_handled_in_request
431
- stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
432
- actions = []
433
- mod = Class.new(Fetch::Module) do
434
- request do |req|
435
- req.url = "http://test.com/one"
436
- req.failure do |code, url|
437
- actions << "handled error #{code} from #{url} (#{some_instance_method})"
438
- end
439
- req.process do |body|
440
- actions << "body: #{body}"
441
- end
442
- end
443
-
444
- def some_instance_method
445
- "it worked"
446
- end
447
- end
448
- MockFetcher(mod).new.fetch
449
- assert_equal ["handled error 500 from http://test.com/one (it worked)"], actions
450
- end
451
-
452
- def test_http_failure_handled_in_module
453
- stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
454
- actions = []
455
- mod = Class.new(Fetch::Module) do
456
- request do |req|
457
- req.url = "http://test.com/one"
458
- req.process do |body|
459
- actions << "body: #{body}"
460
- end
461
- end
462
- failure do |code, url|
463
- actions << "handled error #{code} from #{url}"
464
- end
465
- end
466
- MockFetcher(mod).new.fetch
467
- assert_equal ["handled error 500 from http://test.com/one"], actions
468
- end
469
-
470
- def test_http_failure_scope_handled_in_module
471
- stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
472
- actions = []
473
- mod = Class.new(Fetch::Module) do
474
- request do |req|
475
- req.url = "http://test.com/one"
476
- req.process do |body|
477
- actions << "body: #{body}"
478
- end
479
- end
480
- failure do |code, url|
481
- actions << "handled error #{code} from #{url} (#{some_instance_method})"
482
- end
483
- def some_instance_method
484
- "it worked"
485
- end
486
- end
487
- MockFetcher(mod).new.fetch
488
- assert_equal ["handled error 500 from http://test.com/one (it worked)"], actions
489
- end
490
-
491
- def test_unhandled_process_error
492
- stub_request(:get, "http://test.com/one").to_return(body: "ok")
493
- actions = []
494
- mod = Class.new(Fetch::Module) do
495
- request do |req|
496
- req.url = "http://test.com/one"
497
- req.process do |body|
498
- this_wont_work
499
- end
500
- end
501
- end
502
- assert_raises NameError do
503
- MockFetcher(mod).new.fetch
504
- end
505
- end
506
-
507
- def test_process_error_handled_in_request
508
- stub_request(:get, "http://test.com/one").to_return(body: "ok")
509
- actions = []
510
- mod = Class.new(Fetch::Module) do
511
- request do |req|
512
- req.url = "http://test.com/one"
513
- req.error do |e|
514
- actions << "handled #{e.class.name}"
515
- end
516
- req.process do |body|
517
- this_wont_work
518
- end
519
- end
520
- end
521
- MockFetcher(mod).new.fetch
522
- assert_equal ["handled NameError"], actions
523
- end
524
-
525
- def test_process_error_scope_handled_in_request
526
- stub_request(:get, "http://test.com/one").to_return(body: "ok")
527
- actions = []
528
- mod = Class.new(Fetch::Module) do
529
- request do |req|
530
- req.url = "http://test.com/one"
531
- req.error do |e|
532
- actions << "handled #{e.class.name} (#{some_instance_method})"
533
- end
534
- req.process do |body|
535
- this_wont_work
536
- end
537
- end
538
-
539
- def some_instance_method
540
- "it worked"
541
- end
542
- end
543
- MockFetcher(mod).new.fetch
544
- assert_equal ["handled NameError (it worked)"], actions
545
- end
546
-
547
- def test_process_error_handled_in_module
548
- stub_request(:get, "http://test.com/one").to_return(body: "ok")
549
- actions = []
550
- mod = Class.new(Fetch::Module) do
551
- request do |req|
552
- req.url = "http://test.com/one"
553
- req.process do |body|
554
- this_wont_work
555
- end
556
- end
557
- error do |e|
558
- actions << "handled #{e.class.name}"
559
- end
560
- end
561
- MockFetcher(mod).new.fetch
562
- assert_equal ["handled NameError"], actions
563
- end
564
-
565
- def test_process_error_scope_handled_in_module
566
- stub_request(:get, "http://test.com/one").to_return(body: "ok")
567
- actions = []
568
- mod = Class.new(Fetch::Module) do
569
- request do |req|
570
- req.url = "http://test.com/one"
571
- req.process do |body|
572
- this_wont_work
573
- end
574
- end
575
- error do |e|
576
- actions << "handled #{e.class.name} (#{some_instance_method})"
577
- end
578
- def some_instance_method
579
- "it worked"
580
- end
581
- end
582
- MockFetcher(mod).new.fetch
583
- assert_equal ["handled NameError (it worked)"], actions
584
- end
585
-
586
- def test_progress_with_single_module
587
- stub_request(:get, "http://test.com/one").to_return(body: "got one")
588
-
589
- mod = Class.new(Fetch::Module) do
590
- 3.times do
591
- request do |req|
592
- req.url = "http://test.com/one"
593
- end
594
- end
595
- end
596
-
597
- updates = []
598
-
599
- klass = Class.new(MockFetcher(mod)) do
600
- progress do |percent|
601
- updates << percent
602
- end
603
- end
604
-
605
- klass.new.fetch
606
- assert_equal [0, 33, 66, 100], updates
607
- end
608
-
609
- def test_progress_with_multiple_modules
610
- stub_request(:get, "http://test.com/one").to_return(body: "got one")
611
-
612
- mods = 3.times.map do
613
- Class.new(Fetch::Module) do
614
- 2.times do
615
- request do |req|
616
- req.url = "http://test.com/one"
617
- end
618
- end
619
- end
620
- end
621
-
622
- updates = []
623
-
624
- klass = Class.new(MockFetcher(mods)) do
625
- progress do |percent|
626
- updates << percent
627
- end
628
- end
629
-
630
- klass.new.fetch
631
- assert_equal [0, 16, 33, 50, 66, 83, 100], updates
632
- end
633
-
634
- def test_progress_with_http_failure
635
- stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
636
- updates = []
637
- mods = 3.times.map do
638
- Class.new(Fetch::Module) do
639
- request do |req|
640
- req.url = "http://test.com/one"
641
- end
642
- end
643
- end
644
- klass = Class.new(MockFetcher(mods)) do
645
- progress do |percent|
646
- updates << percent
647
- end
648
- end
649
-
650
- klass.new.fetch
651
- assert_equal [0, 33, 66, 100], updates
652
- end
653
-
654
- def test_progress_with_handled_process_error
655
- stub_request(:get, "http://test.com/one").to_return(body: "ok")
656
- updates = []
657
- mods = 3.times.map do
658
- Class.new(Fetch::Module) do
659
- request do |req|
660
- req.url = "http://test.com/one"
661
- req.process do |body|
662
- wont_work
663
- end
664
- req.error { }
665
- end
666
- end
667
- end
668
- klass = Class.new(MockFetcher(mods)) do
669
- progress do |percent|
670
- updates << percent
671
- end
672
- end
673
-
674
- klass.new.fetch
675
- assert_equal [0, 33, 66, 100], updates
676
- end
677
- end