diff options
author | George Claghorn <george@basecamp.com> | 2018-10-30 17:44:11 -0400 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-10-30 17:44:11 -0400 |
commit | c474daefb18e9bab96f6f0bb0bb30dfc00058cb3 (patch) | |
tree | a0baefe64e6f304de78f45f6c29f6de7bf98b863 | |
parent | ec1ad80b0b1b5652459b41957f345779a4d4c246 (diff) | |
download | rails-c474daefb18e9bab96f6f0bb0bb30dfc00058cb3.tar.gz rails-c474daefb18e9bab96f6f0bb0bb30dfc00058cb3.tar.bz2 rails-c474daefb18e9bab96f6f0bb0bb30dfc00058cb3.zip |
Validate address on route definition
-rw-r--r-- | lib/action_mailbox/router/route.rb | 12 | ||||
-rw-r--r-- | test/unit/router_test.rb | 3 |
2 files changed, 10 insertions, 5 deletions
diff --git a/lib/action_mailbox/router/route.rb b/lib/action_mailbox/router/route.rb index e123a93bb1..34cc684381 100644 --- a/lib/action_mailbox/router/route.rb +++ b/lib/action_mailbox/router/route.rb @@ -1,10 +1,10 @@ class ActionMailbox::Router::Route - class InvalidAddressError < StandardError; end - attr_reader :address, :mailbox_name def initialize(address, to:) @address, @mailbox_name = address, to + + ensure_valid_address end def match?(inbound_email) @@ -16,7 +16,7 @@ class ActionMailbox::Router::Route when Proc address.call(inbound_email) else - address.try(:match?, inbound_email) || raise(InvalidAddressError) + address.match?(inbound_email) end end @@ -25,6 +25,12 @@ class ActionMailbox::Router::Route end private + def ensure_valid_address + unless [ String, Regexp, Proc ].any? { |klass| address.is_a?(klass) } || address.respond_to?(:match?) + raise ArgumentError, "Expected a String, Regexp, Proc, or matchable, got #{address.inspect}" + end + end + def recipients_from(mail) Array(mail.to) + Array(mail.cc) + Array(mail.bcc) end diff --git a/test/unit/router_test.rb b/test/unit/router_test.rb index cc87ae6c0e..b8c187f95e 100644 --- a/test/unit/router_test.rb +++ b/test/unit/router_test.rb @@ -112,9 +112,8 @@ module ActionMailbox end test "invalid address" do - assert_raises(ActionMailbox::Router::Route::InvalidAddressError) do + assert_raises(ArgumentError) do @router.add_route Array.new, to: :first - @router.route create_inbound_email_from_mail(to: "replies-nowhere@example.com", subject: "This is a reply") end end end |