Skip to content

Commit

Permalink
Merge 22634c7 into e5ac93d
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Jul 11, 2018
2 parents e5ac93d + 22634c7 commit bc1f390
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 10 deletions.
5 changes: 4 additions & 1 deletion lib/unleash/client.rb
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 = (Unleash.configuration.custom_http_headers || {}).dup
headers['Content-Type'] = 'application/json'

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

Expand Down
12 changes: 11 additions & 1 deletion lib/unleash/configuration.rb
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,11 @@ def initialize(opts = {})
self.url = opts[:url] || nil
self.instance_id = opts[:instance_id] || SecureRandom.uuid

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 Expand Up @@ -42,7 +48,11 @@ def metrics_interval_in_millis

def validate!
if self.app_name.nil? or self.url.nil?
raise ArgumentError, "URL and app_name are required"
raise ArgumentError, "URL and app_name are required parameters."
end

if ! self.custom_http_headers.is_a?(Hash)
raise ArgumentError, "custom_http_headers must be a hash."
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/unleash/metrics_reporter.rb
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 = (Unleash.configuration.custom_http_headers || {}).dup
headers['Content-Type'] = 'application/json'
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = generated_report.to_json

Expand Down
9 changes: 7 additions & 2 deletions lib/unleash/toggle_fetcher.rb
Expand Up @@ -38,6 +38,7 @@ def toggles
end

# rename to refresh_from_server! ??
# TODO: should simplify by moving uri / http initialization to class initialization
def fetch
Unleash.logger.debug "fetch()"
Unleash.logger.debug "ETag: #{self.etag}" unless self.etag.nil?
Expand All @@ -47,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 = (Unleash.configuration.custom_http_headers || {}).dup
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
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -2,9 +2,13 @@
require "unleash"
require "unleash/client"

require 'webmock/rspec'

require 'coveralls'
Coveralls.wear!

WebMock.disable_net_connect!(allow_localhost: false)

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand Down
56 changes: 56 additions & 0 deletions spec/unleash/client_spec.rb
Expand Up @@ -5,6 +5,62 @@
expect(Unleash::VERSION).not_to be nil
end

it "Uses custom http headers when initializing client" do
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'=>'Ruby',
'X-Api-Key'=>'123'
})
.to_return(status: 200, body: "", headers: {})
WebMock.stub_request(:post, "http://test-url//client/metrics").
with(
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "", headers: {})


Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.instance_id = 'rspec/test'
config.custom_http_headers = {'X-API-KEY' => '123'}
end

unleash_client = Unleash::Client.new(
url: 'http://test-url/',
app_name: 'my-test-app',
instance_id: 'rspec/test',
custom_http_headers: {'X-API-KEY' => '123'}
)

expect(
a_request(:post, "http://test-url//client/register")
.with( headers: {'Content-Type': 'application/json'})
.with( headers: {'X-API-KEY': '123', 'Content-Type': 'application/json'})
).to have_been_made.once

expect(
a_request(:get, "http://test-url//client/features")
.with( headers: {'X-API-KEY': '123'})
).to have_been_made.once

# Test now sending of metrics
Unleash.reporter.send
expect(
a_request(:post, "http://test-url//client/metrics")
.with( headers: {'Content-Type': 'application/json'})
.with( headers: {'X-API-KEY': '123', 'Content-Type': 'application/json'})
).to have_been_made.once
end

it "does something useful" do
expect(false).to eq(false)
end
Expand Down
63 changes: 58 additions & 5 deletions spec/unleash/configuration_spec.rb
Expand Up @@ -4,25 +4,31 @@
RSpec.describe Unleash do

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

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
config = Unleash::Configuration.new()
config = Unleash::Configuration.new
expect{ config.validate! }.to raise_error(ArgumentError)
end

Expand Down Expand Up @@ -50,6 +56,53 @@
expect(config.client_register_url).to eq('https://testurl/api/client/register')
end

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

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

it "should not accept invalid custom_http_headers via new client" do
WebMock.stub_request(:post, "http://test-url//client/register").
with(
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "", headers: {})

expect{ Unleash::Client.new(
url: 'https://testurl/api',
app_name: 'test-app',
custom_http_headers: 123.0,
disable_metrics: true
) }.to raise_error(ArgumentError)
end
end
end
1 change: 1 addition & 0 deletions unleash-client.gemspec
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "webmock", "~> 3.0"
spec.add_development_dependency "coveralls"

end

0 comments on commit bc1f390

Please sign in to comment.