Testing Response Cookies with Rspec v.1

Upon entering my application, the server sends me cookies (credentials and some application cookies):

Response sent 170 bytes of Cookie data: Set-Cookie: user_credentials=val; path=/; HttpOnly; Secure Response sent 554 bytes of Cookie data: Set-Cookie: _app_session=otherVal; path=/; HttpOnly; Secure 

... and then redirected to the home page;

Cookies include some flags: for example. httpOnly , Secure , etc.

How to check if cookies enable these flags using rspec?

At least where can I find these cookies?

 it "should generate cookies with proper flags" do params = Factory.attributes_for(:user, :username => "uname", :password => "upass" ) # login post 'create', params response.should redirect_to home_url # => pass puts "response cookie = #{response.cookies.inspect}" # => {} // no cookies in response, why? end 
+7
source share
1 answer

Controller specifications do not generate / run real HTTP requests, they simply configure the controller under test and invoke the requested action on it. No http request is made, and no real HTTP response is generated. That way, you can test the inner workings of the Rails controller at a more abstract level.

The handling of cookies in these specifications is quite simple, setting cookies in the following step:

 def set_cookies cookies[:foo] = 'bar' cookies[:lorem] = {:value => 'ipsum', :expires => 3.days.from_now} render :nothing => true end 

results in the following values ​​available in the specification:

 it "should set some cookie values" do get :set_cookies # response.cookies looks like this: # {'foo' => 'bar', 'lorem' => 'ipsum'} response.cookies['foo'].should == 'bar' response.cookies['lorem'].should == 'ipsum' end 

In order to check the types of cookie flags that you see in your responses, you will need to use what real HTTP requests do. Maybe you can use capybara gem for this?

+6
source

All Articles