# Rspec with cookies, testing authentication controller

**URL:** <https://www.ruby-forum.com/t/rspec-with-cookies-testing-authentication-controller/137445>\
**Category:** Rails\
**Created:** [May 13, 2008, 7:59am UTC](https://www.ruby-forum.com/t/rspec-with-cookies-testing-authentication-controller/137445 "2008-05-13T07:59:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![xwordguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/65b543/32.png) [@xwordguy](https://www.ruby-forum.com/u/xwordguy)\
**Post date:** [May 13, 2008, 7:59am UTC](https://www.ruby-forum.com/t/rspec-with-cookies-testing-authentication-controller/137445/1 "2008-05-13T07:59:53Z")

</div>

I am trying use Rspec to test our authentication, this requires me to  
set a cookie and pass  
it in the header.  
when I inspect the headers the cookie is blank  
Any ideas why this is not working?

— The Spec  
it “should redirect to overview page if exists” do  
get ‘login’  
request.session\_options[:session\_key].should ==  
‘\_CRS\_session\_id’  
request.session\_options[:secret].should\_not == nil  
cookie = request.session\_options[:session\_key] + ‘=’ +  
request.session\_options[:secret]  
headers = {  
‘cookie’ =\> cookie,  
‘Referer’ =\> ‘[http://localhost:3000/auth/login](http://localhost:3000/auth/login)’,  
‘Content-Type’ =\> ‘application/x-www-form-urlencoded’  
}

```
    post('login', {:username => 'test', :password => 'password'},

```

headers)  
puts response.headers[“cookie”].inspect  
puts request.inspect

```
    response.should redirect_to(controller => 'profiles', :action =>

```

‘overview’)  
end

– Controller method for login  
def login  
if request.method == :post && !cookies[‘\_CRS\_session\_id’]  
flash[:auth\_error] = :cookies\_off  
elsif request.method == :post && params[:login\_form]  
username = params[:login\_form][:username]  
password = params[:login\_form][:password]  
ldap\_authenticate(username, password)  
end  
end

— HTTP RESPONSE

{“Status”=\>“200 OK”, “ETag”=\>“"4ff001373a428161bc75da425b5dc4be"”,  
“X-Runtime”  
=\>“0.00010”, “type”=\>“text/html; charset=utf-8”, “cookie”=\>[],  
“Cache-Control”=\>  
“private, max-age=0, must-revalidate”, “Content-Length”=\>8140}

— HTTP REQUEST

#\<ActionController::TestRequest:0xf2d026c  
@session=#\<ActionController::TestSessi  
on:0xf2c0a24 @attributes=nil, @session\_id=“”,  
@saved\_attributes={“cookie”=\>“\_CRS  
\_session\_id=2887ccde4594cb37332cce1c974962957cf9e102b38155067f175cefbc633e26547c  
b4e17c9710a71cf39288560d88a9b8b4b707610c9afddswer5530640”,  
“Referer”=\>“http:  
[//localhost:3000/auth/login](https://localhost:3000/auth/login)”,  
“Content-Type”=\>“application/x-www-form-urlencoded  
“}\>, @path=nil, @accepts=nil, @user\_agent=“Rails Testing”,  
@env={“REMOTE\_ADDR”=\>  
“0.0.0.0”, “REQUEST\_URI”=\>”/auth/login”, “SERVER\_PORT”=\>80,  
“REQUEST\_METHOD”=\>“P  
OST”}, @session\_options={:session\_key=\>“\_CRS\_session\_id”,  
:secret=\>“2887ccde45  
94cb37332cce1c974962957cf9e102b38155067f175cefbc633e26547cb4e17c9710a71cf3928856  
0d88a9b8b4b707610c9afddswer5530640”}, @cookies={},  
@request\_method=:post, @req  
uest\_parameters={:username=\>“test”, :password=\>“password”},  
@request\_uri=nil, @symb  
olized\_path\_parameters={:action=\>“login”, :controller=\>“auth”},  
@query\_parameter  
s={“action”=\>“login”}, @path\_parameters={“action”=\>“login”,  
“controller”=\>“auth”  
}, @content\_type=nil, @parameters={“username”=\>“test”,  
“action”=\>“login”, “cont  
roller”=\>“auth”, “password”=\>“password”}, @host=“test.host”\>  
…  
I’ve been struggling with this for ages so any input will be greatly  
appreciated.  
I’ve also looked at mocks and could not get that to work.  
Kevin.
