Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Specs for Sinatra and Rails adapters complete

  • Loading branch information...
commit 6b17a8a21544dc4226a8092c98ed5686487fda31 1 parent c5c775e
Winton Welsh winton authored
24 lib/lilypad/adapters/rails.rb
@@ -3,15 +3,31 @@ module Rails
3 3
4 4 def self.included(base)
5 5 ENV['RACK_ENV'] = ENV['RAILS_ENV']
6   - base.send(:include, LilypadMethods) if Lilypad.production?
  6 + if Lilypad.production? && !base.included_modules.include?(InstanceMethods)
  7 + base.send :extend, ClassMethods
  8 + base.send :include, InstanceMethods
  9 + base.send :alias_method_chain, :rescue_action, :lilypad
  10 + base.class_eval do
  11 + class <<self
  12 + alias_method_chain :call_with_exception, :lilypad
  13 + end
  14 + end
  15 + end
  16 + end
  17 +
  18 + module ClassMethods
  19 +
  20 + def call_with_exception_with_lilypad(env, exception)
  21 + raise exception
  22 + end
7 23 end
8 24
9   - module LilypadMethods
  25 + module InstanceMethods
10 26
11 27 private
12 28
13   - def rescue_action_without_handler(exception)
14   - super
  29 + def rescue_action_with_lilypad(exception)
  30 + rescue_action_without_lilypad exception
15 31 Config::Request.action params[:action]
16 32 Config::Request.component params[:controller]
17 33 raise exception
3  lib/lilypad/adapters/sinatra.rb
@@ -7,4 +7,5 @@ def self.included(base)
7 7 end
8 8 end
9 9
10   -Sinatra::Base.send(:include, Lilypad::Sinatra)
  10 +Sinatra::Base.send(:include, Lilypad::Sinatra)
  11 +Sinatra::Application.send(:include, Lilypad::Sinatra)
6 lib/lilypad/config.rb
@@ -37,12 +37,6 @@ def rails
37 37 require "#{File.dirname(__FILE__)}/adapters/rails"
38 38 end
39 39
40   - def reset!
41   - self.instance_variables.each do |name|
42   - eval "#{name} = nil"
43   - end
44   - end
45   -
46 40 def sinatra
47 41 require "#{File.dirname(__FILE__)}/adapters/sinatra"
48 42 end
5 lib/lilypad/config/request.rb
@@ -14,9 +14,8 @@ def component(component=nil)
14 14 end
15 15
16 16 def reset!
17   - self.instance_variables.each do |name|
18   - eval "#{name} = nil"
19   - end
  17 + @action = nil
  18 + @component = nil
20 19 end
21 20 end
22 21 end
4 lib/lilypad/hoptoad/notify.rb
@@ -36,7 +36,7 @@ def backtrace
36 36 end
37 37
38 38 def filter(hash)
39   - return unless Config.filters
  39 + return hash unless Config.filters
40 40 hash.inject({}) do |acc, (key, val)|
41 41 match = Config.filters.any? { |f| key.to_s =~ Regexp.new(f) }
42 42 acc[key] = match ? "[FILTERED]" : val
@@ -62,6 +62,7 @@ def http_start(&block)
62 62
63 63 def parse
64 64 env = filter ENV.to_hash.merge(@env || {})
  65 +
65 66 if @env
66 67 request = Rack::Request.new @env
67 68 request_path = request.script_name + request.path_info
@@ -69,6 +70,7 @@ def parse
69 70 request = nil
70 71 request_path = 'Internal'
71 72 end
  73 +
72 74 [ backtrace, env, @exception, request, request_path ]
73 75 end
74 76
2  lib/lilypad/hoptoad/xml.rb
@@ -36,7 +36,7 @@ def build(backtrace, env, exception, request, request_path)
36 36 end
37 37 end
38 38 end
39   - if env.any?
  39 + if env && env.any?
40 40 r.tag! 'cgi-data' do |c|
41 41 env.each do |key, value|
42 42 c.var value.to_s, :key => key
2  lib/rack/lilypad.rb
@@ -3,7 +3,7 @@ class Lilypad
3 3
4 4 def initialize(app, api_key=nil, &block)
5 5 @app = app
6   - Lilypad api_key, &block
  6 + ::Lilypad.config api_key, &block
7 7 end
8 8
9 9 def call(env)
5 spec/fixtures/sinatra.rb
... ... @@ -1,7 +1,4 @@
1   -Sinatra::Base.class_eval do
2   - # Make sure the Sinatra adapter does its job
3   - set :raise_errors, false
4   -end
  1 +Sinatra::Base.set :raise_errors, false
5 2
6 3 class SinatraApp < Sinatra::Base
7 4
67 spec/lilypad/adapters/rails_spec.rb
... ... @@ -0,0 +1,67 @@
  1 +require File.expand_path("#{File.dirname __FILE__}/../../spec_helper")
  2 +
  3 +describe Lilypad::Rails do
  4 +
  5 + include Rack::Test::Methods
  6 +
  7 + def app
  8 + ActionController::Dispatcher.new
  9 + end
  10 +
  11 + describe :post do
  12 +
  13 + after(:each) do
  14 + ENV['RACK_ENV'] = 'production'
  15 + end
  16 +
  17 + before(:each) do
  18 + stub_net_http
  19 + end
  20 +
  21 + it "should post an error to Hoptoad" do
  22 + @http.should_receive(:post)
  23 + get "/test" rescue nil
  24 + end
  25 +
  26 + it "should post middleware exceptions" do
  27 + @http.should_receive(:post)
  28 + get "/nothing?test_exception=1" rescue nil
  29 + end
  30 +
  31 + it "should not post anything if non-production environment" do
  32 + ENV['RACK_ENV'] = 'development'
  33 + @http.should_not_receive(:post)
  34 + get "/test" rescue nil
  35 + end
  36 + end
  37 +
  38 + describe :RACK_ENV do
  39 +
  40 + before(:each) do
  41 + ENV['RACK_ENV'] = nil
  42 + ENV['RAILS_ENV'] = 'production'
  43 + ActionController::Base.send :include, Lilypad::Rails
  44 + end
  45 +
  46 + it "should set ENV['RACK_ENV']" do
  47 + ENV['RACK_ENV'].should == 'production'
  48 + end
  49 + end
  50 +
  51 + describe :rescue_action_without_handler do
  52 +
  53 + it "should set Config::Request.action" do
  54 + Lilypad::Config::Request.should_receive(:action).with('test')
  55 + get "/test" rescue nil
  56 + end
  57 +
  58 + it "should set Config::Request.component" do
  59 + Lilypad::Config::Request.should_receive(:component).with('application')
  60 + get "/test" rescue nil
  61 + end
  62 +
  63 + it "should re-raise the exception (ActionController::Failsafe disabled)" do
  64 + lambda { get "/test" }.should raise_error(TestError)
  65 + end
  66 + end
  67 +end
49 spec/lilypad/adapters/sinatra_spec.rb
... ... @@ -0,0 +1,49 @@
  1 +require File.expand_path("#{File.dirname __FILE__}/../../spec_helper")
  2 +
  3 +describe Lilypad::Sinatra do
  4 +
  5 + include Rack::Test::Methods
  6 +
  7 + def app
  8 + SinatraApp.new
  9 + end
  10 +
  11 + describe :post do
  12 +
  13 + after(:each) do
  14 + ENV['RACK_ENV'] = 'production'
  15 + end
  16 +
  17 + before(:each) do
  18 + stub_net_http
  19 + end
  20 +
  21 + it "should post an error to Hoptoad" do
  22 + @http.should_receive(:post)
  23 + get "/test" rescue nil
  24 + end
  25 +
  26 + it "should post middleware exceptions" do
  27 + @http.should_receive(:post)
  28 + get "/nothing?test_exception=1" rescue nil
  29 + end
  30 +
  31 + it "should not post anything if non-production environment" do
  32 + ENV['RACK_ENV'] = 'development'
  33 + @http.should_not_receive(:post)
  34 + get "/test" rescue nil
  35 + end
  36 + end
  37 +
  38 + describe :raise_errors do
  39 +
  40 + it "should set raise_errors to true" do
  41 + get "/nothing"
  42 + Sinatra::Base.raise_errors?.should == true
  43 + end
  44 +
  45 + it "should re-raise the exception" do
  46 + lambda { get "/test" }.should raise_error(TestError)
  47 + end
  48 + end
  49 +end
68 spec/lilypad_spec.rb
@@ -7,11 +7,7 @@
7 7 before(:each) do
8 8 @app = lambda { |env| raise TestError, 'Test' }
9 9 @env = Rack::MockRequest.env_for("/test")
10   - @http = mock(:http)
11   - @http.stub!(:read_timeout=)
12   - @http.stub!(:open_timeout=)
13   - @http.stub!(:post).and_return Net::HTTPOK.new(nil, nil, nil)
14   - Net::HTTP.stub!(:start).and_yield(@http)
  10 + stub_net_http
15 11 end
16 12
17 13 it "should yield a configuration object to the block when created" do
@@ -78,66 +74,4 @@
78 74 :repository => 't4'
79 75 )
80 76 end
81   -
82   - describe 'Rails' do
83   -
84   - def app
85   - ActionController::Dispatcher.new
86   - end
87   -
88   - it "should set ENV['RACK_ENV']" do
89   - ENV['RACK_ENV'].should == 'production'
90   - end
91   -
92   - it "should post an error to Hoptoad" do
93   - @http.should_receive(:post)
94   - get "/test" rescue nil
95   - end
96   -
97   - it "should re-raise the exception (with ActionController::Failsafe disabled)" do
98   - lambda { get "/test" }.should raise_error(TestError)
99   - end
100   -
101   - it "should catch middleware exceptions" do
102   - @http.should_receive(:post)
103   - get "/nothing?test_exception=1" rescue nil
104   - end
105   -
106   - it "should not do anything if non-production environment" do
107   - ENV['RACK_ENV'] = 'development'
108   - @http.should_not_receive(:post)
109   - get "/test" rescue nil
110   - end
111   - end
112   -
113   - describe 'Sinatra' do
114   -
115   - def app
116   - SinatraApp.new
117   - end
118   -
119   - before(:each) do
120   - ENV['RACK_ENV'] = 'production'
121   - end
122   -
123   - it "should post an error to Hoptoad" do
124   - @http.should_receive(:post)
125   - get "/test" rescue nil
126   - end
127   -
128   - it "should re-raise the exception" do
129   - lambda { get "/test" }.should raise_error(TestError)
130   - end
131   -
132   - it "should catch middleware exceptions" do
133   - @http.should_receive(:post)
134   - get "/nothing?test_exception=1" rescue nil
135   - end
136   -
137   - it "should not do anything if non-production environment" do
138   - ENV['RACK_ENV'] = 'development'
139   - @http.should_not_receive(:post)
140   - get "/test" rescue nil
141   - end
142   - end
143 77 end
8 spec/spec_helper.rb
@@ -27,6 +27,14 @@ def debug(object)
27 27 puts "</pre>"
28 28 end
29 29
  30 +def stub_net_http
  31 + @http = mock(:http)
  32 + @http.stub!(:read_timeout=)
  33 + @http.stub!(:open_timeout=)
  34 + @http.stub!(:post).and_return Net::HTTPOK.new(nil, nil, nil)
  35 + Net::HTTP.stub!(:start).and_yield(@http)
  36 +end
  37 +
30 38 def validate_xml
31 39 xsd = Nokogiri::XML::Schema(File.read(SPEC + '/fixtures/hoptoad_2_0.xsd'))
32 40 doc = Nokogiri::XML(Lilypad::Hoptoad::XML.last_request)

0 comments on commit 6b17a8a

Please sign in to comment.
Something went wrong with that request. Please try again.