diff --git a/lib/active_resource/base.rb b/lib/active_resource/base.rb index edf92e5c25..e9b360258b 100644 --- a/lib/active_resource/base.rb +++ b/lib/active_resource/base.rb @@ -795,6 +795,7 @@ def primary_key # Gets the \prefix for a resource's nested URL (e.g., prefix/collectionname/1.json) # This method is regenerated at runtime based on what the \prefix is set to. def prefix(options = {}) + raise ArgumentError, "Missing site URI" unless site default = site.path default << "/" unless default[-1..-1] == "/" # generate the actual method based on the current site path @@ -899,6 +900,7 @@ def element_path(id, prefix_options = {}, query_options = nil) # # => https://37s.sunrise.com/posts/5/comments/1.json?active=1 # def element_url(id, prefix_options = {}, query_options = nil) + raise ArgumentError, "Missing site URI" unless site URI.join(site, element_path(id, prefix_options, query_options)).to_s end @@ -971,6 +973,7 @@ def collection_path(prefix_options = {}, query_options = nil) # # => https://example.com/posts/5/comments.json?active=1 # def collection_url(prefix_options = {}, query_options = nil) + raise ArgumentError, "Missing site URI" unless site URI.join(site, collection_path(prefix_options, query_options)).to_s end diff --git a/test/cases/base_test.rb b/test/cases/base_test.rb index 462920ac92..4ca2544646 100644 --- a/test/cases/base_test.rb +++ b/test/cases/base_test.rb @@ -33,6 +33,20 @@ def teardown # Tests relating to setting up the API-connection configuration ######################################################################## + def test_site_missing_error_message + Person.site = nil + + assert_raises ArgumentError, match: "Missing site URI" do + Person.find(1) + end + assert_raises ArgumentError, match: "Missing site URI" do + Person.element_url(1) + end + assert_raises ArgumentError, match: "Missing site URI" do + Person.collection_url(1) + end + end + def test_site_accessor_accepts_uri_or_string_argument site = URI.parse("http://localhost") diff --git a/test/cases/connection_test.rb b/test/cases/connection_test.rb index 9be07f4917..03f5d39f02 100644 --- a/test/cases/connection_test.rb +++ b/test/cases/connection_test.rb @@ -345,6 +345,12 @@ def test_disable_net_connection end end + def test_site_missing_error_message + assert_raises ArgumentError, match: "Missing site URI" do + ActiveResource::Connection.new(nil) + end + end + def keep_net_connection_status old = ActiveResource::HttpMock.net_connection_enabled? begin