# encoding: utf-8 # This file is distributed under New Relic's license terms. # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details. require File.expand_path(File.join(File.dirname(__FILE__),'..', '..', 'test_helper')) require 'rack/test' require 'new_relic/rack/browser_monitoring' ENV['RACK_ENV'] = 'test' # we should expand the environments we support, any rack app could # benefit from auto-rum, but the truth of the matter is that atm # we only support Rails >= 2.3 def middleware_supported? ::Rails::VERSION::STRING >= '2.3' end if middleware_supported? class BrowserMonitoringTest < Test::Unit::TestCase include Rack::Test::Methods class TestApp def self.doc=(other) @@doc = other end def call(env) @@doc ||= <<-EOL im a title im some body text EOL [200, {'Content-Type' => 'text/html'}, Rack::Response.new(@@doc)] end include NewRelic::Agent::Instrumentation::Rack end def app NewRelic::Rack::BrowserMonitoring.new(TestApp.new) end def setup super clear_cookies @config = { :browser_key => 'some browser key', :beacon => 'beacon', :application_id => 5, :'rum.enabled' => true, :episodes_file => 'this_is_my_file', :license_key => 'a' * 40 } NewRelic::Agent.config.apply_config(@config) NewRelic::Agent.manual_start config = NewRelic::Agent::BeaconConfiguration.new NewRelic::Agent.instance.stubs(:beacon_configuration).returns(config) NewRelic::Agent.stubs(:is_transaction_traced?).returns(true) end def teardown super clear_cookies mocha_teardown TestApp.doc = nil NewRelic::Agent.config.remove_config(@config) end def test_make_sure_header_is_set assert NewRelic::Agent.browser_timing_header.size > 0 end def test_make_sure_footer_is_set assert NewRelic::Agent.browser_timing_footer.size > 0 end def test_should_only_instrument_successfull_html_requests assert app.should_instrument?(200, {'Content-Type' => 'text/html'}) assert !app.should_instrument?(500, {'Content-Type' => 'text/html'}) assert !app.should_instrument?(200, {'Content-Type' => 'text/xhtml'}) end def test_should_not_instrument_when_content_disposition assert !app.should_instrument?(200, {'Content-Type' => 'text/html', 'Content-Disposition' => 'attachment; filename=test.html'}) end def test_insert_timing_header_right_after_open_head_if_no_meta_tags get '/' assert(last_response.body.include?("head>#{NewRelic::Agent.browser_timing_header}"), last_response.body) TestApp.doc = nil end def test_insert_timing_header_right_before_head_close_if_ua_compatible_found TestApp.doc = <<-EOL im a title im some body text EOL get '/' assert(last_response.body.include?("#{NewRelic::Agent.browser_timing_header}"), last_response.body) end def test_insert_timing_footer_right_before_html_body_close get '/' assert_match(/.*NREUMQ\.push.*new Date\(\)\.getTime\(\),"","","","",""\]\);<\/script><\/body>/, last_response.body) end def test_should_not_throw_exception_on_empty_reponse TestApp.doc = '' get '/' assert last_response.ok? end def test_token_is_set_in_footer_when_set_by_cookie token = '1234567890987654321' set_cookie "NRAGENT=tk=#{token}" get '/' assert(last_response.body.include?(token), last_response.body) end def test_guid_is_set_in_footer_when_token_is_set guid = 'abcdefgfedcba' NewRelic::TransactionSample.any_instance.stubs(:generate_guid).returns(guid) set_cookie "NRAGENT=tk=token" with_config(:apdex_t => 0.0001) do get '/' assert(last_response.body.include?(guid), last_response.body) end end end else warn "Skipping rum auto instrumentation tests" end