Permalink
Browse files

Update tests

  • Loading branch information...
1 parent 1d927d0 commit 66ce9426ba409a33b93609204cb567425b5e0bb7 @wycats committed Apr 16, 2010
Showing with 144 additions and 72 deletions.
  1. +1 −1 .gitignore
  2. +6 −0 Gemfile
  3. +28 −0 Gemfile.lock
  4. +16 −14 lib/rack/offline.rb
  5. +5 −21 spec/base_offline_spec.rb
  6. +66 −36 spec/cached_offline_spec.rb
  7. +22 −0 spec/spec_helper.rb
View
2 .gitignore
@@ -1,4 +1,4 @@
.bundle
public
config.ru
-Gemfile*
+spec/fixture_root
View
6 Gemfile
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+group :test do
+ gem "rspec", "~> 2.0.0.beta.4"
+ gem "rack-test", :require => "rack/test"
+end
View
28 Gemfile.lock
@@ -0,0 +1,28 @@
+---
+dependencies:
+ rspec:
+ group:
+ - :test
+ version: ~> 2.0.0.beta.4
+ rack-test:
+ group:
+ - :test
+ version: ">= 0"
+ require:
+ - rack/test
+specs:
+- rack:
+ version: 1.1.0
+- rack-test:
+ version: 0.5.3
+- rspec-core:
+ version: 2.0.0.beta.4
+- rspec-expectations:
+ version: 2.0.0.beta.4
+- rspec-mocks:
+ version: 2.0.0.beta.4
+- rspec:
+ version: 2.0.0.beta.4
+hash: cffb78a951f7b10fea8527f366f4830e6045ae1e
+sources: []
+
View
30 lib/rack/offline.rb
@@ -10,13 +10,13 @@ def self.configure(*args, &block)
end
def initialize(options = {}, &block)
- @cache = options[:cache]
+ @cache = options[:cache]
- @logger = options[:logger] || begin
+ @logger = options[:logger] || begin
::Logger.new(STDOUT).tap {|logger| logger.level = 1 }
end
- @root = Pathname.new(options[:root] || Dir.pwd)
+ @root = Pathname.new(options[:root] || Dir.pwd)
if block_given?
@config = Rack::Offline::Config.new(&block)
@@ -25,21 +25,13 @@ def initialize(options = {}, &block)
if @cache
raise "In order to run Rack::Offline in cached mode, " \
"you need to supply a root so Rack::Offline can " \
- "calculate a hash of the files." unless root
- precache_key
+ "calculate a hash of the files." unless @root
+ precache_key!
end
end
- def precache_key
- hash = @config.cached.map do |item|
- Digest::SHA2.hexdigest(@root.join(item).read)
- end
-
- @key = Digest::SHA2.hexdigest(hash.join)
- end
-
def call(env)
- key = @key || Digest::SHA2.hexdigest(Time.now.to_s)
+ key = @key || Digest::SHA2.hexdigest(Time.now.to_s + Time.now.usec.to_s)
body = ["CACHE MANIFEST"]
body << "# #{key}"
@@ -65,5 +57,15 @@ def call(env)
[200, {"Content-Type" => "text/cache-manifest"}, body.join("\n")]
end
+
+ private
+
+ def precache_key!
+ hash = @config.cached.map do |item|
+ Digest::SHA2.hexdigest(@root.join(item).read)
+ end
+
+ @key = Digest::SHA2.hexdigest(hash.join)
+ end
end
end
View
26 spec/base_offline_spec.rb
@@ -7,28 +7,12 @@
cache "images/masthead.png"
end
- before do
- get "/"
- end
-
- it "returns the response as text/cache-manifest" do
- headers["Content-Type"].should == "text/cache-manifest"
- end
-
- it "returns a 200 status code" do
- status.should == 200
- end
+ it_should_behave_like "a cache manifest"
- it "includes the text CACHE MANIFEST" do
- body.should =~ /\ACACHE MANIFEST\n/
- end
-
- it "includes the entry to be cached on its own line" do
- body.should =~ %r{^images/masthead.png$}
- end
-
- it "includes a cache-busting comment" do
- body.should =~ %r{^# .{64}$}
+ it "returns a different cache-busting comment each time" do
+ cache_buster = body[/^# .{64}$/]
+ get "/"
+ body[/^# .{64}$/].should_not == cache_buster
end
it "doesn't contain a network section" do
View
102 spec/cached_offline_spec.rb
@@ -4,50 +4,80 @@
describe "Generating a manifest in cached mode" do
include Rack::Test::Methods
- root = File.expand_path("../fixture_root", __FILE__)
- self.app = Rack::Offline.configure(:root => root, :cache => true) do
- cache "hello.html"
- cache "hello.css"
- cache "javascripts/hello.js"
+ def self.new_app
+ root = File.expand_path("../fixture_root", __FILE__)
+ Rack::Offline.configure(:root => root, :cache => true) do
+ cache "hello.html"
+ cache "hello.css"
+ cache "javascripts/hello.js"
+ end
+ end
+
+ def self.setup_fixtures
+ fixture_root = Pathname.new(File.expand_path("../fixture_root", __FILE__))
+ FileUtils.rm_rf(fixture_root)
+ FileUtils.mkdir_p(fixture_root)
+
+ File.open(fixture_root.join("hello.css"), "w") do |file|
+ file.puts "#hello {\n display: false\n}\n"
+ end
+
+ File.open(fixture_root.join("hello.html"), "w") do |file|
+ file.puts "<!DOCTYPE html>\n<html manifest='hello.manifest'>\n</html>"
+ end
+
+ FileUtils.mkdir_p(fixture_root.join("javascripts"))
+ File.open(fixture_root.join("javascripts/hello.js"), "w") do |file|
+ file.puts "var x = 1;"
+ end
+ end
+
+ def self.reload_server
+ setup_fixtures
+ self.app = new_app
+ end
+
+ def reload_server
+ self.class.reload_server
end
before :all do
- FileUtils.rm_rf File.expand_path("../fixture_root", __FILE__)
+ reload_server
end
before do
get "/"
end
+ it_should_behave_like "a cache manifest"
+
it "returns the same cache-busting header every time" do
-
- end
-
- # it "returns the response as text/cache-manifest" do
- # headers["Content-Type"].should == "text/cache-manifest"
- # end
- #
- # it "returns a 200 status code" do
- # status.should == 200
- # end
- #
- # it "includes the text CACHE MANIFEST" do
- # body.should =~ /\ACACHE MANIFEST\n/
- # end
- #
- # it "includes the entry to be cached on its own line" do
- # body.should =~ %r{^images/masthead.png$}
- # end
- #
- # it "includes a cache-busting comment" do
- # body.should =~ %r{^# .{64}$}
- # end
- #
- # it "doesn't contain a network section" do
- # body.should_not =~ %r{^NETWORK:}
- # end
- #
- # it "doesn't contain a fallback section" do
- # body.should_not =~ %r{^FALLBACK:}
- # end
+ cache_buster = body[/^# .{64}$/]
+ get "/"
+ body[/^# .{64}$/].should == cache_buster
+ end
+
+ it "updates the cache-busting header if the files change and the server restarts" do
+ cache_buster = body[/^# .{64}$/]
+
+ root = File.expand_path("../fixture_root", __FILE__)
+ File.open("#{root}/hello.css", "w") {|file| file.puts "OMG"}
+
+ self.class.app = self.class.new_app
+
+ with_session :secondary do
+ get "/"
+ body[/^# .{64}$/].should_not == cache_buster
+ end
+
+ reload_server
+ end
+
+ it "doesn't contain a network section" do
+ body.should_not =~ %r{^NETWORK:}
+ end
+
+ it "doesn't contain a fallback section" do
+ body.should_not =~ %r{^FALLBACK:}
+ end
end
View
22 spec/spec_helper.rb
@@ -29,4 +29,26 @@ def headers
def app
self.class.app
end
+end
+
+shared_examples_for "a cache manifest" do
+ before do
+ get "/"
+ end
+
+ it "returns the response as text/cache-manifest" do
+ headers["Content-Type"].should == "text/cache-manifest"
+ end
+
+ it "returns a 200 status code" do
+ status.should == 200
+ end
+
+ it "includes the text CACHE MANIFEST" do
+ body.should =~ /\ACACHE MANIFEST\n/
+ end
+
+ it "includes a cache-busting comment" do
+ body.should =~ %r{^# .{64}$}
+ end
end

0 comments on commit 66ce942

Please sign in to comment.