Add option for Authorization header in consume tag

Nick Cappadona committed Jun 09, 2016
commit de73d6afa7158087cda1acec191133eef81fcb20
Showing 2 changed files with 18 additions and 1 deletions
locomotive/steam/services/external_api_service.rb b/lib/locomotive/steam/services/external_api_service.rb +8 -1
@@ @@ -16,6 +16,10 @@ module Locomotive
username, password = options.delete(:username), options.delete(:password)
options[:basic_auth] = { username: username, password: password } if username
+ # authorization header ?
+ header_auth = options.delete(:header_auth)
+ options[:headers] = { 'Authorization' => header_auth } if header_auth
+
perform_request_to(path, options)
end
@@ @@ -37,7 +41,10 @@ module Locomotive
# sanitize the options
options[:format] = options[:format].gsub(/[\'\"]/, '').to_sym if options.has_key?(:format)
- options[:headers] = { 'User-Agent' => 'LocomotiveCMS' } if options[:with_user_agent]
+ if options[:with_user_agent]
+ user_agent = { 'User-Agent' => 'LocomotiveCMS' }
+ options[:headers] ? options[:headers].merge!(user_agent) : options[:headers] = user_agent
+ end
response = self.class.get(path, options)
parsed_response = response.parsed_response
spec/unit/services/external_api_service_spec.rb +10 -0
@@ @@ -75,6 +75,16 @@ describe Locomotive::Steam::ExternalAPIService do
end
end
+ describe 'sets authorization header' do
+
+ let(:url) { 'http://blog.locomotiveapp.org' }
+ let(:options) { { header_auth: 'letmein' } }
+ it do
+ expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org', headers: { 'Authorization' => 'letmein' } }).and_return(response)
+ subject
+ end
+ end
+
end
end