Permalink
Browse files

Specs for Sinatra and Rails adapters complete

  • Loading branch information...
1 parent c5c775e commit 6b17a8a21544dc4226a8092c98ed5686487fda31 @winton winton committed Dec 6, 2009
@@ -3,15 +3,31 @@ module Rails
def self.included(base)
ENV['RACK_ENV'] = ENV['RAILS_ENV']
- base.send(:include, LilypadMethods) if Lilypad.production?
+ if Lilypad.production? && !base.included_modules.include?(InstanceMethods)
+ base.send :extend, ClassMethods
+ base.send :include, InstanceMethods
+ base.send :alias_method_chain, :rescue_action, :lilypad
+ base.class_eval do
+ class <<self
+ alias_method_chain :call_with_exception, :lilypad
+ end
+ end
+ end
+ end
+
+ module ClassMethods
+
+ def call_with_exception_with_lilypad(env, exception)
+ raise exception
+ end
end
- module LilypadMethods
+ module InstanceMethods
private
- def rescue_action_without_handler(exception)
- super
+ def rescue_action_with_lilypad(exception)
+ rescue_action_without_lilypad exception
Config::Request.action params[:action]
Config::Request.component params[:controller]
raise exception
@@ -7,4 +7,5 @@ def self.included(base)
end
end
-Sinatra::Base.send(:include, Lilypad::Sinatra)
+Sinatra::Base.send(:include, Lilypad::Sinatra)
+Sinatra::Application.send(:include, Lilypad::Sinatra)
View
@@ -37,12 +37,6 @@ def rails
require "#{File.dirname(__FILE__)}/adapters/rails"
end
- def reset!
- self.instance_variables.each do |name|
- eval "#{name} = nil"
- end
- end
-
def sinatra
require "#{File.dirname(__FILE__)}/adapters/sinatra"
end
@@ -14,9 +14,8 @@ def component(component=nil)
end
def reset!
- self.instance_variables.each do |name|
- eval "#{name} = nil"
- end
+ @action = nil
+ @component = nil
end
end
end
@@ -36,7 +36,7 @@ def backtrace
end
def filter(hash)
- return unless Config.filters
+ return hash unless Config.filters
hash.inject({}) do |acc, (key, val)|
match = Config.filters.any? { |f| key.to_s =~ Regexp.new(f) }
acc[key] = match ? "[FILTERED]" : val
@@ -62,13 +62,15 @@ def http_start(&block)
def parse
env = filter ENV.to_hash.merge(@env || {})
+
if @env
request = Rack::Request.new @env
request_path = request.script_name + request.path_info
else
request = nil
request_path = 'Internal'
end
+
[ backtrace, env, @exception, request, request_path ]
end
@@ -36,7 +36,7 @@ def build(backtrace, env, exception, request, request_path)
end
end
end
- if env.any?
+ if env && env.any?
r.tag! 'cgi-data' do |c|
env.each do |key, value|
c.var value.to_s, :key => key
View
@@ -3,7 +3,7 @@ class Lilypad
def initialize(app, api_key=nil, &block)
@app = app
- Lilypad api_key, &block
+ ::Lilypad.config api_key, &block
end
def call(env)
View
@@ -1,7 +1,4 @@
-Sinatra::Base.class_eval do
- # Make sure the Sinatra adapter does its job
- set :raise_errors, false
-end
+Sinatra::Base.set :raise_errors, false
class SinatraApp < Sinatra::Base
@@ -0,0 +1,67 @@
+require File.expand_path("#{File.dirname __FILE__}/../../spec_helper")
+
+describe Lilypad::Rails do
+
+ include Rack::Test::Methods
+
+ def app
+ ActionController::Dispatcher.new
+ end
+
+ describe :post do
+
+ after(:each) do
+ ENV['RACK_ENV'] = 'production'
+ end
+
+ before(:each) do
+ stub_net_http
+ end
+
+ it "should post an error to Hoptoad" do
+ @http.should_receive(:post)
+ get "/test" rescue nil
+ end
+
+ it "should post middleware exceptions" do
+ @http.should_receive(:post)
+ get "/nothing?test_exception=1" rescue nil
+ end
+
+ it "should not post anything if non-production environment" do
+ ENV['RACK_ENV'] = 'development'
+ @http.should_not_receive(:post)
+ get "/test" rescue nil
+ end
+ end
+
+ describe :RACK_ENV do
+
+ before(:each) do
+ ENV['RACK_ENV'] = nil
+ ENV['RAILS_ENV'] = 'production'
+ ActionController::Base.send :include, Lilypad::Rails
+ end
+
+ it "should set ENV['RACK_ENV']" do
+ ENV['RACK_ENV'].should == 'production'
+ end
+ end
+
+ describe :rescue_action_without_handler do
+
+ it "should set Config::Request.action" do
+ Lilypad::Config::Request.should_receive(:action).with('test')
+ get "/test" rescue nil
+ end
+
+ it "should set Config::Request.component" do
+ Lilypad::Config::Request.should_receive(:component).with('application')
+ get "/test" rescue nil
+ end
+
+ it "should re-raise the exception (ActionController::Failsafe disabled)" do
+ lambda { get "/test" }.should raise_error(TestError)
+ end
+ end
+end
@@ -0,0 +1,49 @@
+require File.expand_path("#{File.dirname __FILE__}/../../spec_helper")
+
+describe Lilypad::Sinatra do
+
+ include Rack::Test::Methods
+
+ def app
+ SinatraApp.new
+ end
+
+ describe :post do
+
+ after(:each) do
+ ENV['RACK_ENV'] = 'production'
+ end
+
+ before(:each) do
+ stub_net_http
+ end
+
+ it "should post an error to Hoptoad" do
+ @http.should_receive(:post)
+ get "/test" rescue nil
+ end
+
+ it "should post middleware exceptions" do
+ @http.should_receive(:post)
+ get "/nothing?test_exception=1" rescue nil
+ end
+
+ it "should not post anything if non-production environment" do
+ ENV['RACK_ENV'] = 'development'
+ @http.should_not_receive(:post)
+ get "/test" rescue nil
+ end
+ end
+
+ describe :raise_errors do
+
+ it "should set raise_errors to true" do
+ get "/nothing"
+ Sinatra::Base.raise_errors?.should == true
+ end
+
+ it "should re-raise the exception" do
+ lambda { get "/test" }.should raise_error(TestError)
+ end
+ end
+end
View
@@ -7,11 +7,7 @@
before(:each) do
@app = lambda { |env| raise TestError, 'Test' }
@env = Rack::MockRequest.env_for("/test")
- @http = mock(:http)
- @http.stub!(:read_timeout=)
- @http.stub!(:open_timeout=)
- @http.stub!(:post).and_return Net::HTTPOK.new(nil, nil, nil)
- Net::HTTP.stub!(:start).and_yield(@http)
+ stub_net_http
end
it "should yield a configuration object to the block when created" do
@@ -78,66 +74,4 @@
:repository => 't4'
)
end
-
- describe 'Rails' do
-
- def app
- ActionController::Dispatcher.new
- end
-
- it "should set ENV['RACK_ENV']" do
- ENV['RACK_ENV'].should == 'production'
- end
-
- it "should post an error to Hoptoad" do
- @http.should_receive(:post)
- get "/test" rescue nil
- end
-
- it "should re-raise the exception (with ActionController::Failsafe disabled)" do
- lambda { get "/test" }.should raise_error(TestError)
- end
-
- it "should catch middleware exceptions" do
- @http.should_receive(:post)
- get "/nothing?test_exception=1" rescue nil
- end
-
- it "should not do anything if non-production environment" do
- ENV['RACK_ENV'] = 'development'
- @http.should_not_receive(:post)
- get "/test" rescue nil
- end
- end
-
- describe 'Sinatra' do
-
- def app
- SinatraApp.new
- end
-
- before(:each) do
- ENV['RACK_ENV'] = 'production'
- end
-
- it "should post an error to Hoptoad" do
- @http.should_receive(:post)
- get "/test" rescue nil
- end
-
- it "should re-raise the exception" do
- lambda { get "/test" }.should raise_error(TestError)
- end
-
- it "should catch middleware exceptions" do
- @http.should_receive(:post)
- get "/nothing?test_exception=1" rescue nil
- end
-
- it "should not do anything if non-production environment" do
- ENV['RACK_ENV'] = 'development'
- @http.should_not_receive(:post)
- get "/test" rescue nil
- end
- end
end
View
@@ -27,6 +27,14 @@ def debug(object)
puts "</pre>"
end
+def stub_net_http
+ @http = mock(:http)
+ @http.stub!(:read_timeout=)
+ @http.stub!(:open_timeout=)
+ @http.stub!(:post).and_return Net::HTTPOK.new(nil, nil, nil)
+ Net::HTTP.stub!(:start).and_yield(@http)
+end
+
def validate_xml
xsd = Nokogiri::XML::Schema(File.read(SPEC + '/fixtures/hoptoad_2_0.xsd'))
doc = Nokogiri::XML(Lilypad::Hoptoad::XML.last_request)

0 comments on commit 6b17a8a

Please sign in to comment.