Skip to content

Commit

Permalink
added specs
Browse files Browse the repository at this point in the history
  • Loading branch information
eval committed Nov 1, 2011
1 parent 600875f commit 9adff92
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/rack/pjax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rack-pjax.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions spec/rack/pjax_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/spec.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
34 changes: 34 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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.