Sha256: fca59a425a423751ca93c7ee2d14805eef0b21bc4bcc6c9d21aa52014288f564

Contents?: true

Size: 1.69 KB

Versions: 7

Compression:

Stored size: 1.69 KB

Contents

module OAuthenticator
  module RackTestSigner
    # takes a block. for the duration of the block, requests made with Rack::Test will be signed
    # with the given oauth_attrs. oauth_attrs are passed to {OAuthenticator::SignableRequest}. 
    #
    # attributes of the request are set from the Rack::Test request, so you should not provide those in 
    # the outh_attrs.
    #
    # These are the options you should or may provide (see {OAuthenticator::SignableRequest} for details of 
    # what options are required, what options have default or generated values, and what may be omitted):
    #
    # - signature_method
    # - consumer_key
    # - consumer_secret
    # - token
    # - token_secret
    # - version
    # - realm
    # - hash_body?
    def signing_rack_test(oauth_attrs, &block)
      begin
        Thread.current[:oauthenticator_rack_test_attributes] = oauth_attrs
        return yield
      ensure
        Thread.current[:oauthenticator_rack_test_attributes] = nil
      end
    end
  end

  # you can run OAuthenticator.signing_rack_test(attrs) { stuff }
  extend RackTestSigner
end

class Rack::Test::Session
  actual_process_request = instance_method(:process_request)
  define_method(:process_request) do |uri, env, &block|
    oauth_attrs = Thread.current[:oauthenticator_rack_test_attributes]
    if oauth_attrs
      request = Rack::Request.new(env)

      env['HTTP_AUTHORIZATION'] = OAuthenticator::SignableRequest.new(oauth_attrs.merge({
        :request_method => request.request_method,
        :uri => request.url,
        :media_type => request.media_type,
        :body => request.body,
      })).authorization
    end

    actual_process_request.bind(self).call(uri, env, &block)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
oauthenticator-1.3.5 lib/oauthenticator/rack_test_signer.rb
oauthenticator-1.3.4 lib/oauthenticator/rack_test_signer.rb
oauthenticator-1.3.3 lib/oauthenticator/rack_test_signer.rb
oauthenticator-1.3.2 lib/oauthenticator/rack_test_signer.rb
oauthenticator-1.3.1 lib/oauthenticator/rack_test_signer.rb
oauthenticator-1.3.0 lib/oauthenticator/rack_test_signer.rb
oauthenticator-1.2.0 lib/oauthenticator/rack_test_signer.rb