Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/oauth2/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ def no_tokens_warning(hash, key)
# @option opts [FixNum, String] :expires_in (nil) the number of seconds in which the AccessToken will expire
# @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire
# @option opts [FixNum, String] :expires_latency (nil) the number of seconds by which AccessToken validity will be reduced to offset latency, @version 2.0+
# @option opts [Symbol] :mode (:header) the transmission mode of the Access Token parameter value
# one of :header, :body or :query
# @option opts [Symbol or callable] :mode (:header) the transmission mode of the Access Token parameter value:
# either one of :header, :body or :query, or a callable that accepts a request-verb parameter
# and returns one of these three symbols.
# @option opts [String] :header_format ('Bearer %s') the string format to use for the Authorization header
# @option opts [String] :param_name ('access_token') the parameter name to use for transmission of the
# Access Token value in :body or :query transmission mode
Expand Down Expand Up @@ -324,7 +325,7 @@ def to_hash
#
# @see OAuth2::Client#request
def request(verb, path, opts = {}, &block)
configure_authentication!(opts)
configure_authentication!(opts, verb)
@client.request(verb, path, opts, &block)
end

Expand Down Expand Up @@ -370,8 +371,9 @@ def headers

private

def configure_authentication!(opts)
case options[:mode]
def configure_authentication!(opts, verb)
mode = options[:mode].respond_to?(:call) ? options[:mode].call(verb) : options[:mode]
case mode
when :header
opts[:headers] ||= {}
opts[:headers].merge!(headers)
Expand All @@ -389,7 +391,7 @@ def configure_authentication!(opts)
end
# @todo support for multi-part (file uploads)
else
raise("invalid :mode option of #{options[:mode]}")
raise("invalid :mode option of #{mode}")
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/oauth2/access_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,26 @@ def assert_initialized_token(target)
end
end

context "with verb-dependent mode" do
let(:mode) do
lambda do |verb|
case verb
when :get then :query
when :post, :delete then :header
when :put, :patch then :body
end
end
end

let(:options) { {mode: mode} }

VERBS.each do |verb|
it "correctly handles a #{verb.to_s.upcase}" do
expect(subject.__send__(verb, "/token/#{mode.call(verb)}").body).to include(token)
end
end
end

context "with client.options[:raise_errors] = false" do
let(:options) { {raise_errors: false} }

Expand Down
Loading