Skip to content

Commit

Permalink
Raise exception/fail consistently if custom_http_headers is of an inv…
Browse files Browse the repository at this point in the history
…alid type
  • Loading branch information
rarruda committed Jul 11, 2018
1 parent 24dad0a commit 7c1e1d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/unleash/configuration.rb
Expand Up @@ -14,7 +14,11 @@ 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] : {}
if opts[:custom_http_headers].is_a?(Hash) || opts[:custom_http_headers].nil?
self.custom_http_headers = opts[:custom_http_headers] || {}
else
raise ArgumentError, "custom_http_headers must be a hash."
end
self.disable_metrics = opts[:disable_metrics] || false
self.refresh_interval = opts[:refresh_interval] || 15
self.metrics_interval = opts[:metrics_interval] || 10
Expand Down
17 changes: 9 additions & 8 deletions spec/unleash/configuration_spec.rb
Expand Up @@ -4,6 +4,10 @@
RSpec.describe Unleash do

describe 'Configuration' do
before do
Unleash.configuration = nil
end

it "should have the correct defaults" do
config = Unleash::Configuration.new

Expand All @@ -24,7 +28,7 @@
end

it "should by default be invalid" do
config = Unleash::Configuration.new()
config = Unleash::Configuration.new
expect{ config.validate! }.to raise_error(ArgumentError)
end

Expand Down Expand Up @@ -82,8 +86,7 @@
end.to raise_error(ArgumentError)
end

# TODO: consider having consisten behaviour?
it "should swallow silently invalid custom_http_headers if set via client" do
it "should not accept invalid custom_http_headers via new client" do
WebMock.stub_request(:post, "http://test-url//client/register").
with(
headers: {
Expand All @@ -94,14 +97,12 @@
}).
to_return(status: 200, body: "", headers: {})

client = Unleash::Client.new(
expect{ Unleash::Client.new(
url: 'https://testurl/api',
app_name: 'test-app',
custom_http_headers: 123.0,
disable_metrics: true)

expect(Unleash.configuration.custom_http_headers).to eq({})
expect{ Unleash.configuration.validate! }.not_to raise_error
disable_metrics: true
) }.to raise_error(ArgumentError)
end
end
end

0 comments on commit 7c1e1d4

Please sign in to comment.