diff options
Diffstat (limited to 'chef-config')
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 13 | ||||
-rw-r--r-- | chef-config/spec/unit/config_spec.rb | 6 |
2 files changed, 17 insertions, 2 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index 4e9355192a..e6748897ae 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -76,12 +76,21 @@ module ChefConfig default :formatters, [] + def self.is_valid_url? uri + url = uri.to_s.strip + /^http:\/\// =~ url || /^https:\/\// =~ url || /^chefzero:/ =~ url + end # Override the config dispatch to set the value of multiple server options simultaneously # # === Parameters # url<String>:: String to be set for all of the chef-server-api URL's # - configurable(:chef_server_url).writes_value { |url| url.to_s.strip } + configurable(:chef_server_url).writes_value do |uri| + unless is_valid_url? uri + raise ConfigurationError, "#{url} is an invalid chef_server_url." + end + uri.to_s.strip + end # When you are using ActiveSupport, they monkey-patch 'daemonize' into Kernel. # So while this is basically identical to what method_missing would do, we pull @@ -303,7 +312,7 @@ module ChefConfig default :host, 'localhost' default :port, 8889.upto(9999) # Will try ports from 8889-9999 until one works end - default :chef_server_url, "https://localhost:443" + default :chef_server_url, "https://localhost:443" default(:chef_server_root) do # if the chef_server_url is a path to an organization, aka diff --git a/chef-config/spec/unit/config_spec.rb b/chef-config/spec/unit/config_spec.rb index 8e9a499a1a..4af5d4b7c7 100644 --- a/chef-config/spec/unit/config_spec.rb +++ b/chef-config/spec/unit/config_spec.rb @@ -60,6 +60,12 @@ RSpec.describe ChefConfig::Config do expect(ChefConfig::Config.chef_server_url).to eq("https://junglist.gen.nz") end end + + context "when the url is invalid" do + it "raises an exception" do + expect { ChefConfig::Config.chef_server_url = "127.0.0.1" }.to raise_error(ChefConfig::ConfigurationError) + end + end end describe "when configuring formatters" do |