more tests for task, real valid? method for task
[god.git] / test / test_conditions_http_response_code.rb
blobe0c9b44f802c71213cee0cc64f82faf4dd81ee75
1 require File.dirname(__FILE__) + '/helper'
3 class TestHttpResponseCode < Test::Unit::TestCase
4   def valid_condition
5     c = Conditions::HttpResponseCode.new()
6     c.watch = stub(:name => 'foo')
7     c.host = 'localhost'
8     c.port = 8080
9     c.path = '/'
10     c.timeout = 10
11     c.code_is = 200
12     c.times = 1
13     yield(c) if block_given?
14     c.prepare
15     c
16   end
17   
18   # valid?
19   
20   def test_valid_condition_is_valid
21     c = valid_condition
22     assert c.valid?
23   end
24   
25   def test_valid_should_return_false_if_both_code_is_and_code_is_not_are_set
26     c = valid_condition do |cc|
27       cc.code_is_not = 500
28     end
29     no_stdout { assert !c.valid? }
30   end
31   
32   def test_valid_should_return_false_if_no_host_set
33     c = valid_condition do |cc|
34       cc.host = nil
35     end
36     no_stdout { assert !c.valid? }
37   end
38   
39   def test_valid_should_return_false_if_no_port_set
40     c = valid_condition do |cc|
41       cc.port = nil
42     end
43     no_stdout { assert !c.valid? }
44   end
45   
46   def test_valid_should_return_false_if_no_path_set
47     c = valid_condition do |cc|
48       cc.path = nil
49     end
50     no_stdout { assert !c.valid? }
51   end
52   
53   def test_valid_should_return_false_if_no_timeout_set
54     c = valid_condition do |cc|
55       cc.timeout = nil
56     end
57     no_stdout { assert !c.valid? }
58   end
59   
60   # test
61   
62   def test_test_should_return_false_if_code_is_is_set_to_200_but_response_is_500
63     c = valid_condition
64     Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 500)))
65     assert_equal false, c.test
66   end
67   
68   def test_test_should_return_false_if_code_is_not_is_set_to_200_and_response_is_200
69     c = valid_condition do |cc|
70       cc.code_is = nil
71       cc.code_is_not = [200]
72     end
73     Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 200)))
74     assert_equal false, c.test
75   end
76   
77   def test_test_should_return_true_if_code_is_is_set_to_200_and_response_is_200
78     c = valid_condition
79     Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 200)))
80     assert_equal true, c.test
81   end
82   
83   def test_test_should_return_false_if_code_is_not_is_set_to_200_but_response_is_500
84     c = valid_condition do |cc|
85       cc.code_is = nil
86       cc.code_is_not = [200]
87     end
88     Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 500)))
89     assert_equal true, c.test
90   end
91   
92   def test_test_should_return_false_if_code_is_is_set_to_200_but_response_times_out
93     c = valid_condition
94     Net::HTTP.expects(:start).raises(Timeout::Error, '')
95     assert_equal false, c.test
96   end
97   
98   def test_test_should_return_true_if_code_is_not_is_set_to_200_and_response_times_out
99     c = valid_condition do |cc|
100       cc.code_is = nil
101       cc.code_is_not = [200]
102     end
103     Net::HTTP.expects(:start).raises(Timeout::Error, '')
104     assert_equal true, c.test
105   end
106   
107   def test_test_should_return_true_if_code_is_is_set_to_200_and_response_is_200_twice_for_times_two_of_two
108     c = valid_condition do |cc|
109       cc.times = [2, 2]
110     end
111     Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 200))).times(2)
112     assert_equal false, c.test
113     assert_equal true, c.test
114   end