-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #701 from bugsnag/endpoint-configuration
Bring endpoint configuration in line with other notifiers
Showing
9 changed files
with
498 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Bugsnag | ||
class EndpointConfiguration | ||
attr_reader :notify | ||
attr_reader :sessions | ||
|
||
def initialize(notify, sessions) | ||
@notify = notify | ||
@sessions = sessions | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module Bugsnag | ||
# @api private | ||
class EndpointValidator | ||
def self.validate(endpoints) | ||
# ensure we have an EndpointConfiguration object | ||
return Result.missing_urls unless endpoints.is_a?(EndpointConfiguration) | ||
|
||
# check for missing URLs | ||
return Result.missing_urls if endpoints.notify.nil? && endpoints.sessions.nil? | ||
return Result.missing_notify if endpoints.notify.nil? | ||
return Result.missing_session if endpoints.sessions.nil? | ||
|
||
# check for empty URLs | ||
return Result.invalid_urls if endpoints.notify.empty? && endpoints.sessions.empty? | ||
return Result.invalid_notify if endpoints.notify.empty? | ||
return Result.invalid_session if endpoints.sessions.empty? | ||
|
||
Result.valid | ||
end | ||
|
||
# @api private | ||
class Result | ||
# rubocop:disable Layout/LineLength | ||
MISSING_URLS = "Invalid configuration. endpoints must be set with both a notify and session URL. Bugsnag will not send any requests.".freeze | ||
MISSING_NOTIFY_URL = "Invalid configuration. endpoints.sessions cannot be set without also setting endpoints.notify. Bugsnag will not send any requests.".freeze | ||
MISSING_SESSION_URL = "Invalid configuration. endpoints.notify cannot be set without also setting endpoints.sessions. Bugsnag will not send any sessions.".freeze | ||
|
||
INVALID_URLS = "Invalid configuration. endpoints should be valid URLs, got empty strings. Bugsnag will not send any requests.".freeze | ||
INVALID_NOTIFY_URL = "Invalid configuration. endpoints.notify should be a valid URL, got empty string. Bugsnag will not send any requests.".freeze | ||
INVALID_SESSION_URL = "Invalid configuration. endpoints.sessions should be a valid URL, got empty string. Bugsnag will not send any sessions.".freeze | ||
# rubocop:enable Layout/LineLength | ||
|
||
attr_reader :reason | ||
|
||
def initialize(valid, keep_events_enabled_for_backwards_compatibility = true, reason = nil) | ||
@valid = valid | ||
@keep_events_enabled_for_backwards_compatibility = keep_events_enabled_for_backwards_compatibility | ||
@reason = reason | ||
end | ||
|
||
def valid? | ||
@valid | ||
end | ||
|
||
def keep_events_enabled_for_backwards_compatibility? | ||
@keep_events_enabled_for_backwards_compatibility | ||
end | ||
|
||
# factory functions | ||
|
||
def self.valid | ||
new(true) | ||
end | ||
|
||
def self.missing_urls | ||
new(false, false, MISSING_URLS) | ||
end | ||
|
||
def self.missing_notify | ||
new(false, false, MISSING_NOTIFY_URL) | ||
end | ||
|
||
def self.missing_session | ||
new(false, true, MISSING_SESSION_URL) | ||
end | ||
|
||
def self.invalid_urls | ||
new(false, false, INVALID_URLS) | ||
end | ||
|
||
def self.invalid_notify | ||
new(false, false, INVALID_NOTIFY_URL) | ||
end | ||
|
||
def self.invalid_session | ||
new(false, true, INVALID_SESSION_URL) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require "spec_helper" | ||
|
||
require "bugsnag/endpoint_configuration" | ||
|
||
describe Bugsnag::EndpointConfiguration do | ||
it "has notify & session URLs" do | ||
configuration = Bugsnag::EndpointConfiguration.new("notify", "session") | ||
|
||
expect(configuration.notify).to eq("notify") | ||
expect(configuration.sessions).to eq("session") | ||
end | ||
|
||
it "is immutable" do | ||
configuration = Bugsnag::EndpointConfiguration.new("notify", "session") | ||
|
||
expect(configuration).not_to respond_to(:notify=) | ||
expect(configuration).not_to respond_to(:session=) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
require "spec_helper" | ||
|
||
require "bugsnag/endpoint_configuration" | ||
require "bugsnag/endpoint_validator" | ||
|
||
describe Bugsnag::EndpointValidator do | ||
describe "#validate" do | ||
it "returns an invalid result if given nil" do | ||
result = Bugsnag::EndpointValidator.validate(nil) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: false, | ||
keep_events_enabled_for_backwards_compatibility?: false, | ||
reason: Bugsnag::EndpointValidator::Result::MISSING_URLS, | ||
}) | ||
end | ||
|
||
it "returns an invalid result if no URL is set" do | ||
endpoint_configuration = Bugsnag::EndpointConfiguration.new(nil, nil) | ||
result = Bugsnag::EndpointValidator.validate(endpoint_configuration) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: false, | ||
keep_events_enabled_for_backwards_compatibility?: false, | ||
reason: Bugsnag::EndpointValidator::Result::MISSING_URLS, | ||
}) | ||
end | ||
|
||
it "returns an invalid result if notify URL is not set" do | ||
endpoint_configuration = Bugsnag::EndpointConfiguration.new(nil, "sessions.example.com") | ||
result = Bugsnag::EndpointValidator.validate(endpoint_configuration) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: false, | ||
keep_events_enabled_for_backwards_compatibility?: false, | ||
reason: Bugsnag::EndpointValidator::Result::MISSING_NOTIFY_URL, | ||
}) | ||
end | ||
|
||
it "returns an invalid result if session URL is not set" do | ||
endpoint_configuration = Bugsnag::EndpointConfiguration.new("notify.example.com", nil) | ||
result = Bugsnag::EndpointValidator.validate(endpoint_configuration) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: false, | ||
keep_events_enabled_for_backwards_compatibility?: true, | ||
reason: Bugsnag::EndpointValidator::Result::MISSING_SESSION_URL, | ||
}) | ||
end | ||
|
||
it "returns an invalid result if both URLs are empty" do | ||
endpoint_configuration = Bugsnag::EndpointConfiguration.new("", "") | ||
result = Bugsnag::EndpointValidator.validate(endpoint_configuration) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: false, | ||
keep_events_enabled_for_backwards_compatibility?: false, | ||
reason: Bugsnag::EndpointValidator::Result::INVALID_URLS, | ||
}) | ||
end | ||
|
||
it "returns an invalid result if notify URL is empty" do | ||
endpoint_configuration = Bugsnag::EndpointConfiguration.new("", "session.example.com") | ||
result = Bugsnag::EndpointValidator.validate(endpoint_configuration) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: false, | ||
keep_events_enabled_for_backwards_compatibility?: false, | ||
reason: Bugsnag::EndpointValidator::Result::INVALID_NOTIFY_URL, | ||
}) | ||
end | ||
|
||
it "returns an invalid result if session URL is empty" do | ||
endpoint_configuration = Bugsnag::EndpointConfiguration.new("notify.example.com", "") | ||
result = Bugsnag::EndpointValidator.validate(endpoint_configuration) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: false, | ||
keep_events_enabled_for_backwards_compatibility?: true, | ||
reason: Bugsnag::EndpointValidator::Result::INVALID_SESSION_URL, | ||
}) | ||
end | ||
|
||
it "returns a valid result when given two valid URLs" do | ||
endpoint_configuration = Bugsnag::EndpointConfiguration.new("notify.example.com", "session.example.com") | ||
result = Bugsnag::EndpointValidator.validate(endpoint_configuration) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: true, | ||
keep_events_enabled_for_backwards_compatibility?: true, | ||
reason: nil, | ||
}) | ||
end | ||
|
||
it "returns a valid result when given two non-empty strings" do | ||
endpoint_configuration = Bugsnag::EndpointConfiguration.new("a b c", "x y z") | ||
result = Bugsnag::EndpointValidator.validate(endpoint_configuration) | ||
|
||
expect(result).to have_attributes({ | ||
valid?: true, | ||
keep_events_enabled_for_backwards_compatibility?: true, | ||
reason: nil, | ||
}) | ||
end | ||
end | ||
end |