diff --git a/test/integration/app.rb b/test/integration/app.rb index 59790c66..93d409a3 100644 --- a/test/integration/app.rb +++ b/test/integration/app.rb @@ -33,4 +33,32 @@ get '/mainonly' do end end +set :out, nil +get '/async' do + stream(:keep_open) { |o| (settings.out = o) << "hi!" } +end + +get '/send' do + settings.out << params[:msg] if params[:msg] + settings.out.close if params[:close] + "ok" +end + +class Subclass < Sinatra::Base + set :out, nil + get '/subclass/async' do + settings.out << msg and halt(204) if params[:msg] + settings.out.close and halt(204) if params[:close] + stream(:keep_open) { |o| settings.out = o } + end + + get '/subclass/send' do + settings.out << params[:msg] if params[:msg] + settings.out.close if params[:close] + "ok" + end +end + +use Subclass + $stderr.puts "starting" diff --git a/test/integration_test.rb b/test/integration_test.rb index ffbb7750..0ea1f8f5 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -1,5 +1,6 @@ require File.expand_path('../helper', __FILE__) require File.expand_path('../integration_helper', __FILE__) +require 'timeout' # These tests start a real server and talk to it over TCP. # Every test runs with every detected server. @@ -32,6 +33,43 @@ class IntegrationTest < Test::Unit::TestCase assert times[2] - times[1] > 1 end + it 'streams async' do + next unless server.name == 'thin' + + Timeout.timeout(3) do + chunks = [] + server.get_stream '/async' do |chunk| + next if chunk.empty? + chunks << chunk + case chunk + when "hi!" then server.get "/send?msg=hello" + when "hello" then server.get "/send?close=1" + end + end + + assert_equal ['hi!', 'hello'], chunks + end + end + + it 'streams async from subclass' do + next unless server.name == 'thin' + + Timeout.timeout(3) do + chunks = [] + server.get_stream '/subclass/async' do |chunk| + next if chunk.empty? + chunks << chunk + case chunk + when "hi!" then server.get "/subclass/send?msg=hello" + when "hello" then server.get "/subclass/send?close=1" + end + end + + assert_equal ['hi!', 'hello'], chunks + end + end + + it 'starts the correct server' do exp = %r{ ==\sSinatra/#{Sinatra::VERSION}\s