spec/lib/fozzie/rails/middleware_spec.rb in fozzie-0.0.22 vs spec/lib/fozzie/rails/middleware_spec.rb in fozzie-0.0.23
- old
+ new
@@ -1,81 +1,125 @@
require 'spec_helper'
require 'action_controller'
describe Fozzie::Rails::Middleware do
- let(:path_info) { '/somewhere/railsy' }
- let(:fake_env) { ({ 'PATH_INFO' => path_info }) }
- let(:routing) { mock 'routing' }
+ let(:routes) { mock "routes" }
subject do
RailsApp = Class.new do
def call(env); env end
end unless defined?(RailsApp)
- Rails = RailsApp unless defined?(Rails)
Fozzie::Rails::Middleware.new RailsApp.new
end
- describe "subject" do
- it "returns env on call for testing" do
- subject.call({}).should == {}
+ before do
+ Rails = rails unless defined?(Rails)
+ end
+
+ describe "#rails_version" do
+ let(:version) { "10.9.8" }
+ let(:rails) { mock "rails" }
+
+ before do
+ ::Rails.stubs(:version).returns(version)
end
+
+ it "returns the major version from Rails.version" do
+ subject.rails_version.should == 10
+ end
end
+
+ describe "#routing_lookup" do
+ context "when #rails_version == 3" do
+ let(:app) { mock "app" }
+
+ before do
+ subject.stubs(:rails_version).returns(3)
+ end
- describe "rails 2" do
- let!(:version) { RailsApp.stubs(:version).returns("2.3.1") }
-
- it "#generate_key" do
- subject.stubs(:routing_lookup).returns(routing)
- routing.expects(:recognize_path)
- .with(path_info)
- .returns({:controller => 'somewhere', :action => 'railsy'})
-
- subject.generate_key(fake_env).should == 'somewhere.railsy.render'
+ it "returns Rails.application.routes" do
+ Rails.expects(:application).returns(app)
+ app.expects(:routes).returns(routes)
+ subject.routing_lookup.should eq routes
+ end
end
+
+ context "when #rails_version does not == 3" do
+ before do
+ if defined?(ActionController::Routing::Routes)
+
+ @old_routes_const = ActionController::Routing::Routes
+ ActionController::Routing.send(:remove_const, :Routes)
+ end
+ ActionController::Routing::Routes = routes
+ end
- it "returns nil on routing error" do
- subject.stubs(:routing_lookup).returns(routing)
- routing.expects(:recognize_path)
- .with(path_info)
- .raises(RuntimeError)
+ after do
+ if @old_routes_const
+ ActionController::Routing.send(:remove_const, :Routes)
+ ActionController::Routing::Routes = @old_routes_const
+ end
+ end
- subject.generate_key(fake_env).should == nil
+ it "returns ActionController::Routing::Routes" do
+ subject.routing_lookup.should eq routes
+ end
end
-
end
- describe "rails 3" do
- let!(:rails) { RailsApp.stubs(:application).returns(Class.new) }
- let!(:version) { RailsApp.stubs(:version).returns("3.1.1") }
-
- it "#generate_key" do
- subject.stubs(:routing_lookup).returns(routing)
- routing.expects(:recognize_path)
- .with(path_info)
- .returns({:controller => 'somewhere', :action => 'railsy'})
-
- subject.generate_key(fake_env).should == 'somewhere.railsy.render'
+ describe "#generate_key" do
+ let(:env) { mock "env" }
+ let(:path) { mock "path" }
+ let(:request_method) { mock "request_method" }
+
+ it "gets the path_info and request method from env parameter" do
+ env.expects(:[]).with("PATH_INFO")
+ env.expects(:[]).with("REQUEST_METHOD")
+ subject.generate_key(env)
end
- it "returns nil on error" do
- subject.stubs(:routing_lookup).returns(routing)
- routing.expects(:recognize_path)
- .with(path_info)
- .raises(RuntimeError)
+ context "when path_info is nil" do
+ let(:env) { { "PATH_INFO" => nil } }
+
+ it "does not lookup routing" do
+ subject.expects(:routing_lookup).never
+ subject.generate_key(env)
+ end
- subject.generate_key(fake_env).should == nil
+ it "does not register any stats" do
+ S.expects(:increment).never
+ end
+
+ it "returns nil" do
+ subject.generate_key(env).should be_nil
+ end
end
- it "returns nil on routing error" do
- subject.stubs(:routing_lookup).returns(routing)
- routing.expects(:recognize_path)
- .with(path_info)
- .raises(RuntimeError)
+ context "when path info is not nil" do
+ let(:env) { { "PATH_INFO" => path, "REQUEST_METHOD" => request_method } }
+
+ before do
+ subject.stubs(:routing_lookup).returns(routes)
+ routes.stubs(:recognize_path).returns({ :controller => "controller",
+ :action => "action" })
+ end
+
+ it "looks up controller and action for the path and request method" do
+ subject.expects(:routing_lookup).returns(routes)
+ routes.expects(:recognize_path).with(path, :method => request_method)
+ subject.generate_key(env)
+ end
- S.expects(:increment).with('routing.error')
-
- subject.generate_key(fake_env).should == nil
+ it "returns a bucket generated from the controller, action, and 'render'" do
+ subject.generate_key(env).should eq "controller.action.render"
+ end
end
+ end
+
+ describe "subject" do
+ it "returns env on call for testing" do
+ subject.call({}).should == {}
+ end
end
-end
\ No newline at end of file
+end