diff --git a/lib/unleash/feature_toggle.rb b/lib/unleash/feature_toggle.rb index 151693b..48b6ae6 100644 --- a/lib/unleash/feature_toggle.rb +++ b/lib/unleash/feature_toggle.rb @@ -189,7 +189,7 @@ def ensure_valid_context(context) end def initialize_strategies(params, segment_map) - params.fetch('strategies', []) + (params.fetch('strategies', []) || []) .select{ |s| s.has_key?('name') && Unleash.strategies.includes?(s['name']) } .map do |s| ActivationStrategy.new( @@ -202,7 +202,7 @@ def initialize_strategies(params, segment_map) end def resolve_variants(strategy) - strategy.fetch("variants", []) + (strategy.fetch("variants", []) || []) .select{ |variant| variant.is_a?(Hash) && variant.has_key?("name") } .map do |variant| VariantDefinition.new( diff --git a/spec/unleash/client_spec.rb b/spec/unleash/client_spec.rb index f98032f..1ac2a07 100644 --- a/spec/unleash/client_spec.rb +++ b/spec/unleash/client_spec.rb @@ -297,6 +297,62 @@ expect(WebMock).not_to have_requested(:post, 'http://test-url/client/metrics') end + it "should not fail if we are provided no variants from the unleash server" do + WebMock.stub_request(:post, "http://test-url/client/register") + .with( + headers: { + 'Accept' => '*/*', + 'Content-Type' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'User-Agent' => 'Ruby', + 'X-Api-Key' => '123' + } + ) + .to_return(status: 200, body: "", headers: {}) + + features_response_body = '{ + "version": 1, + "features": [{ + "name": "toggleName", + "enabled": true, + "strategies": [{ "name": "default", "constraints": [], "parameters": {}, "variants": null }], + "variants": [] + }] + }' + + WebMock.stub_request(:get, "http://test-url/client/features") + .with( + headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'Unleash-Appname' => 'my-test-app', + 'Unleash-Instanceid' => 'rspec/test', + 'User-Agent' => 'Ruby', + 'X-Api-Key' => '123' + } + ) + .to_return(status: 200, body: features_response_body, headers: {}) + + Unleash.configure do |config| + config.url = 'http://test-url/' + config.app_name = 'my-test-app' + config.instance_id = 'rspec/test' + config.disable_client = false + config.disable_metrics = true + config.custom_http_headers = { 'X-API-KEY' => '123' } + end + + unleash_client = Unleash::Client.new + + expect(unleash_client.is_enabled?('toggleName', {})).to be true + + expect(WebMock).not_to have_requested(:get, 'http://test-url/') + expect(WebMock).to have_requested(:get, 'http://test-url/client/features') + expect(WebMock).to have_requested(:post, 'http://test-url/client/register') + expect(WebMock).not_to have_requested(:post, 'http://test-url/client/metrics') + end + it "should forcefully disable metrics if the client is disabled" do Unleash.configure do |config| config.url = 'http://test-url/'