From 05cf864240ed618d55c68b4473dc4de7f395f32c Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Wed, 7 Mar 2012 22:58:59 +0100 Subject: [PATCH] add streaming test --- test/integration/app.rb | 9 +++++++++ test/integration_helper.rb | 12 +++++++++--- test/integration_test.rb | 12 ++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/test/integration/app.rb b/test/integration/app.rb index 48303aa8..59790c66 100644 --- a/test/integration/app.rb +++ b/test/integration/app.rb @@ -14,6 +14,15 @@ get '/ping' do 'pong' end +get '/stream' do + stream do |out| + sleep 0.1 + out << "a" + sleep 1.2 + out << "b" + end +end + get '/mainonly' do object = Object.new begin diff --git a/test/integration_helper.rb b/test/integration_helper.rb index 86a15f2f..458ec0b0 100644 --- a/test/integration_helper.rb +++ b/test/integration_helper.rb @@ -1,6 +1,7 @@ require 'sinatra/base' require 'rbconfig' require 'open-uri' +require 'net/http' module IntegrationHelper class Server @@ -67,12 +68,17 @@ module IntegrationHelper false end - def open(url) - super("http://127.0.0.1:#{port}#{url}") + def get_stream(url = "/stream", &block) + Net::HTTP.start '127.0.0.1', port do |http| + request = Net::HTTP::Get.new url + http.request request do |response| + response.read_body(&block) + end + end end def get(url) - open(url).read + open("http://127.0.0.1:#{port}#{url}").read end def log diff --git a/test/integration_test.rb b/test/integration_test.rb index 82b339bf..8448d54f 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -14,6 +14,18 @@ class IntegrationTest < Test::Unit::TestCase assert_equal 1, server.log.scan("GET /ping?x=#{random}").count end + it 'streams' do + next if server.name == "webrick" + times, chunks = [], [] + server.get_stream do |chunk| + chunks << chunk + times << Time.now + end + assert_equal ["", "a", "b"], chunks + assert times[1] - times[0] < 1 + assert times[2] - times[1] > 1 + end + it 'starts the correct server' do exp = %r{ ==\sSinatra/#{Sinatra::VERSION}\s