Skip to content

Commit

Permalink
feat: client identification headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Jan 20, 2025
1 parent def92e8 commit ee1a516
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/unleash/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def http_headers
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'UNLEASH-INSTANCEID' => self.instance_id,
'UNLEASH-APPNAME' => self.app_name,
'X-UNLEASH-APPNAME' => self.app_name,
'X-UNLEASH-CONNECTION-ID' => @connection_id,
'X-UNLEASH-SDK' => "unleash-ruby@#{Unleash::VERSION}",
'Unleash-Client-Spec' => CLIENT_SPECIFICATION_VERSION
}.merge!(generate_custom_http_headers)
end
Expand Down Expand Up @@ -87,6 +90,7 @@ def set_defaults
self.environment = 'default'
self.url = nil
self.instance_id = SecureRandom.uuid
@connection_id = SecureRandom.uuid
self.project_name = nil
self.disable_client = false
self.disable_metrics = false
Expand Down
34 changes: 33 additions & 1 deletion spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
require "securerandom"

RSpec.describe Unleash::Client do
after do
WebMock.reset!
File.delete(Unleash.configuration.backup_file) if File.exist?(Unleash.configuration.backup_file)
end

it "Uses custom http headers when initializing client" do
fixed_uuid = "123e4567-e89b-12d3-a456-426614174000"
allow(SecureRandom).to receive(:uuid).and_return(fixed_uuid)

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' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand All @@ -22,7 +28,8 @@
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]"
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}"
}
)
.to_return(status: 200, body: "", headers: {})
Expand All @@ -45,8 +52,11 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'X-Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'X-Unleash-Connection-Id' => fixed_uuid,
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand All @@ -73,14 +83,18 @@
.with(headers: { 'Content-Type': 'application/json' })
.with(headers: { 'X-API-KEY': '123', 'Content-Type': 'application/json' })
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'X-UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'X-UNLEASH-CONNECTION-ID': fixed_uuid })
).to have_been_made.once

expect(
a_request(:get, "http://test-url/client/features")
.with(headers: { 'X-API-KEY': '123' })
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'X-UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'X-UNLEASH-CONNECTION-ID': fixed_uuid })
).to have_been_made.once

# Test now sending of metrics
Expand All @@ -91,7 +105,9 @@
.with(headers: { 'Content-Type': 'application/json' })
.with(headers: { 'X-API-KEY': '123', 'Content-Type': 'application/json' })
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'X-UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'X-UNLEASH-CONNECTION-ID': fixed_uuid })
).not_to have_been_made

# Sending metrics, if they have been evaluated:
Expand All @@ -103,7 +119,9 @@
.with(headers: { 'Content-Type': 'application/json' })
.with(headers: { 'X-API-KEY': '123', 'Content-Type': 'application/json' })
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'X-UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'X-UNLEASH-CONNECTION-ID': fixed_uuid })
.with{ |request| JSON.parse(request.body)['bucket']['toggles']['Feature.A']['yes'] == 2 }
).to have_been_made.once
end
Expand All @@ -116,8 +134,10 @@
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Unleash-Appname' => 'my-test-app',
'X-Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand Down Expand Up @@ -157,8 +177,10 @@
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Unleash-Appname' => 'my-test-app',
'X-Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand Down Expand Up @@ -257,6 +279,7 @@
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand All @@ -269,8 +292,10 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'X-Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand Down Expand Up @@ -305,6 +330,7 @@
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand All @@ -327,8 +353,10 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'X-Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand Down Expand Up @@ -605,8 +633,10 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'X-Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand All @@ -619,8 +649,10 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'X-Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}",
'X-Api-Key' => '123'
}
)
Expand Down
9 changes: 9 additions & 0 deletions spec/unleash/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "unleash/configuration"
require "securerandom"

RSpec.describe Unleash do
describe 'Configuration' do
Expand Down Expand Up @@ -124,7 +125,9 @@
expect{ config.validate! }.not_to raise_error
expect(config.custom_http_headers).to include({ 'X-API-KEY': '123' })
expect(config.http_headers).to include({ 'UNLEASH-APPNAME' => 'test-app' })
expect(config.http_headers).to include({ 'X-UNLEASH-APPNAME' => 'test-app' })
expect(config.http_headers).to include('UNLEASH-INSTANCEID')
expect(config.http_headers).to include('X-UNLEASH-CONNECTION-ID')
end

it "should allow lambdas and procs for custom_https_headers via new client" do
Expand All @@ -133,6 +136,9 @@
end
allow(custom_headers_proc).to receive(:call).and_call_original

fixed_uuid = "123e4567-e89b-12d3-a456-426614174000"
allow(SecureRandom).to receive(:uuid).and_return(fixed_uuid)

config = Unleash::Configuration.new(
url: 'https://testurl/api',
app_name: 'test-app',
Expand All @@ -145,7 +151,10 @@
{
'X-API-KEY' => '123',
'UNLEASH-APPNAME' => 'test-app',
'X-UNLEASH-APPNAME' => 'test-app',
'UNLEASH-INSTANCEID' => config.instance_id,
'X-UNLEASH-CONNECTION-ID' => fixed_uuid,
'X-UNLEASH-SDK' => "unleash-ruby@#{Unleash::VERSION}",
'Unleash-Client-Spec' => '5.1.9',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]"
}
Expand Down
4 changes: 3 additions & 1 deletion spec/unleash/metrics_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'X-Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]"
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'X-Unleash-Sdk' => "unleash-ruby@#{Unleash::VERSION}"
}
)
.to_return(status: 200, body: "", headers: {})
Expand Down

0 comments on commit ee1a516

Please sign in to comment.