<commit_before>require File.expand_path(File.dirname(__FILE__)) + '/../spec_helper.rb'

describe RailsExceptionHandler::Parser do
  before(:each) do
    env = create_env
    controller = mock(ApplicationController, :current_user => mock(Object, :login => 'superman'))
    request = ActionDispatch::Request.new(env)
    @parser = RailsExceptionHandler::Parser.new(create_exception, request, controller)
  end

  describe ".relevant_info" do
    it("should return app_name") { @parser.relevant_info[:app_name].should == 'ExceptionHandlerTestApp' }
    it("should return class_name") { @parser.relevant_info[:class_name].should == 'NoMethodError' }
    it("should return message") { @parser.relevant_info[:message].should == "undefined method `foo' for nil:NilClass" }
    it("should return trace") { @parser.relevant_info[:trace].should match /active_support\/whiny_nil/ }
    it("should return target_url") { @parser.relevant_info[:target_url].should == 'http://example.org/home?foo=bar' }
    it("should return referer_url") { @parser.relevant_info[:referer_url].should == 'http://google.com/' }
    it("should return params") { @parser.relevant_info[:params].should match(/\"foo\"=>\"bar\"/) }
    it("should return user_agent") { @parser.relevant_info[:user_agent].should == "Mozilla/4.0 (compatible; MSIE 8.0)" }
    it("should return user_info") { @parser.relevant_info[:user_info].should == 'superman' }
    it("should return created_at") { @parser.relevant_info[:created_at].should be > 5.seconds.ago }
    it("should return created_at") { @parser.relevant_info[:created_at].should be < Time.now }
  end

  describe ".ignore?" do
    context "routing errors" do
      it "should return true on routing errors when ignore_routing_errors is set to true" do
        RailsExceptionHandler.configure { |config| config.ignore_routing_errors = true }
        exception = create_exception
        exception.stub!(:class => ActionController::RoutingError)
        parser = create_parser(exception, nil, nil)
        parser.ignore?.should == true
      end

      it "should return false on routing errors when ignore_routing_errors is set to false" do
        RailsExceptionHandler.configure { |config| config.ignore_routing_errors = false }
        exception = create_exception
        exception.stub!(:class => ActionController::RoutingError)
        parser = create_parser(exception, nil, nil)
        parser.ignore?.should == false
      end

      it "should return false on non-routing errors when ignore_routing_errors is set to true" do
        RailsExceptionHandler.configure { |config| config.ignore_routing_errors = true }
        exception = create_exception
        exception.stub!(:class => SyntaxError)
        parser = create_parser(exception, nil, nil)
        parser.ignore?.should == false
      end
    end

    context "crawlers" do
      it "should return true on errors created by a crawler when ignore_crawlers is set to true" do
        RailsExceptionHandler.configure { |config| config.ignore_crawlers = true }
        request = ActionDispatch::Request.new(create_env)
        request.stub!(:user_agent => 'foo Slurp bar')
        parser = create_parser(nil, request, nil)<commit_msg>parser specs added for the ignore? function<commit_after>require File.expand_path(File.dirname(__FILE__)) + '/../spec_helper.rb'

describe RailsExceptionHandler::Parser do
  before(:each) do
    env = create_env
    controller = mock(ApplicationController, :current_user => mock(Object, :login => 'superman'))
    request = ActionDispatch::Request.new(env)
    @parser = RailsExceptionHandler::Parser.new(create_exception, request, controller)
  end

  describe ".relevant_info" do
    it("should return app_name") { @parser.relevant_info[:app_name].should == 'ExceptionHandlerTestApp' }
    it("should return class_name") { @parser.relevant_info[:class_name].should == 'NoMethodError' }
    it("should return message") { @parser.relevant_info[:message].should == "undefined method `foo' for nil:NilClass" }
    it("should return trace") { @parser.relevant_info[:trace].should match /active_support\/whiny_nil/ }
    it("should return target_url") { @parser.relevant_info[:target_url].should == 'http://example.org/home?foo=bar' }
    it("should return referer_url") { @parser.relevant_info[:referer_url].should == 'http://google.com/' }
    it("should return params") { @parser.relevant_info[:params].should match(/\"foo\"=>\"bar\"/) }
    it("should return user_agent") { @parser.relevant_info[:user_agent].should == "Mozilla/4.0 (compatible; MSIE 8.0)" }
    it("should return user_info") { @parser.relevant_info[:user_info].should == 'superman' }
    it("should return created_at") { @parser.relevant_info[:created_at].should be > 5.seconds.ago }
    it("should return created_at") { @parser.relevant_info[:created_at].should be < Time.now }
  end

  describe ".ignore?" do
    context "routing errors" do
      it "should return true on routing errors when the filter contains :all_routing_errors" do
        RailsExceptionHandler.configure { |config| config.filters = [:all_routing_errors] }
        exception = create_exception
        exception.stub!(:class => ActionController::RoutingError)
        parser = create_parser(exception, nil, nil)
        parser.ignore?.should == true
      end

      it "should return true on routing errors without referer when the filter contains :routing_errors_without_referer" do
        RailsExceptionHandler.configure { |config| config.filters = [:routing_errors_without_referer] }
        exception = create_exception
        exception.stub!(:class => ActionController::RoutingError)
        parser = create_parser(exception, nil, nil)
        parser.ignore?.should == true
      end

      it "should return true when the user agent matches against the filters :user_agent_regxp" do
        RailsExceptionHandler.configure { |config| config.filters = [{:user_agent_regxp => /\b(Mozilla)\b/}] }
        parser = create_parser(nil, nil, nil)
        parser.ignore?.should == true
      end

      it "should return true when the url matches against the filters :target_url_regxp" do
        RailsExceptionHandler.configure { |config| config.filters = [{:target_url_regxp => /\b(home)\b/}] }
        parser = create_parser(nil, nil, nil)
        parser.ignore?.should == true
      end

      it "should return false when the request is not caught by a filter" do
        RailsExceptionHandler.configure { |config| config.filters = [] }
        parser = create_parser(nil, nil, nil)
        parser.ignore?.should == false
      end
    end

    context "crawlers" do
      it "should return true on errors created by a crawler when ignore_crawlers is set to true" do
        RailsExceptionHandler.configure { |config| config.ignore_crawlers = true }
        request = ActionDispatch::Request.new(create_env)
        request.stub!(:user_agent => 'foo Slurp bar')
        parser = create_parser(nil, request, nil)
{'fraction_non_alphanumeric': 0.11974156496769563, 'fraction_numerical': 0.0014357501794687725, 'mean_word_length': 3.8918539325842696, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5685836', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1960, 'n_words': 493}