about summary refs log tree commit homepage
path: root/test/unit/test_request_progress.rb
diff options
context:
space:
mode:
authorevanweaver <evanweaver@19e92222-5c0b-0410-8929-a290d50e31e9>2008-05-24 23:06:53 +0000
committerevanweaver <evanweaver@19e92222-5c0b-0410-8929-a290d50e31e9>2008-05-24 23:06:53 +0000
commit45ff7b22b67123dd8345f3c50151e89cc91ce2b7 (patch)
treee85c8aa353e3e9f05e90ee8bf76a6593520e1b01 /test/unit/test_request_progress.rb
parentf69f3602b11dc839bb09f8c783b5ec27c54694fd (diff)
parentfa3f48f65222eb5591ef3dd9c05b6c958d564fa4 (diff)
downloadunicorn-45ff7b22b67123dd8345f3c50151e89cc91ce2b7.tar.gz
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@1020 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'test/unit/test_request_progress.rb')
-rw-r--r--test/unit/test_request_progress.rb100
1 files changed, 100 insertions, 0 deletions
diff --git a/test/unit/test_request_progress.rb b/test/unit/test_request_progress.rb
new file mode 100644
index 0000000..a100426
--- /dev/null
+++ b/test/unit/test_request_progress.rb
@@ -0,0 +1,100 @@
+# Copyright (c) 2005 Zed A. Shaw
+# You can redistribute it and/or modify it under the same terms as Ruby.
+#
+# Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html
+# for more information.
+
+require 'test/test_helper'
+
+class UploadBeginHandler < Mongrel::HttpHandler
+  attr_reader :request_began, :request_progressed, :request_processed
+
+  def initialize
+    @request_notify = true
+  end
+
+  def reset
+    @request_began = false
+    @request_progressed = false
+    @request_processed = false
+  end
+
+  def request_begins(params)
+    @request_began = true
+  end
+
+  def request_progress(params,len,total)
+    @request_progressed = true
+  end
+
+  def process(request, response)
+    @request_processed = true
+    response.start do |head,body|
+      body.write("test")
+    end
+  end
+
+end
+
+class RequestProgressTest < Test::Unit::TestCase
+  def setup
+    @port = process_based_port
+    redirect_test_io do
+      @server = Mongrel::HttpServer.new("127.0.0.1", @port)
+    end
+    @handler = UploadBeginHandler.new
+    @server.register("/upload", @handler)
+    @server.run
+  end
+
+  def teardown
+    @server.stop(true)
+  end
+
+  def test_begin_end_progress
+    Net::HTTP.get("localhost", "/upload", @port)
+    assert @handler.request_began
+    assert @handler.request_progressed
+    assert @handler.request_processed
+  end
+
+  def call_and_assert_handlers_in_turn(handlers)
+    # reset all handlers
+    handlers.each { |h| h.reset }
+
+    # make the call
+    Net::HTTP.get("localhost", "/upload", @port)
+
+    # assert that each one was fired
+    handlers.each { |h|
+      assert h.request_began && h.request_progressed && h.request_processed,
+        "Callbacks NOT fired for #{h}"
+    }
+  end
+
+  def test_more_than_one_begin_end_progress
+    handlers = [@handler]
+
+    second = UploadBeginHandler.new
+    @server.register("/upload", second)
+    handlers << second
+    call_and_assert_handlers_in_turn(handlers)
+
+    # check three handlers
+    third = UploadBeginHandler.new
+    @server.register("/upload", third)
+    handlers << third
+    call_and_assert_handlers_in_turn(handlers)
+
+    # remove handlers to make sure they've all gone away
+    @server.unregister("/upload")
+    handlers.each { |h| h.reset }
+    Net::HTTP.get("localhost", "/upload", @port)
+    handlers.each { |h|
+      assert !h.request_began && !h.request_progressed && !h.request_processed
+    }
+
+    # re-register upload to the state before this test
+    @server.register("/upload", @handler)
+  end
+end