44require 'ostruct'
55
66class HttpPullInputTest < Test ::Unit ::TestCase
7+ @stub_server = nil
8+
79 setup do
810 Fluent ::Test . setup
11+
12+ @stub_server = StubServer . new
13+ @stub_server . start
14+ end
15+
16+ teardown do
17+ @stub_server . shutdown
918 end
1019
1120 sub_test_case "default value of each options" do
1221 TEST_DEFAULT_VALUE_CONFIG = %[
1322 tag test
14- url http://127.0.0.1
23+ url http://127.0.0.1:3939
1524
1625 interval 3s
1726 format json
@@ -32,11 +41,10 @@ class HttpPullInputTest < Test::Unit::TestCase
3241 end
3342 end
3443
35- sub_test_case "success case with status only " do
44+ sub_test_case "success case" do
3645 TEST_INTERVAL_3_CONFIG = %[
3746 tag test
38- url http://127.0.0.1
39- timeout 10
47+ url http://127.0.0.1:3939
4048
4149 interval 3s
4250 format none
@@ -45,37 +53,34 @@ class HttpPullInputTest < Test::Unit::TestCase
4553
4654 TEST_INTERVAL_5_CONFIG = %[
4755 tag test
48- url http://127.0.0.1
49- timeout 10
56+ url http://127.0.0.1:3939
5057
5158 interval 5s
5259 format json
5360 ]
5461
55- setup do
56- mock ( RestClient ::Request ) .
57- execute ( method : :get ,
58- url : "http://127.0.0.1" ,
59- timeout : 10 ) .
60- times ( 2 ) do
61- OpenStruct . new ( { code : 200 , body : '{"status": "OK"}' } )
62- end
63- end
62+ TEST_INTERVAL_3_REDIRECT_CONFIG = %[
63+ tag test
64+ url http://127.0.0.1:3939/redirect
65+
66+ interval 3s
67+ format json
68+ ]
6469
6570 test 'interval 3 with status_only' do
6671 d = create_driver TEST_INTERVAL_3_CONFIG
6772 assert_equal ( "test" , d . instance . tag )
6873 assert_equal ( 3 , d . instance . interval )
6974
70- d . run ( timeout : 5 ) do
75+ d . run ( timeout : 8 ) do
7176 sleep 7
7277 end
7378 assert_equal ( 2 , d . events . size )
7479
7580 d . events . each do |tag , time , record |
7681 assert_equal ( "test" , tag )
7782
78- assert_equal ( { "url" => "http://127.0.0.1" , "status" => 200 } , record )
83+ assert_equal ( { "url" => "http://127.0.0.1:3939 " , "status" => 200 } , record )
7984 assert ( time . is_a? ( Fluent ::EventTime ) )
8085 end
8186 end
@@ -85,20 +90,100 @@ class HttpPullInputTest < Test::Unit::TestCase
8590 assert_equal ( "test" , d . instance . tag )
8691 assert_equal ( 5 , d . instance . interval )
8792
88- d . run ( timeout : 7 ) do
93+ d . run ( timeout : 12 ) do
8994 sleep 11
9095 end
9196 assert_equal ( 2 , d . events . size )
9297
9398 d . events . each do |tag , time , record |
9499 assert_equal ( "test" , tag )
95100
96- assert_equal ( { "url" => "http://127.0.0.1" , "status" => 200 , "message" => { "status" => "OK" } } , record )
101+ assert_equal ( { "url" => "http://127.0.0.1:3939" , "status" => 200 , "message" => { "status" => "OK" } } , record )
102+ assert ( time . is_a? ( Fluent ::EventTime ) )
103+ end
104+ end
105+
106+ test 'interval 3 with redirect' do
107+ d = create_driver TEST_INTERVAL_3_REDIRECT_CONFIG
108+ assert_equal ( "test" , d . instance . tag )
109+ assert_equal ( 3 , d . instance . interval )
110+
111+ d . run ( timeout : 8 ) do
112+ sleep 7
113+ end
114+ assert_equal ( 2 , d . events . size )
115+
116+ d . events . each do |tag , time , record |
117+ assert_equal ( "test" , tag )
118+
119+ assert_equal ( { "url" => "http://127.0.0.1:3939/redirect" , "status" => 200 , "message" => { "status" => "OK" } } , record )
97120 assert ( time . is_a? ( Fluent ::EventTime ) )
98121 end
99122 end
100123 end
101124
125+ sub_test_case "fail when not 200 OK" do
126+ TEST_404_INTERVAL_3_CONFIG = %[
127+ tag test
128+ url http://127.0.0.1:3939/not_exist
129+
130+ interval 3s
131+ format none
132+ status_only true
133+ ]
134+
135+ TEST_500_INTERVAL_3_CONFIG = %[
136+ tag test
137+ url http://127.0.0.1:3939/internal_error
138+
139+ interval 3s
140+ format none
141+ status_only true
142+ ]
143+
144+ test '404' do
145+ d = create_driver TEST_404_INTERVAL_3_CONFIG
146+ assert_equal ( "test" , d . instance . tag )
147+ assert_equal ( 3 , d . instance . interval )
148+
149+ d . run ( timeout : 8 ) do
150+ sleep 7
151+ end
152+ assert_equal ( 2 , d . events . size )
153+
154+ d . events . each do |tag , time , record |
155+ assert_equal ( "test" , tag )
156+
157+ assert_equal ( "http://127.0.0.1:3939/not_exist" , record [ "url" ] )
158+ assert ( time . is_a? ( Fluent ::EventTime ) )
159+
160+ assert_equal ( 404 , record [ "status" ] )
161+ assert_not_nil ( record [ "error" ] )
162+ end
163+ end
164+
165+ test '500' do
166+ d = create_driver TEST_500_INTERVAL_3_CONFIG
167+ assert_equal ( "test" , d . instance . tag )
168+ assert_equal ( 3 , d . instance . interval )
169+
170+ d . run ( timeout : 8 ) do
171+ sleep 7
172+ end
173+ assert_equal ( 2 , d . events . size )
174+
175+ d . events . each do |tag , time , record |
176+ assert_equal ( "test" , tag )
177+
178+ assert_equal ( "http://127.0.0.1:3939/internal_error" , record [ "url" ] )
179+ assert ( time . is_a? ( Fluent ::EventTime ) )
180+
181+ assert_equal ( 500 , record [ "status" ] )
182+ assert_not_nil ( record [ "error" ] )
183+ end
184+ end
185+ end
186+
102187 sub_test_case "fail when remote down" do
103188 TEST_REFUSED_CONFIG = %[
104189 tag test
@@ -111,7 +196,7 @@ class HttpPullInputTest < Test::Unit::TestCase
111196 d = create_driver TEST_REFUSED_CONFIG
112197 assert_equal ( "test" , d . instance . tag )
113198
114- d . run ( timeout : 2 ) do
199+ d . run ( timeout : 4 ) do
115200 sleep 3
116201 end
117202
@@ -131,38 +216,27 @@ class HttpPullInputTest < Test::Unit::TestCase
131216 sub_test_case "fail when remote timeout" do
132217 TEST_TIMEOUT_FAIL_CONFIG = %[
133218 tag test
134- url http://127.0.0.1
219+ url http://127.0.0.1:3939/timeout
135220 timeout 2s
136221
137222 interval 3s
138223 format json
139224 ]
140225
141- setup do
142- mock ( RestClient ::Request ) .
143- execute ( method : :get ,
144- url : "http://127.0.0.1" ,
145- timeout : 2 ) .
146- times ( 2 ) do
147- sleep 2
148- raise RestClient ::Exceptions ::Timeout . new
149- end
150- end
151-
152226 test "timeout" do
153227 d = create_driver TEST_TIMEOUT_FAIL_CONFIG
154228 assert_equal ( "test" , d . instance . tag )
155229 assert_equal ( 2 , d . instance . timeout )
156230
157- d . run ( timeout : 5 ) do
231+ d . run ( timeout : 8 ) do
158232 sleep 7
159233 end
160234 assert_equal ( 2 , d . events . size )
161235
162236 d . events . each do |tag , time , record |
163237 assert_equal ( "test" , tag )
164238
165- assert_equal ( "http://127.0.0.1" , record [ "url" ] )
239+ assert_equal ( "http://127.0.0.1:3939/timeout " , record [ "url" ] )
166240 assert ( time . is_a? ( Fluent ::EventTime ) )
167241
168242 assert_equal ( 0 , record [ "status" ] )
0 commit comments