# 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/agent/instrumentation/rack'
require 'new_relic/rack/browser_monitoring'
ENV['RACK_ENV'] = 'test'
class BrowserMonitoringTest < MiniTest::Unit::TestCase
include Rack::Test::Methods
class TestApp
@@doc = nil
@@next_response = nil
def self.doc=(other)
@@doc = other
end
def self.next_response=(next_response)
@@next_response = next_response
end
def self.next_response
@@next_response
end
def call(env)
@@doc ||= <<-EOL
im a title
im some body text
EOL
response = @@next_response || Rack::Response.new(@@doc)
@@next_response = nil
[200, {'Content-Type' => 'text/html'}, response]
end
include NewRelic::Agent::Instrumentation::Rack
end
def app
NewRelic::Rack::BrowserMonitoring.new(TestApp.new)
end
def setup
super
@config = {
:application_id => 5,
:beacon => 'beacon',
:browser_key => 'some browser key',
:'rum.enabled' => true,
:license_key => 'a' * 40,
:js_agent_loader => 'loader',
}
NewRelic::Agent.config.apply_config(@config)
end
def teardown
super
TestApp.doc = nil
NewRelic::Agent.config.remove_config(@config)
NewRelic::Agent.agent.transaction_sampler.reset!
end
def test_make_sure_header_is_set
in_transaction do
assert NewRelic::Agent.browser_timing_header.size > 0
end
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_should_not_instrument_when_already_did
assert !app.should_instrument?({NewRelic::Rack::BrowserMonitoring::ALREADY_INSTRUMENTED_KEY => true}, 200, {'Content-Type' => 'text/html'})
end
def test_insert_header_should_mark_environment
get '/'
assert last_request.env.key?(NewRelic::Rack::BrowserMonitoring::ALREADY_INSTRUMENTED_KEY)
end
# RUM header auto-insertion testing
# We read *.source.html files from the test/rum directory, and then
# compare the results of them to *.result.html files.
source_files = Dir[File.join(File.dirname(__FILE__), "..", "..", "rum", "*.source.html")]
RUM_LOADER = "|||I AM THE RUM HEADER|||"
RUM_CONFIG = "|||I AM THE RUM FOOTER|||"
source_files.each do |source_file|
source_filename = File.basename(source_file).gsub(".", "_")
source_html = File.read(source_file)
result_file = source_file.gsub(".source.", ".result.")
define_method("test_#{source_filename}") do
TestApp.doc = source_html
NewRelic::Agent.stubs(:browser_timing_header).returns(RUM_CONFIG + RUM_LOADER)
get '/'
expected_content = File.read(result_file)
assert_equal(expected_content, last_response.body)
end
define_method("test_dont_touch_#{source_filename}") do
TestApp.doc = source_html
NewRelic::Rack::BrowserMonitoring.any_instance.stubs(:should_instrument?).returns(false)
get '/'
assert_equal(source_html, last_response.body)
end
end
def test_should_close_response
TestApp.next_response = Rack::Response.new("