Devise+Oauth + WebMock

181 views
Skip to first unread message

brianp

unread,
Dec 8, 2010, 9:20:02 PM12/8/10
to webmock-users
Hey,

I'm using devise and Oauth to login via Twitter. I want to test my
controller which handles the session creation and user_settings model
creation etc. But the callback information received from the twitter
api is pretty important in these steps. Is there a way I can mock this
with webmock ?

Bartosz Blimke

unread,
Dec 9, 2010, 5:35:10 AM12/9/10
to webmoc...@googlegroups.com
Hi Brian,

I'm not sure I understood correctly but I assume you want to 'mock' twitter to make a callback to your controller, right? 

You could "simulate" twitter making a request to your server, by evaluating the response in WebMock declared as a block i.e. 

 stub_request( :get, /.*twitter.com\/oauth.*/ 
).to_return { |req| 
    Net::HTTP.get(URI.parse(url_for( :controller => 'twitter', :action => 
'complete', :code => 'abcdefghi' ))) 
    {} 
}) 

This will make a request to twitter controller before returning a response.

This can work if your app is running on localhost and you set WebMock.disable_net_connect!(:allow_localhost => true). Otherwise webmock will intercept the request to your controller. 

The code which executes the request to twitter needs to run in a 
separate process or thread than the twitter controller runs, otherwise the 
server thread with will be blocked (making a request to 
twitter) and won't be able to handle twitter controller action.

Bartosz

2010/12/9 brianp <brian.o...@gmail.com>
Reply all
Reply to author
Forward
0 new messages