Forum: Ruby on Rails Can't set cookies in functional tests

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
48ea812351e5d5b4befe4282bd544838?d=identicon&s=25 micand7 (Guest)
on 2005-12-03 10:14
(Received via mailing list)
Howdy,

I am having difficulty setting cookies in functional
tests. My application contains a user authentication
system based on cookies, and so to test areas of the
application dependent on whether the user is
authorized, I need to be able to set cookies in my
functional tests. The odd thing is that it seems
possible to set cookies only under certain
circumstances. Allow me to explain: my UserController
handles the task of receiving user authentication
credentials and setting a cookie to indicate the user
is logged in, as well as other mundane activities such
as allowing the user to edit the information stored in
his profile. To test the "edit profile" action
as an authenticated user, I first send a POST request
containing the appropriate username and password to
the "login" action. After the request completes, a
cookie containing the user ID and an authentication
token unique to that user will be set, so I simply
call "@request.cookies = cookies" in my test and make
my subsequent request to the "edit profile" action.
ApplicationController "sees" the necessary cookies,
the user is recognized as having logged in, and
everything works just as one would expect.

The trouble comes when attempting to test other
controllers as an authenticated user, for
ApplicationController is unable to see anything I
store in cookies prior to making a request from my
functional test. I will do something such as this in
my test:

@request.cookies = { 'user_id' => '1' }; get
:something

And then insert the following into my
ApplicationController to see exactly what it is
receiving via cookies:

require 'pp'; pp cookies; puts cookies.has_key?
'user_id'; puts cookies['user_id'].nil?; puts
cookies['user_id']

Calling "pp cookies" will print out the same hash I
set in my functional test, and
cookies.has_key?('user_id') returns true, so
everything looks as if it is working as it ought to.
But "cookies['user_id'].nil?" is true, and printing
cookies['user_id'] prints nil! Although calling pp (or
puts) on cookies shows everything is fine, I'm not
able to actually reference the individual values
stored in the hash by key. The
same diagnostic code in ApplicationController run when
testing UserController works fine.

The one peculiarity I ran into is that in testing
UserController, which works properly, calling
cookies() in the functional test after making a
request returns a hash in an odd format:

{"user_id"=>["1"]}

The value, rather than being only a string, is a
string inside a single-element array. Using this exact
format in setting cookies from the problematic
functional tests does not result in different
behaviour in A
pplicationController, however.

Whatever assistance (or sympathy ;)) you can provide
will be much appreciated.

Thanks,

Jeff



__________________________________________
Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
This topic is locked and can not be replied to.