require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe Rack::Pjax do include Rack::Test::Methods # can be moved to config def generate_app(options={}) body = options[:body] Rack::Lint.new( Rack::Pjax.new( lambda do |env| [ 200, {'Content-Type' => 'text/plain', 'Content-Length' => Rack::Utils.bytesize(body).to_s}, [body] ] end ) ) end context "a pjaxified app, upon receiving a pjax-request" do before do self.class.app = generate_app(:body => 'Hello
World!
') end it "should return the title-tag in the body" do get "/", {}, {"HTTP_X_PJAX" => "true"} body.should == "HelloWorld!" end it "should return the inner-html of the pjax-container in the body" do self.class.app = generate_app(:body => '
World!
') get "/", {}, {"HTTP_X_PJAX" => "true"} body.should == "World!" end it "should return the inner-html of the custom pjax-container in the body" do self.class.app = generate_app(:body => '
World!
') get "/", {}, {"HTTP_X_PJAX" => "true", "HTTP_X_PJAX_CONTAINER" => "#container"} body.should == "World!" end it "should handle self closing tags with HTML5 elements" do self.class.app = generate_app(:body => '
World!
') get "/", {}, {"HTTP_X_PJAX" => "true"} body.should == '
World!
' end it "should handle nesting of elements inside anchor tags" do self.class.app = generate_app(:body => '

World!

') get "/", {}, {"HTTP_X_PJAX" => "true"} body.should == '

World!

' end it "should handle html5 br tags correctly" do self.class.app = generate_app(:body => '

foo
bar

') get "/", {}, {"HTTP_X_PJAX" => "true"} body.should == '

foo
bar

' end it "should return the correct Content Length" do get "/", {}, {"HTTP_X_PJAX" => "true"} headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s end it "should return the original body when there's no pjax-container" do self.class.app = generate_app(:body => 'Has no pjax-container') get "/", {}, {"HTTP_X_PJAX" => "true"} body.should == "Has no pjax-container" end it "should preserve whitespaces of the original body" do container = "\n

\nfirst paragraph

Second paragraph

\n" self.class.app = generate_app(:body =><<-BODY)
#{container}
BODY get "/", {}, {"HTTP_X_PJAX" => "true"} body.should == container end end context "a pjaxified app, upon receiving a non-pjax request" do before do self.class.app = generate_app(:body => 'Hello
World!
') end it "should return the original body" do get "/" body.should == 'Hello
World!
' end it "should return the correct Content Length" do get "/" headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s end end end