与Faraday和Omniauth的Rails集成测试

Paw*_*cki 1 ruby ruby-on-rails oauth oauth-2.0 faraday

我的集成测试如下所示:

class OAuthTest < ActionDispatch::IntegrationTest
  ...

  @client = OAuth2::Client.new(                                                
    client_id,                                              
    client_secret,                                          
    { :site => 'https://provider', :token_url => '/oauth/access_token' }
  )                                                                            

  @client.connection.build do |b|                                              
    b.adapter :action_dispatch, self
  end                                                                          

  access_token = @client.auth_code.get_token(...)
  access_token.get("/user.json")
Run Code Online (Sandbox Code Playgroud)

使用法拉第0.7.6它工作正常,action_dispatch适配器将http请求路由到我的Rails应用程序(调用users_controller).但是在Faraday 0.8.0的情况下,action_dispatch适配器被替换为rack(请参阅https://github.com/technoweenie/faraday/pull/134),我无法使其正常工作.

我想我需要将上面的代码更改为:

@client.connection.build do |b|      
  b.adapter :rack, self.app          
end                                  
Run Code Online (Sandbox Code Playgroud)

但是它在Rack(1.2.5)没有期待一个Stream,但它获得了一个Hash并undefined read method for Hash在第149行说道(这里:https://github.com/rack/rack/blob/1.2.5/lib/rack/request .rb#L149).

我怎样才能做到这一点?

小智 5

您需要添加url_encoded中间件才能使其正常工作;

@oauth = OAuth2::Client.new('client_id', 'client_secret') do |b|
  b.request :url_encoded
  b.adapter :rack, Rails.application
end
Run Code Online (Sandbox Code Playgroud)