PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/test/new_relic/agent/beacon_configuration_test.rb

http://github.com/newrelic/rpm
Ruby | 111 lines | 97 code | 14 blank | 0 comment | 2 complexity | ac76edb1a4372cc82d73deda17ffd555 MD5 | raw file
Possible License(s): Apache-2.0
  1. require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
  2. require "new_relic/agent/beacon_configuration"
  3. class NewRelic::Agent::BeaconConfigurationTest < Test::Unit::TestCase
  4. def test_initialize_basic
  5. connect_data = {}
  6. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  7. assert_equal true, bc.rum_enabled
  8. assert_equal '', bc.browser_timing_header
  9. %w[application_id browser_monitoring_key beacon].each do |method|
  10. value = bc.send(method.to_sym)
  11. assert_equal nil, value, "Expected #{method} to be nil, but was #{value.inspect}"
  12. end
  13. end
  14. def test_initialize_with_real_data
  15. connect_data = {'browser_key' => 'a browser monitoring key', 'application_id' => 'an application id', 'beacon' => 'a beacon', 'rum_enabled' => true}
  16. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  17. assert_equal(true, bc.rum_enabled)
  18. assert_equal('a browser monitoring key', bc.browser_monitoring_key)
  19. assert_equal('an application id', bc.application_id)
  20. assert_equal('a beacon', bc.beacon)
  21. s = "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"
  22. assert_equal(s, bc.browser_timing_header)
  23. end
  24. def test_license_bytes_nil
  25. connect_data = {}
  26. NewRelic::Control.instance.expects(:license_key).returns('a' * 40).once
  27. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  28. assert_equal([97] * 40, bc.license_bytes, 'should return the bytes of the license key')
  29. end
  30. def test_license_bytes_existing_bytes
  31. connect_data = {}
  32. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  33. bc.instance_eval { @license_bytes = [97] * 40 }
  34. NewRelic::Control.instance.expects(:license_key).never
  35. assert_equal([97] * 40, bc.license_bytes, "should return the cached value if it exists")
  36. end
  37. def test_license_bytes_should_set_instance_cache
  38. connect_data = {}
  39. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  40. NewRelic::Control.instance.expects(:license_key).returns('a' * 40)
  41. bc.instance_eval { @license_bytes = nil }
  42. bc.license_bytes
  43. assert_equal([97] * 40, bc.instance_variable_get('@license_bytes'), "should cache the license bytes for later")
  44. end
  45. def test_build_browser_timing_header_disabled
  46. connect_data = {}
  47. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  48. bc.instance_eval { @rum_enabled = false }
  49. assert_equal '', bc.build_browser_timing_header, "should not return a header when rum enabled is false"
  50. end
  51. def test_build_browser_timing_header_enabled_but_no_key
  52. connect_data = {}
  53. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  54. bc.instance_eval { @rum_enabled = true; @browser_monitoring_key = nil }
  55. assert_equal '', bc.build_browser_timing_header, "should not return a header when browser_monitoring_key is nil"
  56. end
  57. def test_build_browser_timing_header_enabled_with_key
  58. connect_data = {}
  59. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  60. bc.instance_eval { @browser_monitoring_key = 'a browser monitoring key' }
  61. assert(bc.build_browser_timing_header.include?('NREUMQ'), "header should be generated when rum is enabled and browser monitoring key is set")
  62. end
  63. def test_build_browser_timing_header_should_html_safe_header
  64. mock_javascript = mock('javascript')
  65. connect_data = {'browser_key' => 'a' * 40}
  66. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  67. assert_equal('a' * 40, bc.instance_variable_get('@browser_monitoring_key'), "should save the key from the config")
  68. bc.expects(:javascript_header).returns(mock_javascript)
  69. mock_javascript.expects(:respond_to?).with(:html_safe).returns(true)
  70. mock_javascript.expects(:html_safe)
  71. bc.build_browser_timing_header
  72. end
  73. def test_build_load_file_js_load_episodes_file_false
  74. connect_data = {'rum.load_episodes_file' => false}
  75. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  76. s = "if (!NREUMQ.f) { NREUMQ.f=function() {\nNREUMQ.push([\"load\",new Date().getTime()]);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\n"
  77. assert_equal(s, bc.build_load_file_js(connect_data))
  78. end
  79. def test_build_load_file_js_load_episodes_file_missing
  80. connect_data = {}
  81. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  82. s = "if (!NREUMQ.f) { NREUMQ.f=function() {\nNREUMQ.push([\"load\",new Date().getTime()]);\nvar e=document.createElement(\"script\");\ne.type=\"text/javascript\";e.async=true;e.src=\"\";\ndocument.body.appendChild(e);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\n"
  83. assert_equal(s, bc.build_load_file_js(connect_data))
  84. end
  85. def test_build_load_file_js_load_episodes_file_present
  86. connect_data = {'rum.load_episodes_file' => true}
  87. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  88. s = "if (!NREUMQ.f) { NREUMQ.f=function() {\nNREUMQ.push([\"load\",new Date().getTime()]);\nvar e=document.createElement(\"script\");\ne.type=\"text/javascript\";e.async=true;e.src=\"\";\ndocument.body.appendChild(e);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\n"
  89. assert_equal(s, bc.build_load_file_js(connect_data))
  90. end
  91. def test_build_load_file_js_load_episodes_file_with_episodes_url
  92. connect_data = {'episodes_url' => 'an episodes url'}
  93. bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
  94. assert(bc.build_load_file_js(connect_data).include?('an episodes url'),
  95. "should include the episodes url by default")
  96. end
  97. end