Permalink
Browse files

Tests for basic functionality.

  • Loading branch information...
1 parent e6832f5 commit 5983e4f80e3ac7b8cfa6ae2abe86dc5e5a9a02b0 @oggy committed Mar 30, 2011
Showing with 180 additions and 0 deletions.
  1. +1 −0 Gemfile
  2. +12 −0 spec/spec_helper.rb
  3. +110 −0 spec/support/progressive_rendering_test.rb
  4. +56 −0 spec/template_streaming_spec.rb
  5. +1 −0 template_streaming.gemspec
View
@@ -1,2 +1,3 @@
source :rubygems
+gem 'actionpack', '2.3.11'
gemspec
View
@@ -0,0 +1,12 @@
+$:.unshift File.expand_path('../lib', File.dirname(__FILE__))
+
+ROOT = File.expand_path('..', File.dirname(__FILE__))
+
+require 'bundler'
+Bundler.setup(:default, :development)
+
+require 'action_controller'
+require 'template_streaming'
+require 'temporaries'
+
+require 'support/progressive_rendering_test'
@@ -0,0 +1,110 @@
+module ProgressiveRenderingTest
+ VIEW_PATH = "#{ROOT}/spec/tmp/views"
+
+ def self.included(base)
+ base.before { setup_progressive_rendering_test }
+ base.after { teardown_progressive_rendering_test }
+ end
+
+ def setup_progressive_rendering_test
+ ActionController::Base.session = {:key => "_", :secret => "x"*30}
+ ActionController::Routing::Routes.clear!
+ ActionController::Routing::Routes.add_route('/', :controller => 'test')
+
+ push_constant_value Object, :TestController, Class.new(Controller)
+ TestController.view_paths = [VIEW_PATH]
+
+ FileUtils.rm_rf VIEW_PATH
+ FileUtils.mkdir_p VIEW_PATH
+ $current_spec = self
+ @data = OpenStruct.new
+ end
+
+ def controller
+ TestController
+ end
+
+ def teardown_progressive_rendering_test
+ pop_constant_value Object, :TestController
+ FileUtils.rm_rf VIEW_PATH
+ $current_spec = nil
+ end
+
+ def view(text)
+ write_file("#{controller.view_paths.first}/test/index.html.erb", text)
+ end
+
+ def layout(text)
+ write_file("#{controller.view_paths.first}/layouts/test.html.erb", text)
+ end
+
+ def run(env_overrides={})
+ env = default_env.merge(env_overrides)
+ app = ActionController::Dispatcher.new
+ @data.received = ''
+ @status, @headers, @body = app.call(env)
+ @body.each do |chunk|
+ @data.received << chunk
+ end
+ end
+
+ attr_reader :status, :headers, :body, :data
+
+ def write_file(path, content)
+ FileUtils.mkdir_p File.dirname(path)
+ open(path, 'w') { |f| f.print content }
+ end
+
+ def default_env
+ {
+ 'REQUEST_METHOD' => 'GET',
+ 'SCRIPT_NAME' => '',
+ 'PATH_INFO' => '/',
+ 'QUERY_STRING' => '',
+ 'SERVER_NAME' => 'test.example.com',
+ 'SERVER_PORT' => '',
+ 'rack.version' => [1, 1],
+ 'rack.url_scheme' => 'http',
+ 'rack.input' => StringIO.new,
+ 'rack.errors' => StringIO.new,
+ 'rack.multithread' => false,
+ 'rack.multiprocess' => false,
+ 'rack.run_once' => true,
+ 'rack.logger' => Logger.new(STDERR),
+ }
+ end
+
+ class Controller < ActionController::Base
+ def index
+ end
+
+ def rescue_action(exception)
+ STDERR.puts "#{exception.class}: #{exception.message}"
+ STDERR.puts exception.backtrace.join("\n").gsub(/^/, ' ')
+ raise exception
+ end
+ end
+
+ module Helpers
+ def data
+ $current_spec.data
+ end
+
+ def received
+ data.received
+ end
+
+ def chunks(*chunks)
+ options = chunks.last.is_a?(Hash) ? chunks.pop : {}
+ content = ''
+ chunks.each do |chunk|
+ content << chunk.size.to_s(16) << "\r\n" << chunk << "\r\n"
+ end
+ content << "0\r\n\r\n" if options[:end]
+ content
+ end
+ end
+
+ include Helpers
+ ActionView::Base.send :include, Helpers
+end
@@ -0,0 +1,56 @@
+require 'spec/spec_helper'
+
+describe TemplateStreaming do
+ include ProgressiveRenderingTest
+
+ it "should render progressively when the layout is progressive" do
+ TestController.layout 'test', :progressive => true
+
+ layout <<-'EOS'.gsub(/^ *\|/, '')
+ |1
+ |<% flush -%>
+ |<% received.should == chunks("1\n") -%>
+ |<%= yield -%>
+ |<% flush -%>
+ |<% received.should == chunks("1\n", "a\n", "b\n", "c\n") -%>
+ |2
+ |<% flush -%>
+ |<% received.should == chunks("1\n", "a\n", "b\n", "c\n", "2\n") -%>
+ EOS
+
+ view <<-'EOS'.gsub(/^ *\|/, '')
+ |a
+ |<% flush -%>
+ |<% received.should == chunks("1\n", "a\n") -%>
+ |b
+ |<% flush -%>
+ |<% received.should == chunks("1\n", "a\n", "b\n") -%>
+ |c
+ EOS
+
+ run
+ received.should == chunks("1\n", "a\n", "b\n", "c\n", "2\n", :end => true)
+ end
+
+ it "should render normally when the layout is not progressive" do
+ TestController.layout 'test'
+
+ view <<-'EOS'.gsub(/^ *\|/, '')
+ |a
+ |<% data.order << :view -%>
+ EOS
+
+ layout <<-'EOS'.gsub(/^ *\|/, '')
+ |1
+ |<% data.order << :layout1 -%>
+ |<%= yield -%>
+ |2
+ |<% data.order << :layout2 -%>
+ EOS
+
+ data.order = []
+ run
+ data.order.should == [:view, :layout1, :layout2]
+ received.should == "1\na\n2\n"
+ end
+end
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.6"
s.add_dependency "actionpack", '>= 2.3.11'
s.add_development_dependency "rspec"
+ s.add_development_dependency "temporaries"
s.files = Dir["{doc,lib,rails}/**/*"] + %w(LICENSE README.markdown Rakefile CHANGELOG)
s.test_files = Dir["spec/**/*"]
s.require_path = 'lib'

0 comments on commit 5983e4f

Please sign in to comment.