<commit_before>require 'spec_helper'

describe Bugsnag::MiddlewareStack do
  it "should run before_bugsnag_notify callbacks, adding a tab" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
      event[:metaData][:some_tab].should_not be_nil
      event[:metaData][:some_tab][:info].should be == "here"
      event[:metaData][:some_tab][:data].should be == "also here"
    end
    
    callback_run_count = 0
    Bugsnag.before_notify_callbacks << lambda {|notif|
      notif.add_tab(:some_tab, {
        :info => "here",
        :data => "also here"
      })
      callback_run_count += 1
    }

    Bugsnag.notify(BugsnagTestException.new("It crashed"))
    callback_run_count.should be == 1
  end
  
  it "should run before_bugsnag_notify callbacks, adding custom data" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
      event[:metaData][:custom].should_not be_nil
      event[:metaData][:custom][:info].should be == "here"
      event[:metaData][:custom][:data].should be == "also here"
    end
    
    callback_run_count = 0
    Bugsnag.before_notify_callbacks << lambda {|notif|
      notif.add_custom_data(:info, "here")
      notif.add_custom_data(:data, "also here")
      
      callback_run_count += 1
    }

    Bugsnag.notify(BugsnagTestException.new("It crashed"))
    callback_run_count.should be == 1
  end
  
  it "overrides should override data set in before_notify" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
      event[:metaData][:custom].should_not be_nil
      event[:metaData][:custom][:info].should be == "here2"
      event[:metaData][:custom][:data].should be == "also here"
    end
    
    callback_run_count = 0
    Bugsnag.before_notify_callbacks << lambda {|notif|
      notif.add_custom_data(:info, "here")
      notif.add_custom_data(:data, "also here")
      
      callback_run_count += 1
    }

    Bugsnag.notify(BugsnagTestException.new("It crashed"), {:info => "here2"})
    callback_run_count.should be == 1
  end
  
  it "should have no before or after callbacks by default" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
      event[:metaData].should have(0).items
    end
    
    Bugsnag.before_notify_callbacks.should have(0).items
    Bugsnag.after_notify_callbacks.should have(0).items
    Bugsnag.notify(BugsnagTestException.new("It crashed"))
  end
  
  it "should run after_bugsnag_notify callbacks" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
    end
    
    callback_run_count = 0
    Bugsnag.after_notify_callbacks << lambda {|notif|
      callback_run_count += 1
    }

    Bugsnag.notify(BugsnagTestException.new("It crashed"))
    
    callback_run_count.should be == 1
  end
end<commit_msg>Add test for middleware disabling<commit_after>require 'spec_helper'

describe Bugsnag::MiddlewareStack do
  it "should run before_bugsnag_notify callbacks, adding a tab" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
      event[:metaData][:some_tab].should_not be_nil
      event[:metaData][:some_tab][:info].should be == "here"
      event[:metaData][:some_tab][:data].should be == "also here"
    end
    
    callback_run_count = 0
    Bugsnag.before_notify_callbacks << lambda {|notif|
      notif.add_tab(:some_tab, {
        :info => "here",
        :data => "also here"
      })
      callback_run_count += 1
    }

    Bugsnag.notify(BugsnagTestException.new("It crashed"))
    callback_run_count.should be == 1
  end
  
  it "should run before_bugsnag_notify callbacks, adding custom data" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
      event[:metaData][:custom].should_not be_nil
      event[:metaData][:custom][:info].should be == "here"
      event[:metaData][:custom][:data].should be == "also here"
    end
    
    callback_run_count = 0
    Bugsnag.before_notify_callbacks << lambda {|notif|
      notif.add_custom_data(:info, "here")
      notif.add_custom_data(:data, "also here")
      
      callback_run_count += 1
    }

    Bugsnag.notify(BugsnagTestException.new("It crashed"))
    callback_run_count.should be == 1
  end
  
  it "overrides should override data set in before_notify" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
      event[:metaData][:custom].should_not be_nil
      event[:metaData][:custom][:info].should be == "here2"
      event[:metaData][:custom][:data].should be == "also here"
    end
    
    callback_run_count = 0
    Bugsnag.before_notify_callbacks << lambda {|notif|
      notif.add_custom_data(:info, "here")
      notif.add_custom_data(:data, "also here")
      
      callback_run_count += 1
    }

    Bugsnag.notify(BugsnagTestException.new("It crashed"), {:info => "here2"})
    callback_run_count.should be == 1
  end
  
  it "should have no before or after callbacks by default" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
      event[:metaData].should have(0).items
    end
    
    Bugsnag.before_notify_callbacks.should have(0).items
    Bugsnag.after_notify_callbacks.should have(0).items
    Bugsnag.notify(BugsnagTestException.new("It crashed"))
  end
  
  it "should run after_bugsnag_notify callbacks" do
    Bugsnag::Notification.should_receive(:deliver_exception_payload) do |endpoint, payload|
      event = get_event_from_payload(payload)
    end
    
    callback_run_count = 0
    Bugsnag.after_notify_callbacks << lambda {|notif|
      callback_run_count += 1
    }

    Bugsnag.notify(BugsnagTestException.new("It crashed"))
    
    callback_run_count.should be == 1
  end

  it "should not execute disabled bugsnag middleware" do
    callback_run_count = 0
    Bugsnag.configure do |config|
      config.middleware.disable(Bugsnag::Middleware::Callbacks)
    end
    
    Bugsnag.before_notify_callbacks << lambda {|notif|
      callback_run_count += 1
    }
    
    Bugsnag.notify(BugsnagTestException.new("It crashed"))
    callback_run_count.should be == 0
  end
end
{'fraction_non_alphanumeric': 0.10922730682670667, 'fraction_numerical': 0.005551387846961741, 'mean_word_length': 3.7682403433476397, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 9, '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': '5251345', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2133, 'n_words': 411}