# encoding: UTF-8
require 'spec_helper'
require_relative "../lib/rack/jquery_ui.rb"
describe "The class methods" do
let(:env) { {} }
subject { Rack::JQueryUI.cdn env, :organisation => organisation }
context "Given the organisation option" do
subject { Rack::JQueryUI.cdn env, :organisation => organisation }
let(:expected) { "\n#{Rack::JQueryUI::FALLBACK}" }
context "of nil (the default)" do
let(:organisation) { nil }
let(:src) { Rack::JQueryUI::CDNs::MEDIA_TEMPLE }
it { should == expected }
context "and debug" do
let(:unminified) { "#{src[0..-7]}js" }
subject { Rack::JQueryUI.cdn env, :organisation => organisation, :debug => true }
it { should_not include expected }
it { should include unminified }
end
end
context "of :google" do
let(:organisation) { :google }
let(:src) { Rack::JQueryUI::CDNs::GOOGLE }
it { should == expected }
context "and debug" do
let(:unminified) { "#{src[0..-7]}js" }
subject { Rack::JQueryUI.cdn env, :organisation => organisation, :debug => true }
it { should_not include expected }
it { should include unminified }
end
end
context "of :microsoft" do
let(:organisation) { :microsoft }
let(:src) { Rack::JQueryUI::CDNs::MICROSOFT }
it { should == expected }
context "and debug" do
let(:unminified) { "#{src[0..-7]}js" }
subject { Rack::JQueryUI.cdn env, :organisation => organisation, :debug => true }
it { should_not include expected }
it { should include unminified }
end
end
context "of :media_temple" do
let(:organisation) { :media_temple }
let(:src) { Rack::JQueryUI::CDNs::MEDIA_TEMPLE }
it { should == expected }
context "and debug" do
let(:unminified) { "#{src[0..-7]}js" }
subject { Rack::JQueryUI.cdn env, :organisation => organisation, :debug => true }
it { should_not include expected }
it { should include unminified }
end
end
context "of :cloudflare" do
let(:organisation) { :cloudflare }
let(:src) { Rack::JQueryUI::CDNs::CLOUDFLARE }
it { should == expected }
context "and debug" do
let(:unminified) { "#{src[0..-7]}js" }
subject { Rack::JQueryUI.cdn env, :organisation => organisation, :debug => true }
it { should_not include expected }
it { should include unminified }
end
end
context "of false" do
let(:organisation) { false }
let(:expected) { ""}
it { should == expected }
end
end
context "Given no Rack env argument" do
it "should fail and give a message" do
expect{ Rack::JQueryUI.cdn nil }.to raise_error(ArgumentError)
end
context "and an organisation option" do
it "should fail and give a message" do
expect{ Rack::JQueryUI.cdn nil, {:organisation => :microsoft} }.to raise_error(ArgumentError)
end
end
end
end
describe "Inserting the CDN" do
context "When not given a default" do
include_context "All routes"
context "Check the examples run at all" do
before do
get "/"
end
it_should_behave_like "Any route"
end
context "Google CDN" do
before do
get "/google-cdn"
end
it_should_behave_like "Any route"
subject { last_response.body }
let(:expected) { Rack::JQueryUI::CDNs::GOOGLE }
it { should include expected }
end
context "Cloudflare CDN" do
before do
get "/cloudflare-cdn"
end
it_should_behave_like "Any route"
subject { last_response.body }
context "With :raise set to false" do
let(:expected) { Rack::JQueryUI::CDNs::CLOUDFLARE }
it { should include expected }
end
context "With :raise set to true" do
context "via `use`" do
let(:app) {
Sinatra.new do
use Rack::JQuery
use Rack::JQueryUI, :raise => true
get "/cloudflare-cdn" do
Rack::JQueryUI.cdn( env, :organisation => :cloudflare )
end
end
}
# it "should raise error as it's not supported for this version" do
# expect { subject }.to raise_error
# end
it { should include "//cdnjs.cloudflare.com/ajax/libs/jqueryui/#{Rack::JQueryUI::JQUERY_UI_VERSION}/jquery-ui.min.js" }
end
context "via the method options" do
let(:app) {
Sinatra.new do
use Rack::JQuery
use Rack::JQueryUI, :raise => false
get "/cloudflare-cdn" do
Rack::JQueryUI.cdn( env, :organisation => :cloudflare, :raise => true )
end
end
}
# subject { get "/cloudflare-cdn" }
# it "should raise error as it's not supported for this version" do
# expect { subject }.to raise_error
# end
it { should include "//cdnjs.cloudflare.com/ajax/libs/jqueryui/#{Rack::JQueryUI::JQUERY_UI_VERSION}/jquery-ui.min.js" }
end
end
end
context "Media_temple CDN" do
before do
get "/media_temple-cdn"
end
it_should_behave_like "Any route"
subject { last_response.body }
let(:expected) { Rack::JQueryUI::CDNs::MEDIA_TEMPLE }
it { should include expected }
end
context "Unspecified CDN" do
before do
get "/unspecified-cdn"
end
it_should_behave_like "Any route"
subject { last_response.body }
let(:expected) { Rack::JQueryUI::CDNs::MEDIA_TEMPLE }
it { should include expected }
end
context "No CDN, force the fallback" do
before do
get "/fallback-only"
end
it_should_behave_like "Any route"
subject { last_response.body }
let(:expected) { "" }
it { should include expected }
end
end
# These check the default is overriden
# when `cdn` is given a value
# but when not, the default is used.
context "When given a default" do
include_context "All routes" do
let(:app){ AppWithDefaults }
end
context "Check the examples run at all" do
before do
get "/"
end
it_should_behave_like "Any route"
end
context "Google CDN" do
before do
get "/google-cdn"
end
it_should_behave_like "Any route"
subject { last_response.body }
let(:expected) { Rack::JQueryUI::CDNs::GOOGLE }
it { should include expected }
end
context "Cloudflare CDN" do
before do
get "/cloudflare-cdn"
end
it_should_behave_like "Any route"
subject { last_response.body }
context "With :raise set to false" do
let(:expected) { Rack::JQueryUI::CDNs::CLOUDFLARE }
it { should include expected }
end
context "With :raise set to true" do
context "via `use`" do
let(:app) {
Sinatra.new do
use Rack::JQuery
use Rack::JQueryUI, :raise => true
get "/cloudflare-cdn" do
Rack::JQueryUI.cdn( env, :organisation => :cloudflare )
end
end
}
# it "should raise error as it's not supported for this version" do
# expect { subject }.to raise_error
# end
it { should include "//cdnjs.cloudflare.com/ajax/libs/jqueryui/#{Rack::JQueryUI::JQUERY_UI_VERSION}/jquery-ui.min.js" }
end
context "via the method options" do
let(:app) {
Sinatra.new do
use Rack::JQuery
use Rack::JQueryUI, :raise => false
get "/cloudflare-cdn" do
Rack::JQueryUI.cdn( env, :organisation => :cloudflare, :raise => true )
end
end
}
# subject { get "/cloudflare-cdn" }
# it "should raise error as it's not supported for this version" do
# expect { subject }.to raise_error
# end
it { should include "//cdnjs.cloudflare.com/ajax/libs/jqueryui/#{Rack::JQueryUI::JQUERY_UI_VERSION}/jquery-ui.min.js" }
end
end
end
context "Media_temple CDN" do
before do
get "/media_temple-cdn"
end
it_should_behave_like "Any route"
subject { last_response.body }
let(:expected) { Rack::JQueryUI::CDNs::MEDIA_TEMPLE }
it { should include expected }
end
context "Unspecified CDN" do
before do
get "/unspecified-cdn"
end
it_should_behave_like "Any route"
subject { last_response.body }
let(:expected) { Rack::JQueryUI::CDNs::CLOUDFLARE }
it { should include expected }
end
context "No CDN, force the fallback" do
before do
get "/fallback-only"
end
it_should_behave_like "Any route"
subject { last_response.body }
let(:expected) { "" }
it { should include expected }
end
end
end
require 'timecop'
require 'time'
describe "Serving the fallback jquery" do
include_context "All routes"
before do
get "/js/jquery-ui/#{Rack::JQueryUI::JQUERY_UI_VERSION}/#{Rack::JQueryUI::JQUERY_UI_FILE_NAME}"
end
it_should_behave_like "Any route"
subject { last_response.body }
it { should start_with "/*! jQuery UI - v#{Rack::JQueryUI::JQUERY_UI_VERSION}" }
context "Re requests" do
before do
at_start = Time.parse(Rack::JQueryUI::JQUERY_UI_VERSION_DATE) + 60 * 60 * 24 * 180
Timecop.freeze at_start
get "/js/jquery-ui/#{Rack::JQueryUI::JQUERY_UI_VERSION}/#{Rack::JQueryUI::JQUERY_UI_FILE_NAME}"
Timecop.travel Time.now + 86400 # add a day
get "/js/jquery-ui/#{Rack::JQueryUI::JQUERY_UI_VERSION}/#{Rack::JQueryUI::JQUERY_UI_FILE_NAME}", {}, {"HTTP_IF_MODIFIED_SINCE" => Rack::Utils.rfc2109(at_start) }
end
subject { last_response }
its(:status) { should == 304 }
end
end