Rails integration tests with Faraday & Omniauth
-
03-06-2021 - |
Pregunta
My integration test looks like this:
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")
And with Faraday 0.7.6 it works just fine, the action_dispatch
adapter will route the http request to my Rails app (calling the users_controller
). But with Faraday 0.8.0 the action_dispatch
adapter was replaced by the rack
(see https://github.com/technoweenie/faraday/pull/134) and I cannot make it work.
I think I need to change the above code to this:
@client.connection.build do |b|
b.adapter :rack, self.app
end
But it fails at Rack (1.2.5) expecting a Stream, but it gets a Hash and says undefined read method for Hash
at line 149 (here: https://github.com/rack/rack/blob/1.2.5/lib/rack/request.rb#L149).
How can I make this work?
Solución
You will need to add the middleware url_encoded to make this work;
@oauth = OAuth2::Client.new('client_id', 'client_secret') do |b|
b.request :url_encoded
b.adapter :rack, Rails.application
end
Otros consejos
From your test suite you can access the rack app directly via the app
variable. The code should be:
@client.connection.build do |b|
b.adapter :rack, app
end
I tried this out on a sample app and it's working for me. Good luck!