spec/static_assets_spec.rb in sinatra-exstatic-assets-3.0.0 vs spec/static_assets_spec.rb in sinatra-exstatic-assets-3.1.0
- old
+ new
@@ -1,32 +1,64 @@
require 'spec_helper'
-require_relative "../lib/sinatra/exstatic_assets.rb"
+#require_relative "../lib/sinatra/exstatic_assets.rb"
+require_relative "../lib/sinatra/exstatic_assets/formats.rb"
module Sinatra
module Exstatic
+shared_context "mtime timestamp" do
+ before(:each) do
+ File.expects(:"exists?").with(fullpath)
+ .at_least_once
+ .returns(true)
+ File.expects(:mtime).with(fullpath)
+ .returns(time)
+ end
+end
+
describe Asset, :time_sensitive do
+ shared_examples "for Asset file" do
+ its(:fullpath) { should == fullpath }
+ its(:"is_uri?") { should be_falsy }
+ it { should_not be_nil }
+ it { should == expected }
+ end
let(:asset_dir) { "app/public" }
- subject(:asset){ Asset.new filename, asset_dir }
+ let(:time) { Time.now }
+ subject(:asset){ Asset.new filename, asset_dir, timestamp_format }
context "Given a file" do
- let(:fullpath) { File.join asset_dir, filename }
- before do
- File.stub(:"exists?").with(fullpath).and_return(true)
- File.stub(:mtime).with(fullpath).and_return(Time.now)
- end
let(:filename) { "image.jpg" }
let(:expected) { "image.jpg" }
- it { should_not be_nil }
- it { should == expected }
- its(:fullpath) { should == fullpath }
- its(:timestamp) { should == Time.now.to_i }
- its(:"is_uri?") { should be_falsy }
- its(:querystring) { should == "?ts=#{Time.now.to_i}" }
+ let(:fullpath) { File.join asset_dir, filename }
+ context "Using mtime as the timestamp" do
+ let(:timestamp_format) { :mtime_int }
+ context "" do
+ include_context "mtime timestamp"
+ its(:timestamp) { should == Time.now.to_i }
+ its(:querystring) { should == "?ts=#{Time.now.to_i}" }
+ end
+ include_examples "for Asset file"
+ end
+ context "Using sha1 as the timestamp" do
+ let(:timestamp_format) { :sha1 }
+ context "" do
+ before do
+ digest_mock = mock()
+ digest_mock.expects(:hexdigest).returns( "871cb4397c5f5f146cc5583088b12c7d0a8ddc97" )
+ File.expects(:"exists?").with(fullpath).returns(true)
+ Digest::SHA1.expects(:file).with(fullpath).returns(digest_mock)
+ end
+ its(:timestamp) { should == "871cb4397c5f5f146cc5583088b12c7d0a8ddc97" }
+ its(:querystring) { should == %Q!?ts=#{"871cb4397c5f5f146cc5583088b12c7d0a8ddc97"}! }
+ end
+ include_examples "for Asset file"
+ end
end
context "Given a url" do
let(:filename) { "http://code.jquery.com/jquery-1.9.1.min.js" }
let(:expected) { "http://code.jquery.com/jquery-1.9.1.min.js" }
+ let(:timestamp_format) { :mtime_int }
it { should_not be_nil }
it { should == expected }
its(:fullpath) { should be_nil }
its(:timestamp) { should == false }
its(:"is_uri?") { should be_truthy }
@@ -67,90 +99,113 @@
end
class FakeObject
include Sinatra::Exstatic::Private
- def uri( addr, absolute, script_tag )
- script_tag ? File.join( ENV["SCRIPT_NAME"], addr) : addr
+ def initialize script_name=nil
+ @script_name = script_name || public_folder
end
+ def uri( addr, absolute, script_name )
+ script_name ? File.join( @script_name, addr) : addr
+ end
def settings
self
end
def public_folder
"app/public"
end
+ def xhtml
+ @xhtml ||= false
+ end
end
+
describe "Private methods", :time_sensitive do
+ let(:script_name) { "/bar" }
+ let(:fullpath) { File.join asset_dir, filename }
+ let(:asset_dir) { "app/public/" }
+ let(:time) { Time.now.to_i }
+ let(:timestamp_format) { :mtime_int }
let(:o) {
# A double, I couldn't get RSpec's to work with this
# probably because they're not well documented
# hint hint RSpec team
- o = FakeObject.new
+ o = FakeObject.new script_name
}
- let(:script_name) { "/bar" }
- let(:fullpath) { File.join asset_dir, filename }
- let(:asset_dir) { "app/public/" }
- let(:time) { Time.now.to_i }
- before do
- ENV["SCRIPT_NAME"] = script_name
- File.stub(:"exists?").with(fullpath).and_return(true)
- File.stub(:mtime).with(fullpath).and_return(time)
+
+ context "Favicon" do
+ let(:url) { "/favicon.ico" }
+ let(:filename) { "favicon.ico" }
+ let(:expected) { %Q!<link href="/bar/favicon.ico" rel="icon" />! }
+ subject {
+ o.send :sss_favicon_tag, url, {asset_dir: asset_dir}, {}
+ }
+ it { should == expected }
end
- context "Stylesheets" do
- let(:url) { "/stylesheets/winter.css" }
- let(:filename) { "/stylesheets/winter.css" }
- context "Given a filename" do
- context "But no options" do
- let(:expected) { %Q!<link charset="utf-8" href="/bar/stylesheets/winter.css?ts=#{time}" media="screen" rel="stylesheet" />! }
- subject { o.send :sss_stylesheet_tag, url }
- it { should == expected }
+ context "Accessing the file system" do
+ include_context "mtime timestamp" do
+ end
+ context "Stylesheets" do
+ before do
+ ENV["SCRIPT_NAME"] = script_name
end
- context "with options" do
- context "media=print" do
- let(:expected) { %Q!<link charset="utf-8" href="/bar/stylesheets/winter.css?ts=#{time}" media="print" rel="stylesheet" />! }
- subject { o.send :sss_stylesheet_tag, url, media: "print" }
- it { should == expected }
+ let(:url) { "/stylesheets/winter.css" }
+ let(:filename) { "/stylesheets/winter.css" }
+ context "Given a filename" do
+ context "But no options" do
+ let(:expected) { %Q!<link charset="utf-8" href="/bar/stylesheets/winter.css?ts=#{time}" media="screen" rel="stylesheet" />! }
+ subject { o.send :sss_stylesheet_tag, url, {asset_dir: asset_dir, timestamp_format: timestamp_format}, {} }
+ it { should == expected }
end
+ context "with options" do
+ context "media=print" do
+ let(:expected) { %Q!<link charset="utf-8" href="/bar/stylesheets/winter.css?ts=#{time}" media="print" rel="stylesheet" />! }
+ subject { o.send :sss_stylesheet_tag, url, {asset_dir: asset_dir,media: "print", timestamp_format: timestamp_format}, {} }
+ it { should == expected }
+ end
+ end
end
end
- it { should_not be_nil }
- end
- context "Javascripts" do
+ context "Javascripts" do
let(:url) { "/js/get_stuff.js" }
let(:filename) { "/js/get_stuff.js" }
let(:expected) { %Q!<script charset="utf-8" src="/bar/js/get_stuff.js?ts=#{time}"></script>! }
- subject { o.send :sss_javascript_tag, url }
+ subject { o.send :sss_javascript_tag, url, {asset_dir: asset_dir, timestamp_format: timestamp_format}, {} }
it { should_not be_nil }
it { should == expected }
end
+ end
context "Images" do
context "Local" do
let(:url) { "/images/foo.png" }
let(:filename) { "/images/foo.png" }
let(:expected) { %Q!<img src="/bar/images/foo.png?ts=#{time}" />! }
- subject { o.send :sss_image_tag, url }
- it { should_not be_nil }
- it { should == expected }
+ subject { o.send :sss_image_tag, url, {asset_dir: asset_dir, timestamp_format: :mtime_int}, {} }
+
+ context "Using mtime as the timestamp" do
+ include_context "mtime timestamp"
+ it { should_not be_nil }
+ it { should == expected }
+ end
end
context "Remote" do
let(:url) { "http://example.org/images/foo.png" }
let(:filename) { "/images/foo.png" }
let(:expected) { %Q!<img src="#{url}" />! }
subject {
o.send :sss_image_tag,
- url
+ url, {asset_dir: asset_dir}, {}
}
it { should_not be_nil }
it { should == expected }
end
context "Remote and secure" do
let(:url) { "https://example.org/images/foo.png" }
let(:filename) { "/images/foo.png" }
let(:expected) { %Q!<img src="#{url}" />! }
subject {
o.send :sss_image_tag,
- url
+ url, {asset_dir: asset_dir}, {}
}
it { should_not be_nil }
it { should == expected }
end
end
@@ -162,16 +217,18 @@
describe "Using them with a Sinatra app", :time_sensitive do
include_context "All routes"
let(:expected) { File.read File.expand_path(fixture_file, File.dirname(__FILE__)) }
before do
Sinatra::Exstatic::Asset.any_instance
- .stub(:exists?)
- .and_return(true)
+ .expects(:exists?)
+ .at_least_once
+ .returns(true)
Sinatra::Exstatic::Asset.any_instance
- .stub(:mtime_int)
- .and_return(1367612251)
+ .expects(:mtime_int)
+ .at_least_once
+ .returns(1367612251)
end
context "Main" do
context "/" do
let(:fixture_file) { "./support/fixtures/main.html" }
before do
@@ -203,9 +260,18 @@
end
context "/app2/deeper" do
let(:fixture_file) { "./support/fixtures/app2-deeper.html" }
before do
get "/app2/deeper"
+ end
+ it_should_behave_like "Any route"
+ subject { last_response.body }
+ it { should == expected }
+ end
+ context "/app2/deeper" do
+ let(:fixture_file) { "./support/fixtures/app2-deeper.html" }
+ before do
+ get "/app2/deeper/and-deeper"
end
it_should_behave_like "Any route"
subject { last_response.body }
it { should == expected }
end
\ No newline at end of file