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,104 @@
1
+ require "test_helper"
2
+
3
+ class BeforeProcessTest < Minitest::Test
4
+ def test_before_process_callback_set_in_request
5
+ words = %w{one two}
6
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
7
+
8
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
9
+ actions = []
10
+ mod = Class.new(Fetch::Module) do
11
+ words.each do |word|
12
+ request do |req|
13
+ req.url = "http://test.com/#{word}"
14
+ req.before_process do
15
+ actions << "before process #{word}"
16
+ end
17
+ req.process do |body|
18
+ actions << "process #{word}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ MockFetcher(mod).new.fetch
24
+ assert_equal ["before process one", "process one", "before process two", "process two"], actions
25
+ end
26
+
27
+ def test_before_process_callback_scope_set_in_request
28
+ words = %w{one two}
29
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
30
+
31
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
32
+ actions = []
33
+ mod = Class.new(Fetch::Module) do
34
+ words.each do |word|
35
+ request do |req|
36
+ req.url = "http://test.com/#{word}"
37
+ req.before_process do
38
+ actions << "before process #{word} (#{some_instance_method})"
39
+ end
40
+ req.process do |body|
41
+ actions << "process #{word}"
42
+ end
43
+ end
44
+ end
45
+ def some_instance_method
46
+ "ok"
47
+ end
48
+ end
49
+ MockFetcher(mod).new.fetch
50
+ assert_equal ["before process one (ok)", "process one", "before process two (ok)", "process two"], actions
51
+ end
52
+
53
+ def test_before_process_callback_set_in_module
54
+ words = %w{one two}
55
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
56
+
57
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
58
+ actions = []
59
+ mod = Class.new(Fetch::Module) do
60
+ words.each do |word|
61
+ request do |req|
62
+ req.url = "http://test.com/#{word}"
63
+ req.process do |body|
64
+ actions << "process #{word}"
65
+ end
66
+ end
67
+ end
68
+
69
+ before_process do
70
+ actions << "before process"
71
+ end
72
+ end
73
+ MockFetcher(mod).new.fetch
74
+ assert_equal ["before process", "process one", "before process", "process two"], actions
75
+ end
76
+
77
+ def test_before_process_callback_scope_set_in_module
78
+ words = %w{one two}
79
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
80
+
81
+ stub_request(:get, "http://test.com/two").to_return(body: "got two")
82
+ actions = []
83
+ mod = Class.new(Fetch::Module) do
84
+ words.each do |word|
85
+ request do |req|
86
+ req.url = "http://test.com/#{word}"
87
+ req.process do |body|
88
+ actions << "process #{word}"
89
+ end
90
+ end
91
+ end
92
+
93
+ before_process do
94
+ actions << "before process (#{some_instance_method})"
95
+ end
96
+
97
+ def some_instance_method
98
+ "ok"
99
+ end
100
+ end
101
+ MockFetcher(mod).new.fetch
102
+ assert_equal ["before process (ok)", "process one", "before process (ok)", "process two"], actions
103
+ end
104
+ end
@@ -0,0 +1,98 @@
1
+ require "test_helper"
2
+
3
+ class ErrorTest < Minitest::Test
4
+ def test_unhandled_process_error
5
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
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
+ this_wont_work
12
+ end
13
+ end
14
+ end
15
+ assert_raises NameError do
16
+ MockFetcher(mod).new.fetch
17
+ end
18
+ end
19
+
20
+ def test_process_error_handled_in_request
21
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
22
+ actions = []
23
+ mod = Class.new(Fetch::Module) do
24
+ request do |req|
25
+ req.url = "http://test.com/one"
26
+ req.error do |e|
27
+ actions << "handled #{e.class.name}"
28
+ end
29
+ req.process do |body|
30
+ this_wont_work
31
+ end
32
+ end
33
+ end
34
+ MockFetcher(mod).new.fetch
35
+ assert_equal ["handled NameError"], actions
36
+ end
37
+
38
+ def test_process_error_scope_handled_in_request
39
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
40
+ actions = []
41
+ mod = Class.new(Fetch::Module) do
42
+ request do |req|
43
+ req.url = "http://test.com/one"
44
+ req.error do |e|
45
+ actions << "handled #{e.class.name} (#{some_instance_method})"
46
+ end
47
+ req.process do |body|
48
+ this_wont_work
49
+ end
50
+ end
51
+
52
+ def some_instance_method
53
+ "it worked"
54
+ end
55
+ end
56
+ MockFetcher(mod).new.fetch
57
+ assert_equal ["handled NameError (it worked)"], actions
58
+ end
59
+
60
+ def test_process_error_handled_in_module
61
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
62
+ actions = []
63
+ mod = Class.new(Fetch::Module) do
64
+ request do |req|
65
+ req.url = "http://test.com/one"
66
+ req.process do |body|
67
+ this_wont_work
68
+ end
69
+ end
70
+ error do |e|
71
+ actions << "handled #{e.class.name}"
72
+ end
73
+ end
74
+ MockFetcher(mod).new.fetch
75
+ assert_equal ["handled NameError"], actions
76
+ end
77
+
78
+ def test_process_error_scope_handled_in_module
79
+ stub_request(:get, "http://test.com/one").to_return(body: "ok")
80
+ actions = []
81
+ mod = Class.new(Fetch::Module) do
82
+ request do |req|
83
+ req.url = "http://test.com/one"
84
+ req.process do |body|
85
+ this_wont_work
86
+ end
87
+ end
88
+ error do |e|
89
+ actions << "handled #{e.class.name} (#{some_instance_method})"
90
+ end
91
+ def some_instance_method
92
+ "it worked"
93
+ end
94
+ end
95
+ MockFetcher(mod).new.fetch
96
+ assert_equal ["handled NameError (it worked)"], actions
97
+ end
98
+ end
@@ -0,0 +1,96 @@
1
+ require "test_helper"
2
+
3
+ class FailureTest < Minitest::Test
4
+ def test_unhandled_http_failure
5
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
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
+ assert_equal [], actions
16
+ end
17
+
18
+ def test_http_failure_handled_in_request
19
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
20
+ actions = []
21
+ mod = Class.new(Fetch::Module) do
22
+ request do |req|
23
+ req.url = "http://test.com/one"
24
+ req.failure do |code, url|
25
+ actions << "handled error #{code} from #{url}"
26
+ end
27
+ req.process do |body|
28
+ actions << "body: #{body}"
29
+ end
30
+ end
31
+ end
32
+ MockFetcher(mod).new.fetch
33
+ assert_equal ["handled error 500 from http://test.com/one"], actions
34
+ end
35
+
36
+ def test_http_failure_scope_handled_in_request
37
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
38
+ actions = []
39
+ mod = Class.new(Fetch::Module) do
40
+ request do |req|
41
+ req.url = "http://test.com/one"
42
+ req.failure do |code, url|
43
+ actions << "handled error #{code} from #{url} (#{some_instance_method})"
44
+ end
45
+ req.process do |body|
46
+ actions << "body: #{body}"
47
+ end
48
+ end
49
+
50
+ def some_instance_method
51
+ "it worked"
52
+ end
53
+ end
54
+ MockFetcher(mod).new.fetch
55
+ assert_equal ["handled error 500 from http://test.com/one (it worked)"], actions
56
+ end
57
+
58
+ def test_http_failure_handled_in_module
59
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
60
+ actions = []
61
+ mod = Class.new(Fetch::Module) do
62
+ request do |req|
63
+ req.url = "http://test.com/one"
64
+ req.process do |body|
65
+ actions << "body: #{body}"
66
+ end
67
+ end
68
+ failure do |code, url|
69
+ actions << "handled error #{code} from #{url}"
70
+ end
71
+ end
72
+ MockFetcher(mod).new.fetch
73
+ assert_equal ["handled error 500 from http://test.com/one"], actions
74
+ end
75
+
76
+ def test_http_failure_scope_handled_in_module
77
+ stub_request(:get, "http://test.com/one").to_return(body: "something went wrong", status: 500)
78
+ actions = []
79
+ mod = Class.new(Fetch::Module) do
80
+ request do |req|
81
+ req.url = "http://test.com/one"
82
+ req.process do |body|
83
+ actions << "body: #{body}"
84
+ end
85
+ end
86
+ failure do |code, url|
87
+ actions << "handled error #{code} from #{url} (#{some_instance_method})"
88
+ end
89
+ def some_instance_method
90
+ "it worked"
91
+ end
92
+ end
93
+ MockFetcher(mod).new.fetch
94
+ assert_equal ["handled error 500 from http://test.com/one (it worked)"], actions
95
+ end
96
+ end
@@ -0,0 +1,75 @@
1
+ require "test_helper"
2
+
3
+ class FetchIfTest < Minitest::Test
4
+ def test_positive_fetch_if_filter
5
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
6
+ actions = []
7
+ mod = Class.new(Fetch::Module) do
8
+ fetch_if { true }
9
+
10
+ request do |req|
11
+ req.url = "http://test.com/one"
12
+ req.process do |body|
13
+ actions << "body: #{body}"
14
+ end
15
+ end
16
+ end
17
+ MockFetcher(mod).new.fetch
18
+ assert_equal ["body: got one"], actions
19
+ end
20
+
21
+ def test_negative_fetch_if_filter
22
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
23
+ actions = []
24
+ mod = Class.new(Fetch::Module) do
25
+ fetch_if { false }
26
+
27
+ request do |req|
28
+ req.url = "http://test.com/one"
29
+ req.process do |body|
30
+ actions << "body: #{body}"
31
+ end
32
+ end
33
+ end
34
+ MockFetcher(mod).new.fetch
35
+ assert_equal [], actions
36
+ end
37
+
38
+ def test_nil_fetch_if_filter
39
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
40
+ actions = []
41
+ mod = Class.new(Fetch::Module) do
42
+ fetch_if { nil }
43
+
44
+ request do |req|
45
+ req.url = "http://test.com/one"
46
+ req.process do |body|
47
+ actions << "body: #{body}"
48
+ end
49
+ end
50
+ end
51
+ MockFetcher(mod).new.fetch
52
+ assert_equal [], actions
53
+ end
54
+
55
+ def test_fetch_if_scope
56
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
57
+ actions = []
58
+ mod = Class.new(Fetch::Module) do
59
+ fetch_if { should_i_fetch? }
60
+
61
+ request do |req|
62
+ req.url = "http://test.com/one"
63
+ req.process do |body|
64
+ actions << "body: #{body}"
65
+ end
66
+ end
67
+
68
+ def should_i_fetch?
69
+ true
70
+ end
71
+ end
72
+ MockFetcher(mod).new.fetch
73
+ assert_equal ["body: got one"], actions
74
+ end
75
+ end
@@ -0,0 +1,88 @@
1
+ require "test_helper"
2
+
3
+ class ModuleTest < 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_empty_url
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_multiple_requests
71
+ words = %w{one two three}
72
+ words.each { |w| stub_request(:get, "http://test.com/#{w}").to_return(body: "got #{w}") }
73
+ actions = []
74
+
75
+ mod = Class.new(Fetch::Module) do
76
+ words.each do |w|
77
+ request do |req|
78
+ req.url = "http://test.com/#{w}"
79
+ req.process do |body|
80
+ actions << "body: #{body}"
81
+ end
82
+ end
83
+ end
84
+ end
85
+ MockFetcher(mod).new.fetch
86
+ assert_equal ["body: got one", "body: got two", "body: got three"], actions
87
+ end
88
+ end