Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: client identification headers #224

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will replace UNLEASH-APPNAME over time

'X-UNLEASH-CONNECTION-ID' => @connection_id,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want this one to be private internal detail of each SDK

'X-UNLEASH-SDK' => "unleash-ruby@#{Unleash::VERSION}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common naming convention across SDKs

'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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the dumbest comment I've ever left on a PR ever but the change in syntax here is a bit jarring. Any chance we can move this line either to the top of the bottom of this function?

Honestly I won't even be offended if you just ignore me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually makes perfect sense to me. Had the same thought :) Moving it to the bottom

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I appreciate you haha

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we don't expose connection id we need to mock the generation


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
Loading