2006-06-30 16:42:12 -04:00
|
|
|
# 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.
|
2006-06-28 11:51:06 -04:00
|
|
|
|
2007-10-20 22:55:59 -04:00
|
|
|
require 'test/testhelp'
|
2006-06-28 11:51:06 -04:00
|
|
|
|
|
|
|
class UploadBeginHandler < Mongrel::HttpHandler
|
|
|
|
attr_reader :request_began, :request_progressed, :request_processed
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@request_notify = true
|
|
|
|
end
|
|
|
|
|
2006-12-15 04:02:34 -05:00
|
|
|
def reset
|
2007-10-19 05:29:11 -04:00
|
|
|
@request_began = false
|
|
|
|
@request_progressed = false
|
|
|
|
@request_processed = false
|
2006-12-15 04:02:34 -05:00
|
|
|
end
|
|
|
|
|
2006-06-28 11:51:06 -04:00
|
|
|
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
|
|
|
|
@server = Mongrel::HttpServer.new("127.0.0.1", 9998)
|
|
|
|
@handler = UploadBeginHandler.new
|
|
|
|
@server.register("/upload", @handler)
|
|
|
|
@server.run
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2007-10-20 19:15:19 -04:00
|
|
|
@server.stop(true)
|
2006-06-28 11:51:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_begin_end_progress
|
|
|
|
Net::HTTP.get("localhost", "/upload", 9998)
|
|
|
|
assert @handler.request_began
|
|
|
|
assert @handler.request_progressed
|
|
|
|
assert @handler.request_processed
|
|
|
|
end
|
|
|
|
|
2006-12-15 04:02:34 -05:00
|
|
|
def call_and_assert_handlers_in_turn(handlers)
|
|
|
|
# reset all handlers
|
|
|
|
handlers.each { |h| h.reset }
|
|
|
|
|
|
|
|
# make the call
|
|
|
|
Net::HTTP.get("localhost", "/upload", 9998)
|
|
|
|
|
|
|
|
# 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", 9998)
|
|
|
|
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
|
2006-06-28 11:51:06 -04:00
|
|
|
end
|