From 41e5e7a8fe7544a8dbc63cf5d7df7e9c9b7b45c5 Mon Sep 17 00:00:00 2001 From: Renato Arruda Date: Tue, 3 Jul 2018 06:14:52 +0200 Subject: [PATCH 1/2] First commit in allowing custom http headers when talking to the unleash server --- lib/unleash/configuration.rb | 2 ++ lib/unleash/metrics_reporter.rb | 4 ++++ lib/unleash/toggle_fetcher.rb | 6 ++++++ 3 files changed, 12 insertions(+) diff --git a/lib/unleash/configuration.rb b/lib/unleash/configuration.rb index 32067b5d..f5576158 100644 --- a/lib/unleash/configuration.rb +++ b/lib/unleash/configuration.rb @@ -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 @@ -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] || {} self.disable_metrics = opts[:disable_metrics] || false self.refresh_interval = opts[:refresh_interval] || 15 self.metrics_interval = opts[:metrics_interval] || 10 diff --git a/lib/unleash/metrics_reporter.rb b/lib/unleash/metrics_reporter.rb index 175bebe1..2b401c4a 100755 --- a/lib/unleash/metrics_reporter.rb +++ b/lib/unleash/metrics_reporter.rb @@ -43,6 +43,10 @@ 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 = {} + if Unleash.configuration.custom_http_headers.is_a? Hash + headers = Unleash.configuration.custom_http_headers + end headers = {'Content-Type' => 'application/json'} request = Net::HTTP::Post.new(uri.request_uri, headers) request.body = generated_report.to_json diff --git a/lib/unleash/toggle_fetcher.rb b/lib/unleash/toggle_fetcher.rb index 13ef2d9b..010da23a 100755 --- a/lib/unleash/toggle_fetcher.rb +++ b/lib/unleash/toggle_fetcher.rb @@ -49,6 +49,12 @@ def fetch http.open_timeout = Unleash.configuration.timeout # in seconds http.read_timeout = Unleash.configuration.timeout # in seconds request = Net::HTTP::Get.new(uri.request_uri) + + if Unleash.configuration.custom_http_headers.is_a? Hash + Unleash.configuration.custom_http_headers.each{ |k,v| + request[k] = v + } + end request['If-None-Match'] = self.etag unless self.etag.nil? response = http.request(request) From 868e368c65ff7847eeefe82202b237ba44ee9f9d Mon Sep 17 00:00:00 2001 From: Renato Arruda Date: Fri, 6 Jul 2018 00:13:28 +0200 Subject: [PATCH 2/2] Improved support for custom http headers Still missing better unit test coverage --- lib/unleash/client.rb | 5 +++- lib/unleash/configuration.rb | 2 +- lib/unleash/metrics_reporter.rb | 6 ++-- lib/unleash/toggle_fetcher.rb | 12 ++++---- spec/unleash/configuration_spec.rb | 46 ++++++++++++++++++++++++++++-- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/lib/unleash/client.rb b/lib/unleash/client.rb index 560383ba..3262cb76 100644 --- a/lib/unleash/client.rb +++ b/lib/unleash/client.rb @@ -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 diff --git a/lib/unleash/configuration.rb b/lib/unleash/configuration.rb index f5576158..b26ced11 100644 --- a/lib/unleash/configuration.rb +++ b/lib/unleash/configuration.rb @@ -14,7 +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] || {} + 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 diff --git a/lib/unleash/metrics_reporter.rb b/lib/unleash/metrics_reporter.rb index 2b401c4a..ac10a421 100755 --- a/lib/unleash/metrics_reporter.rb +++ b/lib/unleash/metrics_reporter.rb @@ -44,10 +44,8 @@ def send http.open_timeout = Unleash.configuration.timeout # in seconds http.read_timeout = Unleash.configuration.timeout # in seconds headers = {} - if Unleash.configuration.custom_http_headers.is_a? Hash - headers = Unleash.configuration.custom_http_headers - end - headers = {'Content-Type' => 'application/json'} + 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 diff --git a/lib/unleash/toggle_fetcher.rb b/lib/unleash/toggle_fetcher.rb index 010da23a..cd4aa642 100755 --- a/lib/unleash/toggle_fetcher.rb +++ b/lib/unleash/toggle_fetcher.rb @@ -48,14 +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) + headers = {} + headers.merge(Unleash.configuration.custom_http_headers || {}) + headers['Content-Type'] = 'application/json' + headers['If-None-Match'] = self.etag unless self.etag.nil? - if Unleash.configuration.custom_http_headers.is_a? Hash - Unleash.configuration.custom_http_headers.each{ |k,v| - request[k] = v - } - end - request['If-None-Match'] = self.etag unless self.etag.nil? + request = Net::HTTP::Get.new(uri.request_uri, headers) response = http.request(request) diff --git a/spec/unleash/configuration_spec.rb b/spec/unleash/configuration_spec.rb index 14f5f509..36f6589c 100644 --- a/spec/unleash/configuration_spec.rb +++ b/spec/unleash/configuration_spec.rb @@ -5,11 +5,12 @@ 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) @@ -17,8 +18,9 @@ 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 @@ -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 \ No newline at end of file