Skip to content

Commit

Permalink
Disable sending events if endpoints are invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed Sep 30, 2021
1 parent 246fa0f commit efd9f78
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
66 changes: 66 additions & 0 deletions spec/bugsnag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit efd9f78

Please sign in to comment.