<commit_before>require 'logger'
require 'socket'
require 'spec_helper'

describe Ratchetio do

  context 'report_exception' do
    before(:each) do
      configure
      Ratchetio.configure do |config|
        config.logger = logger_mock
      end
      
      begin
        foo = bar
      rescue => e
        @exception = e
      end
    end
   
    let(:logger_mock) { double("Rails.logger").as_null_object }

    it 'should report exceptions without person or request data' do
      logger_mock.should_receive(:info).with('[Ratchet.io] Success')
      Ratchetio.report_exception(@exception)
    end

    it 'should not report anything when disabled' do
      logger_mock.should_not_receive(:info).with('[Ratchet.io] Success')
      Ratchetio.configure do |config|
        config.enabled = false
      end
      
      Ratchetio.report_exception(@exception)
      
      Ratchetio.configure do |config|
        config.enabled = true
      end
    end

    it 'should report exceptions with request and person data' do
      logger_mock.should_receive(:info).with('[Ratchet.io] Success')
      request_data = {
        :params => { :foo => "bar" },
        :url => 'http://localhost/',
        :user_ip => '127.0.0.1',
        :headers => {},
        :GET => { "baz" => "boz" },
        :session => { :user_id => 123 },
        :method => "GET",
      }
      person_data = {
        :id => 1,
        :username => "test",
        :email => "<EMAIL>"
      }
      Ratchetio.report_exception(@exception, request_data, person_data)
    end

    it 'should ignore ignored exception classes' do
      saved_filters = Ratchetio.configuration.exception_level_filters
      Ratchetio.configure do |config|
        config.exception_level_filters = { 'NameError' => 'ignore' }
      end

      logger_mock.should_not_receive(:info)
      logger_mock.should_not_receive(:warn)
      logger_mock.should_not_receive(:error)
      
      Ratchetio.report_exception(@exception)
      
      Ratchetio.configure do |config|
        config.exception_level_filters = saved_filters
      end
    end

    it 'should report exception objects with no backtrace' do
      payload = nil
      Ratchetio.stub(:schedule_payload) do |*args|
        payload = JSON.parse( args[0] )
      end
      Ratchetio.report_exception(StandardError.new("oops"))
      payload["data"]["body"]["trace"]["frames"].should == []
      payload["data"]["body"]["trace"]["exception"]["class"].should == "StandardError"
      payload["data"]["body"]["trace"]["exception"]["message"].should == "oops"
    end
  end
  
  context 'report_message' do
    before(:each) do
      configure
      Ratchetio.configure do |config|
        config.logger = logger_mock
      end
    end
    
    let(:logger_mock) { double("Rails.logger").as_null_object }

    it 'should report simple messages' do
      logger_mock.should_receive(:info).with('[Ratchet.io] Success')
      Ratchetio.report_message("Test message")
    end

    it 'should not report anything when disabled' do
      logger_mock.should_not_receive(:info).with('[Ratchet.io] Success')
      Ratchetio.configure do |config|
        config.enabled = false
      end<commit_msg>fix test issue on jruby 1.8<commit_after>require 'logger'
require 'socket'
require 'spec_helper'

describe Ratchetio do

  context 'report_exception' do
    before(:each) do
      configure
      Ratchetio.configure do |config|
        config.logger = logger_mock
      end
      
      begin
        foo = bar
      rescue => e
        @exception = e
      end
    end
   
    let(:logger_mock) { double("Rails.logger").as_null_object }

    it 'should report exceptions without person or request data' do
      logger_mock.should_receive(:info).with('[Ratchet.io] Success')
      Ratchetio.report_exception(@exception)
    end

    it 'should not report anything when disabled' do
      logger_mock.should_not_receive(:info).with('[Ratchet.io] Success')
      Ratchetio.configure do |config|
        config.enabled = false
      end
      
      Ratchetio.report_exception(@exception)
      
      Ratchetio.configure do |config|
        config.enabled = true
      end
    end

    it 'should report exceptions with request and person data' do
      logger_mock.should_receive(:info).with('[Ratchet.io] Success')
      request_data = {
        :params => { :foo => "bar" },
        :url => 'http://localhost/',
        :user_ip => '127.0.0.1',
        :headers => {},
        :GET => { "baz" => "boz" },
        :session => { :user_id => 123 },
        :method => "GET",
      }
      person_data = {
        :id => 1,
        :username => "test",
        :email => "<EMAIL>"
      }
      Ratchetio.report_exception(@exception, request_data, person_data)
    end

    it 'should ignore ignored exception classes' do
      saved_filters = Ratchetio.configuration.exception_level_filters
      Ratchetio.configure do |config|
        config.exception_level_filters = { 'NameError' => 'ignore' }
      end

      logger_mock.should_not_receive(:info)
      logger_mock.should_not_receive(:warn)
      logger_mock.should_not_receive(:error)
      
      Ratchetio.report_exception(@exception)
      
      Ratchetio.configure do |config|
        config.exception_level_filters = saved_filters
      end
    end

    it 'should report exception objects with no backtrace' do
      payload = nil
      Ratchetio.stub(:schedule_payload) do |*args|
        payload = JSON.parse( args[0] )
      end
      Ratchetio.report_exception(StandardError.new("oops"))
      payload["data"]["body"]["trace"]["frames"].should == []
      payload["data"]["body"]["trace"]["exception"]["class"].should == "StandardError"
      payload["data"]["body"]["trace"]["exception"]["message"].should == "oops"
    end
  end
  
  context 'report_message' do
    before(:each) do
      configure
      Ratchetio.configure do |config|
        config.logger = logger_mock
      end
    end
    
    let(:logger_mock) { double("Rails.logger").as_null_object }

    it 'should report simple messages' do
      logger_mock.should_receive(:info).with('[Ratchet.io] Scheduling payload')
      logger_mock.should_receive(:info).with('[Ratchet.io] Success')
      Ratchetio.report_message("Test message")
    end

    it 'should not report anything when disabled' do
      logger_mock.should_not_receive(:info).with('[Ratchet.io] Success')
      Ratchetio.configure do |config|
        config.enabled = false
      end
{'fraction_non_alphanumeric': 0.11951144094001237, 'fraction_numerical': 0.0037105751391465678, 'mean_word_length': 2.6610073571024335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7520801', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1908, 'n_words': 417}