Hi *, I need to test cookie creation with rspec and using Rack::Test, but for some reason I''m not able to pair the real app that creates a cookie with the rspec tests. The spec is like this: it "should authenticate using cookies" do user = Factory.create :user remember_token = Charon.make_remember_token set_cookie "warden=#{ remember_token }" user.update :remember_token => remember_token post ''/authenticate'' last_request.env[ ''warden'' ].should be_authenticated end Inside the main app I set the cookie like: if !request.cookies[ ''warden'' ] response.set_cookie( ''warden'', :value => env[ ''warden'' ].user.remember_token ) end I can clearly see it works when testing manually. For some reasons, the rspec cookie is different than the one set by the app. Is someone trying something similar and can share some code ? I think that the problem is in Rack::Test but I''m not sure and I''m not able prove it. ngw
On Apr 16, 2010, at 8:07 AM, Nicholas Wieland wrote:> Hi *, > I need to test cookie creation with rspec and using Rack::Test, but for some reason I''m not able to pair the real app that creates a cookie with the rspec tests. > The spec is like this: > > it "should authenticate using cookies" do > user = Factory.create :user > remember_token = Charon.make_remember_token > set_cookie "warden=#{ remember_token }" > user.update :remember_token => remember_token > post ''/authenticate'' > last_request.env[ ''warden'' ].should be_authenticated > end > > Inside the main app I set the cookie like: > > if !request.cookies[ ''warden'' ] > response.set_cookie( ''warden'', :value => env[ ''warden'' ].user.remember_token ) > end > > I can clearly see it works when testing manually. For some reasons, the rspec cookie is different than the one set by the app. > > Is someone trying something similar and can share some code ? I think that the problem is in Rack::Test but I''m not sure and I''m not able prove it.What versions of rails, rspec, rspec-rails, ruby, etc? Where does this spec live?
On Apr 16, 2010, at 3:12 PM, David Chelimsky wrote:> On Apr 16, 2010, at 8:07 AM, Nicholas Wieland wrote: > > What versions of rails, rspec, rspec-rails, ruby, etc? > > Where does this spec live?It''s sinatra 1.0, not rails. ruby 1.8.7 (2010-01-10 patchlevel 249) rspec (1.3.0) rack-test (0.5.3) rack (1.1.0) ngw -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100416/7ff09bd8/attachment.html>
On Apr 16, 2010, at 8:49 AM, Nicholas Wieland wrote:> On Apr 16, 2010, at 3:12 PM, David Chelimsky wrote: > >> On Apr 16, 2010, at 8:07 AM, Nicholas Wieland wrote: >> >> What versions of rails, rspec, rspec-rails, ruby, etc? >> >> Where does this spec live? > > It''s sinatra 1.0, not rails.In which case this is probably not an rspec problem. Where is the set_cookie method defined in this example (from your earlier post in this thread)? it "should authenticate using cookies" do user = Factory.create :user remember_token = Charon.make_remember_token set_cookie "warden=#{ remember_token }" user.update :remember_token => remember_token post ''/authenticate'' last_request.env[ ''warden'' ].should be_authenticated end Also, can you try the same example using test/unit? Do you get the same result? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100416/7877a7b1/attachment-0001.html>
Try: post ''whatever'', {}, {''rack.session'' => {:something=>''value''}} To put things through the session with rack test/sinatra. Sent from my iPhone On 16 Apr 2010, at 15:20, David Chelimsky <dchelimsky at gmail.com> wrote:> On Apr 16, 2010, at 8:49 AM, Nicholas Wieland wrote: > >> On Apr 16, 2010, at 3:12 PM, David Chelimsky wrote: >> >>> On Apr 16, 2010, at 8:07 AM, Nicholas Wieland wrote: >>> >>> What versions of rails, rspec, rspec-rails, ruby, etc? >>> >>> Where does this spec live? >> >> It''s sinatra 1.0, not rails. > > In which case this is probably not an rspec problem. Where is the set_cookie method defined in this example (from your earlier post in this thread)? > > it "should authenticate using cookies" do > user =Factory.create :user > remember_token = Charon.make_remember_token > set_cookie "warden=#{ remember_token }" > user.update :remember_token => remember_token > post ''/authenticate'' > last_request.env[ ''warden'' ].should be_authenticated > end > > Also, can you try the same example using test/unit? Do you get the same result? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100416/762edbea/attachment.html>
On 16 April 2010 15:59, Ben Lovell <benjamin.lovell at gmail.com> wrote:> Try: > > post ''whatever'', {}, {''rack.session'' => {:something=>''value''}} > > To put things through the session with rack test/sinatra. > > Sent from my iPhone > >Hmm, hold up, that isn''t what you asked for :) You have to remember that the default Rack::Test::CookieJar#[] only returns cookies that exactly match the domain and path of the current request so be sure to check there first.> On 16 Apr 2010, at 15:20, David Chelimsky <dchelimsky at gmail.com> wrote: > > On Apr 16, 2010, at 8:49 AM, Nicholas Wieland wrote: > > On Apr 16, 2010, at 3:12 PM, David Chelimsky wrote: > > On Apr 16, 2010, at 8:07 AM, Nicholas Wieland wrote: > > What versions of rails, rspec, rspec-rails, ruby, etc? > > Where does this spec live? > > > It''s sinatra 1.0, not rails. > > > In which case this is probably not an rspec problem. Where is the > set_cookie method defined in this example (from your earlier post in this > thread)? > > it "should authenticate using cookies" do > user =Factory.create :user > remember_token = Charon.make_remember_token > set_cookie "warden=#{ remember_token }" > user.update :remember_token => remember_token > post ''/authenticate'' > last_request.env[ ''warden'' ].should be_authenticated > end > > Also, can you try the same example using test/unit? Do you get the same > result? > > _______________________________________________ > > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100416/376038ae/attachment.html>
Ben Lovell wrote:> On 16 April 2010 15:59, Ben Lovell <benjamin.lovell at gmail.com> wrote: > >> Try: >> >> post ''whatever'', {}, {''rack.session'' => {:something=>''value''}} >> >> To put things through the session with rack test/sinatra. >> >> Sent from my iPhone >> >> > Hmm, hold up, that isn''t what you asked for :) > > You have to remember that the default Rack::Test::CookieJar#[] only > returns > cookies that exactly match the domain and path of the current request so > be > sure to check there first.I think this is more of a Sinatra issue, and not an rspec issue. But, since I''m a Sinatra fan, I thought I''d weigh in, since I just went through this and it was harder then I would''ve expected. If you want to pass session values in tests, you''ll have to disable sessions in your sinatra app. I''ve got a blog post at http://benprew.posterous.com/ with the details. Also, I had to do this in one of my apps recently, take a look at the Rakefile, picklespears.com and test/test_player.rb at http://github.com/benprew/picklespears -- Posted via http://www.ruby-forum.com/.