Skip to content

Commit 69210d5

Browse files
authored
Merge pull request #670 from mrj/request-dependent-modes
2 parents fc83dfd + 4c6907b commit 69210d5

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/oauth2/access_token.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ def no_tokens_warning(hash, key)
132132
# @option opts [FixNum, String] :expires_in (nil) the number of seconds in which the AccessToken will expire
133133
# @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire
134134
# @option opts [FixNum, String] :expires_latency (nil) the number of seconds by which AccessToken validity will be reduced to offset latency, @version 2.0+
135-
# @option opts [Symbol] :mode (:header) the transmission mode of the Access Token parameter value
136-
# one of :header, :body or :query
135+
# @option opts [Symbol or callable] :mode (:header) the transmission mode of the Access Token parameter value:
136+
# either one of :header, :body or :query, or a callable that accepts a request-verb parameter
137+
# and returns one of these three symbols.
137138
# @option opts [String] :header_format ('Bearer %s') the string format to use for the Authorization header
138139
# @option opts [String] :param_name ('access_token') the parameter name to use for transmission of the
139140
# Access Token value in :body or :query transmission mode
@@ -324,7 +325,7 @@ def to_hash
324325
#
325326
# @see OAuth2::Client#request
326327
def request(verb, path, opts = {}, &block)
327-
configure_authentication!(opts)
328+
configure_authentication!(opts, verb)
328329
@client.request(verb, path, opts, &block)
329330
end
330331

@@ -370,8 +371,9 @@ def headers
370371

371372
private
372373

373-
def configure_authentication!(opts)
374-
case options[:mode]
374+
def configure_authentication!(opts, verb)
375+
mode = options[:mode].respond_to?(:call) ? options[:mode].call(verb) : options[:mode]
376+
case mode
375377
when :header
376378
opts[:headers] ||= {}
377379
opts[:headers].merge!(headers)
@@ -389,7 +391,7 @@ def configure_authentication!(opts)
389391
end
390392
# @todo support for multi-part (file uploads)
391393
else
392-
raise("invalid :mode option of #{options[:mode]}")
394+
raise("invalid :mode option of #{mode}")
393395
end
394396
end
395397

spec/oauth2/access_token_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,26 @@ def assert_initialized_token(target)
411411
end
412412
end
413413

414+
context "with verb-dependent mode" do
415+
let(:mode) do
416+
lambda do |verb|
417+
case verb
418+
when :get then :query
419+
when :post, :delete then :header
420+
when :put, :patch then :body
421+
end
422+
end
423+
end
424+
425+
let(:options) { {mode: mode} }
426+
427+
VERBS.each do |verb|
428+
it "correctly handles a #{verb.to_s.upcase}" do
429+
expect(subject.__send__(verb, "/token/#{mode.call(verb)}").body).to include(token)
430+
end
431+
end
432+
end
433+
414434
context "with client.options[:raise_errors] = false" do
415435
let(:options) { {raise_errors: false} }
416436

0 commit comments

Comments
 (0)