summaryrefslogtreecommitdiff
path: root/t/test-protocol-http.rb
blob: cc8421c949e8220f53883dbc5a90754915585289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/ruby -I../lib/ -Ilib/


require 'test/unit'
require 'webrick'
require 'custodian/protocol-tests/http.rb'





#
# Unit test for the HTTP-protocol probe.
#
class TestHTTPProtocolProbe < Test::Unit::TestCase


  #
  # Holder for the new thread we launch, and the servr
  # we run within it.
  #
  attr_reader :server, :server_thread


  #
  # Create the test suite environment: Launch a HTTP server in a new thread.
  #
  def setup
    @server_thread = Thread.new do
      @server = WEBrick::HTTPServer.new( :Port => 12000,
                                         :DocumentRoot => "/tmp",
                                         :AccessLog => [])
      @server.start
    end
  end


  #
  # Destroy the test suite environment: Kill the HTTP-Server
  #
  def teardown
    @server_thread.kill!
  end


  #
  #  Test we can create a new HTTPTest object.
  #
  def test_init
    test_data_good = {
      "target_host" => "http://www.steve.org.uk/",
      "test_type"   => "http",
      "verbose"     => 1,
      "test_port"   => 80,
      "test_alert"  => "Steve's website is unavailable",
      "http_text"   => "Steve Kemp",
      "http_status" => "200"
    }

    #
    # Missing a port number
    #
    test_data_bad_one = {
      "target_host" => "http://www.steve.org.uk/",
      "test_type"   => "http",
      "verbose"     => 1,
      "test_alert"  => "Steve's website is unavailable",
      "http_text"   => "Steve Kemp",
      "http_status" => "200"
    }

    #
    #  Missing URL to probe
    #
    test_data_bad_two = {
      "test_type"   => "http",
      "test_port"   => 80,
      "verbose"     => 1,
      "test_alert"  => "Steve's website is unavailable",
      "http_text"   => "Steve Kemp",
      "http_status" => "200"
    }


    #
    #  Create a new HTTPTest object.  This should succeed
    #
    good = HTTPTest.new( test_data_good )
    assert( good )

    #
    #  There will be no error setup
    #
    assert( good.error().nil? )

    #
    #  Now create a probe with a missing port.
    #
    assert_raise ArgumentError do
      bad = HTTPTest.new( test_data_bad_one )
    end


    #
    #  Now create a probe with a missing URL.
    #
    assert_raise ArgumentError do
      bad = HTTPTest.new( test_data_bad_two )
    end

  end



  #
  #  Test we can make a HTTP fetch, and retrieve the status code
  # against our stub-Webbrick server.
  #
  def test_http_fetch

    test_probe = {
      "target_host" => "http://localhost:12000/",
      "test_type"   => "http",
      "verbose"     => 1,
      "test_port"   => 12000,
      "http_status" => "200"
    }

    #
    #  Create a new HTTPTest object.  This should succeed
    #
    test = HTTPTest.new( test_probe )
    assert( test )


    #
    #  Make the test - ensure that:
    #
    # a. There is no error before it is tested.
    #
    # b. The test method "run_test" returns true.
    #
    # c. There is no error logged after completion.
    #
    assert( test.error().nil? )
    assert( test.run_test() )
    assert( test.error().nil? )
  end

end