Skip to content

Commit

Permalink
Merge 868e368 into 54c8a81
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Jul 5, 2018
2 parents 54c8a81 + 868e368 commit e8b3aa0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def register
http.use_ssl = true if uri.scheme == 'https'
http.open_timeout = Unleash.configuration.timeout # in seconds
http.read_timeout = Unleash.configuration.timeout # in seconds
headers = {'Content-Type' => 'application/json'}
headers = {}
headers.merge(Unleash.configuration.custom_http_headers || {})
headers['Content-Type'] = 'application/json'

request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = info.to_json

Expand Down
2 changes: 2 additions & 0 deletions lib/unleash/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Unleash
class Configuration
attr_accessor :url, :app_name, :instance_id,
:custom_http_headers,
:disable_metrics, :timeout, :retry_limit,
:refresh_interval, :metrics_interval,
:backup_file, :logger, :log_level
Expand All @@ -13,6 +14,7 @@ def initialize(opts = {})
self.url = opts[:url] || nil
self.instance_id = opts[:instance_id] || SecureRandom.uuid

self.custom_http_headers = (opts[:custom_http_headers].is_a? Hash) ? opts[:custom_http_headers] : {}
self.disable_metrics = opts[:disable_metrics] || false
self.refresh_interval = opts[:refresh_interval] || 15
self.metrics_interval = opts[:metrics_interval] || 10
Expand Down
4 changes: 3 additions & 1 deletion lib/unleash/metrics_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def send
http.use_ssl = true if uri.scheme == 'https'
http.open_timeout = Unleash.configuration.timeout # in seconds
http.read_timeout = Unleash.configuration.timeout # in seconds
headers = {'Content-Type' => 'application/json'}
headers = {}
headers.merge(Unleash.configuration.custom_http_headers || {})
headers['Content-Type'] = 'application/json'
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = generated_report.to_json

Expand Down
8 changes: 6 additions & 2 deletions lib/unleash/toggle_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def fetch
http.use_ssl = true if uri.scheme == 'https'
http.open_timeout = Unleash.configuration.timeout # in seconds
http.read_timeout = Unleash.configuration.timeout # in seconds
request = Net::HTTP::Get.new(uri.request_uri)
request['If-None-Match'] = self.etag unless self.etag.nil?
headers = {}
headers.merge(Unleash.configuration.custom_http_headers || {})
headers['Content-Type'] = 'application/json'
headers['If-None-Match'] = self.etag unless self.etag.nil?

request = Net::HTTP::Get.new(uri.request_uri, headers)

response = http.request(request)

Expand Down
46 changes: 44 additions & 2 deletions spec/unleash/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

describe 'Configuration' do
it "should have the correct defaults" do
config = Unleash::Configuration.new()
config = Unleash::Configuration.new

expect(config.app_name).to be_nil
expect(config.url).to be_nil
expect(config.instance_id).to be_truthy
expect(config.custom_http_headers).to eq({})
expect(config.disable_metrics).to be_falsey

expect(config.refresh_interval).to eq(15)
expect(config.metrics_interval).to eq(10)
expect(config.timeout).to eq(30)
expect(config.retry_limit).to eq(1)

# expect(config.validate!).to raise_error(ArgumentError)
expect(config.backup_file).to_not be_nil

expect{ config.validate! }.to raise_error(ArgumentError)
end

it "should by default be invalid" do
Expand Down Expand Up @@ -50,6 +52,46 @@
expect(config.client_register_url).to eq('https://testurl/api/client/register')
end

it "should allow hashes for custom_http_headers via yield" do
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.custom_http_headers = {'X-API-KEY': '123'}
end
expect{ Unleash.configuration.validate! }.not_to raise_error
expect(Unleash.configuration.custom_http_headers).to eq({'X-API-KEY': '123'})
end

it "should allow hashes for custom_http_headers via new client" do
config = Unleash::Configuration.new(
url: 'https://testurl/api',
app_name: 'test-app',
custom_http_headers: {'X-API-KEY': '123'})

expect{ config.validate! }.not_to raise_error
expect(config.custom_http_headers).to eq({'X-API-KEY': '123'})
end

it "should not accept invalid custom_http_headers via yield" do
expect do
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.custom_http_headers = 123.456
end
end.to raise_error(ArgumentError)
end

# TODO: consider having consisten behaviour?
it "should swallow silently invalid custom_http_headers if set via client" do
config = Unleash::Configuration.new(
url: 'https://testurl/api',
app_name: 'test-app',
custom_http_headers: 123.0)
expect(config.custom_http_headers).to eq({})
expect{ config.validate! }.not_to raise_error
end

end

end

0 comments on commit e8b3aa0

Please sign in to comment.