watcher: set Expires headers for cache invalidation
[raindrops.git] / test / test_watcher.rb
blob86c97d840f0d9091cad6b337906fe7f2c7e985c7
1 # -*- encoding: binary -*-
2 require "test/unit"
3 require "rack"
4 require "raindrops"
6 class TestWatcher < Test::Unit::TestCase
7   TEST_ADDR = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
8   def check_headers(headers)
9     %w(X-Count X-Std-Dev X-Min X-Max X-Mean
10        X-Outliers-Low X-Outliers-Low X-Last-Reset).each { |x|
11       assert_kind_of String, headers[x], "#{x} missing"
12     }
13   end
15   def teardown
16     @app.shutdown
17     @ios.each { |io| io.close unless io.closed? }
18   end
20   def setup
21     @ios = []
22     @srv = TCPServer.new TEST_ADDR, 0
23     @ios << @srv
24     @port = @srv.addr[1]
25     @client = TCPSocket.new TEST_ADDR, @port
26     @addr = "#{TEST_ADDR}:#{@port}"
27     @ios << @client
28     @app = Raindrops::Watcher.new :delay => 0.001
29     @req = Rack::MockRequest.new @app
30   end
32   def test_index
33     resp = @req.get "/"
34     assert_equal 200, resp.status.to_i
35     t = Time.parse resp.headers["Last-Modified"]
36     assert_in_delta Time.now.to_f, t.to_f, 2.0
37   end
39   def test_active_txt
40     resp = @req.get "/active/#@addr.txt"
41     assert_equal 200, resp.status.to_i
42     assert_equal "text/plain", resp.headers["Content-Type"]
43     check_headers(resp.headers)
44   end
46   def test_active_html
47     resp = @req.get "/active/#@addr.html"
48     assert_equal 200, resp.status.to_i
49     assert_equal "text/html", resp.headers["Content-Type"]
50     check_headers(resp.headers)
51   end
53   def test_queued_txt
54     resp = @req.get "/queued/#@addr.txt"
55     assert_equal 200, resp.status.to_i
56     assert_equal "text/plain", resp.headers["Content-Type"]
57     check_headers(resp.headers)
58   end
60   def test_queued_html
61     resp = @req.get "/queued/#@addr.html"
62     assert_equal 200, resp.status.to_i
63     assert_equal "text/html", resp.headers["Content-Type"]
64     check_headers(resp.headers)
65   end
67   def test_reset
68     resp = @req.post "/reset/#@addr"
69     assert_equal 302, resp.status.to_i
70   end
72   def test_tail
73     env = @req.class.env_for "/tail/#@addr.txt"
74     status, headers, body = @app.call env
75     assert_equal "text/plain", headers["Content-Type"]
76     assert_equal 200, status.to_i
77     tmp = []
78     body.each do |x|
79       assert_kind_of String, x
80       tmp << x
81       break if tmp.size > 1
82     end
83   end
85   def test_tail_queued_min
86     env = @req.class.env_for "/tail/#@addr.txt?queued_min=1"
87     status, headers, body = @app.call env
88     assert_equal "text/plain", headers["Content-Type"]
89     assert_equal 200, status.to_i
90     tmp = []
91     body.each do |x|
92       tmp = TCPSocket.new TEST_ADDR, @port
93       @ios << tmp
94       assert_kind_of String, x
95       assert_equal 1, x.strip.split(/\s+/).last.to_i
96       break
97     end
98   end
99 end if RUBY_PLATFORM =~ /linux/