Skip to content
Browse files

Extract request info from rack env

  • Loading branch information...
1 parent 741a4d9 commit 1bab4e36ed09be130cfb65cf7c74077a39e3ab6e @jferris jferris committed Mar 23, 2010
View
1 features/rails.feature
@@ -78,6 +78,7 @@ Feature: Install the Gem in a Rails application
session[:value] = "test"
raise RuntimeError, "some message"
"""
+ And I route "/test/index" to "test#index"
And I perform a request to "http://example.com:123/test/index?param=value"
Then I should receive the following Hoptoad notification:
| component | test |
View
83 features/step_definitions/rails_application_steps.rb
@@ -1,3 +1,5 @@
+require 'uri'
+
When /^I generate a new Rails application$/ do
@terminal.cd(TEMP_DIR)
version_string = ENV['RAILS_VERSION']
@@ -175,34 +177,45 @@
end
When /^I perform a request to "([^\"]*)"$/ do |uri|
- uri = URI.parse(uri)
- request_script = <<-SCRIPT
- require 'cgi'
- class CGIWrapper < CGI
- def initialize(*args)
- @env_table = {}
- @stdinput = $stdin
- super(*args)
+ if rails_uses_rack?
+ request_script = <<-SCRIPT
+ require 'config/environment'
+ env = Rack::MockRequest.env_for(#{uri.inspect})
+ RailsRoot::Application.call(env)
+ SCRIPT
+ File.open(File.join(RAILS_ROOT, 'request.rb'), 'w') { |file| file.write(request_script) }
+ @terminal.cd(RAILS_ROOT)
+ @terminal.run("./script/rails runner -e production request.rb")
+ else
+ uri = URI.parse(uri)
+ request_script = <<-SCRIPT
+ require 'cgi'
+ class CGIWrapper < CGI
+ def initialize(*args)
+ @env_table = {}
+ @stdinput = $stdin
+ super(*args)
+ end
+ attr_reader :env_table
end
- attr_reader :env_table
- end
- $stdin = StringIO.new("")
- cgi = CGIWrapper.new
- cgi.env_table.update({
- 'HTTPS' => 'off',
- 'REQUEST_METHOD' => "GET",
- 'HTTP_HOST' => #{[uri.host, uri.port].join(':').inspect},
- 'SERVER_PORT' => #{uri.port.inspect},
- 'REQUEST_URI' => #{uri.request_uri.inspect},
- 'PATH_INFO' => #{uri.path.inspect},
- 'QUERY_STRING' => #{uri.query.inspect}
- })
- require 'dispatcher' unless defined?(ActionController::Dispatcher)
- Dispatcher.dispatch(cgi)
- SCRIPT
- File.open(File.join(RAILS_ROOT, 'request.rb'), 'w') { |file| file.write(request_script) }
- @terminal.cd(RAILS_ROOT)
- @terminal.run("./script/runner -e production request.rb")
+ $stdin = StringIO.new("")
+ cgi = CGIWrapper.new
+ cgi.env_table.update({
+ 'HTTPS' => 'off',
+ 'REQUEST_METHOD' => "GET",
+ 'HTTP_HOST' => #{[uri.host, uri.port].join(':').inspect},
+ 'SERVER_PORT' => #{uri.port.inspect},
+ 'REQUEST_URI' => #{uri.request_uri.inspect},
+ 'PATH_INFO' => #{uri.path.inspect},
+ 'QUERY_STRING' => #{uri.query.inspect}
+ })
+ require 'dispatcher' unless defined?(ActionController::Dispatcher)
+ Dispatcher.dispatch(cgi)
+ SCRIPT
+ File.open(File.join(RAILS_ROOT, 'request.rb'), 'w') { |file| file.write(request_script) }
+ @terminal.cd(RAILS_ROOT)
+ @terminal.run("./script/runner -e production request.rb")
+ end
end
Then /^I should receive the following Hoptoad notification:$/ do |table|
@@ -245,3 +258,19 @@ def initialize(*args)
Then /^the command should have run successfully$/ do
@terminal.status.should == 0
end
+
+When /^I route "([^\"]*)" to "([^\"]*)"$/ do |path, controller_action_pair|
+ route = if rails3?
+ %(match "#{path}", :to => "#{controller_action_pair}")
+ else
+ controller, action = controller_aciton_pair.split('#')
+ %(map.connect "#{path}", :controller => "#{controller}", :action => "#{action}")
+ end
+ routes_file = File.join(RAILS_ROOT, "config", "routes.rb")
+ File.open(routes_file, "r+") do |file|
+ content = file.read
+ content.gsub!(/^end$/, " #{route}\nend")
+ file.rewind
+ file.write(content)
+ end
+end
View
4 features/support/rails.rb
@@ -7,6 +7,10 @@ def rails3?
rails_version =~ /^3/
end
+ def rails_uses_rack?
+ rails3?
+ end
+
def rails_version
@rails_version ||= begin
if bundler_manages_gems?
View
19 lib/hoptoad_notifier/notice.rb
@@ -80,9 +80,12 @@ def initialize(args)
self.ignore_by_filters = args[:ignore_by_filters] || []
self.backtrace_filters = args[:backtrace_filters] || []
self.params_filters = args[:params_filters] || []
- self.parameters = args[:parameters] || rack_env(:params) || {}
- self.component = args[:component] || args[:controller]
- self.action = args[:action]
+ self.parameters = args[:parameters] ||
+ action_dispatch_params ||
+ rack_env(:params) ||
+ {}
+ self.component = args[:component] || args[:controller] || parameters['controller']
+ self.action = args[:action] || parameters['action']
self.environment_name = args[:environment_name]
self.cgi_data = args[:cgi_data] || args[:rack_env]
@@ -268,7 +271,7 @@ def filter_key?(key)
end
def find_session_data
- self.session_data = args[:session_data] || args[:session] || {}
+ self.session_data = args[:session_data] || args[:session] || rack_session || {}
self.session_data = session_data[:data] if session_data[:data]
end
@@ -303,5 +306,13 @@ def rack_request
::Rack::Request.new(args[:rack_env])
end
end
+
+ def action_dispatch_params
+ args[:rack_env]['action_dispatch.request.parameters'] if args[:rack_env]
+ end
+
+ def rack_session
+ args[:rack_env]['rack.session'] if args[:rack_env]
+ end
end
end
View
32 test/notice_test.rb
@@ -339,9 +339,6 @@ def stub_request(attrs = {})
end
should "extract data from a rack environment hash" do
- # TODO: extract session data
- # TODO: extract controller
- # TODO: extract action
url = "https://subdomain.happylane.com:100/test/file.rb?var=value&var2=value2"
parameters = { 'var' => 'value', 'var2' => 'value2' }
env = Rack::MockRequest.env_for(url)
@@ -353,6 +350,35 @@ def stub_request(attrs = {})
assert_equal 'GET', notice.cgi_data['REQUEST_METHOD']
end
+ should "extract data from a rack environment hash with action_dispatch info" do
+ params = { 'controller' => 'users', 'action' => 'index', 'id' => '7' }
+ env = Rack::MockRequest.env_for('/', { 'action_dispatch.request.parameters' => params })
+
+ notice = build_notice(:rack_env => env)
+
+ assert_equal params, notice.parameters
+ assert_equal params['controller'], notice.component
+ assert_equal params['action'], notice.action
+ end
+
+ should "extract session data from a rack environment" do
+ session_data = { 'something' => 'some value' }
+ env = Rack::MockRequest.env_for('/', 'rack.session' => session_data)
+
+ notice = build_notice(:rack_env => env)
+
+ assert_equal session_data, notice.session_data
+ end
+
+ should "prefer passed session data to rack session data" do
+ session_data = { 'something' => 'some value' }
+ env = Rack::MockRequest.env_for('/')
+
+ notice = build_notice(:rack_env => env, :session_data => session_data)
+
+ assert_equal session_data, notice.session_data
+ end
+
def assert_accepts_exception_attribute(attribute, args = {}, &block)
exception = build_exception
block ||= lambda { exception.send(attribute) }

0 comments on commit 1bab4e3

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