Skip to content

Commit

Permalink
Merge 14f5baa into 85d430d
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 30, 2023
2 parents 85d430d + 14f5baa commit 8b5e98f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/unleash/feature_toggle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
56 changes: 56 additions & 0 deletions spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
Expand Down

0 comments on commit 8b5e98f

Please sign in to comment.