From efd9f7882c6efc32a37009be0b25296662422ff4 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Thu, 30 Sep 2021 11:51:37 +0100 Subject: [PATCH] Disable sending events if endpoints are invalid --- lib/bugsnag.rb | 2 ++ spec/bugsnag_spec.rb | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/lib/bugsnag.rb b/lib/bugsnag.rb index 4e48c1a6..a2786c97 100644 --- a/lib/bugsnag.rb +++ b/lib/bugsnag.rb @@ -406,6 +406,8 @@ def clear_metadata(section, *args) private def should_deliver_notification?(exception, auto_notify) + return false unless configuration.enable_events + reason = abort_reason(exception, auto_notify) configuration.debug(reason) unless reason.nil? reason.nil? diff --git a/spec/bugsnag_spec.rb b/spec/bugsnag_spec.rb index 4eb91378..1ab82baf 100644 --- a/spec/bugsnag_spec.rb +++ b/spec/bugsnag_spec.rb @@ -145,6 +145,72 @@ end end + describe "endpoint configuration" do + it "does not send events when both endpoints are invalid" do + Bugsnag.configuration.endpoints = {} + + expect(Bugsnag.configuration).not_to receive(:debug) + expect(Bugsnag.configuration).not_to receive(:info) + expect(Bugsnag.configuration).not_to receive(:warn) + expect(Bugsnag.configuration).not_to receive(:error) + + Bugsnag.notify(RuntimeError.new("abc")) + + expect(Bugsnag).not_to have_sent_notification + end + + it "does not send sessions when both endpoints are invalid" do + Bugsnag.configuration.endpoints = {} + + expect(Bugsnag.configuration).not_to receive(:debug) + expect(Bugsnag.configuration).not_to receive(:info) + expect(Bugsnag.configuration).not_to receive(:warn) + expect(Bugsnag.configuration).not_to receive(:error) + + Bugsnag.start_session + + expect(Bugsnag).not_to have_sent_sessions + end + + it "does not send events or sessions when the notify endpoint is invalid" do + Bugsnag.configuration.endpoints = { sessions: "sessions.example.com" } + + expect(Bugsnag.configuration).not_to receive(:debug) + expect(Bugsnag.configuration).not_to receive(:info) + expect(Bugsnag.configuration).not_to receive(:warn) + expect(Bugsnag.configuration).not_to receive(:error) + + Bugsnag.notify(RuntimeError.new("abc")) + Bugsnag.start_session + + expect(Bugsnag).not_to have_sent_notification + expect(Bugsnag).not_to have_sent_sessions + end + + it "does not send sessions when the session endpoint is invalid" do + Bugsnag.configuration.endpoints = Bugsnag::EndpointConfiguration.new("http://notify.example.com", nil) + + expect(Bugsnag.configuration).to receive(:debug).with("Request to http://notify.example.com completed, status: 200").once + expect(Bugsnag.configuration).to receive(:info).with("Notifying http://notify.example.com of RuntimeError").once + expect(Bugsnag.configuration).not_to receive(:warn) + expect(Bugsnag.configuration).not_to receive(:error) + + stub_request(:post, "http://notify.example.com/") + + Bugsnag.notify(RuntimeError.new("abc")) + Bugsnag.start_session + + expect(Bugsnag).to(have_requested(:post, "http://notify.example.com/").with do |request| + payload = JSON.parse(request.body) + exception = get_exception_from_payload(payload) + + expect(exception["message"]).to eq("abc") + end) + + expect(Bugsnag).not_to have_sent_sessions + end + end + describe "add_exit_handler" do before do