From 82d5f158e39939f29212c8e17afdd97019bb69d1 Mon Sep 17 00:00:00 2001 From: fukayatsu Date: Mon, 20 Jan 2020 14:38:33 +0900 Subject: [PATCH] Add specs for default_headers --- esa.gemspec | 1 - spec/esa/client_spec.rb | 64 +++++++++++++++++++++++++++++++++++++---- spec/spec_helper.rb | 8 ------ 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/esa.gemspec b/esa.gemspec index 7eb7485..db8f08a 100644 --- a/esa.gemspec +++ b/esa.gemspec @@ -27,6 +27,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'pry', '~> 0.12' spec.add_development_dependency 'rake', '~> 13.0' spec.add_development_dependency 'rspec', '~> 3.9' - spec.add_development_dependency 'vcr', '~> 5.0.0' spec.add_development_dependency 'webmock', '~> 3.7.6' end diff --git a/spec/esa/client_spec.rb b/spec/esa/client_spec.rb index c9a1fb7..d1b9314 100644 --- a/spec/esa/client_spec.rb +++ b/spec/esa/client_spec.rb @@ -4,11 +4,13 @@ let(:access_token) { nil } let(:api_endpoint) { nil } let(:current_team) { nil } + let(:default_headers) { {} } let(:options) do { access_token: access_token, api_endpoint: api_endpoint, current_team: current_team, + default_headers: default_headers } end subject(:client) { described_class.new(**options) } @@ -42,9 +44,61 @@ end end - # describe "#send_request", :vcr do - # it 'returns Esa::Response' do - # expect(client.teams).to be_a Esa::Response - # end - # end + describe 'default_headers' do + before do + stub_request(:any, 'https://api.esa.io/v1/teams') + .to_return do |request| + { + body: request.body, + headers: request.headers + } + end + end + + context 'no default_headers option' do + it 'request with basic headers' do + response = client.teams + expect(response.headers).to eq( + 'Accept' => 'application/json', + 'User-Agent' => "Esa Ruby Gem #{Esa::VERSION}", + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' + ) + end + + it 'request with basic headers and additional headers ' do + response = client.teams(nil, { 'X-Foo' => 'bar' }) + expect(response.headers).to eq( + 'Accept' => 'application/json', + 'User-Agent' => "Esa Ruby Gem #{Esa::VERSION}", + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'X-Foo' => 'bar' + ) + end + end + + context 'with default_headers option' do + let(:default_headers) { { 'X-Default-Foo' => 'Bar' } } + + it 'request with basic headers and default_headers' do + response = client.teams + expect(response.headers).to eq( + 'Accept' => 'application/json', + 'User-Agent' => "Esa Ruby Gem #{Esa::VERSION}", + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'X-Default-Foo' => 'Bar' + ) + end + + it 'request with basic headers and additional headers ' do + response = client.teams(nil, { 'X-Foo' => 'baz', 'X-Default-Foo' => 'qux' }) + expect(response.headers).to eq( + 'Accept' => 'application/json', + 'User-Agent' => "Esa Ruby Gem #{Esa::VERSION}", + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'X-Default-Foo' => 'qux', + 'X-Foo' => 'baz' + ) + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8702d0a..1e9899b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,5 @@ require 'esa' require 'webmock/rspec' -require 'vcr' # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| @@ -26,10 +25,3 @@ config.order = :random Kernel.srand config.seed end - -VCR.configure do |c| - c.cassette_library_dir = 'spec/cassettes' - c.hook_into :webmock - c.configure_rspec_metadata! - c.filter_sensitive_data('') { ENV['ESA_ACCESS_TOKEN'] } -end