Permalink
Browse files

added specs

  • Loading branch information...
eval committed Nov 1, 2011
1 parent 600875f commit 9adff929afd333a813d65ba159118a18b147b20d
Showing with 95 additions and 2 deletions.
  1. +5 −1 Gemfile
  2. +13 −0 Rakefile
  3. +1 −0 lib/rack/pjax.rb
  4. +1 −1 rack-pjax.gemspec
  5. +40 −0 spec/rack/pjax_spec.rb
  6. +1 −0 spec/spec.opts
  7. +34 −0 spec/spec_helper.rb
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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 = '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>'
+ 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 == "<title>Hello</title>World!"
+ 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 == '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>'
+ end
+
+ it "should have the correct Content Length" do
+ get "/"
+ headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s
+ end
+ end
+end
View
@@ -0,0 +1 @@
+--color
View
@@ -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

0 comments on commit 9adff92

Please sign in to comment.