From 9adff929afd333a813d65ba159118a18b147b20d Mon Sep 17 00:00:00 2001 From: Gert Goet Date: Tue, 1 Nov 2011 12:02:53 +0100 Subject: [PATCH] added specs --- Gemfile | 6 +++++- Rakefile | 13 +++++++++++++ lib/rack/pjax.rb | 1 + rack-pjax.gemspec | 2 +- spec/rack/pjax_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ spec/spec.opts | 1 + spec/spec_helper.rb | 34 ++++++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 spec/rack/pjax_spec.rb create mode 100644 spec/spec.opts create mode 100644 spec/spec_helper.rb diff --git a/Gemfile b/Gemfile index f72bf21..6d32146 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,8 @@ source "http://rubygems.org" -# Specify your gem's dependencies in rack-pjax.gemspec gemspec + +group :test do + gem "rspec", ">2" + gem "rack-test" +end diff --git a/Rakefile b/Rakefile index c702cfc..b36c37d 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,14 @@ require 'bundler/gem_tasks' +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new do |t| + t.rspec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""] +end + +desc "Run the specs" +task :default => :spec + +desc 'Removes trailing whitespace' +task :whitespace do + sh %{find . -name '*.rb' -exec sed -i '' 's/ *$//g' {} \\;} +end diff --git a/lib/rack/pjax.rb b/lib/rack/pjax.rb index 916bb0f..4d179ff 100644 --- a/lib/rack/pjax.rb +++ b/lib/rack/pjax.rb @@ -25,6 +25,7 @@ def call(env) end response = [body] + # headers['Content-Length'] &&= Rack::Utils.bytesize(body).to_s if headers['Content-Length'] length = response.to_ary.inject(0) { |len, part| len + bytesize(part) } headers['Content-Length'] = length.to_s diff --git a/rack-pjax.gemspec b/rack-pjax.gemspec index fa3af02..9bc85d1 100644 --- a/rack-pjax.gemspec +++ b/rack-pjax.gemspec @@ -19,5 +19,5 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.add_dependency('rack', '>1.0') - s.add_dependency('nokogiri', '~>1.4.4') + s.add_dependency('nokogiri', '~>1.5.0') end diff --git a/spec/rack/pjax_spec.rb b/spec/rack/pjax_spec.rb new file mode 100644 index 0000000..14f5dd3 --- /dev/null +++ b/spec/rack/pjax_spec.rb @@ -0,0 +1,40 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe Rack::Pjax do + include Rack::Test::Methods # can be moved to config + + def app + Rack::Builder.app do + use Rack::Pjax + run lambda { |env| + body = 'Hello
World!
' + headers = {"Content-Length" => Rack::Utils.bytesize(body).to_s} + [200, headers, [body]] + } + end + end + + context "when receiving a pjax-request" do + it "should return title-tag and inner-html of the pjax-container" do + get "/", {}, {'HTTP_X_PJAX' => true} + body.should == "HelloWorld!" + end + + it "should recalculate the Content Length" do + get "/", {}, {'HTTP_X_PJAX' => true} + headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s + end + end + + context "when receiving a non-pjax request" do + it "should not alter the body" do + get "/" + body.should == 'Hello
World!
' + end + + it "should have the correct Content Length" do + get "/" + headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s + end + end +end diff --git a/spec/spec.opts b/spec/spec.opts new file mode 100644 index 0000000..4e1e0d2 --- /dev/null +++ b/spec/spec.opts @@ -0,0 +1 @@ +--color diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..fba089f --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,34 @@ +require "rubygems" +require "bundler" +Bundler.setup + +$:.unshift File.expand_path("../../lib", __FILE__) +require "rack-pjax" + +Bundler.require(:test) + +# helpers ripped from wycat's Rack::Offline +# (https://github.com/wycats/rack-offline/blob/master/spec/spec_helper.rb) +module Rack::Test::Methods + def self.included(klass) + class << klass + attr_accessor :app + end + end + + def body + last_response.body + end + + def status + last_response.status + end + + def headers + last_response.headers + end + + def app + self.class.app + end +end