Skip to content

Commit

Permalink
Add unit tests checking that the custom http headers are actually sent
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Jul 11, 2018
1 parent e858a87 commit 0e45358
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ 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 = {}
headers.merge(Unleash.configuration.custom_http_headers || {})

headers = Unleash.configuration.custom_http_headers || {}
headers['Content-Type'] = 'application/json'

request = Net::HTTP::Post.new(uri.request_uri, headers)
Expand Down
4 changes: 2 additions & 2 deletions lib/unleash/metrics_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ 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 = {}
headers.merge(Unleash.configuration.custom_http_headers || {})

headers = 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
5 changes: 3 additions & 2 deletions lib/unleash/toggle_fetcher.rb
Original file line number Diff line number Diff line change
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,8 @@ 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
headers = {}
headers.merge(Unleash.configuration.custom_http_headers || {})

headers = Unleash.configuration.custom_http_headers
headers['Content-Type'] = 'application/json'
headers['If-None-Match'] = self.etag unless self.etag.nil?

Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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(
# body: "{\"appName\":\"my-test-app\",\"instanceId\":\"rspec/test\",\"sdkVersion\":\"unleash-client-ruby:0.1.2\",\"strategies\":[\"gradualRolloutRandom\",\"notImplemented\",\"base\",\"gradualRolloutUserId\",\"unknown\",\"applicationHostname\",\"remoteAddress\",\"gradualRolloutSessionId\",\"userWithId\",\"default\"],\"started\":\"2018-07-11T22:22:19.781+02:00\",\"interval\":10000}",
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(
# body: "{\"appName\":\"my-test-app\",\"instanceId\":\"rspec/test0\",\"bucket\":{\"start\":\"2018-07-11T22:50:46.098+02:00\",\"stop\":\"2018-07-11T22:50:46.099+02:00\",\"toggles\":{}}}",
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

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

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

# unless Unleash.configuration.disable_metrics
# Unleash.toggle_metrics = Unleash::Metrics.new
# Unleash.reporter = Unleash::MetricsReporter.new
end

it "does something useful" do
expect(false).to eq(false)
end
Expand Down
1 change: 1 addition & 0 deletions unleash-client.gemspec
Original file line number Diff line number Diff line change
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 0e45358

Please sign in to comment.